#CF2239F. 彩色作品 / F. Colorful Works

彩色作品 / F. Colorful Works

F. Colorful Works

Source: Codeforces 2239F
Contest: Codeforces Round 1105 (Div. 1)
Time limit: 5 seconds
Memory limit: 256 megabytes

Statement

Gold14526 is a painter. He can paint with nn colors, numbered 1,2,,n1, 2, \ldots, n. Color ii has a constraint interval [li,ri][l_i, r_i].

A work is defined as a rooted tree T=(V,E)T=(V,E) where every edge is colored (with one of the nn colors). A work is called colorful if the following conditions are satisfied:

  • For any three nodes u,v,wVu, v, w \in V, if edges (u,v)(u,v) and (v,w)(v,w) both exist, they must have different colors.
  • For all colors i[1,n]i \in [1,n], let d(u,i)d(u,i) denote the number of edges of color ii on the simple path from node uu to the root. Then maxuVd(u,i)[li,ri]\max_{u \in V} d(u,i) \in [l_i, r_i].

Two works T=(V,E)T=(V,E) and T=(V,E)T'=(V',E') are defined as isomorphic if and only if the following two conditions are met:

  • V=V\lvert V\rvert = \lvert V'\rvert;
  • There exists a bijection f:VVf:V \to V' such that:
  • Let rr be the root of TT and rr' be the root of TT'. Then f(r)=rf(r) = r';
  • For any (u,v)E(u,v) \in E, we have that (f(u),f(v))E(f(u),f(v)) \in E', and the color of edge (u,v)(u,v) is the same as the color of edge (f(u),f(v))(f(u),f(v)).

Gold14526 wants to know the maximum number of colorful works he can choose such that the works are pairwise non-isomorphic. Output the answer modulo 2\bf2.

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 an integer nn (1n21061\le n\le 2\cdot 10^6) — denoting the number of colors.

The following nn lines each contain two integers, the ii -th of them lil_i and rir_i (0liri21050\le l_i\le r_i\le 2\cdot 10^5, ri1r_i\ge 1) — denoting the constraint interval of the ii -th color.

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

Let m=maxi=1nrim=\max_{i=1}^n r_i. Then it is guaranteed that the sum of mm over all test cases does not exceed 21052\cdot 10^5.

Output

For each test case, output 00 or 11, representing the maximum number of works that can be chosen modulo 22.

Note

In the first test case, the constraints for both colors are [0,1][0, 1]. This means on any simple path from the root, there can be at most 11 edge of color 11 and at most 11 edge of color 22. There are exactly 99 valid pairwise non-isomorphic trees:

  • 11 tree with 11 node: just the root.
  • 22 trees with 22 nodes: the root is connected to a child by an edge of color 11, or by an edge of color 22.
  • 33 trees with 33 nodes:
  • the root is connected to two children by edges of color 11 and 22 respectively.
  • a path of 22 edges from the root, colored 11 then 22.
  • a path of 22 edges from the root, colored 22 then 11.
  • 22 trees with 44 nodes:
  • the root has a child via color 11 (which further has a child via color 22), and another child via color 22.
  • the root has a child via color 22 (which further has a child via color 11), and another child via color 11.
  • 11 tree with 55 nodes: the root is connected to two children by colors 11 and 22, and each of these children has exactly one child of the opposite color.

Since 91(mod2)9 \equiv 1 \pmod 2, the output is 11. In the second test case, the constraints for both colors are [1,1][1, 1]. Every valid tree must satisfy the maximum count of each color on the paths to be exactly 11. Therefore, the tree must contain at least one edge of color 11 and at least one edge of color 22. There are exactly 66 valid trees:

  • 33 trees with 33 nodes: the root connected to two children by colors 11 and 22; a path colored 11 then 22; a path colored 22 then 11.
  • 22 trees with 44 nodes: same as the two 44-node trees described in the first test case.
  • 11 tree with 55 nodes: same as the 55-node tree described in the first test case.

Since 60(mod2)6 \equiv 0 \pmod 2, the output is 00.

Examples

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