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 <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define vi vector<int>
#define VS vector<string>
#define all(x) x.begin(),x.end()
#define mp make_pair
#define pb push_back

using namespace std;

bool pairCompare(const std::pair<string, int>& firstElem, const std::pair<string, int>& secondElem) {
    if(firstElem.second != secondElem.second){
        return firstElem.second > secondElem.second;
    }else{
        if(firstElem.first.size() != secondElem.first.size()){
            return firstElem.first.size() < secondElem.first.size();
        }
        else
            return firstElem.first < secondElem.first;
    }
}

int main(){
    int n;
    map<string, int> m;
    int stringSize = 0;

    cin >> n;
    for(int i=0; i < n; i++){
        string s; int p;
        cin >> s >> p;
        stringSize = max(stringSize, (int)s.size());
        m[s] += p;
    }

    for(int i=0; i <= stringSize; i++){
        map<string, int>::iterator it = m.begin();
        while(it != m.end()){
            if(i == (*it).first.size())
                cout << (*it).first << " " << (*it).second << endl;
            ++it;
        }
    }
}