#include #include using namespace std; using namespace atcoder; typedef modint998244353 mint; typedef long long ll; // importbisect template int bisect_left(vector &X, T v){ return lower_bound(X.begin(), X.end(), v) - X.begin(); } template int bisect_right(vector &X, T v){ return upper_bound(X.begin(), X.end(), v) - X.begin(); } // ----- int main(){ int n; cin >> n; vector l(n); vector a(n); vector> ikeru(n, vector(0)); for (int i=0; i> l[i+1] >> a[i+1]; ikeru[a[i+1]-1].push_back(i+1); } priority_queue> pq; vector can(n, 2e9); can[0] = 1; for (int j:ikeru[0]){ pq.push(pair(-l[j], j)); } // heiro ha kangaenakute yoi. ll f = 1; vector tansaku(n); tansaku[0] = true; while(!pq.empty()){ ll lv = -pq.top().first; int ind = pq.top().second; pq.pop(); if (tansaku[ind]) continue; tansaku[ind] = true; can[ind] = max(f, lv); f = max(f, lv); for (int j: ikeru[ind]){ if (!tansaku[j]) pq.push(pair(-l[j], j)); } } vector ls = can; sort(ls.begin(), ls.end()); int q; cin >> q; for (int i=0; i> t; if (t == 1){ int x; cin >> x; cout << bisect_right(ls, (ll)x) << "\n"; }else{ int x; cin >> x; x--; if (can[x] > 15e8){ cout << -1 << "\n"; }else{ cout << can[x] << "\n"; } } } }