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

int main()
{
	int n;
	cin >> n;
	while (n--) {
		int r;
		vector<int> v;
		while (cin >> r, r) {
			v.push_back(r);
		}
		int ma = -1, it = 0;
		int e[100] = {}, t[100] = {}, par[100] = {};
		vector<int> res[100];
		for (auto ri : v) {
			if (ri > 0) {
				int to = ma + 1;
				par[to] = it;
				if (to) {
					res[it].push_back(to);
					res[to].push_back(it);
					t[it]++;
				}
				t[to] = 1;
				e[to] = ri;
				it = to;
			}
			else {
				int to = it;
				for (int i = 0; i < abs(ri); i++) {
					to = par[to];
				}
				t[it]++;
				t[to]++;
				res[it].push_back(to);
				res[to].push_back(it);
			}
			ma = max(ma, it);
			while (it && e[it] == t[it]) {
				it = par[it];
			}
		}
		int sz = 0;
		while (e[sz]) sz++;
		for (int i = 0; i < sz; i++) {
			sort(res[i].begin(), res[i].end());
			cout << i + 1 << ' ';
			for (int j = 0; j < e[i]; j++) {
				cout << res[i][j] + 1 << " \n"[j + 1 == e[i]];
			}
		}
	}
	return 0;
}