### classes

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

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

  def connect(room)
    @nbrs << room.id
    @k += 1
    room.nbrs << @id
    room.k += 1
  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 = []
  prv = -1
  dist = -1
  
  for rcd in rcds
    #p [rcd, prv, dist]
    break if rcd == 0

    if rcd > 0
      dist += 1
      room = Room.new(dist, prv, rcd)
      room.connect(rooms[prv]) if prv >= 0

      rooms << room
      prv = room.id
    else
      dist0 = dist + rcd
      room0 = room = rooms[prv]

      while room0.dist > dist0
        room0 = rooms[room0.prv]
      end
      room.connect(room0)

      while room.full?
        prv = room.prv
        break if prv < 0
        room = rooms[prv]
        dist -= 1
      end
    end      
    #p rooms
    #p rhash
  end

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