Time Limit: 1 second
Memory Limit: 32 MB
There are
-
Shift+:
$z := z + d$ -
Shift−:
$z := z - d$
Visitors do not see
- If
$n = 1$ , the shown room is always$1$ . - Otherwise, let
$N = n - 1$ ,$L = 2N$ , and$$r = z \bmod L \quad \text{as a value in } [0, L-1]$$ (i.e.$r = ((z \bmod L) + L) \bmod L$ ).- If
$r \le N$ , the shown room is$r + 1$ . - Otherwise, the shown room is
$(L - r) + 1$ .
- If
Initially, the shown room is
For each test case, find the minimum number of button presses needed so that the shown room becomes
Input Format:-
The first line contains an integer
Each test case contains four integers
Output Format:-
For each test case, output one integer — the minimum number of presses, or
Constraints:-
$1 \le t \le 2 \cdot 10^5$ $1 \le n \le 10^{18}$ $1 \le x, y \le n$ $1 \le d \le 10^{18}$
Examples:-
- Input:
1
10 2 10 5
- Output:
2
- Input:
1
5 3 3 3
- Output:
0
Note:-
In the first example,
In the second example,