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 <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int main()
{
    int n;
    cin >> n;

    map<pair<int, string>, int> x;
    for(int i=0; i<n; ++i){
        string s;
        int a;
        cin >> s >> a;
        x[make_pair(s.size(), s)] += a;
    }

    for(map<pair<int, string>, int>::iterator it=x.begin(); it!=x.end(); ++it)
        cout << it->first.second << ' ' << it->second << endl;

    return 0;
}

C++: #include <iostream>
#include <map>
#include <string>
using namespace std;
map <string,int> dic;
int main(){
  int n;
  cin>>n;
  for(int i=0;i<n;i++){
    string s;int a;
    cin>>s>>a;
    dic[s]+=a;
  }
  for(int size=1;size<=5;size++) for(auto i=dic.begin();i!=dic.end();i++) if((int)i->first.size()==size) cout<<i->first<<" "<<\
i->second<<endl;

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
  int n;
  cin >> n;  
  map<string, int> mp;
  for(int i = 0; i < n; i++) {
    string s; int m;
    cin >> s >> m;
    mp[s] += m;
  }
  vector< pair<string, int> > vec;
  for(auto p : mp) {
    vec.push_back(p);
  }
  sort(vec.begin(), vec.end(), [&](pair<string, int> p, pair<string, int> q) ->bool{
      return p.first.size() != q.first.size() ? p.first.size() < q.first.size() : p < q;
    });
  for(auto p : vec) {
    cout << p.first << " " << p.second << endl;
  }
  
  return 0;
}

C++: #include <iostream>
#include <string>
#include <map>
using namespace std;
struct comp{
  bool operator()(const string &s,const string &t){
    return s.size() != t.size() ? s.size() < t.size() : s < t;
  }
};

int main(void){
  int n,a;
  string b;
  map<string,int,comp> d;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>b>>a;
    d[b]+=a;
  }
  for(map<string,int,comp>::iterator it=d.begin();it!=d.end();it++){
    cout<<(*it).first<<" "<<(*it).second<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<cmath>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb(in,tmp) in.push_back(tmp)
#define all(in) in.begin(),in.end()
#define PI acos(-1)
using namespace std;
int main(){
  map<int,map<string,int> >in;
  int n;
  cin>>n;
  rep(i,n){
    int tmp;
    string tmp1;
    cin>>tmp1>>tmp;
    in[tmp1.size()][tmp1]+=tmp;
  }
  map<int,map<string,int> >::iterator ite=in.begin();
  for(;ite!=in.end();ite++){
    map<string,int>::iterator it=in[ite->first].begin();
    for(;it!=in[ite->first].end();it++)
      cout<<it->first<<" "<<it->second<<endl;
  }
}

C++: /* AOJ 0589 Production */

#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <vector>

using namespace std;

bool compare_product_name(string a, string b);
void map_test();

typedef bool (*pname_comparator)(string, string);
typedef map<string, int, pname_comparator> mapp;

int
main()
{
    int n;
    mapp production(compare_product_name);
    mapp::iterator it;

    cin >> n;
    cin.ignore();
    while (n--) {
        string line, name;
        int p;
        int delim_pos;

        getline(cin, line);

        delim_pos = line.find_first_of(' ');
        name = line.substr(0, delim_pos);
        p = atoi(line.substr(delim_pos + 1).data());

        if (production.find(name) == production.end())
            production[name] = p;
        else
            production[name] += p;
    }

    for (it = production.begin(); it != production.end(); ++it)
        cout << it->first << ' ' << it->second << endl;

    return 0;
}

bool
compare_product_name(string a, string b)
{
    size_t len_a = a.size();
    size_t len_b = b.size();
    size_t min_len = min(len_a, len_b);
    size_t i;

    if (len_a != len_b)
        return len_a < len_b;

    for (i = 0; i < min_len; i++) {
        if (a[i] != b[i])
            return a[i] < b[i];
    }

    // a == b なので a < b ではない
    return false;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <stack>
#include <climits>
#include <deque>
#include <bitset>
#include <cassert>
#include <ctime>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
const int dy[]={-1,0,1,0},dx[]={0,1,0,-1};
// adjust problem by problem
const double EPS=1e-8;
const double PI=acos(-1.0);
#ifdef __GNUC__
int popcount(int n){return __builtin_popcount(n);}
int popcount(ll n){return __builtin_popcountll(n);}
#endif
#ifndef __GNUC__
template<class T> int popcount(T n){int cnt=0;while(n){if(n%2)cnt++;n/=2;}return cnt;}
#endif
template<class T>int SIZE(T a){return a.size();}
template<class T>string IntToString(T num){string res;stringstream ss;ss<<num;return ss.str();}
template<class T>T StringToInt(string str){T res=0;for(int i=0;i<SIZE(str);i++)res=(res*10+str[i]-'0');return res;}
template<class T>T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template<class T>T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> void PrintSeq(T &a,int sz){for(int i=0;i<sz;i++){cout<<a[i];if(sz==i+1)cout<<endl;else cout<<' ';}}
bool EQ(double a,double b){return abs(a-b)<EPS;}
void fastStream(){cin.tie(0);std::ios_base::sync_with_stdio(0);}
vector<string> split(string str,char del){
  vector<string> res;
  for(int i=0,s=0;i<SIZE(str);i++){
    if(str[i]==del){if(i-s!=0)res.push_back(str.substr(s,i-s));s=i+1;}
    else if(i==SIZE(str)-1){res.push_back(str.substr(s));}
  }
  return res;
}


int main(){

  int N;
  cin>>N;
  map<string,int> m;
  for(int i=0;i<N;i++){
    string s;
    int num;
    cin>>s>>num;
    m[s]+=num;
  }
  vector<pair< pair<int,string> , int> > vp;
  for(map<string,int>::iterator it=m.begin();it!=m.end();it++){
    string s=it->first;
    vp.push_back(make_pair(make_pair(s.size(),s),it->second));
  }
  sort(vp.begin(),vp.end());
  for(int i=0;i<(int)vp.size();i++){
    cout<<vp[i].first.second<<" "<<vp[i].second<<endl;
  }
  
  return 0;
}

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

int main() {
	int n;
	cin >> n;
	unordered_map<string, int> a;
	for (int i = 0; i < n; ++i) {
		string b;
		int c;
		cin >> b >> c;
		a[b] += c;
	}
	vector<pair<string, int>> b;
	for (unordered_map<string, int>::iterator i = a.begin(); i != a.end(); ++i) b.emplace_back(i->first, i->second);
	sort(b.begin(), b.end(), [](pair<string, int>& a, pair<string, int>& b) {
		if (a.first.size() == b.first.size()) return a < b;
		return a.first.size() < b.first.size();
	});
	for (pair<string, int>& i : b) cout << i.first << ' ' << i.second << endl;
}