n = int(input()); d = {} for i in range(1,n+1): s = input(); d[s] = d.get(s,0)+i*(n+1-i) for k,v in sorted(d.items()): print(v,k)