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 sys
  import math
  import heapq
  import bisect
  from collections import Counter
  from collections import defaultdict
  from io import BytesIO, IOBase
  from itertools import permutations
  import string
  from types import GeneratorType
  
  class FastIO(IOBase):
      newlines = 0
  
      def __init__(self, file):
          import os
          self.os = os
          self._fd = file.fileno()
          self.buffer = BytesIO()
          self.writable = "x" in file.mode or "r" not in file.mode
          self.write = self.buffer.write if self.writable else None
          self.BUFSIZE = 8192
  
      def read(self):
          while True:
              b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
              if not b:
                  break
              ptr = self.buffer.tell()
              self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
          self.newlines = 0
          return self.buffer.read()
  
      def readline(self):
          while self.newlines == 0:
              b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
              self.newlines = b.count(b"\n") + (not b)
              ptr = self.buffer.tell()
              self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
          self.newlines -= 1
          return self.buffer.readline()
  
      def flush(self):
          if self.writable:
              self.os.write(self._fd, self.buffer.getvalue())
              self.buffer.truncate(0), self.buffer.seek(0)
  
  
  class IOWrapper(IOBase):
      def __init__(self, file):
          self.buffer = FastIO(file)
          self.flush = self.buffer.flush
          self.writable = self.buffer.writable
          self.write = lambda s: self.buffer.write(s.encode("ascii"))
          self.read = lambda: self.buffer.read().decode("ascii")
          self.readline = lambda: self.buffer.readline().decode("ascii")
  
  
  sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
  input = lambda: sys.stdin.readline().rstrip("\r\n")
  
  
  def get_int():
      return int(input())
  
  
  def get_ints():
      return list(map(int, input().split(' ')))
  
  
  def get_int_grid(n):
      return [get_ints() for _ in range(n)]
  
  
  def get_str():
      return input().strip()
  
  
  def get_strs():
      return get_str().split(' ')
  
  
  def flat_list(arr):
      return [item for subarr in arr for item in subarr]
  
  
  def yes_no(b):
      if b:
          return "YES"
      else:
          return "NO"
  
  
  def binary_search(good, left, right, delta=1, right_true=False):
      """
      Performs binary search
      ----------
      Parameters
      ----------
      :param good: Function used to perform the binary search
      :param left: Starting value of left limit
      :param right: Starting value of the right limit
      :param delta: Margin of error, defaults value of 1 for integer binary search
      :param right_true: Boolean, for whether the right limit is the true invariant
      :return: Returns the most extremal value interval [left, right] which is good function evaluates to True,
              alternatively returns False if no such value found
      """
  
      limits = [left, right]
      while limits[1] - limits[0] > delta:
          if delta == 1:
              mid = sum(limits) // 2
          else:
              mid = sum(limits) / 2
          if good(mid):
              limits[int(right_true)] = mid
          else:
              limits[int(~right_true)] = mid
      if good(limits[int(right_true)]):
          return limits[int(right_true)]
      else:
          return False
  
  
  def prefix_sums(a):
      p = [0]
      for x in a:
          p.append(p[-1] + x)
      return p
  
  
  def solve_a():
      n = get_int()
      s = get_str()
      cnt = 0
      for i, x in enumerate(s):
  
          if int(s[i]) > 0:
              cnt += int(i < n - 1) + int(s[i])
      return cnt
  
  
  def solve_b():
      n = get_int()
      a = get_ints()
      b = get_ints()
  
      pma = [a[0]]
      pmb = [b[0]]
      for i in range(1, n):
          pma.append(min(pma[-1], a[i]))
          pmb.append(min(pmb[-1], b[i]))
  
      l = -1
      r = 2 * n
  
      while r - l > 1:
          m = (l + r) // 2
  
          f = False
          for i in range(m + 1):
              if pma[i] < pmb[m - i]:
                  r = m
                  f = True
                  break
          if not f:
              l = m
  
      return r
  
  
  
  def bootstrap(f, stack=[]):
      def wrappedfunc(*args, **kwargs):
          if stack:
              return f(*args, **kwargs)
          else:
              to = f(*args, **kwargs)
              while True:
                  if type(to) is GeneratorType:
                      stack.append(to)
                      to = next(to)
                  else:
                      stack.pop()
                      if not stack:
                          break
                      to = stack[-1].send(to)
              return to
      return wrappedfunc
  
  
  def solve_c():
      n = get_int()
      out = defaultdict(list)
      ins = defaultdict(list)
      for i in range(n):
          k, *a = get_ints()
          for x in a:
              out[x - 1].append(i)
              ins[i].append(x - 1)
  
      vis = [0] * n
      topsort = []
      topo = [0] * n
      cycle = [False]
      @bootstrap
      def dfs(v):
          vis[v] = True
          for u in out[v]:
              if not vis[u]:
                  yield dfs(u)
              elif not topo[u]:
                  cycle[0] = True
          topsort.append(v)
          topo[v] = 1
          yield
  
      for i in range(n):
          if not vis[i]:
              dfs(i)
  
      if cycle[0]:
          return -1
  
      dist = [1] * n
      topsort = topsort[::-1]
      for v in topsort:
          for u in out[v]:
              dist[u] = max(dist[u], dist[v] + int(u < v))
  
  
      return max(dist)
  
  
  
  
  
  
      topsort = topsort[::-1]
      print(topsort)
  
  
  
  
  
  
  t = get_int()
  for _ in range(t):
      print(solve_c())
  