模拟链表
#include <iostream>
using namespace std;
const int MAXN = 1e5 + 10;
int head = 1;
struct num
{
int right;
int left;
}arr[MAXN];
void joinl(int x, int y)
{
arr[x].left = y;
arr[x].right = arr[y].right;
arr[ arr[y].right ].left = x;
arr[y].right = x;
}
void joinr(int x, int y)
{
if(y == head)
{
head = x;
}
arr[x].left = arr[y].left;
arr[x].right = y;
arr[y].left = x;
if(arr[x].left != -1)
arr[ arr[x].left ].right = x;
}
void clear(int x)
{
if(x == head)
{
head = arr[x].left;
}
if(arr[x].left != -1)
{
arr[ arr[x].left ].right = arr[x].right;
}
if(arr[x].right != -1)
{
arr[ arr[x].right ].left = arr[x].left;
}
else
{
arr[ arr[x].left ].right = -1;
}
arr[x].left = -1;
arr[x].right = -1;
}
void put(int N)
{
cout<<endl;
for(int i = 1; i <= N; i++)
{
cout<<i<<' '<<arr[i].left<<' '<<arr[i].right<<endl;
}
cout<<endl;
}
int main()
{
int N, k, p, M, t;
cin>>N;
for(int i = 1; i <= N; i++)
{
arr[i].right = -1;
arr[i].left = -1;
}
for(int i = 2; i <= N; i++)
{
cin>>k>>p;
if(p == 0)
{
joinr(i, k);
}
else
{
joinl(i, k);
}
}
//put(N);
cin>>M;
while(M--)
{
cin>>t;
clear(t);
}
int go = head;
while(1)
{
cout<<go<<' ';
if(arr[go].right == -1)
break;
go = arr[go].right;
}
return 0;
}