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<stdio.h>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>     
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <iomanip>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define ld long double
#define rep(a,t) for(int a=0;a<t;a++) 
#define forever while(true)
#define Sort(a) sort(a.begin(),a.end())
#define Reverse(a) reverse(a.begin(),a.end())
#define pb push_back
bool left_large(string a, string b)
{
	ll alen = a.length();
	ll blen = b.length();
	if (alen > blen) { return true; }
	if (alen < blen) { return false; }
	if (a == b) { return false; }
	vector<string>sort_aft;
	sort_aft.pb(a);
	sort_aft.pb(b);
	Sort(sort_aft);
	if (a == sort_aft[0]) {
		return false;
	}
	return true;
}
vector<string>sort1(vector<string> a) {
	ll n = a.size();
	for (ll i = 1; i < n; i++) {
		string  v = a[i];
		ll j = i - 1;
		while (j >= 0 && left_large(a[j], v)) {
			a[j + 1] = a[j];
			j--;
		}
		a[j + 1] = v;
	}
	return a;
}

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	map<string, ll>a;
	ll n;
	cin >> n;
	vector<string>find_;
	while (n--) {
		string p;
		ll q;
		cin >> p >> q;
		a[p] += q;
		for (ll i = 0; i < find_.size(); i++) {
			if (find_[i] == p) { goto G; }
		}
		find_.pb(p);
	G: {}
	}find_ = sort1(find_);
	for (ll i = 0; i < find_.size(); i++) {
		cout << find_[i] << " " << a[find_[i]] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <algorithm>
#define int long long
using namespace std;
signed main() {
	int n; cin >> n;
	set<string>st;
	map<string, int>m;
	for (int i = 0; i < n; i++) {
		string s; int p;
		cin >> s >> p;
		if (*(st.lower_bound(s)) != s) {
			st.insert(s);
			m[s] = p;
		}
		else m[s] += p;
	}
	vector<pair<int, string>>v;
	for (string s : st)v.push_back(make_pair(s.size(), s));
	sort(v.begin(), v.end());
	for (int i = 0; i < v.size(); i++)cout << v[i].second << ' ' << m[v[i].second] << endl;	cin >> n;
}

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

struct PN {
	string s;

	bool operator < (const PN & rhs) const{
		return s.size() < rhs.s.size() || (s.size() == rhs.s.size() && s < rhs.s);
	}
};

int main()
{	
	int n;
	map<PN, int> p;

	cin >> n;

	for (int i = 0; i < n; i++) {
		string s1;
		int t;

		cin >> s1 >> t;

		PN p1{s1};

		if (p.find(p1) != p.end())
			p[p1] += t;
		else
			p[p1] = t;
	}

	for (map<PN, int>::iterator itr = p.begin(); itr != p.end(); itr++)
		cout << itr->first.s << " " << itr->second << endl;
	
	return 0;
}

C++: #include <stdio.h>
#include <map>
#include <utility>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
#define F first
#define S second
#define PB push_back
#define MP make_pair
bool cmp(pair<string,int> a,pair<string,int> b){
    if(a.F.length()!=b.F.length())return a.F.length()<b.F.length();
    int now=0;
    while(true){if(a.F[now]!=b.F[now])return a.F[now]<b.F[now];
        now++;
    }
}
int main(){
    int n,temp;
    map<string,int> m;
    vector<pair<string,int>>ans;
    string s;
    scanf("%d",&n);
    while(n--){
        cin>>s>>temp;
        if(m.find(s)!=m.end())m[s]+=temp;
        else m.insert(MP(s,temp));
    }
    for(auto u:m)ans.PB(u);
    sort(ans.begin(),ans.end(),cmp);
    for(int i=0;i<ans.size();i++)cout<<ans[i].F<<" "<<ans[i].S<<endl;
}

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

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int*)a, (int*)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int n, a;
string s;

bool cmp(const pair<string, int> &a, const pair<string, int> &b){
  if(a.F.size() != b.F.size()) return a.F.size() < b.F.size();
  return a.F < b.F;
}

int main(){
  scanf("%d", &n);
  vector<pair<string, int> >v;
  rep(i, n){
    cin >> s >> a;
    int p = 0;
    while(v.size() > p){
      if(v[p].F == s){ v[p].S += a; break; }
      p++;
    }
    if(v.size() == p) v.PB(MP(s, a));
  }
  sort(v.begin(), v.end(), cmp);

  rep(i, v.size()) cout << v[i].F << ' ' << v[i].S << endl;
  return 0;
}

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

using namespace std;

typedef pair<int , string> t;

int main(void){

  int n,num;
  string s;
  map < t , int > m;

  cin >> n;
  for( int i = 0 ; i < n ; i++ ){
    cin >> s >> num;
    m[ t( s.size() , s ) ] += num;
  }

  map<t,int>::iterator it;
  for( it = m.begin() ; it != m.end() ; it++ ){
    cout << it->first.second <<  " " << it->second << endl;
  }

  return 0;
}

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

class State{
public:
  string name;
  int value;
  State(){}
  State(string n, int v):name(n), value(v){}

  bool operator < ( const State &s) const{
    //    if ( value == s.value ){
      if ( name.size() == s.name.size() ){
	return name < s.name;
      } else {
	return name.size() < s.name.size();
      }
      //    } else return value > s.value;
  }
  
};

main(){
  int n;
  map<string, int> mp;
  cin >> n;
  for ( int i = 0; i < n; i++ ){
    string name;
    int value;
    cin >> name >> value;
    mp[name] += value;
  }

  vector<State> S;
  for ( map<string, int>::iterator it = mp.begin(); it != mp.end(); it++ ){
    S.push_back(State((*it).first, (*it).second));
  }
  sort(S.begin(), S.end());

  for ( int i = 0; i < S.size(); i++ ){
    cout << S[i].name << " " << S[i].value << endl;
  }

}

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

using namespace std;

#define all(c) (c).begin(), (c).end()
#define loop(i,a,b) for(int i=(a); i<(int)(b); i++)
#define rep(i,b) loop(i,0,b)

int main(){
    int n; cin>>n;
    struct data {
        data(){}
        string name;
        bool operator<(const data& r) const {
            if(name.size() != r.name.size())
                return name.size() < r.name.size();
            return name < r.name;
        }
    };

    map<data,int> m;
    rep(i,n){
        data d; int n;
        cin>>d.name>>n;
        m[d]+=n;
    }
    for(const auto& e:m){
        cout<<e.first.name<<" "<<e.second<<endl;
    }
}