Travelguide

Time limit: 2.5s Memory limit: 512MB Input: Output:

Carlo is planning a trip to Budapest and has purchased two different travel guides.

The city is divided into NN quarters, numbered from 00 to N1N-1. Each guide contains an itinerary describing how to explore these quarters:

  • Page ii of a guide contains information about quarter ii along with a (possibly empty) list of suggested nearby quarters.
  • In each guide, every quarter except quarter 00 is recommended exactly once.

Based on this, Carlo defines recursive tours for the guides as follows: the recursive tour starting at quarter ii (and relative to one of the guides) first visits ii, then recursively visits all quarters ss recommended on page ii of the guide, in the order they are given, and performs the recursive tour starting at ss.

Carlo observed that the recursive tour starting at quarter 00 in both guides visit all NN 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 QQ messages on the phone, each containing a question given as two integers UiU_i and ViV_i.

For each question, determine: do the quarters that appear in both the recursive tour starting at UiU_i using the first guide
and the recursive tour starting at ViV_i 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 NN, the number of quarters in Budapest.
The following NN lines describe the suggestions in the first guide: the ii-th (0i<N0 \le i < N) of them contains an integer KiK_i followed by KiK_i integers Si,jS_{i,j}: the suggested quarters near quarter ii.
The following NN lines describe the suggestions in the second guide: the ii-th (0i<N0 \le i < N) of them contains an integer KiK'_i followed by KiK'_i integers Si,jS'_{i,j}: the suggested quarters near quarter ii.
The following line contains the integer QQ: the number of questions.
The following QQ lines describe the questions: the ii-th (0i<Q0 \le i < Q) of them contains two integers UiU_i and ViV_i, describing the question ii.

Output data

You need to output QQ lines, the ii-th of them containing either YES or NO, the answer to question ii.

Constraints and clarifications

  • 1N400 0001 \le N \le 400 \ 000.
  • 0Ki,Ki<N0 \le K_i, K'_i < N for each i=0N1i=0\ldots N-1.
  • 0<Si,j<N,Si,ji0 < S_{i,j} < N, S{i,j} \neq i for each i=0N1,j=0Ki1i=0\ldots N-1, j = 0\ldots{K_i - 1}.
  • Each integer in 1N11 \ldots N-1 appears exactly once in the lists SS.
  • 0<Si,j<N,Si,ji0 < S'_{i,j} < N, S'{i,j} \neq i for each i=0N1,j=0Ki1i=0\ldots N-1, j = 0\ldots{K'_i - 1}.
  • Each integer in 1N11 \ldots N-1 appears exactly once in the lists SS'.
  • 1Q400 0001 \le Q \le 400 \ 000.
  • 0Ui,Vi<N0 \le U_i, V_i < N for each i=0Q1i=0 \ldots Q-1.
# Score Restrictions
0 0 Examples
1 27 N1 000,Q1 000N \leq 1 \ 000, Q \leq 1 \ 000
2 25 Vi=0V_i = 0 for each i=0Q1i=0\ldots Q-1
3 27 Q50 000Q \le 50 \ 000
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 77 quarters and 44 questions.

The first question asks about the tours starting from 55 using the first guide and from 66 using the second one.

  • The first tour visits quarters 55, 11 and 44, in this order.
  • The second tour visits quarters 66, 55, 11, 33, 44 and 22, in this order.

The quarters visited by both tours are 55, 11 and 44, which appear in the same order in both tours. The answer is therefore YES.

The second question asks about the tours starting from 00 using the first guide and from 11 using the second one.

  • The first tour visits quarters 00, 22, 55, 11, 44, 33 and 66, in this order.
  • The second tour visits quarters 11, 33, 44 and 22, in this order.

The quarters visited by both tours are 11, 22 33 and 44, which appear in different orders. The answer is therefore NO.

The third question asks about the tours starting from 55 using the first guide and from 55 using the second one.

  • The first tour visits quarters 55, 11 and 44, in this order.
  • The second tour visits only quarter 55.

The only node visited by both tours is number 55. The answer is therefore YES.

The fourth question asks about the tours starting from 33 using the first guide and from 66 using the second one.

  • The first tour visits quarters 33 and 66, in this order.
  • The second tour visits quarters 66, 55, 11, 33, 44 and 22, in this order.

The quarters visited by both tours are 33 and 66, which appear in different orders. The answer is therefore NO.

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