Stack 'em Up

Time limit1sMemory limit128 MB

Problem

A standard deck has 52 cards: 13 values in each of four suits. In order, the values are 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace. The suits are Clubs, Diamonds, Hearts, Spades. A card is identified uniquely by its value and suit, written as <value> of <suit> — for example "9 of Hearts" or "King of Spades". A brand-new deck is ordered first alphabetically by suit, and then by value in the order given above.

A crooked dealer knows several shuffles. Each shuffle rearranges the cards in exactly the same way every time it is used. For example, the simple "bottom card" shuffle removes the bottom card and places it on top. By combining the shuffles she knows, the dealer can arrange the deck into almost any order.

You are given the full list of shuffles the dealer knows, together with a record of which shuffle she uses at each step. Predict the order of the deck after each shuffle in the sequence.

Input

The first line contains an integer $n$ ($n \le 100$), the number of shuffles the dealer knows.

Then $52n$ integers follow. Each consecutive group of 52 integers describes one shuffle and is a permutation of $1$ through $52$. Within a group, if the value $i$ appears at position $j$, the shuffle moves the card at position $i$ to position $j$ — that is, position $j$ of the new order receives the card that was at position $i$.

After the shuffle descriptions, several lines follow. Each contains an integer $k$ ($1 \le k \le n$), meaning the dealer applies the $k$-th shuffle from the input.

Output

The dealer starts with a brand-new deck ordered as described above. Apply the observed shuffles in order, keeping the deck's state between shuffles. After each shuffle, print the names of the 52 cards in their new order, one per line. Print a single blank line between the outputs of two consecutive shuffles.