n = int(input()) #输入学生人数
#输入学生名单
stu_list = []
for i in range(n):
stu = input().split()
stu[0] = int(stu[0]) #学号
stu[2] = int(stu[2]) #年龄
stu_list.append(stu)
print(stu_list)
stu_list.sort(key = lambda stu: stu[1]) #按姓名排序
for stu in stu_list:
print("%3d%6s%3d" % (stu[0], stu[1], stu[2]))
print('')
stu_list.sort(key = lambda stu: stu[2])
for stu in stu_list:
print("%3d%6s%3d" % (stu[0], stu[1], stu[2]))