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;
#define all(c) (c).begin(),(c).end()
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin(); it!=(c).end(); it++)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define pr(a) cout << (a) << endl
typedef long long ll;
typedef pair<int,int> P;
const int MAX=1000000000;
int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
map<string,int> m;

bool comp(string a, string b) {
  if(a.size()==b.size()) return a<b;
  else return a.size()<b.size();
}

int main() {
  m.clear();
  int n;
  cin >> n;
  while(n--) {
    string s;
    int a;
    cin >> s >> a;
    m[s]+=a;
  }
  vector<string> v;
  tr(it,m) v.pb(it->first);
  sort(all(v),comp);
  rep(i,v.size()) cout << v[i] << " " << m[v[i]] << endl;
  return 0;
}

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

using namespace std;

bool cmp(const pair<string, int>& x, const pair<string, int>& y)
{
	if (x.first.size() < y.first.size()){
		return true;
	}
	return x.first < y.first;
}

int main()
{
	map<string, int> m;
	string pn;
	int n, c;

	scanf("%d", &n);
	
	for (int i = 0; i < n; i++){
		cin >> pn >> c;

		if (m.count(pn)) m[pn] += c;
		else m.insert(make_pair(pn, c));
	}

	vector< pair<string, int> > v;

	for (map<string, int>::iterator it = m.begin(); it != m.end(); it++){
		v.push_back(*it);
	}

	sort(v.begin(), v.end(), cmp);

	for (vector< pair<string, int> >::iterator it = v.begin(); it != v.end(); it++){
		cout << it->first << " " << it->second << endl;
	}

	return (0);
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999

bool bigger( string a,string b ){
    if(a.size()>b.size())return true;
    if(a.size()<b.size())return false;
    if(a>b)return true;
    return false;
}


void sort(vector<pair<string,int>> &x){
    rep(i,x.size()){
        for(int j=i+1;j<x.size();j++){
            if(bigger(x[i].first,x[j].first)){
                swap(x[i],x[j]);
            }
        }
    }
}



int main(){
    map<string,int> mp;
    int n;
    cin>>n;
    rep(i,n){
        string s;
        int a;
        cin>>s>>a;
        mp[s] +=a;
    }
    
    vector<pair<string,int>> x;
    
    
    for(auto &data:mp){
        x.pb(make_pair(data.first,data.second));
    }
    
    sort(x);
    
    rep(i,x.size()){
        cout<<x[i].first<<" "<<x[i].second<<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;
    }
    cout << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define NCLR(a) memset((a), -1 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
typedef complex<double> P;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 29;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { -1, 0, 1, 0, 1, -1, 1, -1 };
static const int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };

int main() {
	int n;
	scanf( "%d", &n );
	map<string, int> m;
	for ( int i = 0; i < n; i++ ) {
		string s;
		int a;
		cin >> s >> a;
		m[ s ] += a;
	}
	vector<pair<pair<int, string>, int> > vp;
	for ( auto i = m.begin(); i != m.end(); ++i ) {
		vp.push_back( make_pair( make_pair( i->first.size(), i->first ), i->second ) );
	}
	sort( vp.begin(), vp.end() );
	for ( int i = 0; i < vp.size(); i++ ) {
		printf( "%s %d\n", vp[ i ].first.second.c_str(), vp[ i ].second );
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using ll  = long long;
#define int ll
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define erep(e,v) for(auto && e :v)
#define all(in) in.begin(), in.end()
#define MP make_pair
#define INF (sizeof(int) == 4 ? (int)1e9:(int)1e18)
#define EPS 0.0000000001
using namespace std;
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
template<class T> void join(T a){for(auto itr :a){if(itr != *a.begin())cout << " "; cout << itr;} }
using ld  = long double;
using pii = pair<int,int>;
using piii = pair<int,pii>;
int W,H;
int dx[]={0,0,1,-1}, dy[]={1,-1,0,0};
bool valid(int x,int y){return (0<=x&&x<W)&&(0<=y&&y<H);}
auto f (pair<string,int> a,pair<string, int> b){
    if(a.first.size() < b.first.size())return true;
    else if(a.first.size() == b.first.size())return a.first < b.first;
    return false;
}
signed main(){
    int n; cin >> n;
    map<string,int>mp;
    vector<pair<string, int>>v;
    rep(_,n){
        string s;int num;
        cin >> s >> num;
        mp[s] += num;
    }
    for(auto itr : mp){
        v.push_back(make_pair(itr.first, itr.second));
    }
    sort(all(v),f);
    erep(e,v){
        cout << e.first << " " << e.second << endl;
    }
}