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