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++: // だーれだ？
//qqqqqqHHMqqqqqHf!(:;jHmmkf``(:;jkqqqqqqqqqmMH#HMqqK_````````````````(kqqqqqqHMMM
//qqqqqqHHmqqqqH=` :;;jkqH=?~..;;jqqqqqqqqqmM#HM#MHH!```` ```````..-``dqWbWkRqHMMM
//qqqqqqMHmqqqK!``.;;;dqH: ```.?TUqqHmqqqqmHHHM4Mqkt..```` ..JgHHW@P`.HHkqHHHqM#NN
//qqqqqmHMmqHTYWHgHmmgHR...``` :;Jkq9qqqqqgMH#1dHk$...-gW@HY"=!``dH! dHNmqqqqmM###
//qqqqqqgMmH[````` ?dHMMMHHHHHJ<;JkK>XqqqmMH#<+Hb%.WYYTHppf-````.gt`.HHMmmqqqmM###
//qqkqqqm@H?S,```` Jpbbpp:```` :!dK<;JkqqMMY(;dW^````.ppbpf!```.Y<`.HHHMgmqqqmH###
//qqqqqqqqD`` ````(ppbppW````` ~.f`:;jkqM#^.;jf!````.Wppbpf````````(HH#HmmqqmH#NN#
//qqqqqqqkP``````.fpbpbpf``` ` `.``~:;WHY``(+=``````Jppbpf\```` ``.MH#HHgqqqmHNNNN
//qqqqqqqk]```` `,fpbbpf>```` `` ``_:<? ``.?!``` `` Wpppp%``` ``` dHH##HmmqqmH####
//qqqqqqqk]``````,fpbppt``` `` `` `_~````` `` `` ```WppW=``` ````.HHHHMHmmqqmM####
//qqqqqqqk$`` ````4ppfY```` `` `` `- `` `` `` ``` ``(T=`````` ``.MH##HMMmmqqmM##HH
//qqqqqqHqP``` ````?7!```` `` `` ``` `` ` ` ``  ````````` ` ````.MHH#HgMmqmqmHHHHH
//qqqkqqmkR```` `        `` ` `` ` ` ` ``` ` ``` `   ......_. ```dH#HHgMqqgqqmH#HH
//NqqqqqHqR```  _ ........_` `` ``` ``` ````` ``` ............```,HHHHmqqqHqqmMH##
//MmqqqqHqR```  _........_ `` `` ``` `` `  `` ` ` `  `__     ` ``,HHHMmmqqMHmmMHH#
//HMqqqqXkH `````````````` ` `` `  `` `` `` `` `` ```` ````` ````.HHHMmMHm@HHqmMHH
//HHHmqqfHk;``` `  ` `` ` ` `` ``` ` ` ``` `` `` `` ````  `..```.HHHHMMHMg@HHHHHMH
//HHMmmqMHkP<.`` ``` ` ` ``` `` ``` ``` ` `` `` `` `  ````  ```.MH##HM#HMg@@@@H@HH
//##HNmqR`jW<<_ ``` ````` `` ` ` ``` `` `` `` `` ````  ````` .dHH##H#HHHMHH@@H@@@@
//HH##NqH..X2:<<-.`` ` `` ``` ``` ` `` `` `` ` `` ` ``` ` .gMHHH#H#HHH#HM@@H@@H@@H
//-WHHHNH[ =v._:::<-. ` `` ` ``  ```````.`` `````````     .MHH###H#MHHHHH@H@H@@HH@
//` 7MHHMH.l==.  ~:::::<_-.. ````` ``````` ``  ...uX;     `,MHHHH#M@@MM@@@@@@H@@@@
//  . TMHHbZl=1.   _~::::::(<::<:<___-___:<<:::::jX3=. ` ` `,MMHM@@@@Mg@@@@@@@@@H@
//
#include <bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define erep(i,a,b) for(int i=a;i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>(b);--i)
#define eper(i,a,b) for(int i=(a);i>=b;--i)
#define fore(i, x, a) for(auto &&x:a) 
#define ITR(i,b,e) for(auto i=(b);i!=(e);++i)
#define IN(i,a,b) (a)<=(i) && (i)<(b)
#define pb push_back
#define mp make_pair
#define ALL(x) begin(x),end(x)
#define F first
#define S second
#define debug(x)  cout << #x << ": " << (x) << '\n';
const long long INF=1001001001001001001;
const int MOD=(int)1e9 + 7;
const double EPS=1e-9;
using namespace std;
using Pii = pair<int,int>;
using vii = vector<int>;
template<class T>using  PS_queue = priority_queue<T, vector<T>, greater<T> >;
template<class T>using vv = vector<T>;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
template<typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val){fill( (T*)array, (T*)(array+N), val );}
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) {
    ITR(i,begin(v),end(v))os<<*i<<(i==end(v)-1?"":" ");return os;}
