#include #include #define N 500005 using namespace std; int test,i,j,k,l,s,n,m,f[N][11],last[N],next[N],to[N],tot; int a,bb,x,q[N],r,fa[N],d[N][11],ans[N],g[N]; inline void build() { int l=0,r=1; q[1]=1; while (l'9') c=getchar(); while (c>='0'&&c<='9') s=s*10+c-48,c=getchar(); return s; } int main() { scanf("%d",&test); while (test--) { scanf("%d%d%d%d",&n,&m,&a,&bb); tot=0; for (i=1;i<=n;i++) for (j=0;j<=m;j++) f[i][j]=d[i][j]=0; for (i=1;i<=n;i++) { f[i][0]=1; last[i]=ans[i]=0; } for (i=2;i<=n;i++) { x=(1ll*a*i+bb)%(i-1)+1; add(x,i); } build(); for (i=n;i;i--) for (j=last[q[i]];j;j=next[j]) for (k=0;k