using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static long[] NList => ReadLine().Split().Select(long.Parse).ToArray(); public static void Main() { Solve(); } static void Solve() { var t = NN; var ans = new long[t]; for (var u = 0; u < t; ++u) { var c = NList; ans[u] = Count(c[0], c[1], c[2], c[3]); } WriteLine(string.Join("\n", ans)); } static long Count(long n, long k, long h, long y) { if (h >= n && h >= k) return Count2(n, k, h, y); if (n >= k && n >= h) return Count2(k, h, n, y); return Count2(h, n, k, y); } static long Count2(long n, long k, long h, long y) { var ans = 0L; var mod = 1_000_000_007; var g = XGcd(n, k); var da = k / g.g; var db = n / g.g; for (var c = 0L; c * h <= y; ++c) { var p = y - c * h; if (p % g.g == 0) { var a = g.x * (p / g.g); var b = g.y * (p / g.g); if (a < 0) { var d = (-a + da - 1) / da * da; a += d; b -= d / da * db; } else if (b < 0) { var d = (-b + db - 1) / db * db; a -= d / db * da; b += d; } if (a >= 0 && b >= 0) { ans = (ans + a / da + b / db + 1) % mod; } } } return ans; } static long GCD(long a, long b) { if (a < b) return GCD(b, a); if (a % b == 0) return b; return GCD(b, a % b); } // 拡張ユークリッド互除法 ax + by = gcd(a, b) を満たす x, y を求める public static (long g, long x, long y) XGcd(long a, long b) { long x0 = 1, y0 = 0, x1 = 0, y1 = 1; while (b != 0) { var q = a / b; var prevA = a; a = b; b = prevA % b; var prevX0 = x0; var prevY0 = y0; x0 = x1; x1 = prevX0 - q * x1; y0 = y1; y1 = prevY0 - q * y1; } return (a, x0, y0); } }