/*               ∧∧∩               ( ゚∀゚)/ キタ━━━━━━(゚∀゚)━━━━━━!!!!             ⊂   ノ  ゆっくり見てってね!!!!!!!!              (つ ノ     o          (ノ      \      ☆              |      o           (⌒ ⌒ヽ   /     ☆     \  (´⌒  ⌒  ⌒ヾ   /       ('⌒ ; ⌒   ::⌒  )      (´     )     ::: ) /   ☆─ (´⌒;:    ::⌒`) :;  ) */ #include using namespace std; #define rep(i, s, n) for (int i = (s); i < (int)(n); i++) typedef long long ll; #define _GLIBCXX_DEBUG ll l[200009], a[200009]; bool f[200009]; ll gcd(int i, vector& was, vector& pnt) { f[i] = true; ll w, num; if(!f[a[i]]) { num = gcd(a[i], was, pnt); } else { num = was[a[i]]; } if(num == -1) { return -1; } w = max(l[i], num); pnt.push_back(w); return w; } int countElementsLessThanOrEqual(const vector& nums, int a) { int left = 0; int right = nums.size() - 1; // 二分探索 while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] <= a) { left = mid + 1; } else { right = mid - 1; } } // leftはa以下の要素の個数を表す return left; } int main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; cin >> n; rep(i, 2, n+1) { cin >> l[i] >> a[i]; } vector was(n+1); rep(i, 1, n+1) { was[i] = -1; } vector pnt; f[1] = true; was[1] = 0; pnt.push_back(0); rep(i, 2, n+1) { if(f[i]) continue; f[i] = true; ll num; if(!f[a[i]]) { num = gcd(a[i], was, pnt); } else { num = was[a[i]]; } if(num == -1) { was[i] = -1; continue; } was[i] = max(l[i], num); pnt.push_back(was[i]); } sort(pnt.begin(), pnt.end()); int q; cin >> q; while(q--) { int z; cin >> z; if(z == 1) { int x; cin >> x; int cnt = countElementsLessThanOrEqual(pnt, x); cout << cnt << endl; } else { int y; cin >> y; cout << was[y] << endl; } } }