Go: package main

import (
	"fmt"
	"sort"
)

type node struct {
	Key string
	Num int
}

func main() {
	var n int
	fmt.Scan(&n)
	m := make(map[string]int)
	for i := 0; i < n; i++ {
		var s string
		var num int
		fmt.Scan(&s, &num)
		if _, ok := m[s]; !ok {
			m[s] = 0
		}
		m[s] += num
	}
	list := make([]node, 0, len(m))
	for k, v := range m {
		list = append(list, node{Key: k, Num: v})
	}
	sort.Slice(list, func(i, j int) bool {
		if len(list[i].Key) == len(list[j].Key) {
			return list[i].Key < list[j].Key
		} else {
			return len(list[i].Key) < len(list[j].Key)
		}
	})
	for _, v := range list {
		fmt.Println(v.Key, v.Num)
	}
}

Python: o={}
for _ in range(int(input())):p,m=input().split();o[p]=o.get(p,0)+int(m)
k=sorted([(len(x),x) for x in o.keys()])
for _,i in k:print(i,o[i])

Python: o={}
for _ in range(int(input())):p,m=input().split();o[p]=o.get(p,0)+int(m)
k=sorted([(len(x),x) for x in list(o.keys())])
for _,i in k:print(i,o[i])

Python: d={}
for _ in[0]*int(input()):
 k,v=input().split()
 d[k]=d.get(k,0)+int(v)
for k in sorted(d,key=lambda x:(sum(27**i*(ord(k)-64)for i,k in enumerate(x[::-1])),x)):
 print(k,d[k])

Python: n=int(input())
d=dict()
for i in range(n):
    a,b=input().split()
    if a in d:d[a]+=int(b)
    else:d[a]=int(b)
s=list(d.items())
s.sort(key=lambda y: y[0])
s.sort(key=lambda y: len(y[0]))
for i in s:print(i[0],i[1])

Python: data = {}
for _ in range(int(input())):
    p,n = input().split();
    data[p] = data.get(p,0) + int(n)
k = sorted([[len(a),a] for a in data.keys()])
for _, i in k:
    print (i, data[i])

Python: x ={}
n = int(input())
for i in range(n):
    k,v = input().split()
    if k in x:
        x[k] += int(v)
    else:
        x[k] = int(v)
p=list(x.items())
p.sort(key=lambda x: x[0])
p.sort(key=lambda x: len(x[0]))
for i in p:
    print(i[0],i[1])

Python: d={}
for _ in[0]*int(input()):
 k,v=input().split()
 d[k]=d.get(k,0)+int(v)
for k in sorted(d,key=lambda x:(len(x),x)):
 print(k,d[k])

Python: n = int(input())
dic = {}
for i in range(n):
	a, b = input().split()
	a = a.zfill(5)  # キーを0埋め5桁
	if a in dic:
		dic[a] = dic[a] + int(b)
	else:
		dic[a] = int(b)

dic = sorted(dic.items())
# 出力部
for i in dic:
	print('{0} {1}'.format(i[0].replace('0', ''), i[1]))

Python: d={}
for _ in[0]*int(input()):
 k,v=input().split()
 d[k]=d.get(k,0)+int(v)
for k in sorted(d,key=lambda x:(sum(27**i*(ord(k)-64)for i,k in enumerate(x[::-1])),x)):
 print(k,d[k])

Python: data = {}
for i in range(int(input())):
	p,n = input().split();
	data[p] = data.get(p,0) + int(n)
for i, j in sorted([[len(a),a] for a in data.keys()]):
    print (j, data[j])

Python: o={}
for _ in range(int(input())):p,m=input().split();o[p]=o.get(p,0)+int(m)
k=sorted([(len(x),x)for x in o.keys()])
for _,i in k:print(i,o[i])

Python: N = int(input())
pdict = {}

p, n = [], []

for i in range(N) :
    _p, _n = input().split()
    _n = int(_n)
    
    if _p not in pdict.keys() :
        pdict[_p] = _n
    else :
        pdict[_p] += _n
        
plist = list(pdict.keys())

# fetch maximum length
max_len = 0
for i in set(plist) :
    max_len = max(max_len, len(i))

# append  key array corresponding to maximum length
plist.sort()

keys_sorted = []

for i in range(max_len + 1) :
    for j in plist :
        if i == len(j) :
            keys_sorted.append(j)
            
for i in keys_sorted :
    print(i, pdict[i])

Python: d={}
for _ in[0]*int(input()):
 k,v=input().split()
 d[k]=d.get(k,0)+int(v)
for k in sorted(d,key=lambda x:(len(x),x)):
 print(k,d[k])

Python: d={}
for _ in[0]*int(input()):
 k,v=input().split()
 d[k]=d.get(k,0)+int(v)
for l,k in sorted([len(x),x]for x in d):
 print(k,d[k])

