import Data.List (sortBy, groupBy)
import Control.Monad (forM_)

readTestcase :: IO [(String, Int)]
readTestcase = do
    nStr <- getLine
    readData (read nStr :: Int) []

readData :: Int -> [(String, Int)] -> IO [(String, Int)]
readData 0 res = do return res
readData n res = do
    line <- getLine
    let [id, numStr] = words line
    let num = read numStr :: Int
    readData (n - 1) ((id, num) : res)

-- False < True
compare' :: (String, Int) -> (String, Int) -> Ordering
compare' (a, _) (b, _)
    | (length a) /= (length b) = compare (length a) (length b)
    | otherwise = compare a b

sort' :: [(String, Int)] -> [(String, Int)]
sort' = sortBy compare'

equalFirst :: (String, Int) -> (String, Int) -> Bool
equalFirst x y = (fst x) == (fst y)

groupBy' :: [(String, Int)] -> [[(String, Int)]]
groupBy' = groupBy equalFirst

sum' :: [(String, Int)] -> (String, Int)
sum' xs = (id, acc)
    where
        id = fst $ head xs
        acc = sum $ map snd xs

main = do
    dat <- readTestcase
    let res = (map sum' (groupBy' (sort' dat)))
    forM_ res (\ (id, num) -> putStrLn (id ++ " " ++ (show num)))

