#include using namespace std; #define int long long const int MOD = 1000000007; int extGCD(int a, int b, int &x, int &y) { if (b==0) { x = 1; y = 0; return a; } int d = extGCD(b, a%b, y, x); y -= a/b * x; return d; } signed main() { int T; cin >> T; int x, y; while (T--) { int N, K, H, Y; cin >> N >> K >> H >> Y; int M = max({N, K, H}); int c = Y/M; int ans = 0; for (int i=0; i<=c; i++) { int nokori = Y-i*M; if (M==N) { int G = extGCD(K, H, x, y); if (nokori%G==0) { x *= nokori/G; y *= nokori/G; ans += y/K-(-x+H-1)/H+1; } } else if (M==K) { int G = extGCD(H, N, x, y); if (nokori%G==0) { x *= nokori/G; y *= nokori/G; ans += y/H-(-x+N-1)/N+1; } } else { int G = extGCD(N, K, x, y); if (nokori%G==0) { x *= nokori/G; y *= nokori/G; ans += y/N-(-x+K-1)/K+1; } } ans %= MOD; } cout << ans << endl; } }