#include <bits/stdc++.h>
using namespace std;
int n, f[100010];
namespace LCA {
const int LOG = 20;
int dep[100010], fa[100010][20];
int g[100010][20];
void Build() {
  for (int j = 1; j < LOG; ++j)
    for (int i = 1; i <= n; ++i) {
      fa[i][j] = fa[fa[i][j - 1]][j - 1];
      g[i][j] = g[i][j - 1] | g[fa[i][j - 1]][j - 1];
    }
}
bool LCA(int x, int y, int k) {
  int res1 = 0, res2 = 0, flag = 0;
  if (dep[x] < dep[y]) swap(x, y), flag = 1;
  for (int i = 19; ~i; --i)
    if (dep[x] - (1 << i) >= dep[y]) res1 |= g[x][i], x = fa[x][i];
  if (x == y) {
    if (flag)
      return res2 == k;
    else
      return res1 == k;
  } else if (k == 1)
    return false;
  for (int i = 19; ~i; --i)
    if (fa[x][i] != fa[y][i]) {
      res1 |= g[x][i], res2 |= g[y][i];
      x = fa[x][i], y = fa[y][i];
    }
  res1 |= g[x][0];
  res2 |= g[y][0];
  if (flag)
    return res2 == 2 && res1 == 1;
  else
    return res1 == 2 && res2 == 1;
}
};  // namespace LCA
int Find(int u) { return f[u] ? f[u] = Find(f[u]) : u; }
void Init() {
  int i, fat, k, fu, fv;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i) {
    scanf("%d %d", &fat, &k);
    if (fat == -1) continue;
    LCA::fa[i][0] = fat;
    LCA::g[i][0] = 1 << k;
    LCA::dep[i] = LCA::dep[fat] + 1;
    fu = Find(i);
    fv = Find(fat);
    if (fu == fv) continue;
    f[fv] = fu;
  }
  LCA::Build();
}
void Work() {
  int q, t, u, v;
  for (scanf("%d", &q); q; --q) {
    scanf("%d %d %d", &t, &u, &v);
    if (u == v)
      printf("NO\n");
    else if (Find(u) != Find(v))
      printf("NO\n");
    else
      printf("%s\n", LCA::LCA(v, u, t) ? "YES" : "NO");
  }
}
int main() {
  Init();
  Work();
  return 0;
}
