10
4 108 114
5 30 155
8 193 197
9 71 169
2 163 166
8 120 144
8 22 151
4 21 166
2 46 127
8 38 51
 20
9 142 172
7 132 256
8 245 315
9 496 496
3 345 362
8 13 162
5 342 470
9 16 488
9 467 482
4 471 478
10 92 224
6 228 261
9 54 167
2 402 409
5 9 10
7 225 360
3 438 483
6 252 342
3 491 493
6 296 367
 1
8 1234567890123 123456789012345

0
3
0
1
3
0
5
16
9
1
 0
0
0
0
4
6
13
7
0
0
1
2
3
0
0
2
12
9
1
4
 774752389568

#include <bits/stdc++.h>
using namespace std;
const long long M = 1000;
long long n, m, u, v, timer, counter;
vector<vector<long long> > data, tt;
long long binup[M][20];
vector<long long> tin, tout, depth, cs, os, ch;
void dfs(long long vertex, long long last, long long d, long long type) {
  cs[vertex] = counter;
  tin[vertex] = timer;
  timer += 1;
  depth[vertex] = d;
  os[vertex] = (type == 0);
  ch[vertex] = (type == 1);
  if (last != -1) {
    os[vertex] += os[last];
    ch[vertex] += ch[last];
    binup[vertex][0] = last;
    long long now_father = last;
    long long now_level = 0;
    while (binup[now_father][now_level] != -1) {
      binup[vertex][now_level + 1] = binup[now_father][now_level];
      now_father = binup[now_father][now_level];
      now_level += 1;
    }
  }
  for (long long i = 0; i < data[vertex].size(); i++) {
    long long to = data[vertex][i];
    if (last == to) continue;
    dfs(to, vertex, d + 1, tt[vertex][i]);
  }
  tout[vertex] = timer;
  timer += 1;
}
long long lca(long long fv, long long sv) {
  if (tin[fv] <= tin[sv] && tout[fv] >= tout[sv]) return fv;
  if (tin[sv] <= tin[fv] && tout[sv] >= tout[fv]) return sv;
  long long now_vertex = fv;
  for (long long level = 19; level >= 0; level--) {
    if (binup[now_vertex][level] == -1) continue;
    long long candidate = binup[now_vertex][level];
    if (tin[candidate] < tin[sv] && tout[candidate] > tout[sv]) continue;
    now_vertex = candidate;
  }
  return binup[now_vertex][0];
}
signed main() {
  cin >> n;
  vector<bool> fathers;
  for (long long i = 0; i < n; i++) {
    tin.push_back(-1);
    tout.push_back(-1);
    depth.push_back(-1);
    vector<long long> help, h;
    tt.push_back(h);
    data.push_back(help);
    fathers.push_back(true);
    cs.push_back(-1);
    os.push_back(-1);
    ch.push_back(-1);
  }
  for (long long i = 0; i < n; i++) {
    long long p, t;
    cin >> p >> t;
    if (p == -1) continue;
    data[p - 1].push_back(i);
    fathers[i] = false;
    tt[p - 1].push_back(t);
  }
  cin >> m;
  for (long long i = 0; i < M; i++) {
    for (long long j = 0; j < 20; j++) {
      binup[i][j] = -1;
    }
  }
  timer = 0;
  counter = 0;
  for (long long i = 0; i < n; i++) {
    if (fathers[i]) {
      cout << i << endl;
      dfs(i, -1, 0, -1);
      counter++;
    }
  }
  return 0;
}
