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: from __future__ import print_function
  from math import *
  from collections import deque
  import os
  import sys
  from io import BytesIO, IOBase
  #import time
   
  def main():
      pass
   
  # region fastio
   
  BUFSIZE = 8192
   
   
  class FastIO(IOBase):
      newlines = 0
   
      def __init__(self, file):
          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
   
      def read(self):
          while True:
              b = os.read(self._fd, max(os.fstat(self._fd).st_size, 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 = os.read(self._fd, max(os.fstat(self._fd).st_size, 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:
              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)
  #sys.stdin, sys.stdout =open("test.txt","r"),open("result.txt","w")
  #ini=time.time()
  input = lambda: sys.stdin.readline().rstrip("\r\n")
  mod=10**9+7
  ############ ---- Input Functions ---- ############
  def inp():
      return(int(input()))
  def inlt():
      return(list(map(int,input().split())))
  def insr():
      s = input()
      return(list(s[:len(s) ]))
  def invr():
      return(map(int,input().split()))
  from collections import defaultdict
   
  class Graph():
      def __init__(self,vertices):
          self.graph = defaultdict(list)
          self.V = vertices
   
      def addEdge(self,u,v):
          self.graph[u].append(v)
   
      def isCyclicUtil(self, v, visited, recStack):
   
          # Mark current node as visited and
          # adds to recursion stack
          visited[v] = True
          recStack[v] = True
   
          # Recur for all neighbours
          # if any neighbour is visited and in
          # recStack then graph is cyclic
          for neighbour in self.graph[v]:
              if visited[neighbour] == False:
                  if self.isCyclicUtil(neighbour, visited, recStack) == True:
                      return True
              elif recStack[neighbour] == True:
                  return True
   
          # The node needs to be poped from
          # recursion stack before function ends
          recStack[v] = False
          return False
   
      # Returns true if graph is cyclic else false
      def isCyclic(self):
          visited = [False] * (self.V + 1)
          recStack = [False] * (self.V + 1)
          for node in range(self.V):
              if visited[node] == False:
                  if self.isCyclicUtil(node,visited,recStack) == True:
                      return True
          return False
      def topologicalSortUtil(self, v, visited, stack):
       
          # Mark the current node as visited.
          visited[v] = True
          # Recur for all the vertices adjacent to this vertex
          for i in self.graph[v]:
              if visited[i] == False:
                  self.topologicalSortUtil(i, visited, stack)
   
          # Push current vertex to stack which stores result
          stack.append(v)
   
      # The function to do Topological Sort. It uses recursive
      # topologicalSortUtil()
      def topologicalSort(self):
          # Mark all the vertices as not visited
          visited = [False]*self.V
          stack = []
   
          # Call the recursive helper function to store Topological
          # Sort starting from all vertices one by one
          for i in range(self.V):
              if visited[i] == False:
                  self.topologicalSortUtil(i, visited, stack)
   
          # Print contents of the stack
          stack.reverse()
          return(stack)  # return list in reverse order
  for _ in range(inp()):
      n=inp()
      l=[]
      for i in range(n):
          l.append(inlt())
      g=Graph(n)
      for i in range(n):
          for j in range(1,l[i][0]+1):
              g.addEdge(l[i][j]-1,i)
      #print(g.graph)
      if g.isCyclic():
          print(-1)
          continue
      v=[0]*n
      c=1
      #l.sort(key=lambda x:(len(x),x[0]))
      h=g.topologicalSort()
      for i in range(n-1):
          if h[i+1]<h[i]:
              c+=1
      #print(h,l)
      for i in h:
          if l[i][0]==0:
              v[i]=1
              continue
          #print(i)
          for j in range(1,l[i][0]+1):
              #print(j,"a")
              if i+1>l[i][j]:
                  v[i]=max(v[l[i][j]-1],v[i])
              else:
                  v[i]=max(v[l[i][j]-1]+1,v[i])
      print(max(v))