This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
//#define int ll
const int N = 5e5 + 10;
const int INF = 1e9 + 10;
struct ST {
  vector<int> mn, mx, a;
  ST(int n, vector<int> _a) {
    a = _a;
    mn.resize(4 * n + 10);
    mx.resize(4 * n + 10);
    build(1, 1, n);
  }
  void build(int v, int l, int r) {
    if (l == r) {
      mn[v] = mx[v] = a[l];
      return;
    }
    int mid = (l + r) / 2;
    build(2 * v, l, mid);
    build(2 * v + 1, mid + 1, r);
    mn[v] = min(mn[2 * v], mn[2 * v + 1]);
    mx[v] = max(mx[2 * v], mx[2 * v + 1]);
  }
  int get_max(int v, int l, int r, int a, int b) {
    if (r < a || l > b) {
      return 0;
    }
    if (l >= a && r <= b) {
      return mx[v];
    }
    int mid = (l + r) / 2;
    return max(get_max(2 * v, l, mid, a, b), get_max(2 * v + 1, mid + 1, r, a, b));
  }
  int get_min(int v, int l, int r, int a, int b) {
    if (r < a ||l > b) {
      return INF;
    }
    if (l >= a && r <= b) {
      return mn[v];
    }
    int mid = (l + r) / 2;
    return min(get_min(2 * v, l, mid, a, b), get_min(2 * v + 1, mid + 1, r, a, b));
  }
};
int c[N];
vector<int> have[N];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> c[i];
  }
	for (int i = 1; i <= n; i++) {
    int cnt;
    cin >> cnt;
    have[i].resize(cnt);
    for (auto &it : have[i]) {
      cin >> it;
    }
  }
  map<int, int> prv;
  vector<int> need_l(n + 1);
  for (int i = 1; i < n; i++) {
    for (auto it : have[i]) {
      prv[it] = i;
    }  
    need_l[i] = prv[c[i]];
  }
  prv = {};
  vector<int> need_r(n + 1);
  for (int i = n; i > 1; i--) {
    for (auto it : have[i]) {
      prv[it] = i;
    }
    need_r[i] = prv[c[i - 1]];
    if (need_r[i] == 0) {
      need_r[i] = n + 1;
    }
  }
  ST nr(n, need_r), nl(n, need_l);
  vector<int> mn(n + 1), mx(n + 1);
  for (int i = 1; i < n; i++) {
    if (need_l[i] == 0) {
      mx[i] = 0;
      continue;
    }
    int l = need_l[i], r = i + 1;
    while (r - l > 1) {
      int mid = (l + r) / 2;
      if (nr.get_max(1, 1, n, need_l[i] + 1, mid) > i) {
        r = mid;
      }
      else {
        l = mid;
      }
    }
    mx[i] = l;
  }
  for (int i = 2; i <= n; i++) {
    if (need_r[i] == 0) {
      mn[i] = n + 1;
      continue;
    }
    int l = i - 1, r = need_r[i];
    while (r - l > 1) {
      int mid = (l + r) / 2;
      if (nl.get_min(1, 1, n, mid, need_r[i] - 1) < i) {
        l = mid;
      }
      else {
        r = mid;
      }
    }
    mn[i] = r;
  } 
  ST MN(n, mn), MX(n, mx);
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    if (l == r) {
      cout << "YES\n";
      continue;
    }
    if (l < r) {
      bool ok = true;
      for (int j = l; j < r; j++) {
        if (l > mx[j]) {
          ok = false;
          break;
        }
      }
      if (MX.get_min(1, 1, n, l, r - 1) < l) {
        cout << "NO\n";
      }
      else {
        cout << "YES\n";
      }
    }
    else {
      bool ok = true;
      for (int j = l; j > r; j--) {
        if (l < mn[j]) {
          ok = false;
          break;
        }
      }
      if (l < MN.get_max(1, 1, n, r + 1, l)) {
        cout << "NO\n";
      }
      else {
        cout << "YES\n";
      }
      //cout << (ok ? "YES\n" : "NO\n");
    }
  }
  /*int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    if (l == r) {
      cout << "YES\n";
      continue;
    }
    if (l < r) {
      bool ok = true;
      for (int j = l; j < r; j++) {
        if (l > mx[j]) {
          ok = false;
          break;
        }
      }
      cout << (ok ? "YES\n" : "NO\n");
    }
    else {
      bool ok = true;
      for (int j = l; j > r; j--) {
        if (l < mn[j]) {
          ok = false;
          break;
        }
      }
      cout << (ok ? "YES\n" : "NO\n");
    }
  }*/
} 
Compilation message (stderr)
long_mansion.cpp: In function 'int main()':
long_mansion.cpp:142:12: warning: variable 'ok' set but not used [-Wunused-but-set-variable]
       bool ok = true;
            ^~
long_mansion.cpp:157:12: warning: variable 'ok' set but not used [-Wunused-but-set-variable]
       bool ok = true;
            ^~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |