#include using namespace std; string to_string(string s) { return '"' + s + '"'; } string to_string(bool b) { return b ? "true" : "false"; } template string to_string(bitset bs) { string res; for (size_t i = 0; i < N; ++i) res += '0' + bs[i]; return res; } string to_string(vector v) { string res = "{"; for (bool e : v) res += to_string(e) + ", "; return res += "}"; } template string to_string(pair p); template string to_string(C c) { string res = "{"; for (auto e : c) res += to_string(e) + ", "; return res += "}"; } template string to_string(pair p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } void debug() { cerr << '\n'; } template void debug(Head head, Tail... tail) { cerr << ' ' << to_string(head), debug(tail...); } #ifdef LOCAL #define DEBUG(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__) #else #define DEBUG(...) #endif template struct XorFenwickTree { const int n; vector t; XorFenwickTree(int _n) : n(_n), t(n + 1) {} void add(int i, T x) { for (++i; i <= n; i += i & -i) t[i] ^= x; } T sum(int i) const { T s = 0; for (; i; i -= i & -i) s ^= t[i]; return s; } }; template struct FenwickTree { const int n; vector t; FenwickTree(int _n) : n(_n), t(n + 1) {} void add(int i, T x) { for (++i; i <= n; i += i & -i) t[i] += x; } T sum(int i) const { T s = 0; for (; i; i -= i & -i) s += t[i]; return s; } T sum(int l, int r) const { return sum(r) - sum(l); } }; constexpr int th = 256; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; vector h(n); for (auto&& e : h) { cin >> e; } struct Event { bool add; int c, l, r; }; vector< vector > es(2 * n); vector cnt(n); for (int i = 0; i < n; ++i) { int a, b, c, d, e; cin >> a >> b >> c >> d >> e; --e; es[a].emplace_back(Event{true, e, b, d}); es[c].emplace_back(Event{false, e, b, d}); ++cnt[e]; } int q; cin >> q; struct Query { int id, i; }; vector< vector > qs(2 * n); for (int i = 0; i < q; ++i) { int x, y; cin >> x >> y; qs[x].emplace_back(Query{i, y}); } vector res(q); XorFenwickTree ft(2 * n); vector< map > mp(n); for (int t = 0; t < 2 * n; ++t) { for (auto e : es[t]) { if (cnt[e.c] >= th) { continue; } if (e.add) { vector v{0}; int sum = 0; for (auto p : mp[e.c]) { if (sum == 0 ^ (sum + p.second) == 0) { v.push_back(p.first); } sum += p.second; } v.push_back(2 * n); for (int i = 0; i < (int)v.size(); i += 2) { if (v[i + 1] <= e.l or e.r <= v[i]) { continue; } ft.add(max(v[i], e.l), h[e.c]); ft.add(min(v[i + 1], e.r), h[e.c]); } ++mp[e.c][e.l]; --mp[e.c][e.r]; } else { --mp[e.c][e.l]; ++mp[e.c][e.r]; vector v{0}; int sum = 0; for (auto p : mp[e.c]) { if (sum == 0 ^ (sum + p.second) == 0) { v.push_back(p.first); } sum += p.second; } v.push_back(2 * n); for (int i = 0; i < (int)v.size(); i += 2) { if (v[i + 1] <= e.l or e.r <= v[i]) { continue; } ft.add(max(v[i], e.l), h[e.c]); ft.add(min(v[i + 1], e.r), h[e.c]); } } } for (auto e : qs[t]) { res[e.id] = ft.sum(e.i + 1); } } vector idx; for (int i = 0; i < n; ++i) { if (cnt[i] >= th) { idx.push_back(i); } } vector< FenwickTree > fts(idx.size(), 2 * n); for (int t = 0; t < 2 * n; ++t) { for (auto e : es[t]) { if (cnt[e.c] < th) { continue; } e.c = lower_bound(begin(idx), end(idx), e.c) - begin(idx); if (e.add) { fts[e.c].add(e.l, 1); fts[e.c].add(e.r, -1); } else { fts[e.c].add(e.l, -1); fts[e.c].add(e.r, +1); } } for (auto e : qs[t]) { for (int k = 0; k < (int)idx.size(); ++k) { if (fts[k].sum(e.i + 1)) { res[e.id] ^= h[idx[k]]; } } } } for (auto e : res) { cout << e << '\n'; } }