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.BufferedReader;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.math.BigInteger;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.BitSet;
  import java.util.Calendar;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.LinkedList;
  import java.util.PriorityQueue;
  import java.util.SortedSet;
  import java.util.Stack;
  import java.util.StringTokenizer;
  import java.util.TreeMap;
  import java.util.TreeSet;
  
  /**
   * #
   *
   * @author pttrung
   */
  public class C_Round_743_Div2 {
      public static long MOD = 998244353;
  
      static int[] dp;
  
  
      public static void main(String[] args) throws FileNotFoundException {
          // PrintWriter out = new PrintWriter(new FileOutputStream(new File(
          // "output.txt")));
          PrintWriter out = new PrintWriter(System.out);
          Scanner in = new Scanner();
          int T = in.nextInt();
          for (int Z = 0; Z < T; Z++) {
              int n = in.nextInt();
              int[][] data = new int[n][];
              for (int i = 0; i < n; i++) {
                  int k = in.nextInt();
                  data[i] = new int[k];
                  for (int j = 0; j < k; j++) {
                      data[i][j] = in.nextInt() - 1;
                  }
              }
              dp = new int[n];
              Arrays.fill(dp, -2);
              int result = 0;
              boolean[] s = new boolean[n];
              for (int i = 0; i < n; i++) {
                  int v = cal(i, s, data);
                  if (v == -1) {
                      result = -1;
                      break;
                  }
                  result = Integer.max(result, v);
              }
              out.println(result);
          }
          out.close();
      }
  
      static int cal(int index, boolean[] s, int[][] data) {
          if (dp[index] != -2) {
              return dp[index];
          }
          if (s[index]) {
              return -1;
          }
          s[index] = true;
          int result = 1;
          for (int i : data[index]) {
              int v = cal(i, s, data);
              if (v == -1) {
                  result = -1;
                  break;
              }
              if (i > index) {
                  v++;
              }
              result = Integer.max(result, v);
          }
          s[index] = false;
          return dp[index] = result;
      }
  
  
      static int dist(int from, int to, int n) {
          return Integer.min((from - to + n) % n, (to - from + n) % n);
      }
  
  
      static int find(int index, int[] u) {
          if (u[index] != index) {
              return u[index] = find(u[index], u);
          }
          return index;
      }
  
      static int abs(int a) {
          return a < 0 ? -a : a;
      }
  
  
      public static int[] KMP(String val) {
          int i = 0;
          int j = -1;
          int[] result = new int[val.length() + 1];
          result[0] = -1;
          while (i < val.length()) {
              while (j >= 0 && val.charAt(j) != val.charAt(i)) {
                  j = result[j];
              }
              j++;
              i++;
              result[i] = j;
          }
          return result;
  
      }
  
      public static boolean nextPer(int[] data) {
          int i = data.length - 1;
          while (i > 0 && data[i] < data[i - 1]) {
              i--;
          }
          if (i == 0) {
              return false;
          }
          int j = data.length - 1;
          while (data[j] < data[i - 1]) {
              j--;
          }
          int temp = data[i - 1];
          data[i - 1] = data[j];
          data[j] = temp;
          Arrays.sort(data, i, data.length);
          return true;
      }
  
      public static int digit(long n) {
          int result = 0;
          while (n > 0) {
              n /= 10;
              result++;
          }
          return result;
      }
  
      public static double dist(long a, long b, long x, long y) {
          double val = (b - a) * (b - a) + (x - y) * (x - y);
          val = Math.sqrt(val);
          double other = x * x + a * a;
          other = Math.sqrt(other);
          return val + other;
  
      }
  
      public static class Point {
  
          int x;
          int y;
  
          public Point(int start, int end) {
              this.x = start;
              this.y = end;
          }
  
          public String toString() {
              return x + " " + y;
          }
  
      }
  
      public static class FT {
  
          long[] data;
  
          FT(int n) {
              data = new long[n];
          }
  
          public void update(int index, long value) {
              while (index < data.length) {
                  data[index] += value;
                  index += (index & (-index));
              }
          }
  
          public long get(int index) {
  
              long result = 0;
              while (index > 0) {
                  result += data[index];
                  index -= (index & (-index));
              }
              return result;
  
          }
      }
  
      public static long gcd(long a, long b) {
          if (b == 0) {
              return a;
          }
          return gcd(b, a % b);
      }
  
      public static long pow(long a, int b) {
          if (b == 0) {
              return 1;
          }
          if (b == 1) {
              return a;
          }
  
          long val = pow(a, b / 2);
          if (b % 2 == 0) {
              return (val * val) % MOD;
          } else {
              return (val * ((val * a) % MOD)) % MOD;
  
          }
      }
  
      static class Scanner {
  
          BufferedReader br;
          StringTokenizer st;
  
          public Scanner() throws FileNotFoundException {
              // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
              br = new BufferedReader(new InputStreamReader(System.in));
              //br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
          }
  
          public String next() {
  
              while (st == null || !st.hasMoreTokens()) {
                  try {
                      st = new StringTokenizer(br.readLine());
                  } catch (Exception e) {
                      throw new RuntimeException();
                  }
              }
              return st.nextToken();
          }
  
          public long nextLong() {
              return Long.parseLong(next());
          }
  
          public int nextInt() {
              return Integer.parseInt(next());
          }
  
          public double nextDouble() {
              return Double.parseDouble(next());
          }
  
          public String nextLine() {
              st = null;
              try {
                  return br.readLine();
              } catch (Exception e) {
                  throw new RuntimeException();
              }
          }
  
          public boolean endLine() {
              try {
                  String next = br.readLine();
                  while (next != null && next.trim().isEmpty()) {
                      next = br.readLine();
                  }
                  if (next == null) {
                      return true;
                  }
                  st = new StringTokenizer(next);
                  return st.hasMoreTokens();
              } catch (Exception e) {
                  throw new RuntimeException();
              }
          }
      }
  }