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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

vector<int> a;
vector<int> ord;
int sz;
vector<int> rest;
int n,vidx=0;
vector<int> g[100];

void dfs(int& i,int pre){
  if(a[i]>0){
    int crtv=vidx++;
    ord.push_back(crtv);
    sz++;
    if(pre!=-1){
      g[pre].push_back(crtv);
      g[crtv].push_back(pre);
    }
    rest[crtv]=a[i]-(pre==-1?0:1);
    while(rest[crtv]){
      dfs(++i,crtv);
      rest[crtv]--;
    }
    ord.pop_back();
    sz--;
  }else{
    int crtv=pre;
    int backv=ord[sz-1+a[i]];
    rest[backv]--;
    g[crtv].push_back(backv);
    g[backv].push_back(crtv);
  }
}

void solve(){
  a.clear();
  n=0;
  while(1){
    int aa;
    cin>>aa;
    if(aa==0)break;
    if(aa>0)n++;
    a.push_back(aa);
  }
  vidx=0;
  ord.clear();
  sz=0;
  rest.clear();
  rest.resize(n);
  rep(i,n)g[i].clear();
  int hoge=0;
  dfs(hoge,-1);
  rep(i,n){
    sort(all(g[i]));
    cout<<i+1<<" "<<g[i][0]+1;
    repl(j,1,g[i].size())cout<<" "<<g[i][j]+1;
    cout<<endl;
  }
}

int main(){
  int cases;
  cin>>cases;
  while(cases--){
    solve();
  }
  return 0;
}

