Under construction

Still being built. Things may change

Shuffle Machine

Interview

Time limit1.5sMemory limit1024 MB

Summary
For each of the K shuffles, apply technique X_i a total of Y_i times in order, starting from the sorted deck, and print the final card order.
Level

Medium4 of 10

Topics
Array, Divide and conquer
Solved
No attempts yet

Problem

Taejun bought a card shuffling machine to play card games with his friends. Wanting to win every game, Taejun analyzes the machine's structure to predict the shuffle results.

The machine has MM built-in shuffle techniques. The ii-th shuffle technique is represented by a sequence SiS_i of length NN in which each number from 11 to NN appears exactly once. When the ii-th shuffle technique is run once, the card at position jj from the top moves to position SijS_{ij} from the top.

When the machine is started, it performs KK shuffles in the given order and then stops. Each shuffle is given by two integers XiX_i and YiY_i, which means that shuffle technique XiX_i is run YiY_i times.

Initially, the cards are sorted from 11 to NN from top to bottom. After the machine is started, find the final arrangement of the cards.

Input

The first line contains NN, MM, and KK, separated by spaces.

In each of the next MM lines, the ii-th line contains Si1,Si2,⋯ ,SiNS_{i1}, S_{i2}, \cdots, S_{iN} in order, separated by spaces, which represent the ii-th shuffle technique.

In each of the next KK lines, the ii-th line contains the number XiX_i of the shuffle technique to run in the ii-th shuffle and the number of times YiY_i to run it, separated by a space.

Output

Print the final order of the cards from top to bottom after the machine is started.

Constraints

  • All given numbers are integers.
  • 2≤N≤1 0002 \leq N \leq 1\,000
  • 1≤M≤1 0001 \leq M \leq 1\,000
  • 1≤K≤1 0001 \leq K \leq 1\,000
  • Each of Si1,Si2,⋯ ,SiNS_{i1}, S_{i2}, \cdots, S_{iN} contains each number from 11 to NN exactly once. (1≤i≤M1 \leq i \leq M)
  • 1≤Xi≤M1 \leq X_i \leq M (1≤i≤K1 \leq i \leq K)
  • 1≤Yi≤1091 \leq Y_i \leq 10^9 (1≤i≤K1 \leq i \leq K)

Examples2

  1. Example 1

    Input
    6 2 3
    6 5 4 3 2 1
    2 4 6 1 3 5
    1 3
    2 1
    1 2
    
    Expected output
    3 6 2 5 1 4
    
  2. Example 2

    Input
    3 2 3
    2 3 1
    3 1 2
    1 2024
    2 7
    1 28
    
    Expected output
    2 3 1