{-# LANGUAGE OverloadedStrings #-} import Control.Monad(replicateM) import Data.Maybe(fromJust) import qualified Data.Map.Strict as Map import qualified Data.ByteString.Char8 as BS main = do [n,m]<-map(fst.fromJust.BS.readInt).BS.words<$>BS.getLine sa <- Map.fromList<$>replicateM n (do [a,b] <- BS.words<$>BS.getLine pure(a,fst(fromJust(BS.readInt b)) )) tb <- replicateM m (do [a,b] <- BS.words<$>BS.getLine pure(a,fst(fromJust(BS.readInt b)) )) mapM_ (BS.putStrLn.uncurry((.(BS.pack.show)).BS.append.flip BS.append " "))$solve sa tb solve = (Map.toAscList .) . foldl go where go ans (t,b) = Map.insertWith const t b ans