CF2035A.Sliding

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

Sliding

Red was ejected. They were not the imposter.

There are nn rows of mm people. Let the position in the rr-th row and the cc-th column be denoted by (r,c)(r, c). Number each person starting from 11 in row-major order, i.e., the person numbered (r1)m+c(r-1)\cdot m+c is initially at (r,c)(r,c).

The person at (r,c)(r, c) decides to leave. To fill the gap, let the person who left be numbered ii. Each person numbered j>ij \gt i will move to the position where the person numbered j1j-1 is initially at. The following diagram illustrates the case where n=2n=2, m=3m=3, r=1r=1, and c=2c=2.

Calculate the sum of the Manhattan distances of each person's movement. If a person was initially at (r0,c0)(r_0, c_0) and then moved to (r1,c1)(r_1, c_1), the Manhattan distance is r0r1+c0c1|r_0-r_1|+|c_0-c_1|.

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 testcase contains 44 integers nn, mm, rr, and cc (1rn1061\le r\le n\le 10^6, 1cm1061 \le c \le m \le 10^6), where nn is the number of rows, mm is the number of columns, and (r,c)(r,c) is the position where the person who left is initially at.

Output

For each test case, output a single integer denoting the sum of the Manhattan distances.

Note

For the first test case, the person numbered 22 leaves, and the distances of the movements of the person numbered 33, 44, 55, and 66 are 11, 33, 11, and 11, respectively. So the answer is 1+3+1+1=61+3+1+1=6.

For the second test case, the person numbered 33 leaves, and the person numbered 44 moves. The answer is 11.

Samples

4
2 3 1 2
2 2 2 1
1 1 1 1
1000000 1000000 1 1
6
1
0
1999998000000

在线编程 IDE

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