#define rep(i,n) for(int i=0;i<(int)(n);i++) #define ALL(v) v.begin(),v.end() typedef long long ll; #include using namespace std; const int MOD=1e9+7; ll extGCD(ll a,ll b,ll &x,ll &y){ if(b==0){ x=1; y=0; return a; } ll d=extGCD(b,a%b,y,x); y-=a/b*x; return d; } int main(){ int t; cin>>t; while(t--){ ll n,k,h,y; cin>>n>>k>>h>>y; if(n>h) swap(n,h); if(k>h) swap(k,h); if(n>k) swap(n,k); int g=gcd(n,k); int ans=0; for(int i=0;i<=y/h;i++){ if((y-h*i)%g!=0) continue; ll r=(y-h*i)/g; ll a=n/g; ll b=k/g; ll p,q; if(extGCD(a,b,p,q)<0) p=-p,q=-q; p*=r,q*=r; if(q/a<-p/b) continue; if(p<=0){ ans=(ans+q/a+p/b)%MOD; if(p%b==0) ans++; } else if(q<=0){ ans=(ans+q/a+p/b)%MOD; if(q%a==0) ans++; } else{ ans=(ans+q/a+p/b+1)%MOD; } } cout<