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>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef pair<string, int>P;

map<string, int>mp;
int main() {
	int n; cin >> n;
	rep(i, n) {
		string s; int a; cin >> s >> a;
		mp[s] += a;
	}
	vector<P>v;
	for (auto m : mp)v.push_back(m);
	stable_sort(v.begin(), v.end(), [](P a, P b) {return a.first.length() < b.first.length(); });
	for (auto m : v)cout << m.first << ' ' << m.second << endl;
	return 0;
}

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

C++: #include<iostream>
#include<string>
#include<cmath>
#include<cstring>
using namespace std;

int k[16000000];
int n, a, z, cnt;
string S;

int main() {
	memset(k, 0, sizeof(k));
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> S;
		z = 0;
		for (int j = 0; j < S.size(); j++) {
			z += (int)(S[j] - 'A' + 1)*pow(27, S.size() - j - 1);
		}
		cin >> a;
		k[z] += a;
	}
	for (int i = 0; i < 16000000; i++) {
		if (k[i] >= 1) {
			for (int j = 4; j >= 0; j--) {
				z = (int)(i / (int)pow(27, j)) % 27;
				if (z >= 1) {
					cout << (char)(z + 'A' - 1);
				}
			}
			cout << ' ';
			cout << k[i] << endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<map>
#include<vector>
using namespace std;
bool cmp(string l,string r);
int main(){
    int n;
    scanf("%d",&n);
    map<string,int> Data;
    for(int i = 0; i < n; i++){
        string str;
        int x;
        cin >> str >> x;
        Data[str] += x;
    }
    map<string,int> :: iterator it = Data.begin();
	vector<string> str_ans;
   	for(it =Data.begin();it != Data.end(); it++){
        str_ans.push_back((*it).first);
    }
	
    sort(str_ans.begin(),str_ans.end(),cmp);
    for(int i = 0; i < str_ans.size(); i++){
        cout << str_ans[i] << " " << Data[str_ans[i]] << endl;
    }
    return 0;
}
bool cmp(string l,string r){
    if(l.size() < r.size())return 1;
    if(l.size() > r.size())return 0;
    else {
        if(l < r)return 1;
        else return 0;
    }
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <utility>
#include <iterator>
using namespace std;

int main() {
    int N; cin >> N;
    map<string, int> dict;
    while(N--) {
        string s; int x; cin >> s >> x;
        dict[s] += x;
    }
    vector<pair<string, int> > v;
    for(auto it : dict) {
        v.push_back(it);
    }
    sort(begin(v), end(v), [](pair<string, int> a, pair<string, int> b) {
            if(a.first.length() != b.first.length()) return a.first.length() < b.first.length();
            else return a.first < b.first;
            });
    for(auto it : v) {
        cout << it.first << ' ' << it.second << endl;
    }

}

C++: #include<iostream>
#include<string>
using namespace std;
string a[1000000];
int b[1000000];
int main(){
	int n;
	cin>>n;
	string z;
	int y;
	int top=0;
	for(int i=0;i<n;i++){
		cin>>z>>y;
		bool t=true;
		for(int j=0;j<top;j++){
			if(a[j]==z){
				b[j]+=y;
				t=false;
			}
		}
		if(t==true){
			a[top]=z;
			b[top]=y;
			top++;
		}
	}
	for(int i=0;i<top;i++){
		for(int j=top-1;j>i;j--){
			if(a[j].size()<a[j-1].size()){
				string tmps=a[j];
				a[j]=a[j-1];
				a[j-1]=tmps;
				int tmp=b[j];
				b[j]=b[j-1];
				b[j-1]=tmp;
			}
			if(a[j].size()==a[j-1].size()){
				int set=0;
				while(true){
					if(a[j][set]!=a[j-1][set])
						break;
					set++;
				}
				if(a[j][set]<a[j-1][set]){
					string tmps=a[j];
				    a[j]=a[j-1];
				    a[j-1]=tmps;
				    int tmp=b[j];
				    b[j]=b[j-1];
				    b[j-1]=tmp;
				}
			}
		}
	}
	for(int i=0;i<top;i++)
		cout<<a[i]<<" "<<b[i]<<endl;
	return 0;
}

C++: #include<iostream>
#include<map>
#include<vector>
#include<algorithm>
 
using namespace std;
 
typedef pair<string,int> P;
map<string,int> M;
vector<P> V;
 
bool comp(const P p1, const P p2){
  if(p1.first.length() != p2.first.length())return p1.first.length()<p2.first.length();
  return p1.first == min(p1.first,p2.first);
}
 
int main(){
  int n;
  cin >> n;
  while(n--){
    string s;
    int a;
    cin >> s >> a;
    M[s] += a;
  }
 
  map<string,int>::iterator ite;
 
  for(ite = M.begin(); ite != M.end(); ite++){
    V.push_back(P(ite->first,ite->second));
  }
   
  sort(V.begin(),V.end(),comp);
   
  for(int i = 0; i < V.size(); i++)
    cout << V[i].first << " " << V[i].second << endl;
  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n;
  vector<string> key;
  map<string, int> m;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string tmp_s;
    int tmp_i;
    cin >> tmp_s >> tmp_i;
    if (m.count(tmp_s) == 0) {
      m[tmp_s] = tmp_i;
      key.push_back(tmp_s);
    } else
      m[tmp_s] += tmp_i;
  }
  sort(key.begin(), key.end());
  sort(key.begin(), key.end(),
       [](string a, string b) { return (a.length() < b.length()); });
  for (auto& v : key) {
    cout << v << " " << m[v] << endl;
  }

  return 0;
}