n = int(input()) d = dict() for i in range(1,n+1): s=input() d.setdefault(s,0) d[s]=d[s]+(n-i+1)*i ans=[] for k,v in d.items(): ans.append([k,v]) for i in sorted(ans): print(i[1],i[0])