for i in range(int(input())): a=[list(input().split())for j in range(int(input()))] a.sort(key=lambda n:(-int(n[1]))) for j in a: print(j[0])