#include #include #include #include #include #include #include #include #define ll long long #define inf 1000000000 using namespace std; int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } const int _K=50268147,_B=6082187,_P=100000007; int _X; inline int get_rand(int _l,int _r){ _X=((long long)_K*_X+_B)%_P; return _X%(_r-_l+1)+_l; } int T; int n,m,k,seed; int x[1000006],y[1000006]; bool p[1000005],q[1000005]; void Init(){ scanf("%d%d%d%d",&n,&m,&k,&seed); _X=seed; for (int i=1;i<=k;++i) x[i]=get_rand(1,n), y[i]=get_rand(1,m); } int main() { T=read(); while(T--) { Init(); memset(p,0,sizeof(p)); memset(q,0,sizeof(q)); for(int i=1;i<=k;i++) p[x[i]]=1,q[y[i]]=1; int ans1=0,ans2=0; for(int i=1;i<=n;i++) if(!p[i])ans1++; for(int i=1;i<=m;i++) if(!q[i])ans2++; cout<