template<class T> istream& operator>>(istream &is,vector<T> &v) {
    ITR(i,begin(v),end(v)) is>>*i;return is;}
template<class T,class U> istream& operator>>(istream &is, pair<T,U> &p) {
    is>>p.first>>p.second;return is;}
template<class T>T gcd(T a, T b){ return b ? gcd(b, a % b) : a; }
template<class T>T lcm(T a, T b){ return a / gcd(a, b) * b; }
struct edge { int from, to, cost; };
int dy[]={0, 1, -1, 0}; int dx[]={1, 0, 0, -1};

map<string, int> prod;
signed main() {
 cin.tie(0);
 ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vv<pair<int, string>> items;
  rep(i, 0, n) {
    string s;
    int item;
    cin >> s >> item;
    if (prod[s] == 0) items.pb(mp(s.size(), s));
    prod[s] += item;
  }
  sort(ALL(items));
  rep(i, 0, items.size()) {
    cout << items[i].second << " " << prod[items[i].second] << endl;
  }
  return 0;
}

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

bool comp(const string &s, const string &t){
	if( s.size() < t.size() ) return true;
	if( s.size() > t.size() ) return false;
	return s < t;
}

int main(){
	int n;
	map<string, int> m;
	vector<string> v;
	
	cin >> n;
	for(int i=0 ; i < n ; i++ ){
		string s;
		int k;
		cin >> s >> k;
		
		if( m.count(s) ){
			m[s] += k;
		}else{
			m[s] = k;
			v.push_back(s);
		}
	}
	sort(v.begin(), v.end(), comp);
	for(int i=0 ; i < v.size() ; i++ ){
		cout << v[i] << " " << m[v[i]] << endl;
	}
}

C++: #include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
 
typedef pair<string, int> P;
 
bool compare(string s1, string s2){
  if(s1.size() < s2.size()) return true;
  else if(s1.size() > s2.size()) return false;
  else{
    if(s1 < s2) return true;
    else return false;
  }
}

int main(){
  map<string, int> data;
  vector<string> 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->first);
  }
  
  sort(ans.begin(), ans.end(), compare);
   
  for(int i = 0 ; i < ans.size() ; i++){
    cout << ans[i] << ' ' << data[ans[i]] << endl;
  }
  return 0;
}

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

using namespace std;

// ??????????????§??????????????¢?´¢?????????????????????

int main(){
	
	map<string, int> m;
	map<string, int>::iterator ite;
	
	int n, num;
	
	cin >> n;
	
	string str;
	
	for(int i = 0; i < n; i++){
		
		cin >> str >> num;
		ite = m.find(str);
		if(ite != m.end()){
			ite -> second += num;
		}else{
			m.insert(make_pair(str, num));
		}
	}
	for(int i = 1; i <= 5; i++){
		for(ite = m.begin(); ite != m.end(); ite++){
			if((ite -> first).size() == i){
				cout << ite -> first << " " << ite -> second << endl;
			}
		}
	}
	
	return 0;
}

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

int N;
typedef pair<string,int>P;

bool cmp(P p1,P p2){
  if(p1.second == p2.second){
    if(p1.first.size() == p2.first.size()){
      return p1.first < p2.first;
    }
    return p1.first.size() < p2.first.size();
  }
  return p1.second > p2.second;
}

int main(){
  cin >> N;
  
  map<string,int>mp;
  vector<P>vec;

  for(int i = 0 ; i < N ; i++){
    string tmp;
    int num;
    cin >> tmp >> num;
    if(mp.find(tmp) == mp.end()){
      mp[tmp] = vec.size();
      vec.push_back(P(tmp,num));
    }
    else {
      vec[mp[tmp]].second += num;
    }

  }

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

  for(int i = 0 ; i < vec.size() ; i++){
    cout << vec[i].first << ' ' << vec[i].second << endl;
  }

  return 0;
}