Case = int(input())
customer_list = []
for i in range(Case):
age, name = input().split()
age = int(age)
customer_list.append([age, name, i])
customer_list = sorted(customer_list, key = lambda x : (x[0], x[2]))
sorted_customer_list = list(map(lambda x : x[:-1], customer_list))
for customer in sorted_customer_list:
print(*customer)
Case = int(input())
coord_list = []
for _ in range(Case):
coord_list.append(list(map(int, input().split())))
coord_list = sorted(coord_list, key = lambda x : (x[0], x[1]))
for coord in coord_list:
print(*coord)