#include<bits/stdc++.h>
using namespace std;

int main(){
  int l;
  cin >> l;
  while(l--){
    vector<int> seq;
    int n=0;
    {
      int tmp;
      while(cin >> tmp, tmp){
	seq.push_back(tmp);
	if(tmp>0)n++;
      }
    }
    
    vector<int> deg(n,0);
    vector< vector<int> > adj(n);

    int id = 0;
    vector<int> stack;
    stack.push_back(0); deg[0] = seq[0];

    for(int i=1;i<(int)seq.size();i++){
      if(seq[i]>0){
	id++;

	adj[stack.back()].push_back(id);
	adj[id].push_back(stack.back());
	deg[id] = seq[i]-1;
	deg[stack.back()]--;

	stack.push_back(id);
      }else{
	int nxt = stack[stack.size()-1+seq[i]];

	adj[stack.back()].push_back(nxt);
	adj[nxt].push_back(stack.back());
	deg[nxt]--;
	deg[stack.back()]--;
      }
      while(stack.size() && deg[stack.back()]==0)stack.pop_back();
    }

    for(int i=0;i<n;i++){
      cout << i+1;
      sort(adj[i].begin(), adj[i].end());
      for(int u : adj[i])cout << " " << u+1;
      cout << endl;
    }
  }
}