#include<bits/stdc++.h>
using namespace std;
#define N 4000005
#define M 35000
#define ll long long
#define ull unsigned long long
int read(){
	int w=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9')f=c=='-'?-f:f,c=getchar();
	while(c>='0'&&c<='9')w=w*10+c-'0',c=getchar();
	return w*f;
}
ll readll(){
	ll w=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9')f=c=='-'?-f:f,c=getchar();
	while(c>='0'&&c<='9')w=w*10+c-'0',c=getchar();
	return w*f;
}
ull readull(){
	ull w=0;
	char c=getchar();
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9')w=w*10+c-'0',c=getchar();
	return w;
}
ull seed;
ull rd(ull&x){
	x^=(x<<13);
	x^=(x>>7);
	x^=(x<<17);
	return x;
}
const int mod=998244353,inv=(mod+1)>>1;
const ll M1=M,M2=1ll*M*M,M3=1ll*M*M*M;
void inc(int&x,int y){
	x+=y,x=x>=mod?x-mod:x;
}
int reduce(int x){
	return x>=mod?x-mod:x;
}
int pown(int x,ll y){
	int res=1;
	while(y){
		if(y&1)res=1ll*res*x%mod;
		x=1ll*x*x%mod,y>>=1;
	}
	return res;
}
int Tid,T,n,q,m,rm,a[N],a0,mn[N],nx[N],tk[N],tb[N],tsk,tsb,cics,cice,sum,vis[N],wk[N],p[4][M],ip[4][M];
ll km;
bool typ[N];
int pown(ll x){
	return 1ll*p[3][x/M3]*p[2][x%M3/M2]%mod*p[1][x%M2/M1]%mod*p[0][x%M1]%mod;
}
int ipown(ll x){
	return 1ll*ip[3][x/M3]*ip[2][x%M3/M2]%mod*ip[1][x%M2/M1]%mod*ip[0][x%M1]%mod;
}
void init(){
	int x=1,k=1,b=0,ct=0;
	vis[1]=++ct,mn[ct]=0,nx[ct]=x;
	while(1){
		if(x<=n)x<<=1,b=reduce(b<<1),typ[ct]=0;
		else x=(x<<1)-(n<<1),b=reduce(b<<1),inc(b,2),typ[ct]=1;
		if(vis[x])break;
		vis[x]=++ct,mn[ct]=b,nx[ct]=x;
	}
	cics=vis[x],a0=mn[cics-1],k=1,b=0,cice=ct;
	for(int i=cics;i<=cice;i++){
		if(typ[i-1])k=reduce(k<<1),b=reduce(b<<1),inc(b,2);
		else k=reduce(k<<1),b=reduce(b<<1);
		tk[i-cics]=k,tb[i-cics]=b;
		// cout<<"kb"<<k<<' '<<b<<endl;
	}
	// cout<<k<<' '<<b<<endl;
	if(k==1)tsk=1,tsb=1ll*b*(cice-cics+1)%mod;
	else{
		int c=1ll*b*pown(k-1,mod-2)%mod;
		inc(a0,c),tsk=k,tsb=reduce(mod-c);
	}
	rm=n*2-m+1;
	memset(wk,-1,sizeof(int)*(n<<1|1));
}
int getmn(ll x){
	if(x<cics)return mn[x];
	x-=cics;
	ll d=x/(cice-cics+1),r=x%(cice-cics+1);
	// if(!r)return (1ll*pown(tsk,d)*a0+tsb)%mod;
	return ((1ll*pown(d*(cice-cics+1))*a0+tsb)%mod*tk[r]+tb[r])%mod;
}
int getnx(ll x){
	if(x<cics)return nx[x];
	return nx[(x-cics)%(cice-cics+1)+cics];
}
int work(int x){
	if(wk[x]!=-1)return wk[x];
	int now=n<<1,l,r,res=0;
	// cout<<m<<' '<<rm<<endl;
	for(int i=1;i<=n;i++){
		l=now+1,r=now+x,now=r<=n*2?r:r-n*2;
		if(l<=m&&m<=r)inc(res,a[i]);
		if(l<=rm&&rm<=r)inc(res,a[i]);
		if(l<=n*2+m&&n*2+m<=r)inc(res,a[i]);
		if(l<=n*2+rm&&n*2+rm<=r)inc(res,a[i]);
		// cout<<"lr"<<l<<' '<<r<<endl;
	}
	return wk[x]=res;
}
int solve(ll x){
	// cout<<"mn"<<getmn(x)<<endl;
	// cout<<"nx"<<getnx(x)<<endl;
	return (1ll*sum*getmn(x)+work(getnx(x)))%mod;
}
void clear(){
	memset(vis,0,sizeof(int)*(n<<1|1));
	memset(wk,0,sizeof(int)*(n<<1|1));
	sum=0;
}
void Init(){
	p[0][0]=1;
	for(int i=1;i<M;i++)
		p[0][i]=reduce(p[0][i-1]<<1);
	for(int i=1;i<4;i++){
		p[i][0]=1;
		p[i][1]=1ll*p[i-1][M-1]*p[i-1][1]%mod;
		for(int j=2;j<M;j++)
			p[i][j]=1ll*p[i][j-1]*p[i][1]%mod;
	}
	ip[0][0]=1;
	for(int i=1;i<M;i++)
		ip[0][i]=1ll*ip[0][i-1]*inv%mod;
	for(int i=1;i<4;i++){
		ip[i][0]=1;
		ip[i][1]=1ll*ip[i-1][M-1]*ip[i-1][1]%mod;
		for(int j=2;j<M;j++)
			ip[i][j]=1ll*ip[i][j-1]*ip[i][1]%mod;
	}
}
signed main(){
	Init();
	// freopen("noodle_ex1.in","r",stdin);
	freopen("noodle.in","r",stdin);
	freopen("noodle.out","w",stdout);
	Tid=read(),T=read(),seed=readull();
	while(T--){
		n=read(),q=read(),m=read(),km=readll();
		for(int i=1;i<=n;i++)
			a[i]=read(),inc(sum,a[i]);
		init();
		// cout<<cics<<' '<<cice<<endl;
		// cout<<a0<<' '<<tsk<<' '<<tsb<<endl;
		// for(int i=1;i<=10;i++)
		// 	cout<<getmn(i)<<" \n"[i==10];
		ull ans=0;
		for(int i=1;i<=q;i++){
			ll k=rd(seed)%km;
			// ll k=readll();
			ull tmp=1llu*solve(k+1)*ipown(k)%mod;
			ans^=tmp*i;
			// cout<<k<<' '<<tmp<<endl;
		}
		printf("%llu\n",ans);
		// puts("---------------");
		clear();
	}
	return 0;
}