#include using namespace std; vector a[101][11]; int T, n, len[20001], t[20001][100], p[20001][100]; int head, tail, q[20001]; bool v[101][11], ans[20001]; void work(int cur) { if (ans[cur]) return; ans[cur] = 1; for (int i = 0; i < len[cur]; ++i) v[t[cur][i]][p[cur][i]] = 1; } int main() { scanf("%d", &T); while (T--) { scanf("%d", &n); for (int i = 1; i <= 100; ++i) for (int j = 1; j <= 10; ++j) a[i][j].clear(); for (int i = 1; i <= n; ++i) { scanf("%d", &len[i]); for (int j = 0; j < len[i]; ++j) { scanf("%d%d", &t[i][j], &p[i][j]); a[t[i][j]][p[i][j]].push_back(i); } } memset(v, 0, sizeof(v)); memset(ans, 0, sizeof(ans)); work(1); for (int i = 1; i <= 100; ++i) for (int j = 1; j <= 10; ++j) if (v[i][j]) for (int k : a[i][j]) work(k); for (int i = 1, flag = 0; i <= n; ++i) if (ans[i]) { if (flag) putchar(' '); else flag = 1; printf("%d", i); } puts(""); } return 0; }