#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
struct treeNode{
int data;
treeNode* lchild;
treeNode* rchild;
};
treeNode* buildTree(int inl, int inr, int postl, int postr, int* inorder, int* postorder){
if(postl > postr)
return NULL;
treeNode* root = new treeNode;
root->data = postorder[postr];
int rootIndex;
for(int i = inl; i <= inr; i++){
if(inorder[i] == root->data){
rootIndex = i;
break;
}
}
root->lchild = buildTree(inl, rootIndex - 1, postl, postl + rootIndex - inl - 1, inorder, postorder);
root->rchild = buildTree(rootIndex + 1, inr, postl + rootIndex - inl, postr - 1, inorder, postorder);
return root;
}
void BFS(treeNode* root, int N){
queue<treeNode*> q;
q.push(root);
int count = 0;
while (q.size() != 0) {
treeNode* node = q.front();
q.pop();
printf("%d", node->data);
count++;
if(count < N)
printf(" ");
if(node->lchild)
q.push(node->lchild);
if(node->rchild)
q.push(node->rchild);
}
}
int main(){
int N, i;
scanf("%d", &N);
int inorder[N], postorder[N];
for(i = 0; i < N; i++){
scanf("%d", &postorder[i]);
}
for(i = 0; i < N; i++){
scanf("%d", &inorder[i]);
}
treeNode* root = buildTree(0, N - 1, 0, N - 1, inorder, postorder);
BFS(root, N);
return 0;
}