#include <bits/stdc++.h>
using namespace std;
const int M = 2e5 + 10;
int k[M];
int fa[M];
int head[M];
int next1[M];
int qu[M];
int inq[M];
int ouq[M];
int ce[M];
int cnt = 0;
void add(int x, int y) {
  next1[++cnt] = head[x];
  qu[cnt] = y;
  head[x] = cnt;
}
struct node {
  int c;
  int xu;
  bool operator<(const node z) const {
    if (c == z.c) return xu > z.xu;
    return c > z.c;
  }
};
int ans = 1;
priority_queue<node> que;
void bfs() {
  while (!que.empty()) {
    node z = que.top();
    que.pop();
    int x1 = z.xu;
    for (int i = head[x1]; i; i = next1[i]) {
      int v = qu[i];
      if (inq[v] == 0) continue;
      inq[v]--;
      if (inq[v] == 0) {
        if (ce[z.c + 1] == 0 && v < x1) {
          ans++;
          ce[z.c + 1] = 1;
        }
        node l;
        if (v < x1)
          l.c = z.c + 1;
        else
          l.c = z.c;
        l.xu = v;
        que.push(l);
      }
    }
  }
}
int fine(int x) { return fa[x] == x ? x : fa[x] = fine(fa[x]); }
bool check(int x, int y) {
  int z = fine(x);
  int z1 = fine(y);
  if (z == z1) return false;
  fa[z] = z1;
  return true;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    while (!que.empty()) que.pop();
    ans = 1;
    cnt = 0;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
      head[i] = 0;
      fa[i] = i;
      inq[i] = 0;
      ce[i] = 0;
    }
    int f = 0;
    for (int i = 1; i <= n; ++i) {
      int k;
      cin >> k;
      if (k == 0) {
        node l;
        l.c = 1;
        l.xu = i;
        que.push(l);
      }
      for (int j = 1; j <= k; ++j) {
        int x;
        cin >> x;
        add(x, i);
        ouq[x]++;
        inq[i]++;
      }
    }
    bfs();
    for (int i = 1; i <= n; ++i) {
      if (inq[i] != 0) {
        f = 1;
        break;
      }
    }
    if (!f)
      cout << ans << "\n";
    else
      cout << "-1\n";
  }
}
