#include <bits/stdc++.h>
using namespace std;
struct man{
    string name;
    int count;
}a[1000];
bool cmp(man m1,man m2){
    if(m1.count > m2.count){
        return true;
    }else{
        if(m1.count == m2.count && m1.name > m2.name){
            return true;
        }else{
            return false;
        }
    }
} 
  
int main(){
    int n,i,k = 0,p,j;
    string s;
      
    cin>>n;
    getchar();
    for(i = 0;i < n;i++){
        cin>>s;
        p = -1; 
        for(j = 0;j < k;j++){
            if(a[j].name == s){
                p = j;
                break;
            }
        } 
          
        if(p == -1){
            a[k].name = s;
            a[k].count = 1;
            k++;
        }else{
            a[p].count = a[p].count + 1;
        }
    }
    sort(a,a+k,cmp);
    for(i = 0;i < k;i++){
        cout<<a[i].name<<" "<<a[i].count<<endl;
    }
}
/**************************************************************
	Problem: 1499
	User: chenlingxuan
	Language: C++
	Result: Accepted
	Time:12 ms
	Memory:2128 kb
****************************************************************/