/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>

typedef long long LL;
using namespace std;

int n, t;

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);

    scanf("%d", &t);
    while(t--){
        scanf("%d", &n);
        string name, st, ed;
        cin >> name >> st >> ed;
        string ansname1 = name, ansst = st, ansname2 = name, ansed = ed;
        for (int i = 2; i <= n; i++){
            cin >> name >> st >> ed;
            if(ansst > st){
                ansname1 = name;
                ansst = st;
            }
            if(ansed < ed){
                ansed = ed;
                ansname2 = name;
            }
        }
        cout << ansname1 << " " << ansname2 << endl;
    }

    return 0;
}
/**/