#include <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int n;
vector<int> m;
vector<vector<int> > to;
vector<int> node;

void dfs()
{
    int k = n;
    node.push_back(k);
    ++ n;

    for(int i=0; i<m[k]; ++i){
        int a;
        cin >> a;
        if(a > 0){
            m.push_back(a-1);
            to[k].push_back(n);
            to.push_back(vector<int>(1, k));
            dfs();
        }else{
            int x = node[node.size()-1+a];
            to[k].push_back(x);
            to[x].push_back(k);
            -- m[x];
        }
    }

    node.pop_back();
}

int main()
{
    int d;
    cin >> d;

    while(--d >= 0){
        int a;
        cin >> a;

        n = 0;
        m = vector<int>(1, a);
        to = vector<vector<int> >(1);
        dfs();

        for(int i=0; i<n; ++i){
            cout << (i+1);
            sort(to[i].begin(), to[i].end());
            for(unsigned j=0; j<to[i].size(); ++j)
                cout << ' ' << (to[i][j] + 1);
            cout << endl;
        }

        cin >> a;
    }
}