Under construction

Still being built. Things may change

Stack 'em Up

Time limit1sMemory limit128 MB

Summary
Apply a sequence of fixed 52-card permutation shuffles to a new deck, printing the deck after each shuffle.
Level

Easy3 of 10

Topics
Simulation, Implementation, Array
Solved
No attempts yet

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 nn (n≤100n \le 100), the number of shuffles the dealer knows.

Then 52n52n integers follow. Each consecutive group of 52 integers describes one shuffle and is a permutation of 11 through 5252. Within a group, if the value ii appears at position jj, the shuffle moves the card at position ii to position jj — that is, position jj of the new order receives the card that was at position ii.

After the shuffle descriptions, several lines follow. Each contains an integer kk (1≤k≤n1 \le k \le n), meaning the dealer applies the kk-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.

Examples1

  1. Example 1

    Input
    2
    2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
    27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 52 51
    52 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
    27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 1
    1
    2
    
    Expected output
    3 of Clubs
    2 of Clubs
    4 of Clubs
    5 of Clubs
    6 of Clubs
    7 of Clubs
    8 of Clubs
    9 of Clubs
    10 of Clubs
    Jack of Clubs
    Queen of Clubs
    King of Clubs
    Ace of Clubs
    2 of Diamonds
    3 of Diamonds
    4 of Diamonds
    5 of Diamonds
    6 of Diamonds
    7 of Diamonds
    8 of Diamonds
    9 of Diamonds
    10 of Diamonds
    Jack of Diamonds
    Queen of Diamonds
    King of Diamonds
    Ace of Diamonds
    2 of Hearts
    3 of Hearts
    4 of Hearts
    5 of Hearts
    6 of Hearts
    7 of Hearts
    8 of Hearts
    9 of Hearts
    10 of Hearts
    Jack of Hearts
    Queen of Hearts
    King of Hearts
    Ace of Hearts
    2 of Spades
    3 of Spades
    4 of Spades
    5 of Spades
    6 of Spades
    7 of Spades
    8 of Spades
    9 of Spades
    10 of Spades
    Jack of Spades
    Queen of Spades
    Ace of Spades
    King of Spades
    
    King of Spades
    2 of Clubs
    4 of Clubs
    5 of Clubs
    6 of Clubs
    7 of Clubs
    8 of Clubs
    9 of Clubs
    10 of Clubs
    Jack of Clubs
    Queen of Clubs
    King of Clubs
    Ace of Clubs
    2 of Diamonds
    3 of Diamonds
    4 of Diamonds
    5 of Diamonds
    6 of Diamonds
    7 of Diamonds
    8 of Diamonds
    9 of Diamonds
    10 of Diamonds
    Jack of Diamonds
    Queen of Diamonds
    King of Diamonds
    Ace of Diamonds
    2 of Hearts
    3 of Hearts
    4 of Hearts
    5 of Hearts
    6 of Hearts
    7 of Hearts
    8 of Hearts
    9 of Hearts
    10 of Hearts
    Jack of Hearts
    Queen of Hearts
    King of Hearts
    Ace of Hearts
    2 of Spades
    3 of Spades
    4 of Spades
    5 of Spades
    6 of Spades
    7 of Spades
    8 of Spades
    9 of Spades
    10 of Spades
    Jack of Spades
    Queen of Spades
    Ace of Spades
    3 of Clubs