output_description: For each vertex $u$, print $id$ and $d$ in a line. $id$ is ID of vertex $u$
  and $d$ is the distance from vertex $1$ to vertex $u$. If there are no path
  from vertex $1$ to vertex $u$, print -1 as the shortest distance. Print in
  order of IDs.
input_description: In the first line, an integer $n$ denoting the number of vertices, is given.
  In the next $n$ lines, adjacent lists of vertex $u$ are given in the following
  format:
  
  $u$ $k$ $v_1$ $v_2$ ... $v_k$
  
  $u$ is ID of the vertex and $k$ denotes its degree.$v_i$ are IDs of vertices
  adjacent to $u$.
attempt: import sys
  import queue
  
  readline = sys.stdin.readline
  write = sys.stdout.write
  
  n = int(input())
  G = [None for i in range(n + 1)]
  dist = [
      -1,
  ] * (n + 1)
  for i in range(n):
      node_id, k, *adj = map(int, readline().split())
      # adj.sort()
      G[node_id] = adj
  
  
  def bfs(G):
      # q1が空でなければcntをインクリメントして下のブロックの処理を行う
      # q1から要素取り出し
      # →既に見た節点ならばそれに対しては処理を行わずq1から要素を取り出す(節点1⇔節点2の時の無限ループ回避)
      # →最短距離を格納した後,その要素の子をq2に入れる
      # →q1から要素が無くなったらq2の要素を全てq1に入れる
      cnt = -1
  
      q1 = queue.Queue()
      q2 = queue.Queue()
      q1.put(1)
      while not q1.empty():
          cnt += 1
          while not q1.empty():
              val = q1.get()
              if dist[val] != -1:
                  continue
              dist[val] = cnt
              listlen = len(G[val])
              for i in range(listlen):
                  q2.put(G[val][i])
          while not q2.empty():
              q1.put(q2.get())
  
  
  bfs(G)
  for i in range(1, n + 1):
      write(str(i) + " " + str(dist[i]) + "\n")
  
problem_description: Breadth First Search
  
  Write a program which reads an directed graph $G = (V, E)$, and finds the
  shortest distance from vertex $1$ to each vertex (the number of edges in the
  shortest path). Vertices are identified by IDs $1, 2, ... n$.
samples: [{"input": "4\n    1 2 2 4\n    2 1 4\n    3 0\n    4 1 3", "output": "1 0\n    2 1\n    3 2\n    4 1"}]