Marincho has a new passion - he loves to paint with watercolors. Now he wants to make a color sequence of cells. For each cell , Marincho has chosen a set of colors of size , which he will mix, to paint the cell fulfilling his aesthetic feel. He has already done an initial painting of all cells numbered from to 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 or (if they exist). Formally, if he has already painted all cells numbered from to , then the next possible cells to paint are:
- If and , then all cells have been painted.
- If and , then the next cell, which can be painted, is the one with number .
- If and , then the next cell, which can be painted, is the one with number .
- If and , then the next cell, which can be painted, is the one with number or .
Task
Unfortunately, after Marincho has painted the cells numbered from to according to the corresponding sets , 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 - pairs and .
Input data
The first line of the standard input contains the positive integer -- the number of cells. The following lines contain and which describe the number of colors of the corresponding set and the colors themselves (the colors are different positive integers). The next line contains the positive integer -- the number of initial paintings. The last lines contain pairs of positive integers and 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 , if it is possible to finish the painting or otherwise.
Constraints and clarifications
# | Score | Constraints |
---|---|---|
1 | 0 | Examples |
2 | 11 | , sum of |
3 | 18 | , sum of |
4 | 24 | , sum of |
5 | 36 | , sum of |
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 |
---|---|---|
The cell with number | ||
The cell with number | ||
The cell with number | ||
The cell with number | ||
- | We have made a successful painting of all cells. |
Note that if we painted the cell with number , 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 |
---|---|---|
The cell with number | ||
The cell with number | ||
The cell with number | ||
We cannot continue the painting. |
Note that if we painted the cell with number , at first, then the next paintings would have been the same and we would still fail. Therefore, the answer here is .