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)
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x,n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
typedef pair<string,int> P;
typedef pair<int,P> PP;

//-----------------------------------------------------------------------------

bool f(const P& a,const P& b) {
	if(a.fi.size()<b.fi.size()) return a.fi.size()<b.fi.size();
	REP(i,(int)a.fi.size()) {
		if(a.fi[i]==b.fi[i]) continue;
		return a.fi[i]<b.fi[i];
	}
	return false;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n;
	cin>>n;
	map<string,int> mp;
	string s;int m;
	REP(i,n) {
		cin>>s>>m;
		mp[s]+=m;
	}
	vector<P> vec;
	for(auto x:mp) vec.pb(P(x.fi,x.se));
	sort(vec.begin(),vec.end(),f);
	for(auto x:vec) cout<<x.fi<<' '<<x.se<<endl;

	return 0;
}