#include<stdio.h>
#include<stdlib.h>
struct loy{
char name[20];
double num;
};
int comp(const void *p,const void *q){
return((struct loy *)q)->num-((struct loy *)p)->num;
}
int main()
{
struct loy x[1000];
int T;
scanf("%d",&T);
while(T--){
int n,i,j,t;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%s %lf",x[i].name,&x[i].num);
}
qsort(x,n,sizeof(struct loy),comp);
for(i=0;i<n;i++){
printf("%s\n",x[i].name);
}
}
}