output_description: For each query, print the LCA of u and v in a line.
input_description: n
      k0 c1 c2 ... ck0
      k1 c1 c2 ... ck1
      :
      kn-1 c1 c2 ... ckn-1
      q
      u1 v1
      u2 v2
      :
      uq vq
      
  
  The first line of the input includes an integer n, the number of nodes of the
  tree.
  
  In the next n lines, the information of node i is given. ki is the number of
  children of node i, and c1, ... cki are node IDs of 1st, ... kth child of node
  i.
  
  In the next line, the number of queryies q is given. In the next q lines,
  pairs of u and v are given as the queries.
attempt: # 参考　https://tjkendev.github.io/procon-library/python/graph/lca-segment-tree.html
  
  # N: 頂点数
  # G[v]: 頂点vの子頂点 (親頂点は含まない)
  
  import sys
  
  sys.setrecursionlimit(10**7)
  
  N = int(input())
  G = [[] for _ in range(N)]
  for v in range(N):
      for j, nv in enumerate(map(int, input().split())):
          if j == 0:
              continue
          G[v].append(nv)
  
  # Euler Tour の構築
  S = []
  F = [0] * N
  depth = [0] * N
  
  
  def dfs(v, d):
      F[v] = len(S)
      depth[v] = d
      S.append(v)
      for w in G[v]:
          dfs(w, d + 1)
          S.append(v)
  
  
  dfs(0, 0)
  
  # 存在しない範囲は深さが他よりも大きくなるようにする
  INF = (N, None)
  
  # LCAを計算するクエリの前計算
  M = 2 * N
  M0 = 2 ** (M - 1).bit_length()
  data = [INF] * (2 * M0)
  for i, v in enumerate(S):
      data[M0 - 1 + i] = (depth[v], i)
  for i in range(M0 - 2, -1, -1):
      data[i] = min(data[2 * i + 1], data[2 * i + 2])
  
  
  # LCAの計算 (generatorで最小値を求める)
  def _query(a, b):
      yield INF
      a += M0
      b += M0
      while a < b:
          if b & 1:
              b -= 1
              yield data[b - 1]
          if a & 1:
              yield data[a - 1]
              a += 1
          a >>= 1
          b >>= 1
  
  
  # LCAの計算 (外から呼び出す関数)
  def query(u, v):
      fu = F[u]
      fv = F[v]
      if fu > fv:
          fu, fv = fv, fu
      return S[min(_query(fu, fv + 1))[1]]
  
  
  ans_list = []
  
  q = int(input())
  for _ in range(q):
      u, v = map(int, input().split())
      ans = query(u, v)
      ans_list.append(ans)
  
  print(*ans_list, sep="\n")
  
problem_description: LCA: Lowest Common Ancestor
  
  For a rooted tree, find the lowest common ancestor of two nodes u and v.
  
  The given tree consists of n nodes and every node has a unique ID from 0 to
  n-1 where 0 is the root.
samples: [{"input": "8\n    3 1 2 3\n    2 4 5\n    0\n    0\n    0\n    2 6 7\n    0\n    0\n    4\n    4 6\n    4 7\n    4 3\n    5 2", "output": "1\n    1\n    0\n    0"}]