import Control.Applicative ((<$>))
import Control.Monad (replicateM, mapM_)
import Data.List
import Data.Map (Map)
import qualified Data.Map as M

main :: IO ()
main = do
  n <- getl read :: IO Int
  solve <$> replicateM n readInfo >>= mapM_ printTag
  where
    printTag :: (String, Int) -> IO ()
    printTag (t, p) = putStrLn $ t ++ " " ++ show p

solve :: [[(String, Int)]] -> [(String, Int)]
solve xs = take 10 $ sortBy f $ M.toList mp
  where mp = M.fromListWith (+) (concat xs)
        f (ta, pa) (tb, pb) = if pa == pb then compare ta tb else compare pb pa

getl :: (String -> a) -> IO a
getl f = f <$> getLine

readInfo :: IO [(String, Int)]
readInfo = do
  getLine
  [_, p] <- getl (map read . words)
  tgs <- getl words
  return $ zip tgs (repeat p)