import Control.Monad
import Control.Applicative

type ConvertList = [(Char, Char)]
type Sentence    = String

main = do
    input <- getData []
    mapM_ putStrLn $ map convertStr input

getData :: [(ConvertList, Sentence)] -> IO [(ConvertList, Sentence)]
getData list = do
    n <- read<$>getLine
    if n == 0 then return list
              else do
                  convs <- map ((\[a,b] -> (a,b)).map head.words)<$>replicateM n getLine :: IO ConvertList 
                  m <- read<$>getLine
                  sentence <- concat<$>replicateM m getLine :: IO Sentence
                  getData (list ++ [(convs, sentence)])

convertStr :: (ConvertList, Sentence) -> Sentence
convertStr (_, [])      = []
convertStr (cl, (x:xs)) = convertChar cl x : convertStr (cl, xs)

convertChar cl x = case lookup x cl of
                     Just x' -> x'
                     Nothing -> x