AtCoder Beginner Contest 408
G
A/B < p/q < C/D
AtCoder
⌀ 2167
原题
翻译中…

Problem Statement

翻译中…

You are given positive integers $A,B,C,D$ satisfying $\displaystyle \frac AB < \frac CD$.

翻译中…

Find the smallest positive integer $q$ satisfying the following condition:

翻译中…
  • There exists a positive integer $p$ such that $\displaystyle \frac AB <\frac pq < \frac CD$.
翻译中…

$T$ test cases are given, so solve each.

翻译中…

Constraints

翻译中…
  • $1\le T\le 2\times 10^5$
  • $1\le A,B,C,D\le 10^{18}$
  • $\displaystyle\frac AB < \frac CD$
  • All input values are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

翻译中…

Here, $\text{case}_i$ represents the $i$-th test case.

翻译中…

Each test case is given in the following format:

翻译中…

A B C D

翻译中…

Output

翻译中…

Output $T$ lines. The $i$-th line should contain the answer for the $i$-th test case.

翻译中…

样例

Input #1
5
3 2 2 1
5 2 8 3
1 2 2 1
60 191 11 35
40 191 71 226
Output #1
3
5
1
226
4
Consider the first test case. For example, if $p=5,q=3$, then $\displaystyle \frac32<\frac53<\frac21$, so $q=3$ satisfies the condition. There is no positive integer less than $3$ that satisfies the condition for $q$, so the answer for the first test case is $3$.
题目信息
题号
abc408_g
时间限制
2 s
内存限制
1024 MB

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