""" """ import sys from sys import stdin import math def extGCD2(a,b): if b: d,y,x = extGCD(b,a % b) y -= a // b * x return d,x,y return a,1,0 def extGCD(a,b): g = math.gcd(a,b) x, y, u, v = 1, 0, 0, 1 while b: k = a // b x -= k * u y -= k * v x, u = u, x y, v = v, y a, b = b, a % b return g ,x, y TT = int(stdin.readline()) mod = 10**9+7 ans = 0 for loop in range(TT): N,K,H,Y = map(int,stdin.readline().split()) A = [N,K,H] A.sort() A.reverse() ans = 0 for i in range(0,Y // A[0] + 1): rem = Y - A[0] * i #print (rem) a,b = A[1],A[2] g,x,y = extGCD(a,b) if rem % g != 0: continue x *= rem // g y *= rem // g lcm = a * b // g p = lcm // a q = lcm // b #xをp増やし、yをq減らす #xをp減らし、yをq増やす ができる if x >= 0: move = x // p xa,ya = x - move * p , y + move * q else: move = (abs(x)+p-1) // p xa,ya = x + move * p , y - move * q if y >= 0: move = y // q xb,yb = x + move * p, y - move * q else: move = (abs(y)+q-1) // q xb,yb = x - move * p , y + move * q if ya < 0 or xb < 0: continue #print (rem , xa , ya , xb , yb) ans += max(0 , (xb - xa) // p + 1) ans %= mod print (ans)