#pragma GCC optimize(3,"Ofast","inline")
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
#define int long long
#define ld long double
int n;
int hou[40],zhong[40];
vector<int>s[40];
int tk=0;
//前序此时区间l,r
//中序此时区间z,y;
//此时第几层K
void build (int u,int l,int r,int z,int y,int k){
tk=max(tk,k);//二叉树最大层
s[k].push_back(hou[r]);//该层从左到右节点
int id;
for(int i=y;i>=z;i--){
if(zhong[i]==hou[r]) {
id=i;break;
}
}
int ylen=y-id;
int zlen=id-z;
if(zlen!=0) {
build(u,l,l+zlen-1,z,z+zlen-1,k+1);
}
if(ylen!=0) {
build(u,r-ylen,r-1,y-ylen+1,y,k+1);
}
}
void solve()
{ cin>>n;
for(int i=1;i<=n;i++) cin>>zhong[i];
for(int i=1;i<=n;i++) cin>>hou[i];
build(1,1,n,1,n,1);
vector<int>z,y;
for(int i=1;i<=tk;i++){
z.push_back(s[i].back());
}
for(int i=1;i<=tk;i++){
y.push_back(s[i][0]);
}
cout<<"R: ";
for(auto k : z){
if(k!=z[0]) cout<<" ";
cout<<k;
}
cout<<endl;
cout<<"L: ";
for(auto k : y){
if(k!=y[0]) cout<<" ";
cout<<k;
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
solve();
return 0;
}