import Control.Monad(replicateM) import Data.List(sortOn) import qualified Data.Map.Strict as Map main = do [n,m]<-map read.words<$>getLine sa <- Map.fromList<$>replicateM n (do [a,b] <- words<$>getLine pure(a,read b :: Int)) tb <- replicateM m (do [a,b] <- words<$>getLine pure(a,read b :: Int)) mapM_ (putStrLn.uncurry((.((' ' :).show)).(++)))$solve sa tb solve = (Map.toAscList .) . foldl go where go ans (t, b) | Map.member t ans = Map.adjust(const b) t ans | otherwise = Map.insert t b ans