#include using namespace std; #define REP(i,a,n) for(int i=(a); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it) #define ALLOF(c) (c).begin(), (c).end() typedef long long ll; typedef unsigned long long ull; static const ll MOD = 1000000007; ll gcd(ll a, ll b){return (b==0?a:gcd(b,a%b));} ll extgcd(ll a, ll b, ll &x, ll &y){ ll d = a; if(b!=0){ d = extgcd(b,a%b,y,x); y -= (a/b)*x; }else{ x = 1; y = 0; } return d; } ll calc(ll a, ll b, ll c, ll g, ll x, ll y){ if(c%g != 0) return 0; a /= g; b /= g; c /= g; ll t = c/a; ll n = (t - x * c)/b; ll m = (- x * c + b-1)/b; m = max(m, 0LL); //cout << c << " " << n << " " << m << endl; return max(0LL, n-m+1); } void solve(){ ll N, K, H, Y; cin >> N >> K >> H >> Y; vector v{N,K,H}; sort(ALLOF(v)); N = v[0]; K = v[1]; H = v[2]; ll x, y; ll d = extgcd(N, K, x, y); ll g = gcd(N, K); ll ret = 0; for(int z=0; z<=Y/H; z++){ ll a = N; ll b = K; ll c = Y-z*H; ret += calc(a, b, c, g, x, y); ret %= MOD; } cout << ret << endl; } int main(){ int t; cin >> t; rep(i,t){ solve(); } return 0; }