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 <map>
using namespace std;

class grtr {
public:
  bool operator()(const string &s, const string &t) {
    return s.size()!=t.size() ? s.size()<t.size() : s<t;
  }
};

int main() {
  
  map<string, int, grtr> mp;
  int n; cin>>n;
  string s; int a;
  
  for(int i=0;i<n;i++) {
    cin>>s>>a;
    mp[s]+=a;
  }

  for(map<string,int,grtr>::iterator it=mp.begin();it!=mp.end();it++)
    cout << it->first << " " << it->second << endl;
  
  return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <string>
#include <map>

using namespace std;

char s[10];

int main()
{
	map<string,int> m;
	int n;
	scanf("%d",&n);
	for(int i = 0; i < n; i++)
	{
		int w;
		scanf("%s %d",s,&w);
		if(m.end() != m.find(s))
		{
			m[s] += w;
		}
		else
		{
			m[s] = w;
		}
	}
	map<string,int>::iterator it = m.begin();
	while(it != m.end())
	{
		printf("%s %d\n",(*it).first.c_str(),(*it).second);
		it++;
	}
	getchar();
	getchar();
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int n;
map <string ,int> mp;

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

int main(){
	scanf("%d", &n);
	
	for(int i = 0; i < n; i++){
		string s;
		int x;
		
		cin >> s >> x;
		
		mp[s] += x;
	}
	
	vector <pair<string, int> > res;
	
	for(auto g : mp)
	res.push_back(g);
	
	sort(res.begin(), res.end(), cmp);
	
	for(int i = 0; i < res.size(); i++)
	cout << res[i].first << " " << res[i].second << endl;
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF (1<<29)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))
const int dx[] = {-1,0,0,1},dy[] = {0,1,-1,0};

typedef long long int ll;

using namespace std;

int main(){
    int n;
    cin >> n;
    vector< pair< pair<int, string >, int > >  v;
    int  m = 100000000;
    REP(i,n){
	string s;
	int a;
	cin >> s >> a;
	bool flg = true;
	REP(j,v.size()){
	    if(v[j].first == pair<int,string>((int)(s.length()),s)) {
		v[j].second -= a;
		flg = false;
	    }
	}
	if(flg)
	    v.PB( make_pair( make_pair(s.length(),s ),m-a));
    }
    sort(ALL(v));
    REP(i,v.size()) printf("%s %d\n",v[i].first.second.c_str(),m-v[i].second);
    return 0;
}

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

using namespace std;

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

int main(){
  int n,num;
  string s;
  cin >> n;
  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++: // includes
#include <bits/stdc++.h>
using namespace std;

// macros
#define pb emplace_back
#define mk make_pair
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for(int i=((int)(n)-1);i>=0;i--)
#define irep(itr, st) for(auto itr = (st).begin(); itr != (st).end(); ++itr)
#define irrep(itr, st) for(auto itr = (st).rbegin(); itr != (st).rend(); ++itr)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define bit(n) (1LL<<(n))
// functions
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(a > b){a = b; return 1;} return 0;}
template <typename T> istream &operator>>(istream &is, vector<T> &vec){for(auto &v: vec)is >> v; return is;}
template <typename T> ostream &operator<<(ostream &os, const vector<T>& vec){for(int i = 0; i < vec.size(); i++){ os << vec[i]; if(i + 1 != vec.size())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const set<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const unordered_set<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const multiset<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p){os << p.first << " " << p.second; return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &mp){for(auto itr = mp.begin(); itr != mp.end(); ++itr){ os << itr->first << ":" << itr->second; auto titr = itr; if(++titr != mp.end())os << " "; } return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp){for(auto itr = mp.begin(); itr != mp.end(); ++itr){ os << itr->first << ":" << itr->second; auto titr = itr; if(++titr != mp.end())os << " "; } return os;}
//  types
using ll = long long int;
using P = pair<int, int>;
// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;
const int mod = 1000000007;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
// io
struct fast_io{
  fast_io(){ios_base::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20);}
} fast_io_;


int main(int argc, char const* argv[])
{
  int n; cin >> n;
  map<string, int> mp;
  rep(i, n){
    string s; cin >> s;
    int u; cin >> u;
    mp[s] += u;
  }
  vector<pair<string, int>> v; v.reserve(sz(mp));
  irep(itr, mp){
    v.pb(*itr);
  }
  sort(all(v), [](pair<string, int> &a, pair<string, int> &b){
    if(sz(a.first) != sz(b.first))return sz(a.first) < sz(b.first);
    return a < b;
  });
  rep(i, sz(v)){
    cout << v[i].first << " " << v[i].second << endl;
  }
  return 0;
}