import std.stdio;
import std.conv;
import std.container;
import std.algorithm;

void main() {
    int n; readf("%d\n", &n);
    int[string] data;
    foreach (_; 0 .. n) {
        string key; int value;
        readf("%s %d\n", &key, &value);
        data[key] = data.get(key, 0) + value;
    }
    static bool compare(string a, string b) {
        if (a.length == b.length) {
            return a < b;
        } else {
            return a.length < b.length;
        }
    }
    auto keys = data.keys.sort!compare;
    foreach (k; keys) {
        writeln(k, " ", data[k]);
    }
}