students = []
for _ in range(int(input())):
students.append(list(input().split()))
[print(student[0]) for student in sorted(students, key = lambda s: (-int(s[1]), int(s[2]), -int(s[3]), s[0]))]
students = []
for _ in range(int(input())):
students.append(list(input().split()))
[print(student[0]) for student in sorted(students, key = lambda s: (-int(s[1]), int(s[2]), -int(s[3]), s[0]))]