#include <iostream>
#include <string>

// Định nghĩa cấu trúc Node cho linked list
struct Node {
    std::string code;
    int count;
    Node* next;

    Node(const std::string& c) : code(c), count(1), next(nullptr) {}
};

// Hàm chèn một node mới vào linked list đã sắp xếp theo mã hàng
void insertSorted(Node*& head, const std::string& code) {
    Node* newNode = new Node(code);
    if (!head || code < head->code) {
        newNode->next = head;
        head = newNode;
    } else {
        Node* current = head;
        while (current->next && current->next->code < code) {
            current = current->next;
        }
        if (current->next && current->next->code == code) {
            current->next->count++;
            delete newNode;
        } else {
            newNode->next = current->next;
            current->next = newNode;
        }
    }
}

// Hàm chuyển linked list sang mảng
void linkedListToArray(Node* head, std::pair<std::string, int> arr[], int& size) {
    size = 0;
    Node* current = head;
    while (current) {
        arr[size++] = {current->code, current->count};
        current = current->next;
    }
}

bool slt(std::string x, std::string y) {
    if (x.size() < y.size()) return true;
    else if (x.size() > y.size()) return false;
    return (x < y);
}

int partition(std::pair<std::string, int> a[], int left, int right) {
    std::pair<std::string, int> pivot = a[right];
    int id = left - 1;
    for (int i = left; i < right; i++) {
        if ((a[i].second > pivot.second) || (a[i].second == pivot.second && slt(a[i].first, pivot.first))) {
            id++;
            std::swap(a[id], a[i]);
        }
    }
    id++;
    std::swap(a[id], a[right]);
    return id;
}

void Sort_pairSecond(std::pair<std::string, int> a[], int left, int right) {
    if (left < right) {
        int id_pivot = partition(a, left, right);
        Sort_pairSecond(a, left, id_pivot - 1);
        Sort_pairSecond(a, id_pivot + 1, right);
    }
}

int main() {
    int n;
    std::cin >> n;
    std::cin.ignore();

    Node* head = nullptr;
    for (int i = 0; i < n; ++i) {
        std::string code;
        std::cin >> code;
        insertSorted(head, code);
    }

    std::pair<std::string, int> arr[n]; // Kích thước tối đa
    int size = 0;
    linkedListToArray(head, arr, size);

    Sort_pairSecond(arr, 0, size - 1);

    for (int i = 0; i < size; ++i) {
        std::cout << arr[i].first << ' ' << arr[i].second << '\n';
    }

    // Giải phóng bộ nhớ của linked list
    Node* current = head;
    while (current) {
        Node* next = current->next;
        delete current;
        current = next;
    }

    return 0;
}