import Data.List.Split (splitOn)
import Data.List (sort)

main = do
  [lm,tm] <- getData
  let customers = sort $ goodCustomer lm tm
  mapM_ (\x -> putStr (show x ++ " ") >> print (tradeTimes x [lm,tm])) customers


getData = do
  lm <- lastMonth
  tm <- thisMonth
  return [lm,tm]

lastMonth :: IO [Int]
lastMonth = do
  line <- getLine
  if null line then return []
               else do
                 dataSet <- lastMonth
                 return $ (head.map read.splitOn "," $ line) : dataSet

thisMonth :: IO [Int]
thisMonth = do
  inputs <- fmap lines getContents 
  return $ map (head.map read.splitOn ",") inputs

goodCustomer :: [Int] -> [Int] -> [Int]
goodCustomer [] _ = []
goodCustomer (l:ls) tm = if l `elem` tm then l : goodCustomer ls tm
                                        else goodCustomer ls tm

tradeTimes :: Int -> [[Int]] -> Int
tradeTimes cid trades = sum.map (length.filter (== cid)) $ trades