#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=1010;
int in[maxn];
int N;
struct Node{
int left, right;
int key;
}p[maxn];
int index=0;
void inOrder(int root){
if(root>=N||root==-1)return;
inOrder(p[root].left);
p[root].key=in[index++];
inOrder(p[root].right);
}
int cnt=0;
void levelOrder(int root){
queue<int>q;
q.push(root);
while(!q.empty()){
int top=q.front();
q.pop();
printf("%d", p[top].key);
cnt++;
if(cnt!=N)printf(" ");
if(p[top].left!=-1)q.push(p[top].left);
if(p[top].right!=-1)q.push(p[top].right);
}
}
int main(){
int left, right;
scanf("%d", &N);
for(int i=0; i<N; i++){
scanf("%d%d", &p[i].left, &p[i].right);
}
for(int i=0; i<N; i++){
scanf("%d", &in[i]);
}
sort(in, in+N);
inOrder(0); //中序遍历构建树
levelOrder(0); //层序遍历输出
return 0;
}