#include<iostream>
#include<algorithm>
using namespace std;
struct room{
char str[21];
int rank;
}student [100];
bool cmp(room a,room b){
return a.rank>b.rank;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
int N;
scanf("%d",&N);
for(int i=0;i<N;i++){
scanf("%s %d",student[i].str,&student[i].rank);
}
sort(student,student+N,cmp);
for(int i=0;i<N;i++){
printf("%s\n",student[i].str);
}
}
return 0;
}