Translation
InterviewTime limit4sMemory limit1024 MB
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.
Problem
A simple translation program translates texts between two languages, call them and . 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 to with this program and then back to , you may not get the same text. In this problem you repeat the procedure until the text no longer changes. Specifically:
- Let be a text (a sequence of words) in language .
- Translate from to , and call the new text .
- Translate from to , and call the new text .
- If , start over from step 1 with . 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 (), the number of words in the dictionary.
The next 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 and the second is in language .
The next line contains an integer (), 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:
programmering ar valdigt roligtprogrammering ar valdigt skojprogrammering ar valdigt kul