#include<iostream>
#include<cmath>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cassert>
#include<climits>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define IINF (INT_MAX)
#define MAX 110

using namespace std;

vector<int> G[MAX];
int remain[MAX];
int depth[MAX];

int main(){
  int T;
  cin >> T;
  while(T--){

    for(int i=0;i<MAX;i++){
      G[i].clear();
      remain[i] = 0;
      depth[i] = -1;
    }
    vector<int> record;
    vector<int> input;

    int r,cur,upper;

    while(cin >> r,r){
      input.push_back(r);
    }

    remain[0] = input[0];
    cur = 0, upper = 1, depth[0] = 0;

    for(int i=1;i<input.size();i++){
      //cout << "the input => " << input[i] << endl;
      //cout << "i = " << i << " cur = " << cur << endl;
      if( input[i] > 0 ){

	G[cur].push_back(upper);
	G[upper].push_back(cur);
	depth[upper] = depth[cur] + 1;
	remain[upper] = input[i] - 1;
	assert(remain[upper] >= 0);
	remain[cur]--;
	assert(remain[cur] >= 0);
	cur = upper++;

      } else {
	assert(depth[cur] != -1);
	int next = depth[cur] + input[i];
	for(int j=record.size()-1;j>=0;j--){
	  if(next == depth[record[j]]){
	    next = record[j];
	    break;
	  }
	}
	assert(next >= 0);
	G[next].push_back(cur);
	G[cur].push_back(next);
	remain[next]--;
	assert(remain[next] >= 0);
	remain[cur]--;
	assert(remain[cur] >= 0);

      }

      record.push_back(cur);

      for(int j=record.size()-1;j>=0;j--){
	if(remain[cur] != 0){
	  break;
	}
	cur = record[j];
      }

      //cout << "i = " << i << " become " << cur << endl;
      /*
      cout << "graph---" << endl;
      for(int j=0;j<upper;j++){
	cout << j+1;
	for(int k=0;k<G[j].size();k++){
	  cout << " " << G[j][k]+1;
	}
	cout << endl;
      }
      cout << endl;
      */
    }

    for(int i=0;i<upper;i++){
      cout << i+1;
      sort(G[i].begin(),G[i].end());
      for(int j=0;j<G[i].size();j++){
	cout << " " <<  G[i][j]+1;
      }
      cout << endl;
    }


  }
  return 0;
}