#include using namespace std; int inv(int a, int m) { int m0 = m; long long y = 0, x = 1; if(m == 1) return 0; while(a > 1) { int q = a / m, t = m; m = a % m, a = t; t = y; y = x - q * 1LL * y; x = t; } if(x < 0) x += m0; return x; } int num(long long Y, int K, int H) { int mx = Y / K; long long g = __gcd(K, H); g = __gcd(g, Y); K /= g, Y /= g, H /= g; if(__gcd(K, H) > 1) return 0; long long st = Y % H * inv(K, H); st %= H; if(st > mx) return 0; return (mx - st) / H + 1; } long long MOD = 1e9 + 7; int main() { int T; cin >> T; while(T--) { int N, K, H; if(N > K) swap(N, K); if(N > H) swap(N, H); long long Y; cin >> N >> K >> H >> Y; long long ans = 0LL; for(int i = 0; i <= Y / N; i++) { ans += num(Y - N * 1LL * i, K, H); } ans %= MOD; cout << ans << "\n"; } }