Carlo is planning a trip to Budapest and has purchased two different travel guides.
The city is divided into quarters, numbered from to . Each guide contains an itinerary describing how to explore these quarters:
- Page of a guide contains information about quarter along with a (possibly empty) list of suggested nearby quarters.
- In each guide, every quarter except quarter is recommended exactly once.
Based on this, Carlo defines recursive tours for the guides as follows: the recursive tour starting at quarter (and relative to one of the guides) first visits , then recursively visits all quarters recommended on page of the guide, in the order they are given, and performs the recursive tour starting at .
Carlo observed that the recursive tour starting at quarter in both guides visit all quarters.
Curious about the differences between the two guides, Carlo compares tours between them. However, it is late night so he quickly becomes overwhelmed and texts you for help. You woke up with messages on the phone, each containing a question given as two integers and .
For each question, determine: do the quarters that appear in both the recursive tour starting at using the first guide
and the recursive tour starting at using the second guide appear in the same relative order in the tours? In other words, is the list of common quarters in both tours ordered the same way in both? Note that the answer is yes if the list of common quarters is empty.
Since you owe Carlo a favour, help him by answering all his questions.
Input data
The first line contains the integer , the number of quarters in Budapest.
The following lines describe the suggestions in the first guide: the -th () of them contains an integer followed by integers : the suggested quarters near quarter .
The following lines describe the suggestions in the second guide: the -th () of them contains an integer followed by integers : the suggested quarters near quarter .
The following line contains the integer : the number of questions.
The following lines describe the questions: the -th () of them contains two integers and , describing the question .
Output data
You need to output lines, the -th of them containing either YES
or NO
, the answer to question .
Constraints and clarifications
- .
- for each .
- for each .
- Each integer in appears exactly once in the lists .
- for each .
- Each integer in appears exactly once in the lists .
- .
- for each .
# | Score | Restrictions |
---|---|---|
0 | 0 | Examples |
1 | 27 | |
2 | 25 | for each |
3 | 27 | |
4 | 21 | No additional limitations. |
Example
stdin
7
3 2 5 3
0
0
1 6
0
2 1 4
0
1 6
3 3 4 2
0
0
0
0
2 5 1
4
5 6
0 1
5 5
3 6
stdout
YES
NO
YES
NO
Explanation
In the first sample case there are quarters and questions.
The first question asks about the tours starting from using the first guide and from using the second one.
- The first tour visits quarters , and , in this order.
- The second tour visits quarters , , , , and , in this order.
The quarters visited by both tours are , and , which appear in the same order in both tours. The answer is therefore YES
.
The second question asks about the tours starting from using the first guide and from using the second one.
- The first tour visits quarters , , , , , and , in this order.
- The second tour visits quarters , , and , in this order.
The quarters visited by both tours are , and , which appear in different orders. The answer is therefore NO
.
The third question asks about the tours starting from using the first guide and from using the second one.
- The first tour visits quarters , and , in this order.
- The second tour visits only quarter .
The only node visited by both tours is number . The answer is therefore YES
.
The fourth question asks about the tours starting from using the first guide and from using the second one.
- The first tour visits quarters and , in this order.
- The second tour visits quarters , , , , and , in this order.
The quarters visited by both tours are and , which appear in different orders. The answer is therefore NO
.