#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <cassert>
#include <string.h>

#include <time.h>
#include <random>
#include <iomanip>
using namespace std;

#define ll long long

void solve() {
    
    map<string, vector<string> > m, m2;
    
    ll n;
    cin >> n;
    
    for (ll i = 0; i < n; i++) {
        string s;
        cin >> s;
        ll a;
        cin >> a;
        vector<string> words(a);
        for (ll i = 0; i < a; i++) {
            cin >> words[i];
        }
        
        ll b;
        cin >> b;
        vector<string> v(b);
        for (ll i = 0; i < b; i++) {
            cin >> v[i];
        }
        
        for (ll i = 0; i < a; i++) {
            string key = words[i];
            
            if (m.find(key) == m.end())
                m[key] = v;
            else {
                vector<string> mv = m[key];
                vector<string> newVec;
                map<string, bool> newMap;
                for (ll i = 0; i < mv.size(); i++)
                    newMap[mv[i]] = true;
                for (ll i = 0; i < v.size(); i++)
                    if (newMap.find(v[i]) != newMap.end())
                        newVec.push_back(v[i]);
                m[key] = newVec;
            }
        }
        
        swap(v, words);
        swap(m, m2);
        for (ll i = 0; i < b; i++) {
            string key = words[i];
            
            if (m.find(key) == m.end())
                m[key] = v;
            else {
                vector<string> mv = m[key];
                vector<string> newVec;
                map<string, bool> newMap;
                for (ll i = 0; i < mv.size(); i++)
                    newMap[mv[i]] = true;
                for (ll i = 0; i < v.size(); i++)
                    if (newMap.find(v[i]) != newMap.end())
                        newVec.push_back(v[i]);
                m[key] = newVec;
            }
        }
        swap(m, m2);
    }
    
    vector<pair<string, vector<string> > > res;
    for (auto it = m.begin(); it != m.end(); it++) {
        res.push_back({it->first, it->second});
    }
    
    sort(res.begin(), res.end());
    
    for (ll i = 0; i < res.size(); i++) {
        sort(res[i].second.begin(), res[i].second.end());
        
        for (ll j = 0; j < res[i].second.size(); j++) {
            
            //check if exists in m2
            bool good = false;
            vector<string> vec = m2[res[i].second[j]];
            for (ll z = 0; z < vec.size(); z++)
                if (vec[z] == res[i].first)
                    good = true;
            if (!good)
                continue;
            
            cout << "(";
            cout << res[i].first;
            cout << ", ";
            cout << res[i].second[j];
            cout << ")" << endl;
        }
    }
    cout << endl;
}

int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    
    ll t;
    cin >> t;
    for (ll i = 0; i < t; i++) {
        solve();
    }
}