while True: n=int(input())# lh=[] for i in range(n): lt=list(input().split()) lh.append(lt) if n=='': break ## print(lh[0][0][0]) for i in range(n): for j in range(i+1,n): if int(lh[i][2])>int(lh[j][2]): lh[i],lh[j]=lh[j],lh[i] elif int(lh[i][2])==int(lh[j][2]): if ord(lh[i][0][0])>ord(lh[j][0][0]): lh[i],lh[j]=lh[j],lh[i] elif ord(lh[i][0][0])==ord(lh[j][0][0]): if int(lh[i][1])>int(lh[j][1]): lh[i],lh[j]=lh[j],lh[i]