#include using namespace std; tuple extgcd(long long a, long long b) { if (b == 0) return {a, 1, 0}; auto [d, x, y] = extgcd(b, a % b); return {d, y, x - a / b * y}; } long long floordiv(long long a, long long b) { assert(b != 0); if (b < 0) a = -a, b = -b; if (a >= 0) return a / b; else return (a - b + 1) / b; } long long ceildiv(long long a, long long b) { assert(b != 0); if (b < 0) a = -a, b = -b; if (a >= 0) return (a + b - 1) / b; else return a / b; } const int MOD = 1000000007; void solve() { vector a(3); for (int i = 0; i < 3; ++i) cin >> a[i]; long long y; cin >> y; sort(a.begin(), a.end()); auto [g, s, t] = extgcd(a[0], a[1]); a[0] /= g; a[1] /= g; long long ans = 0; for (int z = 0; z <= 1000000; ++z) { long long ny = y - a[2] * z; if (ny < 0) break; if (ny % g != 0) continue; ny /= g; long long lower = ceildiv(-s * ny, a[1]), upper = floordiv(t * ny, a[0]); ans += upper - lower + 1; ans %= MOD; } cout << ans << '\n'; } int main() { int t; cin >> t; for (int i = 0; i < t; ++i) solve(); }