import Control.Monad
import Data.Maybe

main = do
    n <- readLn
    if n == 0 then return ()
              else do
                convlist <- getConvertList n
                sentence <- getSentence
                putStrLn $ convertStr convlist sentence
                main

getConvertList n = do
    convlist <- replicateM n $ fmap words getLine
    return $ foldr (\[x,y] -> (:) (head x,head y)) [] convlist

getSentence = do
    n <- readLn
    return =<< fmap concat $ replicateM n getLine

convertStr _    []     = []
convertStr list (c:cs) = (fromMaybe c $ lookup c list) : convertStr list cs