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;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
#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 all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N; cin >> N;
	map<string, int> mp[10];
	rep(i, 0, N) {
		string s; int a;
		cin >> s >> a;
		mp[s.size()][s] += a;
	}
	rep(i, 1, 6) {
		for (auto it = mp[i].begin(); it != mp[i].end(); it++) {
			cout << it->first << " " << it->second << endl;
		}
	}
	return 0;
}

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

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,b,n) for(int i=b;i<n;i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
typedef pair<int,string>P;
int main(){
	int n;
	cin>>n;
	map<P,int> data;
	for(int i =0;i < n;i++){
		string str;
		int x;
		cin>>str>>x;
		data[P(str.length(),str)]+=x;
	}
	map<P,int>::iterator it =data.begin();
	while(it!=data.end()){
		cout <<(*it).first.second<<" "<<(*it).second<<endl;
		it++;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <map>

using namespace std;

int main(){
    int n, a;
    string s;
    map<string, int> m;
    cin >> n;
    for(int i = 0;i < n;++i){
        cin >> s >> a;
        m[s] += a;
    }
    map<string, int>::iterator mit = m.begin();
    for(;mit != m.end(); ++mit){
        cout << mit->first << " " << mit->second << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
  
// #define int long long
#define pb push_back
  
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vector<int> > vii;
 
template <class X> void print(X Target){ cout << Target << '\n'; }

bool less_len(string lhs, string rhs)
{
  if (lhs.size() != rhs.size()){
    return (lhs.size() < rhs.size());
  }
  else {
    return (lhs < rhs);
  }
}

signed main()
{
  ios::sync_with_stdio(false);
  map<string, int> order;
  vector<string> product;
  int n;

  cin >> n;
  while (n--){
    string input;
    int num;
    cin >> input >> num;
    bool same = false;
    for (int i = 0; i < product.size(); i++){
      if (product[i] == input){
        same = true;
        break;
      }
    }
    if (!same){
      product.pb(input);
      order[input] = num;
    }
    else {
      order[input] += num;
    }
  }

  sort(product.begin(), product.end(), less_len);
  for (int i = 0; i < product.size(); i++){
    cout << product[i] << " " << order[product[i]] << '\n';
  }

  return (0);
}

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

using pii = pair<int, int>;
using vi = vector<int>;
using ll = long long;

int main() {
    int n;
    cin >> n;
    map<string, int> cnt;
    for(int i = 0; i < n; ++i) {
        string s; int m;
        cin >> s >> m;
        cnt[s] += m;
    }
    vector<pair<string, int>> res;
    for(auto& p : cnt) {
        res.push_back(p);
    }
    sort(begin(res), end(res), [](auto const& p1, auto const& p2) {
        return p1.first.size() < p2.first.size() || p1.first.size() == p2.first.size() && p1.first < p2.first;
    });
    for(auto& p : res) {
        cout << p.first << ' ' << p.second << endl;
    }
}

C++: #include<iostream>
#include<string>
#include<map>
#define int long long
using namespace std;

int n;
map<string, int> out[10];

signed main() {
cin >> n;
for(int i = 0; i < n; i++ ) {
string s; int p;
cin >> s >> p;
if ( out[s.length()].find(s) == out[s.length()].end() ) out[s.length()].insert(pair<string, int>(s, p) );
else out[s.length()][s] += p;
}

map<string, int>::iterator it;
for(int i = 0; i < 10; i++ ) {

for( it = out[i].begin(); it != out[i].end(); it++ )
cout << (*it).first << " " << (*it).second << endl;
}

return 0;
}

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

typedef struct{
	char name[6];
	long num;
}product;

product p[12000000];

int main(){
	long n, now=0;
	
	scanf(" %ld", &n);
	for(long i=0; i<n; ++i){
		char s[6];
		long a;
		scanf(" %s %ld", s, &a);
		 
		bool find=false;
		for(long j=0; j<now; ++j){
			if(strcmp(p[j].name, s)==0){
				find=true;
				p[j].num+=a;
				break;
			}
		}
		
		if(!find){
			strcpy(p[now].name, s);	
			p[now].num=a;
			++now;
		}
	}

	for(long i=0; i<now; ++i){
		for(long j=i; j<now; ++j){
			if(strlen(p[i].name) > strlen(p[j].name)) swap(p[i], p[j]);
		}
	}
	
	for(long i=0; i<now; ++i){
		for(long j=i; j<now; ++j){
			if(strlen(p[i].name) == strlen(p[j].name)){
				for(int k=0; k<strlen(p[i].name); ++k){
					if(p[i].name[k] > p[j].name[k]){
						swap(p[i], p[j]);
						break;
					}
					else if(p[i].name[k] == p[j].name[k]){
						continue;
					}
					else break;
				}		
			}
		}
	}

	
	for(long i=0; i<now; ++i)
		cout << p[i].name << " " << p[i].num << endl;
	
}

C++: #include <iostream>
#include <string>
#include <map>

using namespace std;

int main(){
    int n, a;
    string s;
    map<string, int> m;
    cin >> n;
    for(int i = 0;i < n;++i){
        cin >> s >> a;
        m[s] += a;
    }
    map<string, int>::iterator mit = m.begin();
    for(;mit != m.end(); ++mit){
        cout << mit->first << " " << mit->second << endl;
    }
    return 0;
}

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

int main(){
    typedef pair<int,string> P;
    map <P,int> data;
    map <P,int>::iterator itr;
    int tmp2;
    string tmp;
    int n;
    cin >> n;
    for(int i=0;i<n;i++){
        cin >> tmp >> tmp2;
        if(data.find(P(tmp.size(),tmp))!=data.end()) data[P(tmp.size(),tmp)] += tmp2;
        data.insert(map <P,int>::value_type(P(tmp.size(),tmp),tmp2));
    }
    for(itr=data.begin();itr!=data.end();itr++) cout << itr->first.second << " " << itr->second << endl;
}

C++: #include <iostream>
#include <string>
#include <map>
using namespace std;struct tring{string g;tring() {};tring(string a) {g=a;}};map<tring,int>RB;bool operator <(tring a, tring b) {if (a.g.size()!=b.g.size()) return a.g.size()<b.g.size();return a.g<b.g;}
int main() {string T;int N,M;cin>>N;while (N--) {cin>>T>>M;RB[tring(T)]+=M;}for (map<tring,int>::iterator it=RB.begin();it!=RB.end();it++) cout<<it->first.g<<" "<<it->second<<endl;return 0;}