count = int(input())
array = []
for _ in range(count):
age, name = input().split(' ')
array.append({'age': age, 'name': name})
array.sort(key=lambda item: int(item.get('age')))
for item in array:
print(f"{item.get('age')} {item.get('name')}")
n = int(input())
array = []
for _ in range(n):
input_data = input().split(' ')
array.append((int(input_data[0]), input_data[1]))
result = sorted(array, key=lambda x: x[0])
for i in result:
print(i[0], i[1])