#include <stdio.h>
#include <sstream>
#include <string.h>
#include <vector>
#include <map>
#include <algorithm>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <limits>
#include <iomanip>
#include <ctype.h>
#define _USE_MATH_DEFINES
#include <iostream>
#include <math.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, char> pic;
#define bit(x,v) ((ll)x << v)

const ll INF = 1000000007;
const int MAX = 210000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % MOD;
		inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
		finv[i] = finv[i - 1] * inv[i] % MOD;
	}
}

// 二項係数計算
long long COM(int n, int k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}
int pr[100010];
void uini(int n) {
	for (size_t i = 0; i <= n; i++)
	{
		pr[i] = i;
	}
}

int parent(int x) {
	if (x == pr[x]) return x;
	return pr[x] = parent(pr[x]);
}

bool unit(int x, int y) {
	int px = parent(x);
	int py = parent(y);

	if (px == py) return false;
	if (px < py) {
		pr[py] = px;
	}
	else {
		pr[px] = py;
	}
	return true;
}

ll bit[200010];
int max_n = 200000;
int pm = 0;
void add(int x) {

	while (max_n >= x)
	{
		bit[x]++;
		x += x & -x;
	}
}
void sub(int x) {
	while (max_n >= x)
	{
		bit[x]--;
		x += x & -x;
	}
}
ll merge(ll *a, int left, int mid, int right) {
	ll n1 = mid - left;
	ll n2 = right - mid;
	vector<int> L(n1 + 1);
	vector<int> R(n2 + 1);
	for (size_t i = 0; i < n1; i++)
	{
		L[i] = a[left + i];
	}
	for (size_t i = 0; i < n2; i++)
	{
		R[i] = a[mid + i];
	}

	L[n1] = INF;
	R[n2] = INF;
	ll i = 0;
	ll j = 0;
	ll r = 0;
	for (size_t k = left; k < right; k++)
	{
		if (L[i] <= R[j]) {
			a[k] = L[i];
			i++;
		}
		else {
			a[k] = R[j];
			r += n1 - i;
			j++;
		}
	}
	return r;
}
ll merge2(pair<int,char> *a, int left, int mid, int right) {
	ll n1 = mid - left;
	ll n2 = right - mid;
	vector<pair<int, char>> L(n1 + 1);
	vector<pair<int, char>> R(n2 + 1);
	for (size_t i = 0; i < n1; i++)
	{
		L[i] = a[left + i];
	}
	for (size_t i = 0; i < n2; i++)
	{
		R[i] = a[mid + i];
	}

	L[n1] = make_pair(INF,' ');
	R[n2] = make_pair(INF,' ');
	ll i = 0;
	ll j = 0;
	ll r = 0;
	for (size_t k = left; k < right; k++)
	{
		if (L[i].first <= R[j].first) {
			a[k] = L[i];
			i++;
		}
		else {
			a[k] = R[j];
			r += n1 - i;
			j++;
		}
	}
	return r;
}
ll mergeSort2(pair<int, char> *a, int left, int right) {
	ll res = 0;
	if (left + 1 < right) {
		int mid = (left + right) / 2;
		res = mergeSort2(a, left, mid);
		res += mergeSort2(a, mid, right);
		res += merge2(a, left, mid, right);
	}
	return res;
}
ll mergeSort(ll *a, int left, int right) {
	ll res = 0;
	if (left + 1 < right) {
		int mid = (left + right) / 2;
		res = mergeSort(a, left, mid);
		res += mergeSort(a, mid, right);
		res += merge(a, left, mid, right);
	}
	return res;
}
int partition(pair<int,char> *a, int p, int r) {
	pair<int, char> x = a[r];
	int i = p - 1;
	for (size_t j = p; j < r; j++)
	{
		if (a[j].first <= x.first) {
			i++;
			swap(a[i], a[j]);
		}
	}
	swap(a[i + 1], a[r]);
	return i + 1;
}
void quick(pair<int, char> *a, int p, int r) {
	if (p < r) {
		int q = partition(a, p, r);
		quick(a, p, q - 1);
		quick(a, q + 1, r);
	}
}
int n;
vector<pii> es[100010];
vector<int> res;
int rec(pii x,int par,int dep,int type)
{
	if (x.first == -1) {
		return -1;
	}

	// node 0: parent = -1, depth = 0, root, [1, 4, 10]
	// node id: parent = p , sibling = s , degree = deg, depth = dep, height = h, type
	//int height = max(rec(es[x.first][0], x.first, dep + 1), rec(es[x.first][1], x.first, dep + 1)) + 1;
	//int deg = (es[x.first][0].first != -1 && es[x.first][1].first != -1) ? 2  :
	//	es[x.first][0].first != -1 || es[x.first][1].first != -1 ? 1 : 0;
	//string r = "node " + to_string(x.first) + ": parent = " + to_string(par)
	//	+ ", sibling = " + to_string(x.second) 
	//	+ ", degree = " + to_string(deg)
	//	+ ", depth = " + to_string(dep) 
	//	+ ", height = " + to_string(height) + ", ";
	//if (par == -1) r+= "root";
	//else if (es[x.first][0].first != -1 || es[x.first][1].first != -1) r += "internal node";
	//else r += "leaf";
	rec(es[x.first][0], 0, 0, type);
	rec(es[x.first][1], 0, 0, type);
	res.push_back(x.first);
	
}
int a[51];
int b[45];
int mp[45];
int inf = 50;
void make(int x,int s,int t) {


	int sp = 0;
	for (size_t i = 0; i < n; i++)
	{
		if (b[i] == x) {
			sp = i;
		}
	}
	int mi = inf;

	for (size_t i = s; i < sp; i++)
	{
		mi = min(mp[b[i]], mi);
	}
	if (mi != inf) {
		make(a[mi], s, sp - 1);
	}
	int m2 = inf;
	for (size_t i = sp + 1; i <= t; i++)
	{
		m2 = min(mp[b[i]], m2);
	}
	if (m2 != inf)
		make(a[m2], sp + 1, t);
	es[x].push_back(make_pair(a[mi], a[m2]));
	es[x].push_back(make_pair(a[m2], a[mi]));
}
void solv() {
	cin >> n;
	a[inf] = -1;
	for (size_t i = 0; i < n; i++)
	{
		cin >> a[i];
		mp[a[i]] = i;
	}
	
	for (size_t i = 0; i < n; i++)
	{
		cin >> b[i];
	}

	make(a[0], 0, n - 1);
	rec(make_pair(a[0], -1), -1, 0, 3);
	for (size_t i = 0; i < n; i++)
	{
		if (i > 0) cout << " ";
		cout  << res[i];
	}
	cout << endl;
}
int main() {
	//	COMinit();
	solv();
	return 0;
}



Measured memory_bytes: 5500