#include using std::cin; using std::cout; typedef long long ll; typedef std::vector vector; int n; int ans[20054]; bool ok[20054]; vector g[1005]; void work() { int i, l, t, p; bool u; cin >> n; for (i = 1; i <= n; ++i) for (ok[i] = false, cin >> l; l; --l) cin >> t >> p, g[(t - 1) * 10 + p].emplace_back(i); ok[1] = true; for (t = 1; t <= 1000; ++t) { u = false; for (int x : g[t]) u = u || ok[x]; for (int x : g[t]) ok[x] = u; g[t].clear(); } for (p = 0, i = 1; i <= n; ++i) if (ok[i]) ans[p++] = i; for (i = 0; i < p; ++i) cout << ans[i] << (i == p - 1 ? '\n' : ' '); if (!p) cout.put(10); } int main() { int T; std::ios::sync_with_stdio(false), cin.tie(NULL); for (cin >> T; T; --T) work(); return 0; }