#include using namespace std; // Correct but intentionally too slow. // It copies the whole array for every independent subproblem and processes // every range element one by one: O(QN + sum(q_i)N) in the worst case. static int clamp_index(uint32_t value, int n) { if (value < 1) return 1; if (value > static_cast(n)) return n; return static_cast(value); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); constexpr uint32_t MASK = (uint32_t(1) << 30) - 1; int N, M; cin >> N >> M; vector initial(N + 1); for (int i = 1; i <= N; ++i) cin >> initial[i]; vector l(M + 1), r(M + 1), x(M + 1), L(M + 1), R(M + 1); for (int i = 1; i <= M; ++i) cin >> l[i]; for (int i = 1; i <= M; ++i) cin >> r[i]; for (int i = 1; i <= M; ++i) cin >> x[i]; for (int i = 1; i <= M; ++i) cin >> L[i]; for (int i = 1; i <= M; ++i) cin >> R[i]; int Q; cin >> Q; for (int i = 1; i <= Q; ++i) { int s, q; cin >> s >> q; // This O(N) copy alone makes the maximum tests infeasible. vector a = initial; uint32_t y = static_cast(i); for (int j = 1; j <= q; ++j) { const int z = ((s + j) % M) + 1; const int u = clamp_index(l[z] ^ y, N); const int v = clamp_index(r[z] ^ y, N); const int ql = min(u, v); const int qr = max(u, v); const int U = clamp_index(L[z] ^ y, N); const int V = clamp_index(R[z] ^ y, N); const int sum_l = min(U, V); const int sum_r = max(U, V); const uint32_t update_mask = x[z] ^ y; if ((z & 1) == 0) { for (int k = ql; k <= qr; ++k) a[k] |= update_mask; } else { for (int k = ql; k <= qr; ++k) a[k] &= update_mask; } uint64_t sum = 0; for (int k = sum_l; k <= sum_r; ++k) sum += a[k]; y = static_cast(sum) & MASK; } cout << y << '\n'; } return 0; }