import Data.List
import Data.List.Split
import Data.Function
import Data.Map(fromList,keys,member,(!))

main = do
  s <- getContents
  let [u,v] =splitOn "\n\n" s
  mapM_ (\x->putStrLn $ (show.fst$x) ++ " "++ (show.snd$x)) $ aux u v
  
aux :: String -> String -> [(Int,Int)]
aux u v =   [(x,fore!x+aft!x)|x<-keys fore,x `member` aft]
  where
    insort :: String -> [[Int]]
    insort s = sortBy (\x y->if (head x) < (head y) then LT else GT) . map (map (read::String->Int).splitOn ",") . lines $ s
    
    grp :: [[Int]] -> [(Int,Int)]
    grp w =map (\x->((head.head $ x),length x) ). groupBy ((==) `on` head) $ w
    fore= fromList . grp . insort $ u
    aft = fromList . grp . insort $ v