place表示位置
#include<stdio.h>
#include<iostream>
#include<vector>
using namespace std;
typedef vector<int>pile;
vector<pile> Pile;
pile temp;
int place[30];
void clear(int a){
int k=place[a];
int tmp=Pile[k][Pile[k].size()-1];
while(tmp!=a){
place[tmp]=tmp;
Pile[k].pop_back();
Pile[tmp].push_back(tmp);
tmp=Pile[k][Pile[k].size()-1];
}
}
void move_out_to_temp(int a,int b){
temp.clear();
int tmp=Pile[place[a]][Pile[place[a]].size()-1];
while(tmp!=a){
place[tmp]=place[b];
Pile[place[a]].pop_back();
temp.push_back(tmp);
tmp=Pile[place[a]][Pile[place[a]].size()-1];
}
Pile[place[a]].pop_back();
temp.push_back(a);
place[a]=place[b];
}
int main(){
int i,n,a,b,j;
char s1[8],s2[8];
scanf("%d",&n);
temp.clear();
Pile.clear();
for(i=0;i<n;i++){
place[i]=i;
temp.push_back(i);
Pile.push_back(temp);
temp.clear();
}
while(scanf("%s",&s1)!=EOF){
if(s1[0]=='q')
break;
scanf("%d%s%d",&a,&s2,&b);
if(place[a]==place[b])
continue;
if(s1[0]=='m' && s2[1]=='n'){
clear(a);
clear(b);
Pile[place[a]].pop_back();
Pile[place[b]].push_back(a);
place[a]=place[b];
}
else if(s1[0]=='m' && s2[1]=='v'){
clear(a);
Pile[place[a]].pop_back();
Pile[place[b]].push_back(a);
place[a]=place[b];
}
else if(s1[0]=='p' && s2[1]=='n'){
clear(b);
move_out_to_temp(a,b);
while(!temp.empty()){
Pile[place[b]].push_back(temp[temp.size()-1]);
temp.pop_back();
}
}
else if(s1[0]=='p' && s2[1]=='v'){
move_out_to_temp(a,b);
while(!temp.empty()){
Pile[place[b]].push_back(temp[temp.size()-1]);
temp.pop_back();
}
}
}
for(i=0;i<n;i++){
printf("%d:",i);
if(!Pile[i].empty()){
for(j=0;j<(Pile[i].size());j++){
printf(" %d",Pile[i][j]);
}
}
printf("\n");
}
return 0;
}