#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ld> vd;
typedef vector<ll> vll;
typedef vector<string> vstr;
typedef vector<vector<ll>> vvll;
typedef vector<pair<ll, ll>> vpll;
typedef priority_queue<ll, vector<ll>, greater<ll>> spqll;  // 小さい順に取り出し
typedef priority_queue<ll, vector<ll>, less<ll>> bpqll;     // 大きい順に取り出し

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define IREP(i, v) for (auto i = (v).begin(); i != (v).end(); i++)
#define FI first
#define SE second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define TS to_string
#define NP next_permutation
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) (ll) x.size()
#define SP(x) setprecision((ll)x)


ll INF = 1e9;
ll NIL = -1;
ll MOD = 1000000007;
ll LINF = 1e18;
ld EPS = 1e-9;
ld PI = M_PI;
vll dx = {1, 0, -1, 0, 1, -1, -1, 1};
vll dy = {0, 1, 0, -1, 1, 1, -1, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a;} //最大公約数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b;}     //最小公倍数

void yes(){ cout << "yes" << endl;}
void no(){ cout << "no" << endl;}

//-----------------------------------------
int a[100 + 10];
int b[100 + 10];


struct Node{
  int par, rig, lef;
};

Node T[100];

int par(int i){
  return T[i].par;
}
int sib(int i){
  int p = T[i].par;
  if(p == NIL) return NIL;
  Node t = T[p];
  if(t.lef == i) return t.rig;
  return t.lef;
}
int deg(int i){
  int res = 0;
  if(T[i].lef != NIL) res++;
  if(T[i].rig != NIL) res++;
  return res;
}
int dep(int i){
  int p = T[i].par;
  if(p == NIL) return 0;
  return dep(p) + 1;
}
int hei(int i){
  if(i == NIL) return -1;
  Node t = T[i];
  return max(hei(t.lef),hei(t.rig)) + 1;
}
string tag(int i){
  if(T[i].par == NIL) return "root";
  if(T[i].lef == NIL && T[i].rig == NIL) return "leaf";
  return "internal node";
}
void print(int i){
  cout << "node " << i <<":";
  cout << " parent = " << par(i) << ",";
  cout << " sibling = " << sib(i) << ",";
  cout << " degree = " << deg(i) << ",";
  cout << " depth = " << dep(i) << ",";
  cout << " height = " << hei(i) << ", ";
  cout << tag(i) <<endl;
}
void prepri(int i){
  cout << " " << i;
  if(T[i].lef != NIL) prepri(T[i].lef);
  if(T[i].rig != NIL) prepri(T[i].rig);
}
void inpri(int i){
  if(T[i].lef != NIL) inpri(T[i].lef);
  cout << " " << i;
  if(T[i].rig != NIL) inpri(T[i].rig);
}
void pospri(int i){
  if(T[i].lef != NIL) pospri(T[i].lef);
  if(T[i].rig != NIL) pospri(T[i].rig);
  cout << i+1 ;
  if(tag(i)!="root") cout << " ";
}

int n;
int make(int *A,int m){
  if(m == 0) return NIL; 
  if(m == 1) return A[0];

  int mini = INF;
  int tmp = INF;
  int len = 100;
  for(int i=0;i<m;i++){
    if(a[A[i]]<tmp){
      tmp = a[A[i]];
      mini = A[i];
      len = i;
    }
  }

  int l = make(A,len);
  int r = make(A+(len+1),m-len-1);
  if(r != NIL){
    T[r].par = mini;
    T[mini].rig = r;
  }
  if(l != NIL){
    T[l].par = mini;
    T[mini].lef = l;
  }
  return mini;
}

//-----------------------------------------

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;
  for(int i=0;i<=n;i++){
    T[i].par = NIL;
    T[i].lef = NIL;
    T[i].rig = NIL;
  }

  REP(i,n){
    int k;
    cin >> k;
    a[k-1] = i;
  }
  REP(i,n){
    cin >> b[i];
    b[i]--;
  }

  make(b,n);

  for(int i=0;i<n;i++){
    if(tag(i)=="root"){
      pospri(i);
      cout << endl;
    }
  }


  return 0;
}




Measured memory_bytes: 3252