#include<bits/stdc++.h>
using namespace std;
int n,k; 
string s;
bool f;
//我是张爱lin
struct tp{
    string mz;
    int c; 
}a[2000]; 
bool cmp(tp x,tp y){
    if(x.c>y.c||(x.c==y.c&&x.mz>y.mz)){
        return true;
    }else{
        return false;
    }
}
//^_^^_^hahahahahah
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>s;
        f=false;
        for(int j=1;j<=k;j++){
            if(a[j].mz==s){
                a[j].c++;
                f=true;
                break;
            }
        }
        if(f==false){
            k++;
            a[k].mz=s;
            a[k].c=1;
        }   
    }
   sort(a+1,a+1+k,cmp);
   //嘻嘻嘻嘻嘻嘻
   
    for(int i=1;i<=k;i++){
        cout<<a[i].mz<<" "<<a[i].c<<endl;
    }
 
    return 0;
}
 
 
/**************************************************************
	Problem: 1499
	User: zhangailin
	Language: C++
	Result: Accepted
	Time:10 ms
	Memory:2168 kb
****************************************************************/