#include using namespace std; const int _K=50268147,_B=6082187,_P=100000007; int _X,T,f1[1000006],f2[1000006]; inline int get_rand(int _l,int _r){ _X=((long long)_K*_X+_B)%_P; return _X%(_r-_l+1)+_l; } int n,m,k,seed; int x[1000006],y[1000006]; 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),f1[x[i]]=true, y[i]=get_rand(1,m),f2[y[i]]=true; } int main() { int num1=0,num2=0; scanf("%d",&T); while (T--) { memset(f1,0,sizeof(f1)),memset(f2,0,sizeof(f2)),Init(),num1=0,num2=0; for (int i=1;i<=n;++i) if (!f1[i]) num1++; for (int i=1;i<=m;++i) if (!f2[i]) ++num2; cout<