for s in sorted([input().split() for _ in [0]*int(input())], key=lambda x: (x[0], ['iti','ni','san','yon'].index(x[1]))): print(*s)