include<bits/stdc++.h>
include
define int1 LL
define FOR(i,n) for (int i = 0; i < n; ++i)
define FORN(i, a, b) for (int i = a; i < b; ++i)
define FORE(i, a, b) for (int i = a; i <= b; ++i)
define DOWN(i,n) for (int i = n — 1; i >= 0; --i)
define SQR(x) ((x)*(x))
define ALL(c) (c).begin(), (c).end()
//#define map make_pair
define pb push_back
define fi first
define se second
define LL long long
//typedef pair <int, int> PII; //typedef pair <LL, LL> PLL; //typedef vector VI; //typedef vector VVI; //typedef vector VL; //typedef vector VVL; //typedef vector VLP; using namespace std; typedef long long ll; typedef long double LDB; typedef float doubl; typedef unsigned long long ULL; const ll INF = 2e9; const ll N = 400 + 1, K = 1e9 + 7; vector<vector>g, ginv; vectoru; bool has = 0; void dfs(ll v) { if (has)return; u[v] = 1; for (auto to : g[v]) { if (u[to] == 1) { has = 1; return; } if(u[to] == 0)dfs(to); } u[v] = 2; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); ll t; cin >> t; while (t--) { ll n; cin >> n; has = 0; g.clear(); ginv.clear(); u.clear(); g.resize(n); ginv.resize(n); u.resize(n); vectorw(n); fill(ALL(u), 0); queueq; sets; for (int i = 0;i < n;++i) { ll k; cin >> k; w[i] = k; if (k == 0) { s.insert(i); } for (int j = 0;j < k;++j) { ll a; cin >> a; a--; g[i].push_back(a); ginv[a].push_back(i); } } for (int i = 0;i < n;++i) { if (u[i] == 0) { dfs(i); } } if (has == 1) { cout << "-1\n"; continue; } ll res = 0; do { while (!q.empty()) { ll a = q.front(); q.pop(); s.insert(a); } while (!s.empty()) { auto a = (--s.end()); s.erase(a); for (auto to : ginv[a]) { w[to]--; if (w[to] == 0 && to > a) { s.insert(to); } else if (w[to] == 0) { q.push(to); } } } res++; } while (!q.empty()); cout << res << "\n"; } } /
*/