#include <iostream>
#include<vector>
#include<cstring>
#include <algorithm>
using namespace std;
vector<int> list[25];
int n;
int find(int a)
{
int i;
for(i = 0; i < n; i++)
{
vector<int>::iterator result = find( list[i].begin( ), list[i].end( ), a );
if(result != list[i].end())
return i;
}
}
void mvon(int a, int b, int ap, int bp)
{
vector<int>::iterator it;
for(it = list[ap].end()-1;*it!= a; it--)
{
list[*it].push_back(*it);
list[ap].pop_back();
}
for(it = list[bp].end()-1;*it!= b; it--)
{
list[*it].push_back(*it);
list[bp].pop_back();
}
it = list[ap].end()-1;
list[bp].push_back(*it);
list[ap].pop_back();
}
void mvov(int a, int b, int ap, int bp)
{
vector<int>::iterator it;
for(it = list[ap].end()-1;*it!= a; it--)
{
list[*it].push_back(*it);
list[ap].pop_back();
}
it = list[ap].end()-1;
list[bp].push_back(*it);
list[ap].pop_back();
}
void ston(int a, int b, int ap, int bp)
{
vector<int>::iterator it;
for(it = list[bp].end()-1;*it!= b; it--)
{
list[*it].push_back(*it);
list[bp].pop_back();
}
for(it = list[ap].begin();it != list[ap].end();it++)
{
if(*it == a)
break;
}
list[bp].insert(list[bp].end(),it,list[ap].end());
for(it = list[ap].end() -1 ;*it!=a;it-- )
{
list[ap].pop_back();
}
list[ap].pop_back();
}
void stov(int a, int b,int ap, int bp)
{
vector<int>::iterator it;
list[bp].insert(list[bp].end(),it,list[ap].end());
for(it = list[ap].end() -1 ;*it!=a;it-- )
{
list[ap].pop_back();
}
list[ap].pop_back();
}
using namespace std;
int main()
{
int a,b,ap,bp,i;
cin>>n;
for(i = 0; i < n; i++)
list[i].push_back(i);
char op1[10],op2[10];
while(1)
{
cin>>op1;
if(strcmp(op1,"q") == 0)
break;
cin>>a>>op2>>b;
ap = find(a);
bp = find(b);
if(ap == bp)
continue;
if(strcmp(op1,"mv"))
{
if(strcmp(op2,"on") == 0)
{
mvon(a,b,ap,bp);
}
else
{
mvov(a,b,ap,bp);
}
}
else if(strcmp(op1,"st") == 0)
{
if(strcmp(op2,"on") == 0)
{
ston(a,b,ap,bp);
}
else
{
stov(a,b,ap,bp);
}
}
else
list[ap].swap(list[bp]);
}
vector<int>::iterator it;
for(i = 0; i < n; i++)
{
cout<<i<<":";
if(list[i].size()!= 0)
{
for(it = list[i].begin();it != list[i].end(); it ++)
cout<<" "<<*it;
}
cout<<endl;
}
return 0;
}