#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

struct BSTNode {
    string key;
    int value;
    int left;
    int right;

    BSTNode() : key(""), value(0), left(-1), right(-1) {}
};

int insertNodeBST(vector<BSTNode>& tree, int rootIndex, const string& key, int& nextAvailableIndex) {
    if (rootIndex == -1) {
        if (nextAvailableIndex >= tree.size()) {
            tree.resize(nextAvailableIndex + 1);
        }
        tree[nextAvailableIndex].key = key;
        tree[nextAvailableIndex].value = 1;
        return nextAvailableIndex++;
    }

    if (key < tree[rootIndex].key) {
        tree[rootIndex].left = insertNodeBST(tree, tree[rootIndex].left, key, nextAvailableIndex);
    } else if (key > tree[rootIndex].key) {
        tree[rootIndex].right = insertNodeBST(tree, tree[rootIndex].right, key, nextAvailableIndex);
    } else {
        tree[rootIndex].value++;
    }
    return rootIndex;
}

void inOrderTraversalBST(const vector<BSTNode>& tree, int index, vector<pair<string, int>>& result) {
    if (index != -1) {
        inOrderTraversalBST(tree, tree[index].left, result);
        result.push_back({tree[index].key, tree[index].value});
        inOrderTraversalBST(tree, tree[index].right, result);
    }
}

bool slt(string x, string y)
{
	if (x.size() == y.size())
		return (x < y);
	return (x.size() < y.size());
}

bool comparePairs(const pair<string, int>& a, const pair<string, int>& b) {
    if (a.second != b.second) {
        return (a.second > b.second);
    }
    return slt(a.first, b.first);
}

int partition(vector<pair<string, int>>& a, int left, int right) {
    pair<string, int> pivot = a[right];
    int id = left - 1;
    for (int i = left; i < right; i++) {
        if (comparePairs(a[i], pivot)) {
            id++;
            swap(a[id], a[i]);
        }
    }
    id++;
    swap(a[id], a[right]);
    return id;
}

void quickSort(vector<pair<string, int>>& a, int left, int right) {
    if (left < right) {
        int pivot_index = partition(a, left, right);
        quickSort(a, left, pivot_index - 1);
        quickSort(a, pivot_index + 1, right);
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int n;
    cin >> n;
    cin.ignore();

    vector<BSTNode> tree(n); // Kích thước tối đa
    int rootIndex = -1;
    int nextAvailableIndex = 0;

    for (int i = 0; i < n; ++i) {
        string x;
        cin >> x;
        rootIndex = insertNodeBST(tree, rootIndex, x, nextAvailableIndex);
    }

    vector<pair<string, int>> SortedResult;
    inOrderTraversalBST(tree, rootIndex, SortedResult);

    quickSort(SortedResult, 0, SortedResult.size() - 1);

    for (const auto& p : SortedResult) {
        cout << p.first << ' ' << p.second << '\n';
    }
	
	//cout << slt("3", "123");
    return 0;
}