#include <iostream>
#include <map>

using namespace std;
typedef long long ll;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int ncase; cin >> ncase;
	for (int csnum = 1; csnum <= ncase; csnum++) {
		int n; cin >> n;
		map<string, ll> ing1, ing2;
		for (int i = 0; i < n; i++) {
			string name; cin >> name;
			int m1; cin >> m1;
			for (int j = 0; j < m1; j++) {
				string s; cin >> s;
				ing1[s] |= (1LL << i);
			}
			int m2; cin >> m2;
			for (int j = 0; j < m2; j++) {
				string s; cin >> s;
				ing2[s] |= (1LL << i);
			}
		}
		for (auto it1 = ing1.begin(); it1 != ing1.end(); ++it1) {
			for (auto it2 = ing2.begin(); it2 != ing2.end(); ++it2) {
				if (it1->second == it2->second) {
					cout << "(" << it1->first << ", " << it2->first << ")" << endl;
				}
			}
		}
		cout << endl;
	}
}