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 <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;

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

int main(){
  map<string, int, Compare> mp;
  int n;
  cin >> n;

  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;
  }
}

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;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cctype>
#include<map>
#include<vector>
using namespace std;
bool cmp(string l,string r);
int main(){
	map<string, int> Data;
	
	int n;
	scanf("%d",&n);
	
	for(int i = 0 ; i < n; i++){
		string str;
		int x;
		cin >> str >> x;
		Data[str] += x;
	}
	map<string ,int> :: iterator it =  Data.begin();
	vector<string> ans_str;
	
	for(it == Data.begin(); it != Data.end(); it++){
		ans_str.push_back((*it).first);
	}
	sort(ans_str.begin(),ans_str.end(),cmp);
	
	for(int i = 0; i < ans_str.size(); i++){
		cout << ans_str[i] << " " << Data[ans_str[i]] <<endl;
	}
	
	return 0;
}
bool cmp(string l,string r){
	bool f;
	if(l.size() > r.size())f = false;
	if(l.size() < r.size())f = true;
	else (l > r) ? f = false : f = true;
	
	return f;
}

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

using namespace std;

int main(){
    int n,b;
    map<string,int> mm[5];
    string a;
    
    
    scanf("%d",&n);
    
    for(int i=0;i<n;i++){
        cin >> a >> b;
        
        if(mm[a.size()-1].find(a)==mm[a.size()-1].end()){
            mm[a.size()-1][a]=b;
        }else{
            mm[a.size()-1][a]+=b;
        }
    }
    
    for(int i=0;i<5;i++)
        for(map<string,int>::iterator it=mm[i].begin();it!=mm[i].end();it++){
        cout << it->first << " " << it->second << endl;
        }
    
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x,n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
typedef pair<string,int> P;
typedef pair<int,P> PP;

//-----------------------------------------------------------------------------

bool f(const string& a,const string& b) {
	if(a.size()!=b.size()) return a.size()<b.size();
	REP(i,(int)a.size()) {
		if(a[i]==b[i]) continue;
		return a[i]<b[i];
	}
	return false;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n;
	cin>>n;
	map<string,int> mp;
	string s;int m;
	REP(i,n) {
		cin>>s>>m;
		mp[s]+=m;
	}
	vector<string> vec;
	for(auto x:mp) vec.pb(x.fi);
	sort(ALL(vec),f);
	for(auto x:vec) cout<<x<<' '<<mp[x]<<endl;

	return 0;
}

C++: #include<bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef pair<string,int>P;

int main(void){
	int n;
	P p[1000];
	P a[1000];
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>p[i].F>>p[i].S;
		a[i].S=0;
	}
	sort(p,p+n);
	
	int j;
	j=0;
	a[j].F=p[0].F;
	a[j].S+=p[0].S;
	for(int i=1;i<n;i++){
		if(p[i].F==p[i-1].F){
			a[j].F=p[i].F;
			a[j].S+=p[i].S;
		}
		else{
			j++;
			a[j].F=p[i].F;
			a[j].S+=p[i].S;
		}
	}
	for(int i=0;i<=j;i++){
			cout<<a[i].F<<" "<<a[i].S<<endl;
	}
	return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <limits>
#include <fstream>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
bool operator<(string a, string b){
  if(a.length()==b.length()){
    return a < b;
  }
  return a.length() < b.length();
}
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  map<string, int> mp;
  FOR(i,0,n){
      string s;
      int x;
      cin >> s >> x;
      mp[s] += x;
  }
  FOR(i,1,6){
    for(auto m : mp){
      if(m.first.length() == i) cout << m.first <<  " " << m.second << endl;
    }
  }
}

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

/*
const int MAX_E=2*(100-1);
const int MAX_V=100;
const long long int INF=100000000;
struct edge { int from,to,cost;};
edge es[MAX_E];
//int d[MAX_V];
int V,E;
void shortest_path(int s,int e,vector<int>& d);
int main(){
	int n,k;
	while(cin >> n >> k&&n!=0&&k!=0){
		int V=n;
		int E=2*(n-1);
		vector<int> land(V);
		int l=0;
		for(int i=0; i<E; ++i){
			es[i].cost=-1;
			es[i].from=-1;
			es[i].to=-1;
		}
		for(int i=0; i<k; ++k){
			int m;
			cin >> m;
			if(m==0){
				int a,b;
				cin >> a >> b;
				shortest_path(a,b,land);
				cout << land[b] << "\n";
			} else if(m==1){
				int c,d,e;
				cin >> c >> d >> e;
				es[l].from=c;
				es[l].to=d;
				es[l].cost=e;
				es[l+1].from=d;
				es[l+1].to=c;
				es[l+1].cost=e;
				l+=2;
			}
		}
	}
	return 0;
}
*/

int main(){
	int n;
	cin >> n;
	map<string,int> names;
	for(int i=0; i<n; ++i){
		int a;
		string b;
		cin >> b >> a;
		names[b]+=a;
	}
	for(int i=1; i<=5; ++i){
		for(map<string,int>::iterator itpairstri=names.begin(); itpairstri!=names.end(); ++itpairstri){
			if(i==itpairstri->first.size())cout << itpairstri->first << " " << itpairstri->second << "\n";
		}
	}
	return 0;
}