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.io.OutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.PrintWriter;
  import java.util.Collection;
  import java.io.IOException;
  import java.util.Deque;
  import java.io.InputStreamReader;
  import java.util.TreeSet;
  import java.util.ArrayList;
  import java.util.StringTokenizer;
  import java.io.BufferedReader;
  import java.util.ArrayDeque;
  import java.util.LinkedList;
  import java.io.InputStream;
  
  /**
   * Built using CHelper plug-in
   * Actual solution is at the top
   *
   * @author xwchen
   */
  public class Main {
      public static void main(String[] args) {
          InputStream inputStream = System.in;
          OutputStream outputStream = System.out;
          InputReader in = new InputReader(inputStream);
          PrintWriter out = new PrintWriter(outputStream);
          TaskC solver = new TaskC();
          solver.solve(1, in, out);
          out.close();
      }
  
      static class TaskC {
          public void solve(int testNumber, InputReader in, PrintWriter out) {
              int testCase = in.nextInt();
              for (int t = 0; t < testCase; t++) {
                  int n = in.nextInt();
                  TreeSet<Integer>[] adj = new TreeSet[n];
                  int[] ind = new int[n];
                  for (int i = 0; i < n; i++) {
                      adj[i] = new TreeSet<>();
                  }
                  for (int i = 0; i < n; i++) {
                      int m = in.nextInt();
                      int[] a = in.nextIntArray(m);
                      for (int k = 0; k < m; k++) {
                          --a[k];
                          adj[a[k]].add(i);
                          ind[i]++;
                      }
                  }
                  int result = Graph.topologicalSort(adj, ind, n, new ArrayList<>());
                  out.println(result);
              }
          }
  
      }
  
      static class InputReader {
          private BufferedReader reader;
          private StringTokenizer tokenizer = new StringTokenizer("");
  
          public InputReader(InputStream inputStream) {
              this.reader = new BufferedReader(
                      new InputStreamReader(inputStream));
          }
  
          public String next() {
              while (!tokenizer.hasMoreTokens()) {
                  try {
                      tokenizer = new StringTokenizer(reader.readLine());
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
              return tokenizer.nextToken();
          }
  
          public int nextInt() {
              return Integer.parseInt(next());
          }
  
          public int[] nextIntArray(int n) {
              int[] a = new int[n];
              for (int i = 0; i < n; i++)
                  a[i] = nextInt();
              return a;
          }
  
      }
  
      static class ArrayUtils {
          public static int maxElement(int[] a) {
              return maxElement(a, 0, a.length);
          }
  
          public static int maxElement(int[] a, int start, int end) {
              int ret = a[start];
              for (int i = start; i < end; ++i) {
                  ret = Math.max(ret, a[i]);
              }
              return ret;
          }
  
      }
  
      static class Graph {
          public LinkedList<Edge>[] adj;
          int n;
  
          public Graph() {
  
          }
  
          public Graph(int n) {
              this.n = n;
              adj = new LinkedList[n + 1];
              for (int i = 0; i <= n; ++i) {
  //            adj[i] = new TreeSet<>((o1, o2) -> {
  //                if (o1.value != o2.value) {
  //                    return o1.value - o2.value;
  //                } else {
  //                    return o1.type - o2.type;
  //                }
  //            });
                  adj[i] = new LinkedList<>();
              }
          }
  
          public static int topologicalSort(TreeSet<Integer>[] adj, int[] inDegree, int n, ArrayList<Integer> ret) {
              Deque<Integer> dq = new ArrayDeque<>();
              int[] dis = new int[n];
              for (int i = 0; i < n; ++i) {
                  if (inDegree[i] == 0) {
                      dq.addLast(i);
                      dis[i] = 1;
                  }
              }
  
              while (!dq.isEmpty()) {
                  int cur = dq.removeFirst();
                  ret.add(cur);
                  for (int v : adj[cur]) {
                      --inDegree[v];
                      if (cur < v) {
                          dis[v] = Math.max(dis[v], dis[cur]);
                      } else {
                          dis[v] = Math.max(dis[v], dis[cur] + 1);
                      }
                      if (inDegree[v] == 0) {
                          dq.addLast(v);
                      }
                  }
              }
              if (ret.size() < n) {
                  return -1;
              } else {
                  return ArrayUtils.maxElement(dis);
              }
          }
  
          public class Edge {
              public int u;
              public int v;
              public int value;
              public int type;
  
              public Edge(int u, int v, int value, int type) {
                  this.u = u;
                  this.v = v;
                  this.value = value;
                  this.type = type;
              }
  
              public Edge(int u, int v, int value) {
                  this.u = u;
                  this.v = v;
                  this.value = value;
              }
  
              public Edge(int v, int value) {
                  this.v = v;
                  this.value = value;
              }
  
          }
  
      }
  }
  
  