#include<bits/stdc++.h>
using namespace std;
int i,n,op,k[99],a[99][99],zer[99],y[99],i2;
char z[99];
void xb(int x) {
cout<<z[x];
i=1;
while(i<k[x]) {
xb(a[x][i]);
i++;
}
}
void hb(int x) {
i=1;
while(i<k[x]) {
xb(a[x][i]);
i++;
}
cout<<z[x];
}
void j(int op) {
int emm ;
i=1;
while(i<k[op]) {
emm=a[op][i];
k[emm]++;
a[emm][k[emm]]=a[op][i+1];
j(emm);
i++;
}
}
int main() {
cin>>n;
for(i=1; i<=n; i++) {
cin>>z[i];
while(1) {
cin>>op;
if(op) {
k[i]++;
a[i][k[i]]=op;
} else
break;
}
}
int zx,xc;
j(1);
xb(1);
cout<<endl;
hb(1);
return 0;
}
我这菜鸟。。。