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.*;
  public class C {
      static class Scan {
          private byte[] buf=new byte[1024];
          private int index;
          private InputStream in;
          private int total;
          public Scan()
          {
              in=System.in;
          }
          public int scan()throws IOException
          {
              if(total<0)
              throw new InputMismatchException();
              if(index>=total)
              {
                  index=0;
                  total=in.read(buf);
                  if(total<=0)
                  return -1;
              }
              return buf[index++];
          }
          public int scanInt()throws IOException
          {
              int integer=0;
              int n=scan();
              while(isWhiteSpace(n))
              n=scan();
              int neg=1;
              if(n=='-')
              {
                  neg=-1;
                  n=scan();
              }
              while(!isWhiteSpace(n))
              {
                  if(n>='0'&&n<='9')
                  {
                      integer*=10;
                      integer+=n-'0';
                      n=scan();
                  }
                  else throw new InputMismatchException();
              }
              return neg*integer;
          }
          public double scanDouble()throws IOException
          {
              double doub=0;
              int n=scan();
              while(isWhiteSpace(n))
              n=scan();
              int neg=1;
              if(n=='-')
              {
                  neg=-1;
                  n=scan();
              }
              while(!isWhiteSpace(n)&&n!='.')
              {
                  if(n>='0'&&n<='9')
                  {
                      doub*=10;
                      doub+=n-'0';
                      n=scan();
                  }
                  else throw new InputMismatchException();
              }
              if(n=='.')
              {
                  n=scan();
                  double temp=1;
                  while(!isWhiteSpace(n))
                  {
                      if(n>='0'&&n<='9')
                      {
                          temp/=10;
                          doub+=(n-'0')*temp;
                          n=scan();
                      }
                      else throw new InputMismatchException();
                  }
              }
              return doub*neg;
          }
          public String scanString()throws IOException
          {
              StringBuilder sb=new StringBuilder();
              int n=scan();
              while(isWhiteSpace(n))
              n=scan();
              while(!isWhiteSpace(n))
              {
                  sb.append((char)n);
                  n=scan();
              }
              return sb.toString();
          }
          private boolean isWhiteSpace(int n)
          {
              if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
              return true;
              return false;
          }
      }
      
      public static void sort(int arr[],int l,int r) {    //sort(arr,0,n-1);
          if(l==r) {
              return;
          }
          int mid=(l+r)/2;
          sort(arr,l,mid);
          sort(arr,mid+1,r);
          merge(arr,l,mid,mid+1,r);
      }
      public static void merge(int arr[],int l1,int r1,int l2,int r2) {
          int tmp[]=new int[r2-l1+1];
          int indx1=l1,indx2=l2;
          //sorting the two halves using a tmp array
          for(int i=0;i<tmp.length;i++) {
              if(indx1>r1) {
                  tmp[i]=arr[indx2];
                  indx2++;
                  continue;
              }
              if(indx2>r2) {
                  tmp[i]=arr[indx1];
                  indx1++;
                  continue;
              }
              if(arr[indx1]<arr[indx2]) {
                  tmp[i]=arr[indx1];
                  indx1++;
                  continue;
              }
              tmp[i]=arr[indx2];
              indx2++;
          }
          //Copying the elements of tmp into the main array
          for(int i=0,j=l1;i<tmp.length;i++,j++) {
              arr[j]=tmp[i];
          }
      }
      
      public static void sort(long arr[],int l,int r) {    //sort(arr,0,n-1);
          if(l==r) {
              return;
          }
          int mid=(l+r)/2;
          sort(arr,l,mid);
          sort(arr,mid+1,r);
          merge(arr,l,mid,mid+1,r);
      }
      public static void merge(long arr[],int l1,int r1,int l2,int r2) {
          long tmp[]=new long[r2-l1+1];
          int indx1=l1,indx2=l2;
          //sorting the two halves using a tmp array
          for(int i=0;i<tmp.length;i++) {
              if(indx1>r1) {
                  tmp[i]=arr[indx2];
                  indx2++;
                  continue;
              }
              if(indx2>r2) {
                  tmp[i]=arr[indx1];
                  indx1++;
                  continue;
              }
              if(arr[indx1]<arr[indx2]) {
                  tmp[i]=arr[indx1];
                  indx1++;
                  continue;
              }
              tmp[i]=arr[indx2];
              indx2++;
          }
          //Copying the elements of tmp into the main array
          for(int i=0,j=l1;i<tmp.length;i++,j++) {
              arr[j]=tmp[i];
          }
      }
      
      static ArrayList<Integer> adj_lst[];
      static boolean vis[];
      //-1->Black 0->White 1->Grey
      static int color[];
      static int dp[];
      public static void main(String args[]) throws IOException {
          Scan input=new Scan();
          StringBuilder ans=new StringBuilder("");
          int test=input.scanInt();
          for(int tt=1;tt<=test;tt++) {
              int n=input.scanInt();
              adj_lst=new ArrayList[n];
              vis=new boolean[n];
              color=new int[n];
              dp=new int[n];
              Arrays.fill(dp, -1);
              for(int i=0;i<n;i++) {
                  adj_lst[i]=new ArrayList<>();
              }
              for(int i=0;i<n;i++) {
                  int k=input.scanInt();
                  for(int j=0;j<k;j++) {
                      adj_lst[i].add(input.scanInt()-1);
                  }
              }
              boolean cyc=false;
              for(int i=0;i<n;i++) {
                  if(vis[i]) {
                      continue;
                  }
                  cyc|=mod_DFS(i);
              }
              if(cyc) {
                  ans.append(-1+"\n");
                  continue;
              }
              vis=new boolean[n];
              int max=0;
              for(int i=0;i<n;i++) {
                  if(vis[i]) {
                      continue;
                  }
                  max=Math.max(max,max_path(i));
              }
              ans.append(max+"\n");
          }
          System.out.println(ans);
      }
      public static boolean mod_DFS(int root) {
          color[root]=1;
          vis[root]=true;
          for(int i=0;i<adj_lst[root].size();i++) {
              if(color[adj_lst[root].get(i)]==1) {
                  return true;
              }
              if(!vis[adj_lst[root].get(i)]) {
                  if(mod_DFS(adj_lst[root].get(i))) {
                      return true;
                  }
              }
          }
          color[root]=-1;
          return false;
      }
      public static int max_path(int root) {
          vis[root]=true;
          if(dp[root]!=-1) {
              return dp[root];
          }
          if(adj_lst[root].size()==0) {
              dp[root]=1;
              return dp[root];
          }
          int ans=0;
          for(int i=0;i<adj_lst[root].size();i++) {
              if(adj_lst[root].get(i)>root) {
                  ans=Math.max(ans, max_path(adj_lst[root].get(i))+1);
              }
              else {
                  ans=Math.max(ans, max_path(adj_lst[root].get(i)));
              }
          }
  //        ans++;
          dp[root]=ans;
          return dp[root];
      }
  }
  