# coding: utf-8
require "pry"
topnum = gets.to_i
arr = []
for topnum in 0..(topnum-1)
  inputNum = gets.chomp.split(' ').map(&:to_i)
  inputNum.delete_at(1)
  arrIndex = inputNum.shift
  arr[arrIndex-1] = inputNum
end

$res = []
$cnt = 1;
$arrIndex = 0
$res[0] = 0
def bread_first_search(arr)

  if arr.length  == $arrIndex
    $res.length.times{ |i| 
      $res[i]  = [i+1,$res[i]]
    }
    $res.each{ |i| 
      print i[0]
      print ' '
      puts i[1]
    }
    return
  end

  tops = arr[$arrIndex]

  tops.each { |value|
    if $res[value - 1] == nil
      $res[value - 1] = $cnt;
    end
  }

  $cnt = $res.compact.max+1
  $arrIndex +=1
  bread_first_search(arr) 
end

bread_first_search(arr)