CF1920A.Satisfying Constraints

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

Satisfying Constraints

Alex is solving a problem. He has nn constraints on what the integer kk can be. There are three types of constraints:

  1. kk must be greater than or equal to some integer xx;
  2. kk must be less than or equal to some integer xx;
  3. kk must be not equal to some integer xx.

Help Alex find the number of integers kk that satisfy all nn constraints. It is guaranteed that the answer is finite (there exists at least one constraint of type 11 and at least one constraint of type 22). Also, it is guaranteed that no two constraints are the exact same.

Input

Each test consists of multiple test cases. The first line contains a single integer tt (1t5001 \leq t \leq 500) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (2n1002 \leq n \leq 100) — the number of constraints.

The following nn lines describe the constraints. Each line contains two integers aa and xx (a{1,2,3},1x109a \in \{1,2,3\}, \, 1 \leq x \leq 10^9). aa denotes the type of constraint. If a=1a=1, kk must be greater than or equal to xx. If a=2a=2, kk must be less than or equal to xx. If a=3a=3, kk must be not equal to xx.

It is guaranteed that there is a finite amount of integers satisfying all nn constraints (there exists at least one constraint of type 11 and at least one constraint of type 22). It is also guaranteed that no two constraints are the exact same (in other words, all pairs (a,x)(a, x) are distinct).

Output

For each test case, output a single integer — the number of integers kk that satisfy all nn constraints.

Note

In the first test case, k3k \geq 3 and k10k \leq 10. Furthermore, k1k \neq 1 and k5k \neq 5. The possible integers kk that satisfy the constraints are 3,4,6,7,8,9,103,4,6,7,8,9,10. So the answer is 77.

In the second test case, k5k \ge 5 and k4k \le 4, which is impossible. So the answer is 00.

Samples

6
4
1 3
2 10
3 1
3 5
2
1 5
2 4
10
3 6
3 7
1 2
1 7
3 100
3 44
2 100
2 98
1 3
3 99
6
1 5
2 10
1 9
2 2
3 2
3 9
5
1 1
2 2
3 1
3 2
3 3
6
1 10000
2 900000000
3 500000000
1 100000000
3 10000
3 900000001
7
0
90
0
0
800000000

在线编程 IDE

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