#include<bits/stdc++.h>
using namespace std;
int n,k; 
string s;
bool f;
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;
	}
}
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: hulaoshi
	Language: C++
	Result: Accepted
	Time:11 ms
	Memory:2168 kb
****************************************************************/