Python: dic = {}
n = int(input())
for i in range(n):
  name, num = input().split()
  num = int(num)
  l = len(name)
  if (l, name) in dic:
    dic[(l, name)] += num
  else:
    dic[(l, name)] = num
dic = sorted(dic.items())
for d in dic:
  print(d[0][1], d[1])

Python: p={}
for _ in range(int(input())):
    a,b=input().split()
    if a in p:p[a]+=int(b)
    else:p[a]=int(b)
for i in sorted(p,key=lambda x:(len(x),x)):
    print(i,p[i])

Python: # AOJ 0589: Production
# Python3 2018.6.30 bal4u

dic = {}
n = int(input())
for i in range(n):
	a, p = input().split()
	if a in dic: dic[a] += int(p)
	else: dic[a] = int(p)
s = list(dic.items())
s.sort(key=lambda x:(len(x[0]),x[0]))
for x in s: print(*x)

Python: n = int(input())

namelist = []
numlist = []
lenlist = []
for _ in range(n):
    name, num = map(str, input().split())
    if name in namelist:
        numlist[namelist.index(name)] += int(num)
    else:
        namelist.append(name)
        numlist.append(int(num))
        lenlist.append(len(name))

data = []
for name, num, l in zip(namelist, numlist, lenlist):
    data.append([name, num, l])

data = sorted(data, key=lambda x: (x[2], x[0]))

for d in data:
    print(d[0], d[1])

Python: nums = int(input())
data = {}
for i in range(nums):
    name, num = input().split()
    data[name] = data.get(name, 0) + int(num)
sorteddata = sorted(list(data.items()), key=lambda x: (len(x[0]), x))
for name, num in sorteddata:
    print(name, num)

Python: n = int(input())
d, order = {}, {}
for _ in range(n):
    a, num = input().split()
    d[a] = d[a] + int(num) if a in d else int(num)
    if len(a) in order:
        if a not in order[len(a)]:
            order[len(a)].append(a)
    else:
        order[len(a)] = []
        order[len(a)].append(a)
for k, v in sorted(order.items(), key=lambda x: x[0]):
    for a in sorted(v):
        print(a, d[a])

Python: d={}
for _ in[0]*int(input()):
 k,v=input().split()
 d[k]=d.get(k,0)+int(v)
for l,k in sorted([len(x),x]for x in d):
 print(k,d[k])

Python: data = {}
for i in range(int(input())):
	p,n = input().split();
	data[p] = data.get(p,0) + int(n)
for i, j in sorted(data.items()):
    print (i, j)

Python: o={}
for _ in range(int(input())):p,m=input().split();o[p]=o.get(p,0)+int(m)
k=sorted([(len(x),x) for x in list(o.keys())])
for i,j in k:print(i,o[j])

Python: d={}
for _ in[0]*int(input()):
 k,v=input().split()
 d[k]=d.get(k,0)+int(v)
for l,k in sorted([len(x),x]for x in d):
 print(k,d[k])

Python: # coding=utf-8

if __name__ == '__main__':
    N = int(input())
    product_dict = {}
    for i in range(N):
        p_name, number = input().split()
        number = int(number)
        try:
            product_dict[p_name] += number
        except KeyError:
            product_dict[p_name] = number
    view_list = product_dict.items()
    view_list = sorted(view_list, key=lambda x: (len(x[0]), x[0]))
    [print(' '.join(map(str, list_el))) for list_el in view_list]

Python: d={}
for _ in[0]*int(input()):
 k,v=input().split()
 d[k]=d.get(k,0)+int(v)
for l,k in sorted([len(x),x]for x in d):
 print(k,d[k])

Python: N = int(input())
pdict = {}

p, n = [], []

for i in range(N) :
    _p, _n = input().split()
    _n = int(_n)
    
    if _p not in pdict.keys() :
        pdict[_p] = _n
    else :
        pdict[_p] += _n
        
plist = list(pdict.keys())
plist.sort(key=len)
newdict = {}

for i in plist :
    print(i, pdict[i])
    # newdict[i] = pdict[i]

Python: if __name__ == '__main__':
    # ??????????????\???
    num_of_orders = int(input())
    data = []
    for d in range(num_of_orders):
        data.append(tuple(input().split(' ')))

    # ??¨???????????????????¨?
    orders = dict()
    for p_name, p_count in data:
        if p_name in orders:
            orders[p_name] += int(p_count)
        else:
            orders[p_name] = int(p_count)

    results = []
    for k, v in orders.items():
        results.append(tuple([k, v]))
    results.sort(key=lambda x:(len(x[0]), x[0])) # ?£????????????????????????¨?£????????????¢???????????????????????§?????????

    # ???????????????
    for r in results:
        print('{0} {1}'.format(r[0], r[1]))