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>
#include<string>

using namespace std;


int main(void){

	map<string, int> pro;
	int n;
	int num;
	string name;
    map<string, int>::iterator it;
	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> name >> num;
		if (pro.find(name) != pro.end()) pro[name] += num;
		else pro[name] = num;
	}

	for (int i = 1; i <= 5; i++){
		it = pro.begin();
		while (it != pro.end()){
			if ((*it).first.size() == i){
				cout << (*it).first << " " << (*it).second << endl;
			}
			it++;
		}
	}

	return 0;
}

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

using namespace std;

class S{
public:
  string s;
  int t;
  S(){}
  S(string s, int t): s(s), t(t) {}
  bool operator < (const S a) const {
    if(s.length() == a.s.length()) return s < a.s;
    return s.length() < a.s.length();
  }
};

main(){
  map<string, int> tmp;
  int n;
  cin >> n;
  for(int i=0;i<n;i++){
    string s;
    int t;
    cin >> s >> t;
    tmp[s] += t;
  }
  vector<S> ans;
  map<string, int>::iterator ite = tmp.begin();
  while(ite != tmp.end()){
    ans.push_back(S(ite->first, ite->second));
    ite++;
  }
  sort(ans.begin(), ans.end());
  for(int i=0;i<ans.size();i++) cout << ans[i].s << ' ' << ans[i].t << endl;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

bool compare(pair<string, int> p1, pair<string, int> p2) {
	if(p1.first.size() == p2.first.size()) return p1.first < p2.first;
	return p1.first.size() < p2.first.size();
}

int main() {
	int n;
	cin >> n;

	map<string, int> m;
	string s, t;
	int d;

	rep(i, n) {
		cin >> s >> d;
		m[s] += d;
	}

	vector<pair<string, int> > v;
	map<string, int>::iterator ite;
	for(ite = m.begin(); ite != m.end(); ite++) {
		v.push_back(mp(ite->first, ite->second));
	}

	sort(v.begin(), v.end(), compare);

	rep(i, v.size()) {
		cout << v[i].first << " " << v[i].second << endl;
	}


	return 0;
}

C++: #include <iostream>
#include <map>
#include <string>
using namespace std;
map <string,int> dic;
int main(){
  int n;
  cin>>n;
  for(int i=0;i<n;i++){
    string s;int a;
    cin>>s>>a;
    dic[s]+=a;
  }
  for(int size=1;size<=5;size++) for(auto i=dic.begin();i!=dic.end();i++) if((int)i->first.size()==size) cout<<i->first<<" "<<\
i->second<<endl;

  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<bitset>
#include<functional>
#include<climits>
using namespace std;
typedef pair<int,int>pii;
const int INF=1e9;
const int dx[]={0,-1,0,1},dy[]={-1,0,1,0};


struct data{
    string str;
    data(string _str):str(_str){}
    data(){}
    bool operator<(const data &d)const{
        if(str.length()!=d.str.length()){
            return str.length()<d.str.length();
        }
        else return str<d.str;
    }
};
int main(){
    map<data,int>M;
    int n;
    scanf("%d",&n);
    while(n--){
        string str;int m;
        cin>>str>>m;
        M[data(str)]+=m;
    }
    map<data,int>::iterator it;
    for(it=M.begin();it!=M.end();it++){
        cout<<(it->first).str<<" "<<it->second<<endl;
    }
    return 0;
}

C++: #include<vector>           
#include<algorithm>
#include <string.h>


using namespace std;

int main(){
	int n,total,min_size=1;
	string str;
	map<string, int> products;
	map<string, int>::iterator it;

	cin >> n;

	for (int i = 0; i<n; i++){
		cin >> str >> total;
		if (products.find(str) != products.end()){
			products[str] =  products[str] + total;
		}
		else{
			products[str] = total;
		}
	}

	n = products.size();

	while (n != 0){
		it = products.begin();
			while (it != products.end()){
				if ((*it).first.size() == min_size){
					cout << (*it).first << " " << (*it).second << endl;
					n--;
				}
				it++;
			}
		min_size++;
	}

	return 0;
}

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

using namespace std;

void Insert(const string&, int&, map< int, map<string, int> >*);
void Print(map< int, map<string, int> >&);

int main(){
  int i, n, x;
  string str;
  map< int, map<string, int> > data;

  cin >> n;
  for(i=0; i<n; ++i){
    cin >> str >> x;
    Insert(str, x, &data);
  }

  Print(data);
  return 0;
}

void Insert(const string& str, int& x, map< int, map<string, int> >* data){
  map< int, map<string, int> >::iterator i;
  i = data->find(str.size());
  if(i == data->end()){
    map<string, int> data_;
    data_.insert(make_pair(str, x));
    data->insert(make_pair(str.size(), data_));
    return;
  }
  map<string, int>::iterator j;
  j = i->second.find(str);
  if(j == i->second.end())
    i->second.insert(make_pair(str, x));
  else
    j->second += x;
}

void Print(map< int, map<string, int> >& data){
  map< int, map<string, int> >::iterator i;
  map<string, int>::iterator j;
  for(i=data.begin(); i!=data.end(); ++i){
    for(j=i->second.begin(); j!=i->second.end(); ++j){
      cout << j->first << " " << j->second << endl;
    }
  }
}

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

using namespace std;

bool comp(const string& key1, const string& key2)
{
  if(key1.size() == key2.size()) {
    return key1 < key2;
  }
  return key1.size() < key2.size();
}

int main()
{
  int n;
  cin >> n;

  string key;
  int value;
  map<string, int> products;
  vector<string> keys;

  for(int i = 0; i < n; i+= 1){
    cin >> key >> value;

    if(products.find(key) == products.end()) {
      products[key] = value;
      keys.push_back(key);
    } else {
      products[key] += value;
    }
  }

  sort(keys.begin(), keys.end(), comp);

  for(int i = 0; i < keys.size(); i += 1){
    cout << keys[i] << " " << products[keys[i]] << endl;
  }

  return 0;
}

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

int main()
{
	int n;
	
	cin >> n;
	map<string, int> m;
	while (n-- > 0){
		string s;
		int num;
		
		cin >> s >> num;
		m[s] += num;
	}
	for (int i = 1; i <= 5; i++){
		for (map<string, int>::iterator mi = m.begin(); mi != m.end(); mi++){
			if (mi->first.size() != i) continue;
			cout << mi->first << ' ' << mi->second << '\n';
		}
	}
	
	return 0;
}