#yuki-1358 from math import gcd def num(a, b, s): g = gcd(a, b) if s%g: return 0 if g > 1: return num(a//g, b//g, s//g) ini = s*pow(a, -1, b)%b return (s//a-ini)//b+1 mod = 10**9+7 T = int(input()) #20以下 #testcases for _ in range(T): N, K, H, Y = map(int, input().split()) g = gcd(K, H) h = gcd(N, g) if Y%h: print(0) else: if h-1: N, K, H, Y, g = N//h, K//h, H//h, Y//h, g//h rev = pow(K//g,-1, H//g) ans = 0 if g-1: i = Y*pow(N, -1, g)%g while N*i <= Y: ans += num(K, H, Y-i*N) ans %= mod i += g print(ans) else: i = 0 while i*N <= Y: ans += num(K, H, Y-i*N) ans %= mod i += 1 print(ans)