Under construction

Still being built. Things may change

Translation

Interview

Time limit4sMemory limit1024 MB

Summary
Given a word-pair dictionary and a text, print the text after repeatedly translating A to B and back to A until it stops changing.
Level

Medium4 of 10

Topics
Hash map, String
Solved
No attempts yet

Problem

A simple translation program translates texts between two languages, call them AA and BB. The program translates a text word by word using a dictionary. To translate a word, it finds the first occurrence of that word in the dictionary and translates it accordingly.

If you translate a text from AA to BB with this program and then back to AA, you may not get the same text. In this problem you repeat the procedure until the text no longer changes. Specifically:

  1. Let aa be a text (a sequence of words) in language AA.
  2. Translate aa from AA to BB, and call the new text bb.
  3. Translate bb from BB to AA, and call the new text a′a'.
  4. If a≠a′a \neq a', start over from step 1 with a′a'. Otherwise the text is fully translated.

Given a dictionary and a text, print the text produced by the algorithm above.

Input

The first line contains an integer NN (1≤N≤50 0001 \leq N \leq 50\,000), the number of words in the dictionary.

The next NN lines each contain two words, with no spaces inside a word. Each word contains only the characters a-z and is at most 20 characters long. The first word is in language AA and the second is in language BB.

The next line contains an integer MM (1≤M≤100 0001 \leq M \leq 100\,000), the number of words in the text to translate. The last line contains the text, a sequence of words separated by spaces. It is guaranteed that every word in the text appears in the dictionary.

Output

Print one line: the text produced by the algorithm once it is finished.

Hint

The algorithm translates the sentence in the following steps:

  1. programmering ar valdigt roligt
  2. programmering ar valdigt skoj
  3. programmering ar valdigt kul

Examples1

  1. Example 1

    Input
    8
    programmering programming
    ar is
    valdigt very
    kul fun
    skoj fun
    mycket very
    skoj amusing
    roligt amusing
    4
    programmering ar mycket roligt
    
    Expected output
    programmering ar valdigt kul