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

int N;
vector<int> G[200];
int Gs[200];
int F[200];
vector<int> V;
int cnt;
void solve(int id,int h){
  F[id] = h;
  //  cout << "inid : " << id << " " << h <<  " " << V[cnt] << " F " << F[id] << endl;
  
  for(int i=1;i<=100;i++){
    int d = V[cnt];
    if( d > 0 ){
      if( !Gs[i] ) {
        G[id].push_back( i );
        G[i].push_back( id );
        Gs[i] = d; cnt++;
        solve(i,h+1);
        i = 0;
      }    
    } else {
      //cout << "nex " << d << " i " << i << " = "  << F[i]<<" - " << F[id] <<" = " << F[i]-F[id] << endl;
        if( F[i]!=-1 && F[i] - F[id] == d && Gs[i] > (int)G[i].size() ){
          G[id].push_back(i);
          G[i].push_back(id);
          cnt++;
          i = 0;
        }
    }
    if( (int)G[id].size() == Gs[id] ) {
      //  cout <<"id: " << id << " " <<  G[id].size() <<", " << Gs[id] << endl;
      break;
    }
    assert(i<100);
  }
}

int main(){
  cin >> N;
  for(int i=0;i<N;i++){
    memset(F,-1,sizeof(F));
    memset(Gs,0,sizeof(Gs));
    V.clear();
    for(int i=0;i<200;i++) G[i].clear();
    int t;
    while(cin>>t && t ) V.push_back(t);
    
    cnt = 0;
    Gs[1]=V[cnt++];
    solve(1,0);
    for(int i=1;i<=100;i++){
      if( G[i].empty() ) continue;
      //      cout << Gs[i] << " " << G[i].size() << endl;
      sort(G[i].begin(),G[i].end());
      cout << i;
      for(int j=0;j<(int)G[i].size();j++) cout << " " << G[i][j];
      cout << endl;
    }
  }
}