#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;


int main() {
	int t;
	cin >> t;
	while (t--) {
		map<string, set<string>> m;
		map<string, set<string>> m2;
		int n, n2;
		cin >> n;
		n2 = n;
		while (n2--) {
			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;
}