description: You are given a book with n chapters.
  
  Each chapter has a specified list of other chapters that need to be understood in order to understand this chapter. To understand a chapter, you must read it after you understand every chapter on its required list.
  
  Currently you don't understand any of the chapters. You are going to read the book from the beginning till the end repeatedly until you understand the whole book. Note that if you read a chapter at a moment when you don't understand some of the required chapters, you don't understand this chapter.
  
  Determine how many times you will read the book to understand every chapter, or determine that you will never understand every chapter no matter how many times you read the book.
  
  Input
  
  Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 2⋅10^4).
  
  The first line of each test case contains a single integer n (1 ≤ n ≤ 2⋅10^5) — number of chapters.
  
  Then n lines follow. The i-th line begins with an integer k_i (0 ≤ k_i ≤ n-1) — number of chapters required to understand the i-th chapter. Then k_i integers a_{i,1}, a_{i,2}, ..., a_{i, k_i} (1 ≤ a_{i, j} ≤ n, a_{i, j} ≠ i, a_{i, j} ≠ a_{i, l} for j ≠ l) follow — the chapters required to understand the i-th chapter.
  
  It is guaranteed that the sum of n and sum of k_i over all testcases do not exceed 2⋅10^5.
  
  Output
  
  For each test case, if the entire book can be understood, print how many times you will read it, otherwise print -1.
  
  Example
  
  Input
  
  
  5
  4
  1 2
  0
  2 1 4
  1 2
  5
  1 5
  1 1
  1 2
  1 3
  1 4
  5
  0
  0
  2 1 2
  1 2
  2 2 1
  4
  2 2 3
  0
  0
  2 3 2
  5
  1 2
  1 3
  1 4
  1 5
  0
  
  
  Output
  
  
  2
  -1
  1
  2
  5
  
  Note
  
  In the first example, we will understand chapters \{2, 4\} in the first reading and chapters \{1, 3\} in the second reading of the book.
  
  In the second example, every chapter requires the understanding of some other chapter, so it is impossible to understand the book.
  
  In the third example, every chapter requires only chapters that appear earlier in the book, so we can understand everything in one go.
  
  In the fourth example, we will understand chapters \{2, 3, 4\} in the first reading and chapter 1 in the second reading of the book.
  
  In the fifth example, we will understand one chapter in every reading from 5 to 1.
solution: import java.util.*;
  import java.io.*;
  import java.math.*;
  
  public class Coder {
  
    static StringBuffer str=new StringBuffer();
    static List<List<Integer>> l, rl;
    static int n;
    static int ind[];
    static List<Integer> topo;
  
    static void topSort(){
      Queue<Integer> q=new LinkedList<>();
      boolean vis[]=new boolean[n+1];
      for(int i=1;i<=n;i++) if(ind[i]==0) q.add(i);
      topo=new ArrayList<>();
      while(!q.isEmpty()){
        int u=q.remove();
        vis[u]=true;
        topo.add(u);
        for(int v:l.get(u)){
          if(--ind[v]==0){
            q.add(v);
          }
        }
      }
    }
    static void solve(){
      topSort();
      if(topo.size()!=n){
        str.append("-1\n");
        return;
      }
      int dp[]=new int[n+1];
      Arrays.fill(dp, 1);
      int ans=1;
      for(int i:topo){
        for(int v:rl.get(i)){
          if(v<i) dp[i]=Math.max(dp[i], dp[v]);
          else dp[i]=Math.max(dp[i], dp[v]+1);
        }
        ans=Math.max(dp[i], ans);
      }
      str.append(ans).append("\n");
    }
    
    public static void main(String[] args) throws java.lang.Exception {
      BufferedReader bf;
      PrintWriter pw;
      boolean lenv=false;
      if(lenv){
        bf = new BufferedReader(
                            new FileReader("input.txt"));
        pw=new PrintWriter(new
              BufferedWriter(new FileWriter("output.txt")));
      }else{
        bf = new BufferedReader(new InputStreamReader(System.in));
        pw = new PrintWriter(new OutputStreamWriter(System.out));
      }
  
      int q = Integer.parseInt(bf.readLine().trim());
      while (q-- > 0) {
         n=Integer.parseInt(bf.readLine().trim());
         l=new ArrayList<>();
         rl=new ArrayList<>();
         ind=new int[n+1];
         for(int i=0;i<=n;i++){l.add(new ArrayList<>()); rl.add(new ArrayList<>());}
         for(int i=1;i<=n;i++){
          String s[]=bf.readLine().trim().split("\\s+");
          int k=Integer.parseInt(s[0]);
          for(int j=1;j<=k;j++){
            int u=Integer.parseInt(s[j]);
            l.get(u).add(i);
            ind[i]++;
            rl.get(i).add(u);
          }
         }
         solve();
      }
      pw.print(str);
      pw.flush();
      // System.out.print(str);
    }
  }