n = gets.to_i
h = Hash.new(0)
n.times{
    name, number = gets.chomp.split
    h[name] += number.to_i
}
h2 = h.to_a.map{|name, number| [name, name.length, number]}.sort_by{|name, length, number| [length, name]}
h2.each{|name, length, number| puts "#{name} #{number}"}
