#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
struct node
{
char s[50];
int d;
} loy[105];
int cmp(const node x,const node y)
{
return x.d > y.d ;
}
int main()
{
int t=0;
scanf("%d",&t);
while(t--)
{
int n=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s%d",loy[i].s ,&loy[i].d );
}
sort(loy,loy+n,cmp);
for(int i=0;i<n;i++)
{
printf("%s\n",loy[i].s);
}
}
return 0;
}