import Control.Monad
import Control.Applicative

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

main = do
  n <- readLn
  if n == 0 then return ()
            else do
              cl <- getConvertList n
              m  <- readLn
              st <- getSentence m
              putStrLn.convertStr cl $ st
              main


getConvertList :: Int -> IO ConvertList
getConvertList n = do
  convl <- replicateM n getElem
  return convl

getElem = do
  [a,b] <- map head.words<$>getLine
  return (a,b)

getSentence :: Int -> IO Sentence
getSentence n = do
  return.concat =<< replicateM n getLine

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

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