CF2122B.Pile Shuffling

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

Pile Shuffling

You are given nn binary piles, the ii-th of which consists of aia_i zeros on the top and bib_i ones on the bottom.

In one operation, you can take the top element of any pile and move it to any position in any pile, including the pile it was taken from.

Solution of the first example test case. Calculate the minimum number of operations required to make it so that the ii-th pile consists of cic_i zeros on the top and did_i ones on the bottom.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn (1n21051 \leq n \leq 2 \cdot 10^5) — the number of piles.

Then nn lines follow, the ii-th containing four integers aia_i, bib_i, cic_i, did_i (0ai,bi,ci,di1090 \leq a_i, b_i, c_i, d_i \leq 10^9) — the original and target state of the ii-th pile.

It is guaranteed that there exists a sequence of operations that transforms the piles into the target state.

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output

For each test case, output one integer — the minimum number of operations required to achieve the target state.

Note

The solution of the first test case is shown in the statement.

In the second test case, an optimal solution is to do the following:

In the third test case, all piles are already in their target state.

Samples

3
2
1 3 1 2
1 1 1 2
3
2 0 2 2
0 1 1 0
1 1 0 0
3
1 2 1 2
3 4 3 4
0 0 0 0
2
3
0

在线编程 IDE

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