#include<bits/stdc++.h>
using namespace std;
template<typename T>inline void read(T &x,bool op=0){
x=0;int f=1;char c=getchar();
while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
if(op) return x=c^48,void();
while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
x*=f;
}
const int N=2e5+5,R=2147483647;
int T,K,n,m,a[N],b[N],c[N],nn,x[4];
vector<int> d[20];
struct node{int l,r,i;};
vector<node> t[N<<1];
long long sum[N<<1],val[N];
void solve(int p,int l,int r,int dep,int id){
if(l==r){
t[p].resize(1);
t[p][0]=(l<=nn?(node){a[l],a[l],l}:(node){0,R,l});
sum[p]=l;
return;
}
int mid=(l+r)>>1;
solve(2*p,l,mid,dep-1,id*2),solve(2*p+1,mid+1,r,dep-1,id*2+1);
t[p].clear();sum[p]=0;
vector<node> &ls=t[2*p],&rs=t[2*p+1];
if(d[dep][id]) swap(ls,rs);
bool ok=0;
for(node s:ls){
if(s.r>=dep) t[p].emplace_back((node){max(s.l,dep),s.r,s.i}),sum[p]+=s.i;
if(s.l<dep) ok=1;
}
if(ok) t[p].insert(t[p].end(),rs.begin(),rs.end()),sum[p]+=(d[dep][id]?sum[2*p]:sum[2*p+1]);
if(l==1) val[r]=sum[p];
}
bool sub=1;
inline void SUB(){
while(T--){
read(x[0]),read(x[1]),read(x[2]),read(x[3]);
for(int i=1;i<=n;++i) a[i]=b[i]^x[i%4];
nn=n;
solve(1,1,1<<K,K,0);
long long ans=0;
val[1]=1;
for(int i=1;i<=m;++i) ans^=val[c[i]]*i;
printf("%lld\n",ans);
}
}
int main(){
freopen("arena.in","r",stdin);
freopen("arena.out","w",stdout);
read(n),read(m);
for(int i=1;i<=n;++i) read(b[i]);
for(int i=1;i<=m;++i) read(c[i]),sub&=(c[i]==(c[i]&-c[i]));
K=__lg(n-1)+1;
for(int i=1;i<=K;++i){
int le=1<<(K-i);
d[i].resize(le);
for(int j=0;j<le;++j) read(d[i][j],1);
}
read(T);
if(sub) return SUB(),0;
while(T--){
read(x[0]),read(x[1]),read(x[2]),read(x[3]);
for(int i=1;i<=n;++i) a[i]=b[i]^x[i%4],val[i]=-1;
val[1]=1;
long long ans=0;
for(int i=1;i<=m;++i){
nn=c[i];
if(~val[nn]){ans^=val[nn]*i;continue;}
int k=__lg(nn-1)+1;
solve(1,1,1<<k,k,0);
val[nn]=sum[1];
ans^=sum[1]*i;
}
printf("%lld\n",ans);
}
return 0;
}