#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <clocale>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define FOR(i, s, n) for(int i = (s), i##_len=(n); i < i##_len; ++i)
#define FORS(i, s, n) for(int i = (s), i##_len=(n); i <= i##_len; ++i)
#define VFOR(i, s, n) for(int i = (s); i < (n); ++i)
#define VFORS(i, s, n) for(int i = (s); i <= (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FORS(i, 0, n)
#define VREP(i, n) VFOR(i, 0, n)
#define VREPS(i, n) VFORS(i, 0, n)
#define RFOR(i, s, n) for(int i = (s), i##_len=(n); i >= i##_len; --i)
#define RFORS(i, s, n) for(int i = (s), i##_len=(n); i > i##_len; --i)
#define RREP(i, n) RFOR(i, n, 0)
#define RREPS(i, n) RFORS(i, n, 0)
#define ALL(v) (v).begin(), (v).end()
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(ALL(v), greater<decltype(v[0])>())
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())

using ll = long long;
using Pi_i = pair<int, int>;
using VB = vector<bool>;
using VC = vector<char>;
using VD = vector<double>;
using VI = vector<int>;
using VLL = vector<ll>;
using VS = vector<string>;
using VSH = vector<short>;

const int MOD = 1000000007;
const int INF = 1000000000;
const int NIL = -1;

template<class T>
bool chmax(T &a, const T &b){
    if(a < b){
        a = b; return true;
    }
    return false;
}
template<class T>
bool chmin(T &a, const T &b){
    if(b < a){
        a = b; return true;
    }
    return false;
}




class BinaryTree{
    struct Node{//par :親, lt:左の子, rt:右の子
        int par, lt, rt, d, h;
        Node(): par(NIL), lt(NIL), rt(NIL), d(0), h(0){};
        Node(int P, int L, int R): par(P), lt(L), rt(R), d(0), h(0){};
    };
    vector<Node> T;
    int pos = 0; //rec用
public:
    BinaryTree(int n){
        T.resize(n, Node(NIL, NIL, NIL));
    }

    void addNode(int p, int l, int r) {
        T[p].lt = l;
        T[p].rt = r;
        if(l != NIL) T[l].par = p;
        if(r != NIL) T[r].par = p;
    }

    void setParent(int v, int p){
        T[v].par = p;
    }

    int getParent(int v){
        return T[v].par;
    }

    void setLeft(int v, int l){
        T[v].lt = l;
    }

    int getLeft(int v){
        return T[v].lt;
    }

    void setRight(int v, int r){
        T[v].rt = r;
    }

    int getRight(int v){
        return T[v].rt;
    }

    void setDepth(int v, int dd){
        T[v].d = dd;
        if(T[v].rt != NIL) setDepth(T[v].rt, dd+1);
        if(T[v].lt != NIL) setDepth(T[v].lt, dd+1);
    }

    int setHeight(int v){
        int hl = 0, hr = 0;
        if(T[v].lt != NIL) hl = setHeight(T[v].lt) + 1;
        if(T[v].rt != NIL) hr = setHeight(T[v].rt) + 1;
        return T[v].h = max(hl, hr);
    }

    int getSibling(int v){
        if(T[v].par == NIL) return NIL;
        if(T[T[v].par].lt != v && T[T[v].par].lt != NIL)
            return T[T[v].par].lt;
        if(T[T[v].par].rt != v && T[T[v].par].rt != NIL)
            return T[T[v].par].rt;
        return NIL;
    }

    void printTree(int v){
        cout << "node " << v << ": ";
        cout << "parent = " << T[v].par << ", ";
        cout << "sibling = " << getSibling(v) << ", ";
        int dg = 0;
        if(T[v].lt != NIL) ++dg;
        if(T[v].rt != NIL) ++dg;
        cout << "degree = " << dg << ", ";
        cout << "depth = " << T[v].d << ", ";
        cout << "height = " << T[v].h << ", ";

        if(T[v].par == NIL) cout << "root";
        else if(T[v].lt == NIL && T[v].rt == NIL)
            cout << "leaf";
        else cout << "internal node";
        cout << "\n";
    }

    void preParse(int v){
        if(v == NIL) return;
        cout << " " << v;
        preParse(T[v].lt);
        preParse(T[v].rt);
    }

    void inParse(int v){
        if(v == NIL) return;
        inParse(T[v].lt);
        cout << " " << v;
        inParse(T[v].rt);
    }

    void postParse(int v){
        if(v == NIL) return;
        postParse(T[v].lt);
        postParse(T[v].rt);
        cout << " " << v;
    }

    void postParse1index(int &i, int v){
        if(v == NIL) return;
        postParse1index(i, T[v].lt);
        postParse1index(i, T[v].rt);
        cout << (i ? " " : "") << v + 1;
        ++i;
    }

    int reconstFromPreIn(VI &pre, VI &in, int l, int r){
        if(l >= r) return NIL;
        int root = pre[pos++];
        int m = distance(in.begin(), find(ALL(in), root));
        int lc = reconstFromPreIn(pre, in, l, m);
        int rc = reconstFromPreIn(pre, in, m+1, r);
        addNode(root, lc, rc);
        return root;
    }
};


int main(){
    int n; cin >> n;
    VI pre(n), in(n);
    REP(i, n){
        cin >> pre[i];
        --pre[i];
    }
    REP(i, n){
        cin >> in[i];
        --in[i];
    }
    BinaryTree T(n);
    int root = pre[0];
    T.reconstFromPreIn(pre, in, 0, n);
    int cnt = 0;
    T.postParse1index(cnt, root);
    cout << "\n";
    return 0;
}



Measured memory_bytes: 3108