#include<bits/stdc++.h>
using namespace std;
struct abc{
char str[21];
int count;
};
int cmp(abc a,abc b){
return a.count>b.count;
}
int main(){
int t,i,n;
struct abc o[101];
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i = 0;i<n;i++){
scanf("%s %d",&o[i].str,&o[i].count);
}
sort(o,o+n,cmp);
for(i = 0;i<n;i++){
printf("%s\n",o[i].str);
}
}
}