#include <bits/stdc++.h>
using namespace std;
struct TreeNode{
	int val;
	TreeNode* left;
	TreeNode* right;
	
	TreeNode(int val):val(val),left(nullptr),right(nullptr){};
};
int idx = 0;
unordered_map<int,int>mp;
TreeNode* help(vector<int>&pre,int st,int end){

	if(st>end)return nullptr;
	int rootVal = pre[idx++];
	int mid = mp[rootVal];
	
	TreeNode* root = new TreeNode(rootVal);
    root->left = help(pre,st,mid-1);
	root->right = help(pre,mid+1,end);
	return root;
}
TreeNode* bt(vector<int>&pre,vector<int>&in){
	for(int i = 0;i<in.size();i++){
		mp[in[i]] = i;
	}
	return help(pre,0,in.size()-1);
}
TreeNode* buildTree(){
	int x;cin>>x;
	if(x==-1)return nullptr;
	TreeNode*  root = new TreeNode(x);
	
	queue<TreeNode*>q;
	q.push(root);
	
	while(!q.empty()){
		auto u = q.front();
		q.pop();
		
		if(cin>>x && x!=-1){
			u->left = new TreeNode(x);
			q.push(u->left);
		}
		
		if(cin>>x && x!=-1){
			u->right = new TreeNode(x);
			q.push(u->right);
		}
		
	}
	return root;
}
void preO(TreeNode* root){
	if(!root)return;
	cout<<root->val<<endl;
	preO(root->left);
	preO(root->right);
	
}
void print(TreeNode* root) {
    if (!root) {
        cout << "[]" << endl;
        return;
    }

    vector<string> result;
    queue<TreeNode*> q;
    q.push(root);

    while (!q.empty()) {
        TreeNode* curr = q.front();
        q.pop();

        if (curr) {
            result.push_back(to_string(curr->val));
            // Push children even if they are null
            q.push(curr->left);
            q.push(curr->right);
        } else {
            result.push_back("null");
        }
    }

    // Remove all the trailing "null"s from the back of the result
    while (!result.empty() && result.back() == "null") {
        result.pop_back();
    }

    // Print the array with brackets and commas
    cout << "[";
    for (int i = 0; i < result.size(); i++) {
        cout << result[i];
        if (i < result.size() - 1) cout << ",";
    }
    cout << "]" << endl;
}
int main() {
//	TreeNode* root  = buildTree();
	int n;
	cin>>n;
	vector<int>pre(n),in(n);
	
	for(int i = 0;i < n ;i++){
		cin>>pre[i];
	}
	
		for(int i = 0;i < n ;i++){
		cin>>in[i];
	}
	
	TreeNode* Croot = bt(pre,in);
	preO(Croot);
	print(Croot);
	return 0;
}