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<iostream>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
int main() {
	int n;
	cin >> n;
	map<string, int>a;
	map<string, int>::iterator it;
	for (int z = 0; z < n; z++) {
		string b;
		int c;
		cin >> b >> c;
		it = a.find(b);
		if (it == a.end())
			a.insert(map<string, int>::value_type(b, c));
		else
			a[b] += c;
	}
	it = a.begin();
	int k = a.size();
	for (int i = 0; i < k; i++) {
		string g = "AAAAAAAAA";
		map<string, int>::iterator it2;
		it = a.begin();
		while (it != a.end()) {
			if ((*it).first.length() < g.length()) {
				g = (*it).first;
				it2 = it;
			}
			else if (g.length() == (*it).first.length()) {
				for (int j = 0; j < g.length(); j++) {
					if (g[j] > (*it).first[j]) {
						g = (*it).first;
						it2 = it;
						break;
					}
					else if (g[j] < (*it).first[j]) {
						break;
					}
				}
			}
			it++;
		}
		cout << (*it2).first << " " << (*it2).second << endl;
		a.erase(it2);
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  map<pair<int,string>,int>m;
  cin>>n;
  while(n--){
    string s;
    int p;
    cin>>s>>p;
    pair<int,string>pp=make_pair(s.size(),s);
    m[pp]+=p;
  }
  for(map<pair<int,string>,int>::iterator it=m.begin();it!=m.end();it++)
    cout<<(*it).first.second<<' '<<(*it).second<<endl;
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
const double EPS = 1e-10;
bool com(const string &a, const string &b) {
	if (a.size() != b.size()) return a.size() < b.size();
	else {
		for (int i = 0; i < a.size(); i++) {
			if (a[i] != b[i])return a[i] < b[i];
		}
	}
}
signed main() {
	int n; cin >> n; vector<string>v;
	map<string, int>m;
	rep(i, 0, n) {
		string s; int a; cin >> s >> a;
		m[s] += a;
		if (find(all(v), s) == v.end())v.push_back(s);
	}
	sort(all(v), com);
	rep(i, 0, v.size()) {
		cout << v[i] << " " << m[v[i]] << endl;
	}

	return 0;
}

C++: #include<stdio.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#include<string.h>
#include<queue>

using namespace std;

typedef pair<int,int> pii;
typedef pair<int,string> pis;
typedef pair<pis,int> tri;
#define rep(i,j) for(int i=0;i<(j);i++)
#define reps(i,j,k) for(int i=j;i<=k;i++)

int main(){
	int n,m,p;
	string s;
	tri t,*po;
	pis pi;
	vector< tri > v;
	scanf("%d",&n);
	rep(i,n){
		cin >> s;
		cin >> m;
		pi = pis(s.length(),s);
		po = lower_bound(v.begin(),v.end(),tri(pi,0));
		if(po != v.end() && po->first.second == s){
			po->second += m;
		}else{
			if(v.size()==0)v.push_back(tri(pi,m));
			else v.insert(po,tri(pi,m));
		}
	}
	rep(i,v.size())printf("%s %d\n",v[i].first.second.c_str(),v[i].second);
	return 0;
}

C++: #include<bits/stdc++.h>
#define Rep(i, n) for(int i=0;i<n;i++)
using namespace std;

map<string, int> mp;
vector<string> v;

bool desc(const string& s1, const string& s2){
  if(s1.size() == s2.size()) return s1 < s2;
  return s1.size() < s2.size();
}

void disp(string str){
  cout << str << " " << mp[str] << endl;
}

int main(){
  int n;
  cin >> n;
  Rep(i, n){
    string s;
    int m;
    cin >> s >> m;
    mp[s] += m;
    v.push_back(s);
    sort(v.begin(), v.end(), desc);
  }
  for_each(v.begin(), unique(v.begin(), v.end()), disp);
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define vi vector<int>
#define VS vector<string>
#define all(x) x.begin(),x.end()
#define mp make_pair
#define pb push_back

using namespace std;

bool pairCompare(const std::pair<string, int>& firstElem, const std::pair<string, int>& secondElem) {
    if(firstElem.second != secondElem.second)
        return firstElem.second > secondElem.second;
    else
        {
        if(firstElem.first.size() != secondElem.first.size()){
            return firstElem.first.size() < secondElem.first.size();
        }
        return firstElem.first < secondElem.first;
        }
}

int main(){
    int n;
    map<string, int> m;
    set<string> st;
    cin >> n;
    for(int i=0; i < n; i++){
        string s; int p;
        cin >> s >> p;
        st.insert(s);
        m[s] += p;
    }

    set<string>::iterator it = st.begin();
    vector<pair<string, int> > ans;
    while(it != st.end()){
        ans.push_back(mp(*it, m[*it]));
        ++it;
    }
    sort(all(ans), pairCompare);
    for(int i=0; i < ans.size(); i++){
        cout << ans[i].first << " " << ans[i].second << endl;
    }
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
int n;
map<string,int> m;
vector<pair<int,pair<int,string> > > v;
int main(void){
  cin >> n;
  for(int i = 0; i < n; i++){
    string s;
    int a;
    cin >> s >> a;
    if(m.find(s) == m.end()){
      m[s] = v.size();
      v.push_back(make_pair(-a,make_pair(s.size(),s)));
    }else{
      v[m[s]].first -= a;
    }
  }
  sort(v.begin(),v.end());
  for(int i = 0; i < (int)v.size(); i++){
    cout << v[i].second.second << " " << -v[i].first << endl;
  }
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int main(){
  int N;
  cin>>N;
  map<string, int> m;
  vector<string> name[5];
  REP(i, N){
    string s; int n;
    cin>>s>>n;
    m[s] += n;
    name[s.size() - 1].push_back(s);
  }
  REP(i, 5){
    sort(name[i].begin(), name[i].end());
    name[i].erase(unique(name[i].begin(), name[i].end()), name[i].end());
    REP(j, name[i].size()){
      cout<<name[i][j]<<" "<<m[name[i][j]]<<endl;
    }
  }
  return 0;
}