#include <bits/stdc++.h>
using namespace std;
long long mod1 = 1000000007, mod2 = 998244353;
long long mn = LLONG_MAX;
long long mx = LLONG_MIN;
long long mod;
vector<int> adj[200007];
int siz[200007], vis[200007], ord[200007], dro[200007], dp[200007], flg, ttl;
stack<int> stc;
void dfs(int src) {
  vis[src] = 1;
  int sz = siz[src];
  for (int lp = 0; lp < sz; lp++) {
    int u = adj[src][lp];
    if (vis[u] == 1) {
      flg = 0;
    }
    if (vis[u] == 0) {
      dfs(u);
    }
  }
  vis[src] = 2;
  stc.push(src);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int tc, o;
  cin >> tc;
  for (o = 1; o <= tc; o++) {
    int n, m, a, b, c, i, j, k, x = 0;
    flg = 1;
    vector<int> v;
    cin >> n;
    for (i = 1; i <= n; i++) {
      cin >> k;
      if (k == 0) {
        v.push_back(i);
        x++;
      } else {
        for (j = 1; j <= k; j++) {
          cin >> a;
          adj[a].push_back(i);
          siz[a]++;
        }
      }
    }
    for (i = 1; i <= n; i++) {
      if (vis[i] == 0) dfs(i);
    }
    if (flg == 0)
      cout << "-1" << endl;
    else {
      int mx = -9, cnt = 0, e, f, g, h, z, idx;
      while (!stc.empty()) {
        a = stc.top();
        cnt++;
        ord[a] = cnt;
        dro[cnt] = a;
        stc.pop();
      }
      for (idx = n; idx >= 1; idx--) {
        dp[idx] = 1;
        e = dro[idx];
        f = siz[e];
        for (j = 0; j < f; j++) {
          g = adj[e][j];
          z = 0;
          if (g < e) {
            z = 1;
          }
          h = ord[g];
          dp[idx] = max(dp[idx], z + dp[h]);
        }
        mx = max(mx, dp[idx]);
      }
      cout << mx << endl;
    }
    while (!stc.empty()) stc.pop();
    for (i = 1; i <= n; i++) {
      adj[i].clear();
      siz[i] = 0;
      vis[i] = 0;
      ord[i] = 0;
      dro[i] = 0;
      dp[i] = 0;
    }
    v.clear();
  }
  return 0;
}
