import sys import io, os input = sys.stdin.buffer.readline t = int(input()) mod = 10**9+7 def extgcd(a, b): # ax+by = gcd(x, y)の整数解を求める # x, y, gcd(a, b)を返す if b == 0: return 1, 0, a q, r = divmod(a, b) x, y, d = extgcd(b, r) s, t = y, x-q*y return s, t, d import math for _ in range(t): n, k, h, y = map(int, input().split()) n, k, h = sorted([n, k, h], reverse=True) g = math.gcd(k, h) a, b = k//g, h//g x0, y0, _ = extgcd(a, b) ans = 0 for i in range(y//n+1): c = y-n*i if c%g: continue c = c//g ans += (c*y0)//a-(-c*x0+b-1)//b+1 ans %= mod print(ans)