#include // {{{ Definitions #define reg register #define pr std::pair #define fi first #define se second #define FIN(s) freopen(s, "r", stdin) #define FOUT(s) freopen(s, "w", stdout) #define FERR(s) freopen(s, "w", stderr) #define debug(...) fprintf(stderr, __VA_ARGS__) #define dputs(s) fprintf(stderr, s"\n") #define rep(i, l, r) for (int i = l; i <= r; ++i) #define lep(i, l, r) for (int i = l; i < r; ++i) #define irep(i, r, l) for (int i = r; i >= l; --i) #define ilep(i, r, l) for (int i = r; i > l; --i) #define Rep(i, n) rep(i, 1, n) #define Lep(i, n) lep(i, 1, n) #define IRep(i, n) irep(i, n, 1) #define ILep(i, n) ilep(i, n, 1) typedef long long ll; typedef long double ld; // }}} // {{{ Modular namespace modular { const int MOD = 1000000007; inline int add(int x, int y) { return (x += y) >= MOD ? x -= MOD : x; } inline void inc(int &x, int y) { (x += y) >= MOD ? x -= MOD : 0; } inline int mul(int x, int y) { return 1LL * x * y % MOD; } inline int qpow(int x, int y) { int ans = 1; for (; y; y >>= 1, x = mul(x, x)) if (y & 1) ans = mul(ans, x); return ans; } }; // namespace modular // }}} // {{{ Base namespace Base { template inline Tp input() { Tp x = 0, y = 1; char c = getchar(); while ((c < '0' || '9' < c) && c != EOF) { if (c == '-') y = -1; c = getchar(); } if (c == EOF) return 0; while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getchar(); return x *= y; } template inline void read(Tp &x) { x = input(); } template inline void chmax(Tp &x, Tp y) { x < y ? x = y : 0; } template inline void chmin(Tp &x, Tp y) { x > y ? x = y : 0; } }; // namespace Base // }}} using namespace Base; #define MAX_N 20007 int N, tot; std::vectorG[106][15]; int ans[MAX_N], top; bool vis[MAX_N], tmp[11]; void solve() { read(N); Rep(i, N) vis[i] = false; vis[1] = true; Rep(i, 100) Rep(j, 10) G[i][j].clear(); int len, t, p, mx = 0; Rep(i, N) { read(len); Rep(j, len) { read(t), read(p); G[t][p].push_back(i); chmax(mx, t); } } Rep(i, mx) { Rep(j, 10) tmp[j] = false; Rep(j, 10) if (G[i][j].size()) { bool asd = false; lep(k, 0, G[i][j].size()) asd |= vis[G[i][j][k]]; if (asd) tmp[j] = true; } Rep(j, 10) if (tmp[j]) { lep(k, 0, G[i][j].size()) vis[G[i][j][k]] = true; } } top = 0; Rep(i, N) if (vis[i]) ans[++top] = i; Lep(i, top) printf("%d ", ans[i]); printf("%d\n", ans[top]); } int main() { #ifndef ONLINE_JUDGE FIN("a.in"); // FOUT("a.out"); // FERR("a.log"); #endif int T = input(); while (T--) solve(); return 0; }