#include const long long mod = 1e9+7; long long g0; long long first[1000005]; long long exgcd(long long a,long long b,long long& x,long long& y){ if(b==0){ x = 1, y = 0; return a; } else{ long long g = exgcd(b,a%b,x,y); long long t = x; x = y; y = t-(a/b)*x; return g; } } long long gcd(long long a,long long b){ return b?gcd(b,a%b):a; } //pair that ax+by = k, mod long long countPair(long long a,long long b,long long k){ long long left = k%b; if(first[left]==-1) return 0; else{ long long s = first[left]*a; long long step = a*b/g0; if(s>k) return 0; else return ((k-s)/step+1ll)%mod; } } void preWork(long long a,long long b){ for(int i = 0; i < b; i++) first[i] = -1; first[0] = 0; long long ptr = 1; while(true){ long long left = a*ptr%b; if(first[left]==-1) first[left] = ptr; else break; ptr++; } } int main(){ int T; scanf("%d",&T); while(T--){ long long n,k,h; long long y; scanf("%lld%lld%lld%lld",&n,&k,&h,&y); preWork(k,h); g0 = gcd(k,h); long long ans = 0; for(int i = 0; i*1ll*n<=y; i++){ long long add = countPair(k,h,y-i*1ll*n); // printf("left = %lld, add = %lld\n",y-i*1ll*n,add); ans += add; if(ans >= mod) ans -= mod; } printf("%lld\n",ans); } return 0; }