#!/usr/bin/ruby                                                                                                                                                           
# -*- coding: utf-8 -*-                                                                                                                                                   

issue = {}
issue.default = 0
n = gets.to_i
n.times {
  product = gets.split(' ')
  issue[product[0]] += product[1].to_i
}

i = issue.keys.sort {|l, r|
  if l.length < r.length
    -1
  elsif l.length == r.length
    l <=> r
  else
    1
  end
}

i.each{ |x|
  puts "#{x} #{issue[x]}"
}