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++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

void solve() {
  int n;
  cin >> n;
  map<string, int> m;
  while (n--) {
    string s;
    int x;
    cin >> s >> x;
    m[s] += x;
  }
  vector<pair<string, int>> v;
  v.reserve(m.size());
  for (auto &p : m) v.pb(p);
  sort(all(v), [](auto &a, auto &b) {
    if (a.fi.size() != b.fi.size()) return a.fi.size() < b.fi.size();
    return a.fi < b.fi;
  });
  for (auto &p : v) cout << p.fi << ' ' << p.se << endl;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <ctime>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <numeric>
#include <cassert>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define all(a) a.begin(),a.end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SS stringstream
#define DBG1(a) rep(_X,sz(a)){printf("%d ",a[_X]);}puts("");
#define DBG2(a) rep(_X,sz(a)){rep(_Y,sz(a[_X]))printf("%d ",a[_X][_Y]);puts("");}
#define bitcount(b) __builtin_popcount(b)
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)

#define delete(a,n) a.erase(remove(all(a),n),a.end())
template<typename T, typename S> vector<T>& operator<<(vector<T>& a, S b) { a.push_back(b); return a; }
template<typename T> void operator>>(vector<T>& a, int b) {while(b--)if(!a.empty())a.pop_back();}
bool isprime(int n){ if(n<2)return false;  for(int i=2;i*i<=n;i++)if(n%i==0)return false;  return true;} 
ll b_pow(ll x,ll n){return n ? b_pow(x*x,n/2)*(n%2?x:1) : 1ll;}
string itos(int n){stringstream ss;ss << n;return ss.str();}

typedef unsigned int uint;
int in() {
    int x = 0, c;
    for (; (uint)((c = getchar()) - '0') >= 10; ) { if (c == '-') return -in(); if (!~c) throw ~0; }
    do { x = (x << 3) + (x << 1) + (c - '0'); } while ((uint)((c = getchar()) - '0') < 10);
    return x;
}

int main(){
	map<string,int> w;
	int n = in();
	vector< pair< pair<int,string> , int > > v;
	for(int i = 0 ; i < n ; i++){
		string s;int b;
		cin >> s >> b;
		w[s]+=b;
	}
	EACH(it,w){
		v.push_back(mp(mp(it->first.size(),it->first),it->second));
	}
	sort(v.begin(),v.end());
	rep(i,v.size()) cout << v[i].first.second << " " << v[i].second << endl;
}

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

using namespace std;

typedef map< string, set<int> > Pro;
Pro pro;

int main(){
  int num;
  string s;
  while(cin >> s >> num)
  {
    pro[s].insert(num);
  }
  
  for(Pro::iterator it = pro.begin() ; it != pro.end() ; it++)
  {
  	cout << it->first;
  	int total=0;
  	for(set<int>::iterator set_it = it->second.begin() ; set_it != it->second.end() ; set_it++)
  	{
  		total += *set_it;
  	}
  	cout <<" " << total << endl;
  }

  return 0;
}

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

typedef pair<string, int> P;

bool compare(P s1, P s2){
  if(s1.first.size() < s2.first.size()) return true;
  else if(s1.first.size() > s2.first.size()) return false;
  
  else{
    for(int i = 0 ; i < s1.first.size() ; i++){
      if(s1.first[i] < s2.first[i]) return true;
      else if(s1.first[i] > s2.first[i]) return false;
    }
  }
}

int main(){
  map<string, int> data;
  vector<P> ans;
  
  int n;
  cin >> n;
  string s;
  int t;
  for(int i = 0 ; i < n ; i++){
    cin >> s >> t;
    data[s] += t;
  }
  
  for(map<string, int>::iterator it = data.begin() ; it != data.end() ; it++){
    ans.push_back(*it);
  }
  
  sort(ans.begin(), ans.end(), compare);
  
  for(int i = 0 ; i < ans.size() ; i++){
    cout << ans[i].first << ' ' << ans[i].second << endl;
  }
  return 0;
}

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

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

map<string, int> mp;

struct P {
  string name;
  int val;
  bool operator<(P p) const {
    if (name.size() != p.name.size()) return name.size() < p.name.size();
    return name < p.name;
  }
};

int main() {
  int n;
  cin >> n;
  vector<P> p;
  rep (i, n) {
    string name;
    int val;
    cin >> name >> val;
    if (mp.count(name)) {
      p[mp[name]].val += val;
    } else {
      mp[name] = p.size();
      p.push_back((P){name, val});
    }
  }
  sort(p.begin(), p.end());
  rep (i, p.size()) cout << p[i].name << " " << p[i].val << endl;
}

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 <cstdio>
#include <iostream>
#include <map>
#include <algorithm>
#include <string>

using namespace std;

typedef struct map<string, int> Mymap;

void disp(Mymap &obj) {
  Mymap::iterator p;
  for(p=obj.begin(); p!=obj.end(); ++p) {
    cout << p->first << " " << p->second << endl;
  }
}


int main() {
  int num;
  Mymap val;

  // input
  scanf("%d", &num);
  for(int i=0; i<num; i++) {
    string tmpS;
    cin>>tmpS;
    int tmpI;
    cin>>tmpI;
    if(val.find(tmpS)==val.end()) {
      val.insert(make_pair(tmpS, tmpI));
    }
    else {
      val[tmpS]+=tmpI;
    }
  }
  // sort不要

  // output
  disp(val);
}