#include #include #include #include #include using namespace std; const int N = 20005, S = 400005; typedef pair PII; int n, len[N], pos[N], idx, s, q[S], ans[N], cnt; map mp; vector g[S]; bool vis[S]; void inline bfs() { int hh = 0, tt = 0; vis[s] = 0, q[0] = s; while (hh <= tt) { int u = q[hh++]; for (int i = 0; i < g[u].size(); i++){ int v = g[u][i]; if (!vis[v]) { vis[v] = true, q[++tt] = v; } } } } int main() { int T; scanf("%d", &T); while (T--) { idx = 0; memset(pos, 0, sizeof pos); memset(vis, 0, sizeof vis); s = 0; mp.clear(); scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", len + i); int lastIdx = 0; for (int j = 1; j <= len[i]; j++) { int t, p; scanf("%d%d", &t, &p); if (!mp[make_pair(t, p)]) mp[make_pair(t, p)] = ++idx; ++idx; if (s == 0 && i == 1 && j == 1) s = idx; if (j > 1){ g[lastIdx].push_back(idx); //cout << t << " " << p << " )))) " << lastIdx << " idx --> " << idx << endl; } int bel = mp[make_pair(t, p)]; g[bel].push_back(idx); //cout << bel << " ---> belong " << idx << endl; g[idx].push_back(bel); pos[i] = idx; lastIdx = idx; } } bfs(); cnt = 0; for (int i = 1; i <= n; i++) if (vis[pos[i]]) ans[++cnt] = i; for (int i = 1; i < cnt; i++) printf("%d ", ans[i]); if (cnt) printf("%d", ans[cnt]); for (int i = 1; i <= idx; i++) g[i].clear(); puts(""); } return 0; }