#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reg(i,a,b) for(int i=(a);i<=(b);i++)
#define irep(i,n) for(int i=((int)(n))-1;i>=0;i--)
#define ireg(i,a,b) for(int i=(b);i>=(a);i--)
typedef long long int lli;
typedef pair<int,int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX


int main(void){
	int m;
	scanf("%d",&m);
	rep(i,m){
		vector<int> ans[105];
		vector<mp> sta;
		int n=0;
		sta.push_back(mp(10000,-1));
		while(1){
			/*
			rep(i,sta.size()){
				printf("(%d %d)\n",sta[i].sec+1,sta[i].fir);
			}
			printf("\n");*/
			int a;
			scanf("%d",&a);
			if(a==0)break;
			
			while(sta.back().fir==0)sta.pop_back();
			sta.back().fir--;
			if(a>0){
				int no = n;
				int to = sta.back().sec;
				if(to!=-1){
					ans[to].push_back(no);
					ans[no].push_back(to);
				}
				else a++;
				sta.push_back(mp(a-1,no));
				n++;
			}
			else{
				int no = sta.back().sec;
				int to = sta[sta.size()-1 + a].sec;
				sta[sta.size()-1+a].fir--;
				ans[to].push_back(no);
				ans[no].push_back(to);
			}
		}
		
		rep(i,n){
			sort(ans[i].begin(),ans[i].end());
			printf("%d ",i+1);
			rep(j,ans[i].size()){
				printf("%d%c",ans[i][j]+1,j+1==ans[i].size()?'\n':' ');
			}
		}
	}
		
	return 0;
}