### classes

class Room
  attr_accessor :id, :dist, :n, :k, :nbrs

  def initialize(dist, n)
    @id = $newid
    $newid += 1
    @dist = dist
    @n = n
    @k = 0
    @nbrs = []
  end

  def connect(room)
    @nbrs << room.id
    @k += 1
    room.nbrs << @id
    room.k += 1

    if @k > @n || room.k > room.n
      puts 'error'
      exit
    end
  end

  def full?
    @n == @k
  end
end

### main

n = gets.to_i

n.times do
  rcds = []
  loop do
    line = gets.split.map(&:to_i)
    rcds += line
    break if line.last == 0
  end

  $newid = 0

  rooms = []
  drooms = []
  dist = -1
  
  for rcd in rcds
    #p [rcd, dist]
    break if rcd == 0

    if rcd > 0
      dist += 1
      room = Room.new(dist, rcd)
      #p ["new", room]
      room.connect(drooms[dist - 1]) if dist > 0
      rooms[room.id] = room
      drooms[dist] = room
    else
      room = drooms[dist]
      #p ["coonect", room, drooms[dist + rcd]]
      room.connect(drooms[dist + rcd])
    end

    while dist >= 0 && drooms[dist].full?
      dist -= 1
    end
    #p rooms
    #p drooms
  end

  rooms.each_with_index do |room, i|
    puts ([i + 1] + room.nbrs.sort.map{|id| id + 1}).join(' ')
  end
end