14441 - Trading Card Game   

Description

There are N players in a trading card game(集換式卡牌遊戲), each player has a card binder that holds a certain number of cards. Player i 's binder has Mi card slots, and initially, the j-th slot of player 's binder holds a unique card numbered Ai, j.

Since this is a trading card game, "card trading" is naturally an essential aspect. You are given T trades between the players. Each trade involves 4 numbers: x1, y1i , x2i , and y2i . This means player x1i trades the card in slot y1i of their binder with the card in slot y2i of player x2i ’s binder.

The trades occur in the order they are given. That is, the i-th trade happens before the (i +1)-th trade.

After all the trades are completed, you need to answer Q queries. Each query consists of a number, numi. For each query, you need to report two numbers, pi and ci, representing that the card numbered numi is in slot ci of player pi's binder after all the trades have taken place.

Input

The first line of input contains an integer N, representing the number of players.

The second line contains N integers M1, M2, ..., MN, where Mi represents the capacity (number of cards) of the i-th player's card binder.

The next N lines, where the i-th line contains Mi integers, represent the initial card numbers in the binder of the i-th player: Ai, 1, Ai, 2, ..., Ai, Mi.

After that, there is an integer T representing the number of card trades.

The next T lines each contain 4 integers x1i, y1i, x2i, y2i, indicating that the card in the y1-th slot of the x1-th player’s binder is traded with the card in the y2-th slot of the x2-th player’s binder.

The final line contains an integer Q, representing the number of queries.

The next line contains Q integers num1, num2, ..., num, separated by spaces, representing queries asking for the locations of the cards numbered numi.

 

Constraints

  • 1 ≤ ≤ 1000
  • 1 ≤ Q ≤ 105
  • 0 ≤ T ≤ 106
  • 1 ≤ Mi ≤ 105 , Σ Mi ≤ 106
  • 1 ≤ Ai, j ≤ Σ Mi 
  • for all ( i, j ) ≠ ( k, l ) ,  Ai, j ≠ Ak, l 
  • 1 ≤ x1,x2 ≤ N
  • for all α = x1, 1 ≤ y1i ≤ Mα
  • for all α = x2, 1 ≤ y2i ≤ Mα
  • 1 ≤ numi ≤ Σ Mi 

 

Subtasks

  • Testcases 1: N = 1 ; T = 0
  • Testcases 2~4: N = 1 ; T,Q ≤ 100 
  • Testcases 5~6: N = 1
  • Testcases 7: N = 2 ; Q ≤ 100
  • Testcases 8: N,Q ≤ 100
  • Testcases 9~10: No additional restrictions.

Output

For each query, output two integers, pi and ci representing that the card numbered numi is in the ci-th slot of the pi-th player's binder after all the trades have taken place.

Please remember to print "\n" at the end of each query.

When outputting pi and ci, ensure there is a space between them, and make sure there is no space after ci. Otherwise, you may get a Presentation Error.

Sample Input  Download

Sample Output  Download

Tags




Discuss