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++: // -*- coding:utf-8; tab-width:4 -*-
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <complex>
#include <functional>
#include <iterator>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>

// ティツァツ」テァツュツ?
namespace solution {
    using namespace std;
   
    typedef istringstream ISS;
    typedef ostringstream OSS;
    typedef vector<string> VS;
    typedef long long LL;
    typedef int INT;
    typedef vector<INT> VI;
    typedef vector<VI> VVI;
    typedef pair<INT, INT> II;

    struct Comparator {
    bool operator() ( const string& a, const string& b ) {
        if ( a.size() != b.size() )
            return a.size() < b.size();
        return a < b;
    }
    };
    typedef map <string, LL, Comparator> MAP;

    const int SIZE = 100000000;
    int n;
    MAP M;

    struct Solution {
        void init() {
            M.clear();
        }
   
        bool input() {
            return cin >> n;
        }

        MAP solve() {
            MAP res;    
            for ( int i = 0; i < n; ++ i ) {
                string a;
                int b;
                cin >> a >> b;
                res[a] += b;
            }
            return res;
        }

        void output( MAP result ) {
            for ( MAP::iterator it_i = result.begin(); it_i != result.end(); ++ it_i ) {
                cout << (*it_i).first << " " << (*it_i).second << endl;
            }
        }

        int run() {
            while ( init(), input() ) {
                output(solve());
            }
            return 0;
        }
    };
}

int main() {
    return solution::Solution().run();
}

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
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: {}
	}
	Sort(find_);
	for (ll i = 0; i < find_.size(); i++) {
		cout << find_[i] << " " << a[find_[i]] << endl;
	}
	return 0;
}

C++: /*
    これって模擬本戦...？
 */

#include <cstdio>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

int n;
map<string, int> products;
vector<string> names;

bool cmp(const string &a, const string &b) {
    if (a.length() != b.length()) return a.length() < b.length();
    return a < b;
}

int main() {
    scanf("%d", &n);
    for (int i=0; i<n; i++) {
        char buf[6];
        int num;
        string name;

        scanf(" %s", buf);
        scanf("%d", &num);
        name = string(buf);

        if (products.find(name) != products.end()) {
            products[name] += num;
        } else {
            products[name] = num;
        }

        names.push_back(name);
    }

    sort(names.begin(), names.end(), cmp);
    names.erase(unique(names.begin(), names.end()), names.end());

    for (int i=0; i<names.size(); i++) {
        printf("%s %d\n", names[i].c_str(), products[names[i]]);
    }
}

C++: #include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	int n;
	
	scanf("%d", &n);
	
	vector<pair<int, pair<string, int> > > v;
	for (int i = 0; i < n; i++){
		char pName[8];
		int num;
		scanf("%s %d", pName, num);
		v.push_back(make_pair(strlen(pName), make_pair((string)pName, num)));
	}
	
	sort(v.begin(), v.end());
	
	string lead = "$";
	int cnt = 0;
	for (int i = 0; i < n; i++){
		if (lead != v[i].second.first){
			if (lead != "$") printf("%s %d\n", lead.c_str(), cnt);
			lead = v[i].second.first;
			cnt = v[i].second.second;
		}
		else cnt += v[i].second.second;
	}
	return (!printf("%s %d\n", lead.c_str(), cnt));
}

C++: #include<bits/stdc++.h>
using namespace std;
struct sort1 : public binary_function<pair<string, long long>, pair<string, long long>, bool>
{
	bool operator()(pair<string, long long> p1, pair<string, long long> p2)
	{
		int l1 = p1.first.size();
		int l2 = p2.first.size();

		return (l1 < l2 ? true : l1 == l2 ? p1.first < p2.first : false);
	}
}; //by.SQUARE1001
int main(){
  typedef pair<string, int> P;
  int n,count=0,cnt=0;
  cin >> n;
  vector<P> p(n);
  for(int i=0; i<n; i++){
    cin >> p[i].first >> p[i].second;
    //cout << p[i].first << " " << p[i].second << endl;
    /*if(i>0){
      count++;
      for(int j=0; j<count; j++){
        if(p[i].first == p[j].first){
          p[j].second += p[i].second;
          p[i].first = "ZZZZZ";
          p[i].second = 10000;
          cnt++;
        }
      }
    }*/
  }
  //for(int i=0; i<n; i++) cout << p[i].first << " " << p[i].second << endl;
  sort(p.begin(), p.end(), sort1());
  int sum = p[0].second;
  for(int i=1; i<n; i++){
    if(p[i-1].first != p[i].first){
      cout << p[i-1].first << " " << sum << endl;
      sum = 0;
    }
    sum += p[i].second;
  }
  cout << p[n-1].first << " " << sum << endl;
  return 0;
}


/*
int match(string x, int y, int z){
  for(int i=0; i<y; i++){
    if(x == p[i].first){
      z += p[i].second;
      p[i].first = "ZZZZZZZZZZ";
      p[i].second = 100000000;
    }
  }
  return x,z;
}*/

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

class cmp{
public:
  bool operator()(const string &a, const string &b){
    if(a.length() == b.length()){
      return a < b;
    }
    return a.length() < b.length();
  }
};

int main(){
  int n;

  while(cin >> n){
    map<string,int,cmp> mp;

    for(int i = 0; i < n; i++){
      string name;
      int x;
      cin >> name >> x;
      mp[name] += x;
    }
    for(map<string,int>::iterator iter = mp.begin(); iter != mp.end(); iter++){
      cout << (iter->first) << " " << (iter->second) << endl;
    }
  }
}