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.lang.*;
  import java.io.*;
  
  public class Main
  {
      //static Scanner input= new Scanner();
      //static FastReader input=new FastReader();
      static UltraFastReader input = new UltraFastReader();
      static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
      // Output format => out.append("Your Data")+"\n");
      
      
      
  	public static void main (String[] args) throws java.lang.Exception
  	{
  		// your code goes here
  		// Output format => out.append("Your Data")+"\n");
  
  		int t = input.nextInt();
  		while(t-->0){
  		   int n = input.nextInt();
  		   
  		   int [] in = new int[n+1];
  		   int [] out = new int[n+1];
  		   
  		   ArrayList<Integer>[] graph = new ArrayList[n+1];
  		   for(int i=0;i<=n;i++){
  		       graph[i] = new ArrayList<Integer>();
  		   }
  		   
  		   for(int i=1;i<=n;i++){
  		       int k = input.nextInt();
  		       for(int j=1;j<=k;j++){
  		           int adj = input.nextInt();
  		           out[i]++;
  		           in[adj]++;
  		           graph[adj].add(i);
  		       }
  		   }
  		   
  		   PriorityQueue<Integer> queue = new PriorityQueue<>();
  		    PriorityQueue<Integer> queue2 = new PriorityQueue<>();
  		   HashSet<Integer> set = new HashSet<>();
  		   for(int i=1;i<=n;i++){
  		       set.add(i);
  		   }
  		   
  		   int count = 0;
  		   boolean flag = true;
  		   
  		    for(int i:set){
  		           if(out[i]==0){
      		           queue.add(i);
      		       }
  		       }
  		   
  		   while(!set.isEmpty()){
  		       count++;
  		      
  		       
  		       if(queue.isEmpty()){
  		           flag = false;
  		           break;
  		       }
  		       
  		       while(!queue.isEmpty()){
      		       int curr = queue.remove();
      		       for(int j:graph[curr]){
      		           out[j]--;
      		           
      		           if(out[j]==0){
      		               
      		               if(j<curr){
      		                   queue2.add(j);
      		               }
      		               else{
      		                   queue.add(j);
      		               }
      		               
      		           }
      		       }
      		       
      		       set.remove(curr);
      		       
      		       
      		   }
      		   
      		   queue = queue2;
      		   queue2 = new PriorityQueue<>();
  		       
  		   }
  		   
  		   if(flag==false){
  		      System.out.println("-1");
  		   }
  		   else{
  		       System.out.println(count);
  		   }
  		   
  		   
  		   
  		  
  		   
  		    
  		}
  		
  		out.flush();
  		out.close();
  	}
  	
  	public static ArrayList<LinkedList<Integer>> createUndirectedGraph(int n, int m) throws IOException{
          //This function will create graph with n nodes and m edges.
          // It can also create a undirected tree if you pass value m as n-1
          ArrayList<LinkedList<Integer>> graph = new ArrayList<>();
          for(int i=0;i<=n;i++){
              graph.add(new LinkedList<>());
          }
          
          for(int i=1;i<=m;i++){
              int u = input.nextInt();
              int v= input.nextInt();
                  graph.get(u).add(v);
                  graph.get(v).add(u);
          }
          
          return graph;
      }
  	
  	
  	static void merge(int arr[], int l, int m, int r)
      {
          // Find sizes of two subarrays to be merged
          int n1 = m - l + 1;
          int n2 = r - m;
   
          /* Create temp arrays */
          int L[] = new int[n1];
          int R[] = new int[n2];
   
          /*Copy data to temp arrays*/
          for (int i = 0; i < n1; ++i)
              L[i] = arr[l + i];
          for (int j = 0; j < n2; ++j)
              R[j] = arr[m + 1 + j];
   
          /* Merge the temp arrays */
   
          // Initial indexes of first and second subarrays
          int i = 0, j = 0;
   
          // Initial index of merged subarry array
          int k = l;
          while (i < n1 && j < n2) {
              if (L[i] <= R[j]) {
                  arr[k] = L[i];
                  i++;
              }
              else {
                  arr[k] = R[j];
                  j++;
              }
              k++;
          }
   
          /* Copy remaining elements of L[] if any */
          while (i < n1) {
              arr[k] = L[i];
              i++;
              k++;
          }
   
          /* Copy remaining elements of R[] if any */
          while (j < n2) {
              arr[k] = R[j];
              j++;
              k++;
          }
      }
   
      // Main function that sorts arr[l..r] using
      // merge()
      static void mergeSort(int arr[], int l, int r)
      {
          if (l < r) {
              // Find the middle point
              int m =l+ (r-l)/2;
   
              // Sort first and second halves
              mergeSort(arr, l, m);
              mergeSort(arr, m + 1, r);
   
              // Merge the sorted halves
              merge(arr, l, m, r);
          }
      }
  	
  	public static int modPower(int x, int y, int p)
    {
      int res = 1; // Initialize result
   
      x = x % p; // Update x if it is more than or
      // equal to p
   
      if (x == 0)
        return 0; // In case x is divisible by p;
   
      while (y > 0)
      {
   
        // If y is odd, multiply x with result
        if ((y & 1) != 0)
          res = (res * x) % p;
   
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
      }
      return res;
    }
  	
  	 public static LinkedList<Integer> getDivisors(int n){
          LinkedList <Integer> res = new LinkedList<>();
          for(int i=1;i*i<=n;i++){
              if(n%i==0){
                  res.add(i);
                  if(i*i!=n){
                      res.add(n/i);
                  }
              }
          }
          return res;
      }
      
      
      public static void printLL(LinkedList<Integer> ll) throws IOException{
          out.append("{");
          for(int i:ll){
              out.append(i+" ");
          }
          out.append("}\n");
      }
      
      public static boolean isPrime(int n) throws IOException{
          for(int i=2;i*i<=n;i++){
              if(n%i==0){
                  return false;
              }
          }
          return true;
      }
  	
  	
  	//// Fast Reader Start
  	static class FastReader
      {
          BufferedReader br;
          StringTokenizer st;
          public FastReader()
          {
              br = new BufferedReader(new InputStreamReader(System.in));
          }
          String next()
          {
              while (st == null || !st.hasMoreElements())
              {
                  try
                  {
                      st = new StringTokenizer(br.readLine());
                  }
                  catch (IOException e)
                  {
                      e.printStackTrace();
                  }
              }
              return st.nextToken();
          }
          
          public String [] nextStrings(int n) throws IOException{
              String [] res = new String[n];
              for(int i=0;i<n;i++){
                  res[i] = next();
              }
              return res;
          }
          
          int nextInt()
          {
              return Integer.parseInt(next());
          }
          
          public int [] nextInts(int n) throws IOException{
              int [] res = new int[n];
              for(int i=0;i<n;i++){
                  res[i] = input.nextInt();
              }
              return res;
          }
          
          long nextLong()
          {
              return Long.parseLong(next());
          }
          
          public long [] nextLongs(int n) throws IOException{
              long [] res = new long[n];
              for(int i=0;i<n;i++){
                  res[i] = input.nextLong();
              }
              return res;
          }
          
          double nextDouble()
          {
              return Double.parseDouble(next());
          }
          
          public double [] nextDoubles(int n) throws IOException{
              double [] res = new double[n];
              for(int i=0;i<n;i++){
                  res[i] = input.nextDouble();
              }
              return res;
          }
          
          String nextLine()
          {
              String str="";
              try
              {
                  str=br.readLine();
              }
              catch (IOException e)
              {
                  e.printStackTrace();
              }
              return str;
          }
      }
      
      //// Fast Reader End
      
      
      //// Ultra Fast Reader Start
      
      static class UltraFastReader {
          final private int BUFFER_SIZE = 1 << 16;
          private DataInputStream din;
          private byte[] buffer;
          private int bufferPointer, bytesRead;
   
          public UltraFastReader()
          {
              din = new DataInputStream(System.in);
              buffer = new byte[BUFFER_SIZE];
              bufferPointer = bytesRead = 0;
          }
   
          public UltraFastReader(String file_name) throws IOException
          {
              din = new DataInputStream(
                  new FileInputStream(file_name));
              buffer = new byte[BUFFER_SIZE];
              bufferPointer = bytesRead = 0;
          }
   
          public String readLine() throws IOException
          {
              byte[] buf = new byte[64]; // line length
              int cnt = 0, c;
              while ((c = read()) != -1) {
                  if (c == '\n') {
                      if (cnt != 0) {
                          break;
                      }
                      else {
                          continue;
                      }
                  }
                  buf[cnt++] = (byte)c;
              }
              return new String(buf, 0, cnt);
          }
   
          public int nextInt() throws IOException
          {
              int ret = 0;
              byte c = read();
              while (c <= ' ') {
                  c = read();
              }
              boolean neg = (c == '-');
              if (neg)
                  c = read();
              do {
                  ret = ret * 10 + c - '0';
              } while ((c = read()) >= '0' && c <= '9');
   
              if (neg)
                  return -ret;
              return ret;
          }
          
          public int [] nextInts(int n) throws IOException{
              int [] res = new int[n];
              for(int i=0;i<n;i++){
                  res[i] = input.nextInt();
              }
              return res;
          }
   
          public long nextLong() throws IOException
          {
              long ret = 0;
              byte c = read();
              while (c <= ' ')
                  c = read();
              boolean neg = (c == '-');
              if (neg)
                  c = read();
              do {
                  ret = ret * 10 + c - '0';
              } while ((c = read()) >= '0' && c <= '9');
              if (neg)
                  return -ret;
              return ret;
          }
          
           public long [] nextLongs(int n) throws IOException{
              long [] res = new long[n];
              for(int i=0;i<n;i++){
                  res[i] = input.nextLong();
              }
              return res;
          }
   
          public double nextDouble() throws IOException
          {
              double ret = 0, div = 1;
              byte c = read();
              while (c <= ' ')
                  c = read();
              boolean neg = (c == '-');
              if (neg)
                  c = read();
   
              do {
                  ret = ret * 10 + c - '0';
              } while ((c = read()) >= '0' && c <= '9');
   
              if (c == '.') {
                  while ((c = read()) >= '0' && c <= '9') {
                      ret += (c - '0') / (div *= 10);
                  }
              }
   
              if (neg)
                  return -ret;
              return ret;
          }
          
           public double [] nextDoubles(int n) throws IOException{
              double [] res = new double[n];
              for(int i=0;i<n;i++){
                  res[i] = input.nextDouble();
              }
              return res;
          }
   
          private void fillBuffer() throws IOException
          {
              bytesRead = din.read(buffer, bufferPointer = 0,
                                   BUFFER_SIZE);
              if (bytesRead == -1)
                  buffer[0] = -1;
          }
   
          private byte read() throws IOException
          {
              if (bufferPointer == bytesRead)
                  fillBuffer();
              return buffer[bufferPointer++];
          }
   
          public void close() throws IOException
          {
              if (din == null)
                  return;
              din.close();
          }
      }
      //// Ultra  Fast Reader End
      
  }