## main

n = gets.strip.to_i

sums = Hash.new(0)

n.times do
  name, qty = gets.strip.split(' ')
  qty = qty.to_i

  sums[name] += qty
end

names = sums.keys.sort{|a, b| (c = a.length <=> b.length) != 0 ? c : a <=> b}

for name in names
  puts [name, sums[name]].join(' ')
end