#include <cstdio>
int blocks[25][26];
int n;
void find( const int & val, int & y, int & x )
{
bool loop = true;
for ( int i = 0; i < n && loop; ++i )
for ( int j = 0; blocks[i][j] != -1; ++j )
if ( blocks[i][j] == val )
{
y = i;
x = j;
loop = false;
break;
}
}
void move_onto( const int & a, const int & b )
{
int y1, x1, y2, x2;
find( a, y1, x1 );
find( b, y2, x2 );
if ( y1 == y2 )
return ;
// make sure nothing is on a
for ( int x = x1+1; blocks[y1][x] != -1; ++x )
{
blocks[ blocks[y1][x] ][0] = blocks[y1][x];
blocks[y1][x] = -1;
}
// make sure nothing is on b
for ( int x = x2+1; blocks[y2][x] != -1; ++x )
{
blocks[ blocks[y2][x] ][0] = blocks[y2][x];
blocks[y2][x] = -1;
}
// put a on b
blocks[y2][x2+1] = blocks[y1][x1];
blocks[y2][x2+2] = -1;
blocks[y1][x1] = -1;
}
void move_over( const int & a, const int & b )
{
int y1, x1, y2, x2;
find( a, y1, x1 );
find( b, y2, x2 );
if ( y1 == y2 )
return ;
// make sure nothing is on a
for ( int x = x1+1; blocks[y1][x] != -1; ++x )
{
blocks[ blocks[y1][x] ][0] = blocks[y1][x];
blocks[y1][x] = -1;
}
// find top of stack which contains b
for ( ; blocks[y2][x2] != -1; ++x2 );
blocks[y2][x2] = blocks[y1][x1];
blocks[y2][x2+1] = -1;
blocks[y1][x1] = -1;
}
void pile_onto( const int & a, const int & b )
{
int y1, x1, y2, x2;
find( a, y1, x1 );
find( b, y2, x2 );
if ( y1 == y2 )
return ;
// make sure nothing is on b
for ( int x = x2+1; blocks[y2][x] != -1; ++x )
{
blocks[ blocks[y2][x] ][0] = blocks[y2][x];
blocks[y2][x] = -1;
}
// move a and above
++x2;
for ( ; blocks[y1][x1] != -1; ++x1, ++x2 )
{
blocks[y2][x2] = blocks[y1][x1];
blocks[y1][x1] = -1;
}
blocks[y2][x2] = -1;
}
void pile_over( const int & a, const int & b )
{
int y1, x1, y2, x2;
find( a, y1, x1 );
find( b, y2, x2 );
if ( y1 == y2 )
return ;
// find top of stack which contains b
for ( ; blocks[y2][x2] != -1; ++x2 );
// move a and above
for ( ; blocks[y1][x1] != -1; ++x1, ++x2 )
{
blocks[y2][x2] = blocks[y1][x1];
blocks[y1][x1] = -1;
}
blocks[y2][x2] = -1;
}
void print( )
{
for ( int i = 0; i < n; ++i )
{
printf("%d:", i);
for ( int j = 0; blocks[i][j] != -1; ++j )
printf(" %d", blocks[i][j]);
printf("\n");
}
}
int main()
{
int a, b;
char mopi[5], onov[5];
scanf("%d\n", &n);
for ( int i = 0; i < n; ++i )
{
blocks[i][0] = i;
blocks[i][1] = -1;
}
while ( scanf("%s", mopi) == 1 && mopi[0] != 'q' )
{
scanf("%d %s %d", &a, onov, &b);
if ( mopi[0] == 'm' && onov[3] == 'o' )
move_onto( a, b );
else if ( mopi[0] == 'm' && onov[3] == 'r' )
move_over( a, b );
else if ( mopi[0] == 'p' && onov[3] == 'o' )
pile_onto( a, b );
else if ( mopi[0] == 'p' && onov[3] == 'r' )
pile_over( a, b );
}
print( );
return 0;
}