CF2125B.Left and Down

传统题 时间 2000 ms 内存 256 MiB 4 尝试 1 已通过 1 标签

Left and Down

There is a robot located in the cell (a,b)(a,b) of an infinite grid. Misha wants to move it to the cell (0,0)(0,0). To do this, he has fixed some integer kk.

Misha can perform the following operation: choose two integers dxdx and dydy (both from 00 to kk inclusive) and move the robot dxdx cells to the left (in the direction of decreasing xx coordinate) and dydy cells down (in the direction of decreasing yy coordinate). In other words, move the robot from (x,y)(x,y) to (xdx,ydy)(x - dx, y - dy).

The cost of the operation is:

  • 11, if the chosen pair (dx,dy)(dx,dy) is used for the first time;
  • 00, if the pair (dx,dy)(dx,dy) has been chosen before.

Note that if dxdydx \ne dy, the pairs (dx,dy)(dx, dy) and (dy,dx)(dy, dx) are considered different.

Help Misha bring the robot to the cell (0,0)(0,0) with minimum total cost. Note that you don't have to minimize the number of operations.

Input

The first line contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases.

The only line of each test case contains three integers a,ba, b, and kk (1a,b,k10181 \le a, b, k \le 10^{18}).

Output

For each test case, output a single integer — the minimum total cost of operations required to move the robot to the cell (0,0)(0,0).

Note

In the first test case, the operation (3,5)(3,5) can be applied once. The robot will immediately go to (0,0)(0,0), and the cost of the operation will be 11.

In the second test case, the operations: (1,1)(1,1), (0,1)(0,1), and (1,1)(1,1) can be applied. After the first operation, the robot will be at cell (1,2)(1,2), after the second one — at (1,1)(1,1), and after the third one — at (0,0)(0,0). The cost of the first and second operations is 11, while the third is 00, as the pair (1,1)(1,1) has already been used in the first operation.

In the third test case, the pair (4,6)(4,6) can be chosen three times in a row.

In the fourth test case, the operations: (4,2)(4,2) and (5,5)(5,5) can be applied.

Samples

4
3 5 15
2 3 1
12 18 8
9 7 5
1
2
1
2

在线编程 IDE

建议全屏模式获得最佳体验