Map of the Ninja House

Reconstruct the graph of a ninja house from the counter and door records produced by a fixed DFS exploration, handling back edges, skips, and multi-edges.

Hard8GraphDFSImplementationStackNo attempts yetTime limit2sMemory limit512 MB

Problem

An old document says that a ninja house in Kanazawa was in fact a defensive fortress laid out like a maze. Hidden doors connected its rooms in a complicated way, so any invader would get lost. Every room has at least two doors.

A ninja house can be modelled by a graph, as in Figure 1. A circle is a room. A line joining two circles is a door between those two rooms.

Figure 1Figure 2

No map was available, so I decided to draw one. Your job is to reconstruct the map from the record of my exploration.

I started by entering through the single entrance that opens to the outside. The path I walked is drawn in Figure 2 as a line with arrows. The rules for moving between rooms are these.

After entering a room I open the rightmost door and move to the next room. If the room on the other side has already been visited, I close the door without entering and open the next rightmost door instead. When I have inspected every door of a room, I go back through the door I used to enter it. A door that was already opened from the room on its other side is never opened again, so each door is opened exactly once during the whole exploration.

I carry a counter that holds the distance from the first room. The counter goes up by one when I enter a new room and down by one when I go back out of a room. In Figure 2 each number in parentheses is the value of the counter when I entered that room, that is, its distance from the first room. The numbers without parentheses give the order of my visits.

I keep a record as I explore. The first number of the record is the number of doors of the first room, and the entrance from the outside is not counted among them. After that I write one number every time I open a door, by these rules.

  1. If the room on the other side is new, I write the number of doors of that room, which is a positive number.
  2. If the room on the other side is a room RR I have already visited, I write the distance of RR from the first room minus the distance of the current room from the first room, which is a negative number.

Take the example in Figure 2. The first room has three doors leading to other rooms, so I start the record with 3. Moving into the second, third and fourth rooms, which each have three doors, appends 3 3 3. Skipping the entry from the fourth room back into the first room appends the distance difference -3, and so on. When the exploration ends, the record is 3 3 3 3 -3 3 2 -5 3 2 -5 -3.

There are several dozen ninja houses in the city. Given one record per house, print one graph per house.

Input

The first line contains one integer nn, the number of ninja house records. nn is less than 100. The nn records follow. Each record lists the numbers written during one exploration in order, followed by a zero as a terminator. A record takes one or more lines, and each line is shorter than 1000 characters. Numbers are separated by a space or a newline. The number of rooms of one house is less than 100, and the number of doors of one room is less than 40.

Output

For each house of mm rooms, print mm lines. The ii-th of those lines has this form.

i r1 r2 ... rki

Here r1r_1 through rkir_{k_i} are the rooms adjoining room ii, and kik_i is the number of doors of room ii. Numbers are separated by exactly one space. Rooms are numbered from 1 in the order they were visited. Print r1,r2,,rkir_1, r_2, \dots, r_{k_i} in ascending order. Room ii may be joined to another room by more than one door. In that case print that room number as many times as there are doors joining them.