import qualified Data.Map.Lazy as ML
import Data.Maybe
import Control.Monad
import Data.Functor

main::IO ()
main=do
   n<-read <$> getLine::IO Int
   when (n>0) $
     do
        table<- replicateM n getLine
        let transMap=ML.fromList $ map (\(a:_:b:_)->(a,b)) table
        m<-read <$> getLine::IO Int
        str<-map head <$> replicateM m getLine
        putStrLn $ translate transMap str
        main


translate::Ord k=>ML.Map k k->[k]->[k]
translate mp str=map (\c->fromMaybe c (ML.lookup c mp)) str