#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5 , S = (1 << 17) + 5;
int n , m , k , a[S] , aa[S] , c[N] , rd[S] , d[S] , w[S * 2];
bool ex[S]; ll sum[S * 2] , ans[S];
void Init(int x)
{
if(x >= (1 << k))
{
int i = x - (1 << k) + 1;
if(__builtin_popcount(i) > 1)ex[i] = 1;
return ;
}
int lc = x << 1 , rc = lc + 1;
int cnt = __builtin_popcount(x);
if(!d[x])
{
Init(lc);
if(a[w[lc]] < rd[x])Init(rc);
}
else
{
Init(lc);
Init(rc);
}
}
void Dfs1(int x , ll st , ll add)
{
if(x >= (1 << k))
{
int i = x - (1 << k) + 1;
if(ex[i])
{
if(st == -1)ans[i] = i + add;
else ans[i] = st + add;
}
return ;
}
int lc = x << 1 , rc = lc + 1;
int cnt = __builtin_popcount(x);
if(st != -1)
{
Dfs1(lc , st , add);
Dfs1(rc , st , add);
}
else
{
if(!d[x])
{
if(a[w[lc]] >= rd[x])
{
Dfs1(rc , w[lc] , add);
}
else
{
Dfs1(rc , -1 , add);
}
Dfs1(lc , -1 , sum[rc] * (cnt > 1 && (lc < (1 << k))) + add);
}
else
{
Dfs1(rc , -1 , add + w[lc]);
Dfs1(lc , -1 , add + sum[rc] * (cnt > 1));
}
}
}
void Dfs2(int x , ll st , ll add)
{
if(x >= (1 << k))
{
int i = x - (1 << k) + 1;
if(!ex[i])
{
if(st == -1)ans[i] = i + add;
else ans[i] = st + add;
}
return ;
}
int lc = x << 1 , rc = lc + 1;
int cnt = __builtin_popcount(x);
if(st != -1)
{
Dfs2(lc , st , add);
Dfs2(rc , st , add);
}
else
{
if(!d[x])
{
if(a[w[lc]] >= rd[x])
{
Dfs2(rc , w[lc] , add);
Dfs2(lc , -1 , add);
}
else
{
Dfs2(rc , -1 , add);
if(cnt > 1)Dfs2(lc , sum[rc] , add);
else Dfs2(lc , -1 , add);
}
}
else
{
if(a[w[rc]] >= rd[x])
{
Dfs2(rc , -1 , add);
}
else
{
Dfs2(rc , w[lc] , add);
}
Dfs2(lc , -1 , sum[rc] * (cnt > 1) + add);
}
}
}
int Solve()
{
int x[4] = {};
for(int i = 0 ; i < 4 ; i++)cin >> x[i];
for(int i = 1 ; i <= n ; i++)a[i] = aa[i] ^ x[i % 4];
for(int i = n + 1 ; i <= (1 << k) ; i++)a[i] = -1;
for(int i = 0 ; i < (1 << k) ; i++)
sum[i + (1 << k)] = w[i + (1 << k)] = i + 1;
for(int i = (1 << k) - 1 ; i >= 1 ; i--)
{
rd[i] = k - __lg(i);
int j = (i << 1) | d[i];
if(a[w[j]] < rd[i])w[i] = w[j ^ 1];
else w[i] = w[j];
sum[i] = sum[i << 1] + sum[i << 1 | 1];
}
memset(ex , 0 , sizeof ex);
Init(1);
Dfs1(1 , -1 , 0);
Dfs2(1 , -1 , 0);
ll res = 0;
for(int i = 1 ; i <= m ; i++)
res ^= (ll)i * ans[c[i]];
cout << res << "\n";
return 0;
}
int main()
{
freopen("arena.in" , "r" , stdin);
freopen("arena.out" , "w" , stdout);
ios::sync_with_stdio(0);
cin.tie(0) , cout.tie(0);
cin >> n >> m;
for(int i = 1 ; i <= n ; i++)cin >> a[i];
for(int i = 1 ; i <= m ; i++)cin >> c[i];
for(k = 0 ; (1 << k) < n ; k++);
for(int i = 1 ; i <= k ; i++)
{
int pw = (1 << (k - i));
string s; cin >> s;
for(int j = 0 ; j < pw ; j++)
d[pw + j] = s[j] - '0';
}
int T; cin >> T;
while(T--)Solve();
return 0;
}