//stl的list用法的典型题目
#include <cstdio>
#include <list>
using namespace std;
const int maxN = 1e5 + 10;
list<int>::iterator pos[maxN];
list<int> queList;
bool erased[maxN];
int N;
void buildQueue()
{
queList.push_front(1);
pos[1] = queList.begin();
for (int i = 2; i <= N; i++)
{
int k, p;
scanf("%d%d", &k, &p);
if (p == 0)
{
pos[i] = queList.insert(pos[k], i); //left
}
else
{
auto nextIter = next(pos[k]);//向当前元素的前面添加元素
pos[i] = queList.insert(nextIter, i); //right
}
}
int M;
scanf("%d", &M);
for (int x, i = 1; i <= M; i++)
{
scanf("%d", &x);
if (!erased[x])
{
queList.erase(pos[x]);
}
erased[x] = true;
}
}
int main()
{
scanf("%d", &N);
buildQueue();
bool first = true;
for (int x: queList)
{
if (!first)
putchar(' ');
first = false;
printf("%d", x);
}
putchar('\n');
return 0;
}
//ps:stl的list的学习网站 http://c.biancheng.net/view/6892.html