#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; } // a*X+b*Y==cとなる自然数のペア(X,Y)について、 // X = x*c + b*k, Y = (c - a*X)/bとなるkの範囲[mn,mx]を返す(個数はn-m+1個) pair first_order_indeterminate_equation(ll a, ll b, ll c, ll g, ll x, ll y){ if(c%g != 0) return make_pair(1,0); a /= g; b /= g; c /= g; ll t = c/a; ll n = (t - x * c)/b; if((t - x * c) % b < 0) n--; ll m = (- x * c + b-1)/b; if((- x * c + b-1) % b < 0) m--; /* cout << a << " " << b << " " << c << endl; for(ll k=m; k<=n; k++){ ll X = x*c+b*k; ll Y = (c-a*X)/b; cout << "(" << X << " " << Y << ")" << endl; } */ return make_pair(m, n); } 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++){ pair res = first_order_indeterminate_equation(N, K, Y-z*H, g, x, y); ret += res.second - res.first + 1; ret %= MOD; } cout << ret << endl; } int main(){ int t; cin >> t; rep(i,t){ solve(); } return 0; }