#include<bits/stdc++.h>
using namespace std;
struct student{
char name[30];
int gg;
};
bool cmp(const student& n1,const student &n2){
return n1.gg>n2.gg;
}
int main()
{
struct student array[110];
int sum,n;
scanf("%d",&sum);
while(sum--){
scanf("%d",&n);
for(int i=0;i<n;++i){
scanf("%s%d",array[i].name,&array[i].gg);
}
sort(array,array+n,cmp);
for(int i=0;i<n;++i){
printf("%s\n",array[i].name);
}
}
}