#include <bits/stdc++.h>

using namespace std;

#define int long long
#define FR first
#define SC second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, f, n) for(int i = (int)(f); i < (int)(n); i++)
#define each(a, b) for(auto& a : b)

typedef pair<int, int> P;

const int inf = 1LL << 55;

queue<int> record;

vector<int> graph[101];
int doors[101];
int dist[101];
int d2i[101]; // ??¢?´¢???(????????????|??????????????????)??¨?????¨?????????????????°???????????????????????????????????§????????¬?????§?????¢?????????

void dfs(int& l_vis, int par)
{
  int now = l_vis;
  dist[now] = (~par ? dist[par] + 1 : 0LL);
  d2i[dist[now]] = now;
  
  while(doors[now] && record.size()) {
    //assert(record.size());
    int r = record.front(); record.pop();
    
    if(r > 0) {
      doors[now]--; l_vis++;
      graph[now].push_back(l_vis);
      graph[l_vis].push_back(now);
      doors[l_vis] = r - 1;
      dfs(l_vis, now);
    } else if(r < 0) {
      int d = r + dist[now];
      doors[now]--, doors[d2i[d]]--;
      graph[now].push_back(d2i[d]);
      graph[d2i[d]].push_back(now);
    }
    
  }
}

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int N; cin >> N;
  while(N--) {
    for(auto& g : graph) g.clear();
    memset(doors, 0, sizeof(doors));
    memset(dist, -1, sizeof(dist));
    memset(d2i, -1, sizeof(d2i));
    
    int memo;
    while(cin >> memo, memo) record.push(memo);
    doors[0] = record.front(); record.pop();

    int l_vis = 0;
    dfs(l_vis, -1);

    rep(i, l_vis+1) {
      cout << i+1;
      sort(all(graph[i]));
      rep(j, graph[i].size()) cout << " " << graph[i][j]+1;
      cout << endl;
    }
  }
  
  return 0;
}