#include <bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin >> n;
  for (int i = 0; i < n; i++){
    vector<int> a;
    while (1){
      int x;
      cin >> x;
      if (x == 0){
        break;
      }
      a.push_back(x);
    }
    int m = a.size();
    int V = 0;
    vector<vector<int>> E;
    vector<pair<int, int>> R;
    for (int j = 0; j < m; j++){
      if (a[j] > 0){
        R.push_back(make_pair(V, a[j]));
        E.push_back(vector<int>(0));
        V++;
        if (R.size() > 1){
          int v = R[R.size() - 2].first;
          int w = R[R.size() - 1].first;
          E[v].push_back(w);
          E[w].push_back(v);
          R[R.size() - 2].second--;
          R[R.size() - 1].second--;
        }
      } else {
        int v = R[R.size() - 1].first;
        int w = R[R.size() - 1 + a[j]].first;
        E[v].push_back(w);
        E[w].push_back(v);
        R[R.size() - 1].second--;
        R[R.size() - 1 + a[j]].second--;
      }
      while (R.back().second == 0){
        R.pop_back();
        if (R.empty()){
          break;
        }
      }
    }
    for (int j = 0; j < V; j++){
      sort(E[j].begin(), E[j].end());
      cout << j + 1;
      for (int k : E[j]){
        cout << ' ' << k + 1;
      }
      cout << endl;
    }
  }
}
