#define _USE_MATH_DEFINES #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; template using posteriority_queue = priority_queue, greater >; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; // const int MOD = 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } template void unique(vector &a) { a.erase(unique(ALL(a)), a.end()); } struct IOSetup { IOSetup() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); } } iosetup; int main() { int n; cin >> n; vector h(n); REP(i, n) cin >> h[i]; vector a(n), b(n), c(n), d(n), e(n); vector > xs, ys; REP(i, n) { cin >> a[i] >> b[i] >> c[i] >> d[i] >> e[i]; --e[i]; xs.emplace_back(a[i], i); xs.emplace_back(c[i], n + i); ys.emplace_back(b[i], i); ys.emplace_back(d[i], n + i); } int Q; cin >> Q; vector p(Q), q(Q); REP(i, Q) { cin >> p[i] >> q[i]; xs.emplace_back(p[i], n * 2 + i); ys.emplace_back(q[i], n * 2 + i); } sort(ALL(xs)); int X = 0; for (pair pr : xs) { if (pr.second >= n * 2) { p[pr.second - n * 2] = X - 1; } else { if (pr.second < n) { a[pr.second] = X; } else { c[pr.second - n] = X; } ++X; } } ++X; vector in_x(X, -1), out_x(X, -1); REP(i, n) { in_x[a[i]] = i; out_x[c[i]] = i; // cout << 'x' << i << " [" << a[i] << ',' << c[i] << "]\n"; } // REP(i, X) cout << in_x[i] << " \n"[i + 1 == X]; // REP(i, X) cout << out_x[i] << " \n"[i + 1 == X]; sort(ALL(ys)); int Y = 0; for (pair pr : ys) { if (pr.second >= n * 2) { q[pr.second - n * 2] = Y - 1; } else { if (pr.second < n) { b[pr.second] = Y; } else { d[pr.second - n] = Y; } ++Y; } } ++Y; vector in_y(Y, -1), out_y(Y, -1); REP(i, n) { in_y[b[i]] = i; out_y[d[i]] = i; // cout << 'y' << i << " [" << b[i] << ',' << d[i] << "]\n"; } int B = sqrt(X), m = (X + B - 1) / B; vector > query(m); REP(i, Q) { if (p[i] != -1 && q[i] != -1) query[p[i] / B].emplace_back(i); // cout << p[i] << ' ' << q[i] << '\n'; } vector ans(Q, 0); REP(block, m) { sort(ALL(query[block]), [&](int l, int r) { return q[l] < q[r]; }); ll hash = 0; vector cnt(n, 0); int x = block * B, y = 0; for (int idx : query[block]) { for (; y <= q[idx]; ++y) { if (in_y[y] != -1) { int film = in_y[y]; if (a[film] < x && x <= c[film]) { if (cnt[e[film]] == 0) hash ^= h[e[film]]; ++cnt[e[film]]; } } if (out_y[y] != -1) { int film = out_y[y]; if (a[film] < x && x <= c[film]) { --cnt[e[film]]; if (cnt[e[film]] == 0) hash ^= h[e[film]]; } } // cout << '(' << x << ',' << y << ") "; // REP(i, n) cout << cnt[i] << " \n"[i + 1 == n]; } for (; x <= p[idx]; ++x) { if (in_x[x] != -1) { int film = in_x[x]; if (b[film] < y && y <= d[film]) { if (cnt[e[film]] == 0) hash ^= h[e[film]]; ++cnt[e[film]]; } } if (out_x[x] != -1) { int film = out_x[x]; if (b[film] < y && y <= d[film]) { --cnt[e[film]]; if (cnt[e[film]] == 0) hash ^= h[e[film]]; } } // cout << '(' << x << ',' << y << ") "; // REP(i, n) cout << cnt[i] << " \n"[i + 1 == n]; } while (x > p[idx] + 1) { --x; if (in_x[x] != -1) { int film = in_x[x]; if (b[film] < y && y <= d[film]) { --cnt[e[film]]; if (cnt[e[film]] == 0) hash ^= h[e[film]]; } } if (out_x[x] != -1) { int film = out_x[x]; if (b[film] < y && y <= d[film]) { if (cnt[e[film]] == 0) hash ^= h[e[film]]; ++cnt[e[film]]; } } // cout << '(' << x << ',' << y << ") "; // REP(i, n) cout << cnt[i] << " \n"[i + 1 == n]; } ans[idx] = hash; } } REP(i, Q) cout << ans[i] << '\n'; return 0; }