#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;

#define endl '\n'

int main() {
	ios::sync_with_stdio(0);
	int t; cin >> t;
	while(t--) {
		map<string, set<string>> m;
        map<string, set<string>> m2;
		int n; cin >> n;
		for (int N=0; N<n; N++) {
			string name;
            cin >> name;
            int toppings;
            cin >> toppings;
            vector<string> vs;
            vector<string> vsback;
            set<string> ss;
            set<string> ssback;
            for (int i = 0; i < toppings; i++) {
                string s;
                cin >> s;
                vs.push_back(s);
                ssback.insert(s);
            }
            int english;
            cin >> english;
            for (int i = 0; i < english; i++) {
                string s;
                cin >> s;
                vsback.push_back(s);
                ss.insert(s);
            }
            for (string s : vs) {
                if (m.find(s) == m.end()) {
                    m[s] = ss;
                }
                else {
                    set<string> s2;
                    for (string ms : m[s]) {
                        if (ss.count(ms)) {
                            s2.insert(ms);
                        }
                    }
                    m[s] = s2;
                }
            }
            for (string s : vsback) {
                if (m2.find(s) == m2.end()) {
                    m2[s] = ssback;
                }
                else {
                    set<string> s2;
                    for (string ms : m2[s]) {
                        if (ssback.count(ms)) {
                            s2.insert(ms);
                        }
                    }
                    m2[s] = s2;
                }
            }
		}

		for (auto map_entry : m) {
            set<string> ss = m[map_entry.first];
            for (string s : ss) {
                if (m2[s].count(map_entry.first))
                    cout << "(" << map_entry.first << ", " << s << ")" << endl;
            }
        }
        cout << endl;
	}
	return 0;
}