module Main where import Control.Monad import Data.List (sortBy) import qualified Data.Map.Strict as M import Text.Printf (printf) main :: IO () main = do n <- readLn ks <- fmap concat . replicateM n $ do _ <- getLine [_, s] <- fmap read . words <$> getLine :: IO [Int] ts <- words <$> getLine return $ zip ts (repeat s) let m = M.fromListWith (+) ks let as = sortBy by $ M.toList m forM_ (take 10 as) $ \(a, b) -> do printf "%s %d\n" a b where by (k1, v1) (k2, v2) | v1 == v2 = k1 `compare` k2 | otherwise = v2 `compare` v1