#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 18 | 7, M = 18;
#define LL long long
int n, m, t, a[N], aa[N], c[N];
int b[N], r[N], len[N];
LL sum[N];
char s[M][N];
void build()
{
for (int i = 0; i < t; ++i)
{
r[i + t] = 0;
b[i + t] = i + 1;
len[i + t] = 1;
sum[i + t] = i + 1;
}
for (int i = t - 1; i >= 1; --i)
{
r[i] = r[i << 1] + 1;
b[i] = a[b[i << 1]] >= r[i] ? b[i << 1] : b[i << 1 | 1];
len[i] = len[i << 1] + len[i << 1 | 1];
sum[i] = sum[i << 1] + sum[i << 1 | 1];
}
}
LL qry(int x, int v, int k)
{
LL res = 0;
while (len[x] > 1)
{
if (k > len[x << 1])
{
if (a[b[x << 1]] >= max(v, r[x]))
return b[x << 1];
k -= len[x << 1];
x = x << 1 | 1;
}
else
{
res += sum[x << 1 | 1];
v = v == 0 ? r[x] : v;
x = x << 1;
}
}
return a[b[x]] >= v ? b[x] : res;
}
void solve()
{
build();
LL ans = 0;
for (int i = 1, x; i <= m; ++i)
{
for (x = 0; (1 << x) < c[i]; ++x)
;
LL v = ((1 << x) == c[i]) ? b[t / c[i]] : qry(t / (1 << x), 0, c[i]);
ans ^= i * v;
}
printf("%lld\n", ans);
}
int main()
{
freopen("in.txt", "r", stdin);
freopen("std.txt", "w", stdout);
// freopen("arena.in", "r", stdin);
// freopen("arena.out", "w", stdout);
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i)
scanf("%d", &aa[i]);
for (int i = 1; i <= m; ++i)
scanf("%d", &c[i]);
for (t = n; t & (t - 1); t += t & -t)
;
for (int i = 1; (1 << i) <= t; ++i)
scanf("%s", s[i]);
int cases;
scanf("%d", &cases);
while (cases--)
{
int x[4];
scanf("%d%d%d%d", &x[0], &x[1], &x[2], &x[3]);
for (int i = 1; i <= n; ++i)
a[i] = aa[i] ^ x[i & 3];
solve();
}
return 0;
}