#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
const int M = 15;
struct balloon
{
char color[M];
int number;
}s[M];
bool cmp(balloon x,balloon y)
{
return x.number>y.number;
}
int main()
{
int t,n,i;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s%d",s[i].color,&s[i].number);
sort(s,s+n,cmp);
for(i=0;i<n;i++)
printf("%s%c",s[i].color,i!=n-1?' ':'\n');
}
return 0;
}
HDU - 5702