import Control.Monad (replicateM,replicateM_)
import Data.Array (Array,listArray,(!),(//))

main = do
    n <- readLn
    if n == 0 then return () else do
        conv <- readConv n
        m <- readLn
        replicateM_ m (getLine >>= return . (conv !) . head >>= putChar)
        putStrLn ""
        main

readConv :: Int -> IO (Array Char Char)
readConv n = do
    lines <- replicateM n getLine
    let
        conv = map ((\[a,b]->(head a, head b)) . words) lines
    return ((listArray ('0','z') ['0'..'z']) // conv)