Parrots

Time limit1sMemory limit512 MB

Summary
Given N parrot sentences and one written sequence, decide whether distinct words can interleave so that each parrot's words stay in order and no word repeats.
Level

Medium7 of 10

Topics
Simulation, Greedy, Hash map, Implementation
Solved
No attempts yet

Problem

While flying around the world in their private plane, pps789 and cseteram lost an engine and crash landed on an unnamed island. Exploring it, they learned that the parrots living there imitate human speech remarkably well. The two split up to explore separately and agreed to use the parrots if they had to contact each other.

A month later pps789 found decisive evidence about the secret of the island. He wanted to share the discovery with cseteram, but it was far too long for a single parrot to memorize. So pps789 split the discovery across NN parrots and sent them flying to cseteram.

On the other side of the island, cseteram was startled when NN parrots arrived and each started talking. He realized that pps789 was sending a long message, but he wrote the words down in the order he heard them, and the order came out so tangled that the original text was lost. What he did work out, by watching the parrots, were a few rules.

  1. Each parrot memorizes one sentence. A sentence is made of several words, and the parrot says those words in the memorized order.
  2. After a parrot says a word and before it says the next one there is a short gap, and during that gap another parrot can cut in and say its own sentence.
  3. While a parrot is in the middle of saying a word, no other parrot cuts in.
  4. No word appears twice across all of the sentences the parrots say.

Each parrot says its memorized sentence to the end and then flies back to pps789, and cseteram writes down words until every parrot has left. You are given the sentence SiS_i that pps789 gave to each parrot and the sentence LL that cseteram wrote down. Decide whether LL can come out under the rules above.

Input

The first line contains the number of parrots NN (1≤N≤1001 \le N \le 100).

Each of the next NN lines contains the sentence SiS_i (1≤i≤N1 \le i \le N) that one parrot says, one sentence per line. The words of a sentence are separated by a single space. Sentence SiS_i has between 1 and 100 words, and each word consists of 1 to 32 lowercase English letters.

Line N+2N + 2 contains the sentence LL that cseteram wrote down. LL has between 1 and 10000 words, and each word consists of 1 to 32 lowercase English letters.

Output

Print Possible if the sentence LL can come out, and Impossible otherwise.

Examples3

  1. Example 1

    Input
    3
    i want to see you
    next week
    good luck
    i want next good luck week to see you
    
    Expected output
    Possible
    
  2. Example 2

    Input
    2
    i found
    an interesting cave
    i found an cave interesting
    
    Expected output
    Impossible
    
  3. Example 3

    Input
    2
    please
    be careful
    pen pineapple apple pen
    
    Expected output
    Impossible