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;
struct node1 {
  int father, son, sum, dep, top;
} tree[100010];
struct node {
  int l, r, sum, p;
} st[100010 * 5];
vector<int> g[100010];
int ru[100010], chu[100010], seq[100010], tot;
int w[100010], father[100010], sum[100010];
int fa[100010];
int n, q;
int getfather(int x) {
  if (x == fa[x]) return x;
  fa[x] = getfather(fa[x]);
  return fa[x];
}
void dfs1(int x, int pre) {
  int son;
  tree[x].sum = 1;
  tree[x].son = 0;
  int cnt = g[x].size();
  for (int i = 0; i < cnt; i++) {
    son = g[x][i];
    if (son == pre) continue;
    tree[son].father = x;
    tree[son].dep = tree[x].dep + 1;
    dfs1(son, x);
    tree[x].sum += tree[son].sum;
    if (tree[x].son == 0 || tree[tree[x].son].sum < tree[son].sum)
      tree[x].son = son;
  }
}
void dfs2(int x, int pre) {
  int son;
  ru[x] = ++tot;
  seq[tot] = x;
  if (tree[x].son != 0) {
    tree[tree[x].son].top = tree[x].top;
    dfs2(tree[x].son, x);
  }
  int cnt = g[x].size();
  for (int i = 0; i < cnt; i++) {
    son = g[x][i];
    if (son == tree[x].son || son == pre) continue;
    tree[son].top = son;
    dfs2(son, x);
  }
  chu[x] = tot;
}
void prepare() {
  tree[1].dep = 0;
  tree[1].top = 1;
  tree[1].father = 0;
  dfs1(1, 0);
  tot = 0;
  dfs2(1, 0);
  memset(st, 0, sizeof(st));
}
int getlca(int x, int y) {
  while (tree[x].top != tree[y].top) {
    if (tree[tree[x].top].dep > tree[tree[y].top].dep) {
      x = tree[tree[x].top].father;
    } else {
      y = tree[tree[y].top].father;
    }
  }
  if (tree[x].dep < tree[y].dep)
    return x;
  else
    return y;
}
int getsum(int x, int y) {
  int ans = 0;
  while (tree[x].top != tree[y].top) {
    if (tree[tree[x].top].dep > tree[tree[y].top].dep) {
      ans = ans - sum[ru[tree[x].top] - 1] + sum[ru[x]];
      x = tree[tree[x].top].father;
    } else {
      ans = ans - sum[ru[tree[y].top] - 1] + sum[ru[y]];
      y = tree[tree[y].top].father;
    }
  }
  if (tree[x].dep > tree[y].dep) swap(x, y);
  ans = ans - sum[ru[x] - 1] + sum[ru[y]];
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) g[i].clear();
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &father[i], &w[i]);
    if (father[i] == -1) w[i] == 0;
    if (father[i] != -1) {
      g[father[i]].push_back(i);
      fa[getfather(i)] = getfather(father[i]);
    }
  }
  tot = 0;
  for (int i = 1; i <= n; i++)
    if (father[i] == -1) {
      tree[i].dep = 0;
      tree[i].top = i;
      tree[i].father = 0;
      dfs1(i, 0);
      dfs2(i, 0);
    }
  memset(sum, 0, sizeof(sum));
  for (int i = 1; i <= n; i++) sum[ru[i]] = w[i];
  for (int i = 1; i <= n; i++) sum[i] += sum[i - 1];
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int t, x, y;
    scanf("%d%d%d", &t, &x, &y);
    if (getfather(x) != getfather(y) || x == y) {
      puts("NO");
      continue;
    }
    if (t == 1) {
      int lca = getlca(x, y);
      int sum = getsum(x, y) - w[lca];
      if (lca != x || sum != 0)
        puts("NO");
      else
        puts("YES");
    } else if (t == 2) {
      int lca = getlca(x, y);
      int sum1 = getsum(x, lca) - w[lca];
      int sum2 = getsum(lca, y) - w[lca];
      if (sum1 == 0 && sum2 == tree[y].dep - tree[lca].dep && sum2 > 0)
        puts("YES");
      else
        puts("NO");
    }
  }
}
