//#pragma GCC target("avx") //#pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include using namespace std; #include using namespace atcoder; //local debug #ifdef LOCAL #include #define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (static_cast(0)) #endif #define overload4(a, b, c, d, e, ...) e //alias using uint = unsigned int; using ll = long long; using ull = unsigned long long; using ld = long double; using mint = modint998244353; using Mint = modint1000000007; //constants constexpr const long long MOD = 998244353; constexpr const long long MODM = 1000000007; constexpr const int INF = 1e9; constexpr const ll LINF = 1e18; //rep(for-loop) macro #define rep2(i, n) for(ll i = 0; i < n; i++) #define rep3(i, k, n) for(ll i = k; i < n; i++) #define rep4(i, k, n, a) for(ll i = k; i < n; i += a) #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2)(__VA_ARGS__) #define rep1_2(i, n) for(ll i = 1; i <= n; i++) #define rep1_3(i, k, n) for(ll i = k; i <= n; i++) #define rep1_4(i, k, n, a) for(ll i = k; i <= n; i += a) #define rep1(...) overload4(__VA_ARGS__, rep1_4, rep1_3, rep1_2)(__VA_ARGS__) #define Rep2(i, n) for(ll i = n - 1; i >= 0; i--) #define Rep3(i, k, n) for(ll i = n - 1; i >= k; i--) #define Rep4(i, k, n, a) for(ll i = n - 1; i >= k; i -= a) #define Rep(...) overload4(__VA_ARGS__, Rep4, Rep3, Rep2)(__VA_ARGS__) #define Rep1_2(i, n) for(ll i = n; i >= 1; i--) #define Rep1_3(i, k, n) for(ll i = n; i >= k; i--) #define Rep1_4(i, k, n, a) for(ll i = n; i >= k; i -= a) #define Rep1(...) overload4(__VA_ARGS__, Rep1_4, Rep1_3, Rep1_2)(__VA_ARGS__) #define vfor(v, x) for(auto x : v) #define mfor(map) for(auto &[key, value] : map) //vector macro #define vvecc(T, name, n, m) vector> name(n, vector(m)) #define vvec(T, name, n) vector> name(n) #define vvecs(T, name, n, m, s) vector> name(n, vector(m, s)) #define all(x) begin(x), end(x) #define LB(v, x) distance((v).begin(), lower_bound(all(v), (x))) #define UB(v, x) distance((v).begin(), upper_bound(all(v), (x))) //data structure macro #define ef emplace_front #define eb emplace_back #define pf pop_front #define pb pop_back #define mp make_pair #define fi first #define se second #define mt make_tuple #define get(t, x) get(t) #define lb lower_bound #define ub upper_bound template using pq = priority_queue; template using pqmin = priority_queue, greater>; template using pqp = priority_queue>; template using pqpmin = priority_queue, vector>, greater>>; //output #define pl() cout << '\n' template void print(const T& a) {cout << a;} void printl(){} template void printl(const T& t){print(t); pl();} template void printl(const Head& head, const Tail&... tail) {print(head); cout << " "; printl(tail...);} template void fin(const T& t) {printl(t); exit(0);} void Yes(bool a) {cout << (a ? "Yes" : "No") << '\n';} template void Out2(bool a, T yes, U no) {if(a) printl(yes); else printl(no);} //functions int ctoi(char c) {return c - '0';} char to_char(int x) {return x + '0';} template constexpr auto Emin(T... a) {return min(initializer_list>{a...});} template constexpr auto Emax(T... a) {return max(initializer_list>{a...});} template bool chmax(T &a, const U &b) {if (a < b) { a = b; return true;} return false;} template bool chmin(T &a, const U &b) {if (a > b) { a = b; return true;} return false;} template ll Epow(T x, U y) {ll ans = 1; for(int i = 0; i < y; i++) ans *= x; return ans;} template ll Eceil(T x, U y) {return (ll)ceil((ld)x / (ld)y);} template ll Efloor(T x, U y) {return (ll)floor((ld)x / (ld)y);} template bool check_bit(T tar, U bit) {return ((tar & Epow(2, bit)) != 0);} /*Graph Library*/ using Graph = vector>; #define affix(v, a, b) v[a].emplace_back(b) //辺の重みが1の時のstartから各点への距離を求める - O(N + M) vector BFS(int start, Graph &G) { vector dist(G.size(), -1); queue bfs; dist[start] = 0; bfs.push(start); while(!bfs.empty()) { int v = bfs.front(); bfs.pop(); for(int next : G[v]) { if(dist[next] != -1) continue; dist[next] = dist[v] + 1; bfs.push(next); } } return dist; } //--------------------------------------------------------------------------------------------------- int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(10); int N; cin >> N; vector> skill(N + 1); rep1(i, 2, N) cin >> skill[i].fi >> skill[i].se; int Q; cin >> Q; vector> G(N + 1); rep1(i, 2, N) { int v1 = i, v2 = skill[i].se; affix(G, v2, v1); } vector need_lv(N + 1, INF); need_lv[1] = 0; queue bfs; bfs.push(1); while(!bfs.empty()) { int v = bfs.front(); bfs.pop(); int need = need_lv[v]; for(auto next : G[v]) { need_lv[next] = Emax(skill[next].fi, need); bfs.push(next); } } vector lvs; rep1(i, N) lvs.eb(need_lv[i]); sort(all(lvs)); while(Q--) { int QR, x; cin >> QR >> x; if(QR == 1) printl(UB(lvs, x)); if(QR == 2) Out2(need_lv[x] == INF, -1, need_lv[x]); } }