Harry, upon inquiring Helena Ravenclaw's ghost, came to know that she told Tom Riddle or You-know-who about Rowena Ravenclaw's diadem and that he stole it from her. 

Harry thought that Riddle would have assumed that he was the only one to discover the Room of Requirement and thus, would have hidden it there. So Harry is trying to get inside the Room of Requirement to destroy the diadem as he knows that it is a horcrux.

But he has to answer a puzzle in order to enter the room. He is given n objects, numbered from 1 to n. Some of the objects have a parent object, that has a lesser number. Formally, object i may have a parent object parenti such that parenti < i.

There is also a type associated with each parent relation, it can be either of type 1 or type 2. Type 1 relation means that the child object is like a special case of the parent object. Type 2 relation means that the second object is always a part of the first object and all its special cases.

Note that if an object b is a special case of object a, and c is a special case of object b, then c is considered to be a special case of object a as well. The same holds for parts: if object b is a part of a, and object c is a part of b, then we say that object c is a part of a. Also note, that if object b is a part of a, and object c is a special case of a, then b is a part of c as well.

An object is considered to be neither a part of itself nor a special case of itself.

Now, Harry has to answer two type of queries:

  * 1 u v: he needs to tell if object v is a special case of object u. 
  * 2 u v: he needs to tell if object v is a part of object u. 

Input

First line of input contains the number n (1 ≤ n ≤ 105), the number of objects. 

Next n lines contain two integer parenti and typei ( - 1 ≤ parenti < i parenti ≠ 0,  - 1 ≤ typei ≤ 1), implying that the i-th object has the parent parenti. (If typei = 0, this implies that the object i is a special case of object parenti. If typei = 1, this implies that the object i is a part of object parenti). In case the i-th object has no parent, both parenti and typei are -1.

Next line contains an integer q (1 ≤ q ≤ 105), the number of queries. 

Next q lines each represent a query having three space separated integers typei, ui, vi (1 ≤ typei ≤ 2, 1 ≤ u, v ≤ n).

Output

Output will contain q lines, each containing the answer for the corresponding query as "YES" (affirmative) or "NO" (without quotes).

You can output each letter in any case (upper or lower).

Examples

Input

3
-1 -1
1 0
2 0
2
1 1 3
2 1 3


Output

YES
NO


Input

3
-1 -1
1 0
1 1
2
2 2 3
2 3 2


Output

YES
NO

Note

In test case 1, as object 2 is a special case of object 1 and object 3 is a special case of object 2, this makes object 3 a special case of object 1.

In test case 2, as object 2 is a special case of object 1 and object 1 has object 3, this will mean that object 2 will also have object 3. This is because when a general case (object 1) has object 3, its special case (object 2) will definitely have object 3.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int LG = 17;
int anc[N][LG];
int sum[N][LG];
int depth[N];
vector<pair<int, int>> chld[N];
int parent[N], type[N];
int n;
void build(int now, int par, int val) {
  anc[now][0] = par;
  sum[now][0] = val;
  depth[now] = depth[par] + 1;
  for (int i = 1; (1 << i) <= depth[now]; i++) {
    int par = anc[now][i - 1];
    int cur_sum = sum[now][i - 1] + sum[par][i - 1];
    anc[now][i] = anc[par][i - 1];
    sum[now][i] = cur_sum;
  }
}
void dfs(int now) {
  for (pair<int, int> nex : chld[now]) {
    build(nex.first, now, nex.second);
    dfs(nex.first);
  }
}
void read() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", parent + i, type + i);
  }
}
void init() {
  for (int i = 1; i <= n; i++) {
    if (parent[i] != -1) {
      chld[parent[i]].push_back({i, type[i]});
    }
  }
  for (int i = 1; i <= n; i++)
    if (parent[i] == -1) {
      dfs(i);
    }
}
int get_lca(int u, int v) {
  if (depth[v] > depth[u]) {
    swap(u, v);
  }
  int diff = depth[u] - depth[v];
  for (int i = 0; diff; i++)
    if (diff & (1 << i)) {
      u = anc[u][i];
      diff -= (1 << i);
    }
  if (u == v) {
    return u;
  }
  for (int i = LG - 1; i >= 0; i--) {
    if (depth[u] >= (1 << i) && anc[u][i] != anc[v][i]) {
      u = anc[u][i];
      v = anc[v][i];
    }
  }
  if (depth[u] == 0 || anc[u][0] != anc[v][0]) {
    return -1;
  }
  return anc[u][0];
}
int get_sum(int now, int up) {
  int ret = 0;
  for (int i = LG - 1; i >= 0; i--)
    if (up & (1 << i)) {
      ret += sum[now][i];
      now = anc[now][i];
      up -= (1 << i);
    }
  return ret;
}
bool is_special(int u, int v) {
  if (u == v) {
    return false;
  }
  int lca = get_lca(u, v);
  if (lca != u) {
    return false;
  }
  int up = depth[v] - depth[lca];
  if (get_sum(v, up) != 0) {
    return false;
  }
  return true;
}
bool is_part(int u, int v) {
  if (u == v) {
    return false;
  }
  int lca = get_lca(u, v);
  if (lca == -1) {
    return false;
  }
  if (lca == v) {
    return false;
  }
  int up = depth[v] - depth[lca];
  if (get_sum(v, up) != up) {
    return false;
  }
  up = depth[u] - depth[lca];
  if (get_sum(u, up) != 0) {
    return false;
  }
  return true;
}
void work() {
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int t, u, v;
    scanf("%d %d %d", &t, &u, &v);
    bool ret;
    if (t == 1)
      ret = is_special(u, v);
    else
      ret = is_part(u, v);
    printf("%s\n", ret ? "YES" : "NO");
  }
}
int main() {
  read();
  init();
  work();
  return 0;
}
