Score : $500$ points
A train goes back and forth between Town $A$ and Town $B$. It departs Town $A$ at time $0$ and then repeats the following:
More formally, these intervals of time are half-open, that is, for each $n = 0, 1, 2, \dots$:
Takahashi is thinking of taking this train to depart Town $A$ at time $0$ and getting off at Town $B$. After the departure, he will repeat the following:
Again, these intervals of time are half-open, that is, for each $n = 0, 1, 2, \dots$:
He can get off the train at Town $B$ if it is stopping at Town $B$ and he is awake.
Determine whether he can get off at Town $B$. If he can, find the earliest possible time to do so.
Under the constraints of this problem, it can be proved that the earliest time is an integer.
You are given $T$ cases. Solve each of them.
Input is given from Standard Input in the following format:
$T$ $\rm case_1$ $\rm case_2$ $\hspace{9pt}\vdots$ $\rm case_T$
Each case is in the following format:
$X$ $Y$ $P$ $Q$
Print $T$ lines.
The $i$-th line should contain the answer to $\rm case_i$.
If there exists a time such that Takahashi can get off the train at Town $B$, the line should contain the earliest such time; otherwise, the line should contain infinity
.
3 5 2 7 6 1 1 3 1 999999999 1 1000000000 1
20 infinity 1000000000999999999
Let $[a, b)$ denote the interval $a ≤ t < b$.
In the first case, the train stops at Town $B$ during $[5, 7), [19, 21), [33, 35), \dots$, and Takahashi is awake during $[7, 13), [20, 26), [33, 39), \dots$, so he can get off at time $20$ at the earliest.