#include<string>
#include<iomanip>
#include<cmath>
#include<cstdio>
#include<string.h>
using namespace std;
struct Node{
string name;
int h,m,s;
friend bool operator< (Node& n1,Node&n2){
if(n1.h!=n2.h)return n1.h<n2.h;
if(n1.m!=n2.m)return n1.m<n2.m;
if(n1.s!=n2.s)return n1.s<n2.s;
return true;
}
};
int main(){
int T;scanf("%d",&T);
while(T--){
Node mi,mx,no;//最早进入,最晚退出
int n;scanf("%d",&n);n--;
char name[16]; int a,b,c;
scanf("%s",name);
scanf("%d:%d:%d",&a,&b,&c);//进入的时间
mi.name= name;
mi.h=a;
mi.m=b;
mi.s=c;
scanf("%d:%d:%d",&a,&b,&c);
mx.name= name;
mx.h=a;
mx.m=b;
mx.s=c;
while(n--){
scanf("%s",name);
scanf("%d:%d:%d",&a,&b,&c);
no.name= name;
no.h=a;
no.m=b;
no.s=c;
if(no<mi)mi=no;
scanf("%d:%d:%d",&a,&b,&c);
no.name= name;
no.h=a;
no.m=b;
no.s=c;
if(mx<no)mx=no;
}
char c1[16],c2[16];
strcpy(c1,mi.name.c_str());
strcpy(c2,mx.name.c_str());
printf("%s %s\n",c1,c2);
}
return 0;
}