#include <iostream>
#include <vector>
#include <set>

using namespace std;

struct node {
  int id, depth, children = 0;
  node* parent;
};

multiset<int> path[110];
int cur = 0, n;

void dfs_ninja(node x) {
  int info;
  cin >> info;
  if(info > 0) {
    while((x.id == 1 && x.children < info) || (x.id != 1 && x.children < info-1)) {
      cur++;
      node child;
      child.id = cur; child.depth = x.depth + 1;
      child.parent = &x;
      x.children++;
      path[x.id].insert(child.id);
      path[child.id].insert(x.id);
      dfs_ninja(child);
    }
    return;
  }
  else if(info < 0) {
    node* ancestor = x.parent;
    for(int i = 0; i < -info; i++) ancestor = ancestor->parent;
    path[x.parent->id].erase(x.id);
    path[x.id].erase(x.parent->id);
    path[x.parent->id].insert(ancestor->id);
    path[ancestor->id].insert(x.parent->id);
    ancestor->children++;
    cur--;
    return;
  }
  else return;
}

int main() {
  cin >> n;
  for(int roop = 0; roop < n; roop++) {
    node root;
    cur++;
    root.id = cur; root.depth = 0;
    root.parent = nullptr;
    dfs_ninja(root);
    int fin;
    cin >> fin;
    for(int i = 0; i < cur; i++) {
      cout << i+1;
      for(auto itr = path[i+1].begin(); itr != path[i+1].end(); itr++) {
	cout << " " << *itr;
      }
      cout << endl;
      path[i+1].clear();
    }
    cur = 0;
  }
  return 0;
}