#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
struct node
{
char name[105];
int birthday;
friend bool operator <(node a,node b)
{
return a.birthday<b.birthday;
}
};
node q1,q2;
priority_queue<node>q;
int main()
{
int t,n;
while(scanf("%d",&t)!=EOF)
{
for(int i=1; i<=t; i++)
{
scanf("%d",&n);
getchar();
for(int j=1; j<=n; j++)
{
char str[105],strr[105];
int birth;
memset(strr,0,sizeof(strr));
memset(str,0,sizeof(str));
cin.getline(str,105);
int len=strlen(str);
birth=str[len-1]-‘0‘+(str[len-2]-‘0‘)*10+(str[len-3]-‘0‘)*100+(str[len-4]-‘0‘)*1000;
for(int k=0; k<=len-1-5; k++)
{
strr[k]=str[k];
}
strcpy(q1.name,strr);
q1.birthday=birth;
q.push(q1);
}
while(!q.empty())
{
q2=q.top();
q.pop();
cout<<q2.name<<endl;
}
}
}
return 0;
}