//problem D - stacking cubes

#include <iostream>

using namespace std;

void getarray(int, int[64][64]);
void Doleft(int[64][64],int[64][64]);

int main()
{
	int n, A[64][64],B[64][64];

	cin>>n;
	while(n>0)
	{
		getarray(n,A);
		Doleft(A,B);
		cout<<endl;
		Doleft(B,A);      //rotate left again = rotate right
		cin>>n;
      if (n != 0)
			cout<<endl<<endl;

	}
	return 0;
}

void getarray(int n, int A[64][64])
{
	int r,c,x;

	//1st clear A
	for(r=0;r<21;r++)
		for(c=0;c<21;c++)
			A[r][c]=0;

	for(r=0;r<n;r++)
	{
		c=0;
		cin>>x;
		while(x!=0)
		{
			A[r][c]=x;
			c++;
			cin>>x;
		}
	}
}

//left rotation
void Doleft(int A[64][64], int B[64][64])
{
	int L[64][64], r, c, count,row=0,col=0;

	//L <- A, clear B
	for(r=0;r<21;r++)
		for(c=0;c<21;c++)
		{
			B[r][c]=0;
			L[r][c]=A[r][c];
		}

		while(L[0][0]>0)
		{
		    for(r=0;L[r][0]>0;r++)
			{   //see how long row r is
         	if (r > 0)
            	cout << ' ';
				count=0;        
				for(c=0;L[r][c]>0;c++)
				{
					count++;
					L[r][c]--;    //shorten for next pass
				}
				cout<<count;
				B[row][col]=count;
				col++;
			}
			cout<<endl;
			row++; col=0;  //start a new row for B
		}
}
