from collections import defaultdict N=int(input()) dic=defaultdict(int) for i in range(N): S=input() dic[S]+=(i+1)*(N-i) ans=[] for i in dic: ans.append((i,dic[i])) ans.sort() for p,c in ans: print(c,p)