#include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef __int128_t ll; __int128 parse(string &s) { __int128 ret = 0; for (int i = 0; i < s.length(); i++) if ('0' <= s[i] && s[i] <= '9') ret = 10 * ret + s[i] - '0'; return ret; } istream & operator >> (istream &in, __int128_t &m){ string s; cin >> s; m = parse(s); return in; } ostream &operator<<(std::ostream &dest, __int128_t value) { ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } int len = end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(ios_base::badbit); } } return dest; } const ll MOD = 1000000007; class ModInt{ public: ll v; ModInt(ll _v = 0){ if(_v >= MOD) _v %= MOD; v = _v; } ModInt operator+(ll n){ return ModInt((v+n)%MOD); } ModInt operator-(ll n){ return ModInt((v-n+MOD)%MOD); } ModInt operator*(ll n){ if(n >= MOD) n %= MOD; return ModInt((v*n)%MOD); } ModInt operator/(ll n){ return ModInt((ModInt(n).inv()*v).v%MOD); } void operator+=(ll n){ v = (v+n)%MOD; } void operator-=(ll n){ v = (v-n+MOD)%MOD; } void operator*=(ll n){ v = (v*n+MOD)%MOD; } ModInt operator+(ModInt n){ return ModInt((v+n.v)%MOD); } ModInt operator-(ModInt n){ return ModInt((v-n.v+MOD)%MOD); } ModInt operator*(ModInt n){ return ModInt((v*n.v)%MOD); } ModInt operator/(ModInt n){ return ModInt((n.inv()*v).v%MOD); } void operator+=(ModInt n){ v = (v+n.v)%MOD; } void operator-=(ModInt n){ v = (v-n.v+MOD)%MOD; } void operator*=(ModInt n){ v = (v*n.v)%MOD; } void operator=(ModInt n){ v = n.v; } bool operator==(ModInt n){ return v == n.v; } bool operator!=(ModInt n){ return v != n.v; } void operator=(ll n){ v = n; } ModInt inv(){ if(v == 1) return ModInt(1); else return ModInt(MOD-ModInt(MOD%v).inv().v*(MOD/v)%MOD); } }; ostream& operator<<(ostream& os, const ModInt& m){ os << m.v; return os; } istream & operator >> (istream &in, ModInt &m){ in >> m.v; return in; } template T gcd(T a, T b) { if (a < b) swap(a, b); while (b != 0) { T tmp = b; b = a % b; a = tmp; } return a; } template T gcd(initializer_list v) { T ans = 0; for(T x : v) ans = gcd(ans, x); return ans; } template T lcm(T a, T b) { return (a * b) / gcd(a, b); } ll tot(ll n){ ll ans = n; ll tmp = n; for(ll i = 2; i*i <= n; i++){ if(tmp%i == 0){ ans /= i; ans *= (i-1); while(tmp%i == 0){ tmp /= i; } } } if(tmp != 1){ ans /= tmp; ans *= (tmp-1); } return ans; } template T pow(T a, ll n, ll MOD) { T ans = 1; T tmp = a; for (int i = 0; i <= 60; i++) { ll m = (ll)1 << i; if (m & n) { ans *= tmp; ans %= MOD; } tmp *= tmp; tmp %= MOD; } return ans; } void solve(){ vector x(3); ll y; for(int i = 0; i < 3; i++) cin >> x[i]; cin >> y; sort(x.begin(), x.end()); ModInt ans = 0; ll g = gcd(x[0], x[1]); ll a = x[0]/g, b = x[1]/g; ll a_inv = pow(a, tot(b)-1, b); for(ll i = 0; i*x[2] <= y; i++){ ll r = y-(i*x[2]); if(r%g != 0) continue; r /= g; ll s = (r*a_inv)%b; // bで割ってs余る数が解の候補 ll s_max = r/a; if(s_max%b >= s){ ans += s_max/b+1; }else{ ans += s_max/b; } } cout << ans << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; int t; cin >> t; for(int i = 0; i < t; i++) solve(); }