import sys input = sys.stdin.readline from math import gcd def extGCD(a, b): # ax + by = gcd(a, b)の解を求める if b == 0: return 1, 0 q = a // b r = a % b X, Y = extGCD(b, r) return Y, X - q * Y T = int(input()) mod = 10**9 + 7 for _ in range(T): N, K, H, Y = map(int, input().split()) A = [N, K, H] A.sort() g = gcd(A[0], A[1]) ans = 0 for i in range(10 ** 6 + 5): C = Y - i * A[-1] if C < 0: break if C % g != 0: continue c = C // g n, k = A[0]//g, A[1]//g p, q = extGCD(n, k) ma = c * q // n mi = (-c * p + k - 1)//k ans += max(0, (ma - mi) + 1) ans %= mod # print("test", i, p, q, C, g) print(ans)