#include<bits/stdc++.h>
#define N 2000005
#define ll unsigned long long
#define P 998244353
using namespace std;
int test,T,n,q,x,a[N],ni,nm,ans[N];
ll seed,m;
int fpow(int a,int b){
	int res=1;
	while(b){
		if(b&1)res=1ll*res*a%P;
		a=1ll*a*a%P;
		b>>=1;
	}return res;
}
int rd(){
	int x=0;char c=getchar();bool C=1;
	while(c<48||c>57){if(c=='-')C=0;c=getchar();}
	while(c>=48&&c<=57){x=(x<<3)+(x<<1)+(c^48);c=getchar();}
	return C?x:-x;
}
struct z{
	int a[1005][1005];
}h[64],pre,now;
int d[1005][1005];
void solve(z &b,z &c){
	int n=nm;
	for(int i=1;i<=n;++i)for(int j=1;j<=n;++j)d[i][j]=0;
	for(int i=1;i<=n;++i)for(int j=1;j<=n;++j)for(int k=1;k<=n;k++)d[i][j]=(d[i][j]+1ll*b.a[i][k]*c.a[k][j])%P;
	for(int i=1;i<=n;++i)for(int j=1;j<=n;++j)c.a[i][j]=d[i][j];
}
int g[N],tmp[N],o[N];
void init(int t){
	ni=fpow(2,P-2);
	for(int j=1,i=1;i<=n;i+=2,++j){
		tmp[i]=tmp[i+1]=1ll*(a[j]+a[n-j+1])*ni%P;
	}for(int i=1;i<=n;++i)a[i]=tmp[i];
	ans[t]=a[x];
}
ll nex(ll &x){
	x^=(x<<13);
	x^=(x>>7);
	x^=(x<<17);
	return x;
}
int main(){
	freopen("noodle.in","r",stdin);
	freopen("noodle.out","w",stdout);
	test=rd(),T=rd(),scanf("%llu",&seed);
	while(T--){
		n=rd();nm=0;
		q=rd(),x=rd();
		scanf("%llu",&m);
		for(int i=1;i<=n;++i)a[i]=rd();ans[0]=a[x];
		for(int i=1;i<=25;i++)init(i);
		if(m<=25){
			ll res=0;
			for(int i=1;i<=q;++i){
				ll k=nex(seed)%m;
				res^=1ull*ans[k]*i;
			}cout<<res<<"\n";
			continue;
		}
		for(int i=1;i<=n;++i){
			if(i==1||a[i]!=a[i-1])nm++;
			g[i]=nm;o[nm]=a[i];
		}
		for(int i=1;i<=n;++i){
			if(i==1||a[i]!=a[i-1])h[0].a[g[i]][g[i/2+1]]+=ni,h[0].a[g[i]][g[n-i/2]]+=ni;
		}for(int i=1;i<=62;i++)h[i]=h[i-1],solve(h[i-1],h[i]);
		ll res=0;
		for(int i=1;i<=q;++i){
			ll k=nex(seed)%m;
			if(k<=25)res^=1ull*ans[k]*i;
			else {
				k-=25;
				for(int j=1;j<=nm;++j)for(int k=1;k<=nm;++k)now.a[j][k]=(j==k?1:0);
				for(int j=62;j>=0;--j)if(k&(1ull<<j))solve(h[j],now);
				ll ans=0;
				for(int i=1;i<=nm;++i)ans=(ans+1ull*now.a[g[x]][i]*o[i])%P;
				res^=ans*i;
			}
		}cout<<res<<"\n";
	}
}
