Codeforces Round 566 (Div. 2)
F
Maximum Sine
Codeforces
2700
原题
← E→
翻译中…

You have given integers $a$, $b$, $p$, and $q$. Let $f(x) = \text{abs}(\text{sin}(\frac{p}{q} \pi x))$.

翻译中…

Find minimum possible integer $x$ that maximizes $f(x)$ where $a \le x \le b$.

翻译中…

Input

翻译中…

Each test contains multiple test cases.

翻译中…

The first line contains the number of test cases $t$ ($1 \le t \le 100$) — the number of test cases.

翻译中…

The first line of each test case contains four integers $a$, $b$, $p$, and $q$ ($0 \le a \le b \le 10^{9}$, $1 \le p$, $q \le 10^{9}$).

翻译中…

Output

翻译中…

Print the minimum possible integer $x$ for each test cases, separated by newline.

翻译中…

Note

翻译中…

In the first test case, $f(0) = 0$, $f(1) = f(2) \approx 0.866$, $f(3) = 0$.

翻译中…

In the second test case, $f(55) \approx 0.999969$, which is the largest among all possible values.

翻译中…

样例

Input #1
2
0 3 1 3
17 86 389 995
Output #1
1
55
InputCopy OutputCopy
题目信息
题号
1182F
时间限制
3 s
内存限制
256 MB
标签
二分
数据结构
数论

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。