#include<stdio.h>
struct chenghao{
char a[21];
int x;
};
int main(){
int t,i,n,j,pass,temp,b[101];
struct chenghao s[101];
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%s %d",&s[i].a,&s[i].x);
b[i]=s[i].x;
}
for(pass=1;pass<n;pass++){
for(i=0;i<n-pass;i++){
if(b[i]<b[i+1]){
temp=b[i];
b[i]=b[i+1];
b[i+1]=temp;
}
}
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(b[i]==s[j].x)
printf("%s\n",s[j].a);
}
}
}
}