#include using ull = unsigned long long; using usize = std::size_t; int main() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false); usize N; std::cin >> N; std::vector h(N + 1, 0ULL); for (usize i = 1; i <= N; i++) { std::cin >> h[i]; } const usize L = 2 * N; std::vector a(N + 1, 0), b(N + 1, 0), c(N + 1, L), d(N + 1, L), e(N + 1, 0); std::vector> lr(L + 1), du(L + 1); // std::vector lr(L + 1, 0), du(L + 1, 0); // 左右矢印,上下矢印 for (usize i = 1; i <= N; i++) { std::cin >> a[i] >> b[i] >> c[i] >> d[i] >> e[i]; lr[++a[i]].push_back(int(i)); du[++b[i]].push_back(int(i)); lr[++c[i]].push_back(-int(i)); du[++d[i]].push_back(-int(i)); } usize Q; std::cin >> Q; std::vector p(Q), q(Q); for (usize i = 0; i < Q; i++) { std::cin >> p[i] >> q[i], p[i]++, q[i]++; } constexpr usize BS = 800; std::vector inds(Q); std::iota(inds.begin(), inds.end(), 0UL); std::sort(inds.begin(), inds.end(), [&](const usize i, const usize j) { const usize bi = p[i] / BS, bj = p[j] / BS; return bi != bj ? bi < bj : q[i] != q[j] ? (q[i] < q[j]) ^ (bi % 2) : p[i] < p[j]; }); std::vector color_cnt(N + 1, 0); ull hash = 0; auto add_film = [&](const usize i) { // フィルムiを追加 const usize c = e[i]; if (color_cnt[c]++ == 0) { hash ^= h[c]; } }; auto del_film = [&](const usize i) { // フィルムiを削除 const usize c = e[i]; if (--color_cnt[c] == 0) { hash ^= h[c]; } }; auto add_del_film = [&](const int i) { if (i > 0) { add_film(usize(i)); } if (i < 0) { del_film(usize(-i)); } }; std::vector answer(Q); usize x = 0, y = 0; auto von = [&](const usize x, const usize y) { std::vector ans; for (const int f : lr[x]) { const usize yl = b[(usize)std::abs(f)], yu = d[(usize)std::abs(f)]; if (yl <= y and y < yu) { ans.push_back(f); } } return ans; }; auto hon = [&](const usize x, const usize y) { std::vector ans; for (const int f : du[y]) { const usize xl = a[(usize)std::abs(f)], xu = c[(usize)std::abs(f)]; if (xl <= x and x < xu) { ans.push_back(f); } } return ans; }; for (const usize i : inds) { const usize nx = p[i], ny = q[i]; // 次のクエリ点 for (; x < nx; x++) { for (const int f : von(x + 1, y)) { add_del_film(f); } } for (; x > nx; x--) { for (const int f : von(x, y)) { add_del_film(-f); } } for (; y < ny; y++) { for (const int f : hon(x, y + 1)) { add_del_film(f); } } for (; y > ny; y--) { for (const int f : hon(x, y)) { add_del_film(-f); } } answer[i] = hash; } for (usize i = 0; i < Q; i++) { std::cout << answer[i] << "\n"; } return 0; }