from collections import defaultdict

import sys


def printe(*args, end="\n", **kwargs):
    print(*args, end=end, file=sys.stderr, **kwargs)


def main():
    N = int(input())
    presents = [input() for _ in range(N)]
    present_day = defaultdict(int)
    for idx, present in enumerate(presents, 1):
        present_day[present] += idx * (N - idx + 1)
    printe(present_day)
    for present, n in sorted(present_day.items()):
        print(n, present)


if __name__ == "__main__":
    main()