aquarelle

Time limit: 0.35s Memory limit: 256MB Input: Output:

Marincho has a new passion - he loves to paint with watercolors. Now he wants to make a color sequence of NN cells. For each cell i (1iN)i \ (1 \le i \le N), Marincho has chosen a set of colors AiA_i of size KiK_i, which he will mix, to paint the cell fulfilling his aesthetic feel. He has already done an initial painting of all cells numbered from ll to rr in the described manner and is now about to paint the remaining cells. Marincho follows a strictly defined order of painting the cells - the next cell that can be painted will be the one with number l1l-1 or r+1r+1 (if they exist). Formally, if he has already painted all cells numbered from LL to RR, then the next possible cells to paint are:

  • If L=1L = 1 and R=NR = N, then all cells have been painted.
  • If L=1L = 1 and R<NR < N, then the next cell, which can be painted, is the one with number R+1R+1.
  • If L>1L > 1 and R=NR = N, then the next cell, which can be painted, is the one with number L1L-1.
  • If L>1L > 1 and R<NR < N, then the next cell, which can be painted, is the one with number L1L-1 or R+1R+1.

Task

Unfortunately, after Marincho has painted the cells numbered from ll to rr according to the corresponding sets Al,Al+1,,ArA_l, A_{l+1}, \dots, A_r, Marincho thinks that the sequence has become too monochromatic. Therefore, he decided that for every next cell, he paints, there has to be at least one new color, which hasn't been used in the painted cells until now. Help Marincho by writing a program that finds whether it is possible to paint the remaining cells. Also, you have to find the answer for a few initial paintings - QQ pairs ll and rr.

Input data

The first line of the standard input contains the positive integer NN -- the number of cells. The following NN lines contain KiK_i and c1 c2 cKic_1 \ c_2 \dots \ c_{K_i} which describe the number of colors of the corresponding set AiA_i and the colors themselves (the colors are different positive integers). The next line contains the positive integer QQ -- the number of initial paintings. The last QQ lines contain pairs of positive integers ll and rr that specify which sequence has been painted initially.

The numbers in each line are separated by one space.

Output data

For each initial painting, in input order, print 11, if it is possible to finish the painting or 00 otherwise.

Constraints and clarifications

  • 1N5×1051 \leq N \leq 5 \times 10^5
  • 1Ki1.2×106,K1+K2++KN1.2×1061 \leq K_i \leq 1.2 \times 10^6, K_1 + K_2 + \dots + K_N \leq 1.2 \times 10^6
  • 1cj1061 \leq c_j \leq 10^6
  • 1lrN1 \leq l \leq r \leq N
  • 1Q51 \leq Q \leq 5
# Score Constraints
1 0 Examples
2 11 N15N \leq 15, sum of Ki25K_i \leq 25
3 18 N500N \leq 500, sum of Ki1 100K_i \leq 1 \ 100
4 24 N3 000N \leq 3 \ 000, sum of Ki5×105K_i \leq 5 \times 10^5
5 36 N105N \leq 10^5, sum of Ki5×105K_i \leq 5 \times 10^5
6 11 No additional constraints

Example

stdin

6
2 5 3
1 4
1 1
1 2
2 3 4
1 6
3
3 4
2 3
5 5

stdout

1
1
0

Explanation

Let we analyze the first initial painting. Optimal sequence of paintings is described in the following table.

Painted cells Remaining paints Paint
3,43, 4 3,4,5,63, 4, 5, 6 The cell with number 22
2,3,42, 3, 4 3,5,63, 5, 6 The cell with number 55
2,3,4,52, 3, 4, 5 5,65, 6 The cell with number 11
1,2,3,4,51, 2, 3, 4, 5 66 The cell with number 66
1,2,3,4,5,61, 2, 3, 4, 5, 6 - We have made a successful painting of all cells.

Note that if we painted the cell with number 55, at first, we would not have been able to successfully paint all cells.

Let we analyze the third initial painting.

Painted cells Remaining paints Paint
55 1,2,5,61, 2, 5, 6 The cell with number 44
4,54, 5 1,5,61, 5, 6 The cell with number 33
3,4,53, 4, 5 5,65, 6 The cell with number 66
3,4,5,63, 4, 5, 6 55 We cannot continue the painting.

Note that if we painted the cell with number 66, at first, then the next paintings would have been the same and we would still fail. Therefore, the answer here is 00.

Log in or sign up to be able to send submissions!