import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); for (int i = 0; i < t; i++) { String[] sa = br.readLine().split(" "); int[] a = new int[3]; for (int j = 0; j < 3; j++) { a[j] = Integer.parseInt(sa[j]); } long y = Long.parseLong(sa[3]); Arrays.sort(a); long ans = 0; long e = y / a[2]; for (long j = 0; j <= e; j++) { long z = y - a[2] * j; long[] c = crt(new long[] {z, 0}, new long[] {a[0], a[1]}); if (c[1] > 0) { long x = z - c[0]; if (x >= 0) { ans += x / c[1] + 1; } } } System.out.println(ans); } br.close(); } /** *
* 中国剰余定理 * x≡r[i] (mod m[i])を解く。 * O(n log lcm(m[i])) * * ■制約 * |r|=|m| * 1≦m[i] * lcm(m[i])がlongに収まる。 * * ■戻り値 * x≡y (mod z) となる{y, z} (0≦y<z=lcm(m[i])) * 答えがない場合は{0, 0} * n=0の時は{0, 1} ** * https://atcoder.jp/contests/acl1/submissions/16949778 * https://atcoder.jp/contests/abc186/submissions/18908892 */ static long[] crt(long[] r, long[] m) { assert r.length == m.length : "|r|=" + r.length + ", |m|=" + m.length; int n = r.length; long r0 = 0, m0 = 1; for (int i = 0; i < n; i++) { assert 1 <= m[i] : "m[" + i + "]=" + m; long r1 = r[i] % m[i]; if (r1 < 0) { r1 += m[i]; } long m1 = m[i]; if (m0 < m1) { long tmp = r0; r0 = r1; r1 = tmp; tmp = m0; m0 = m1; m1 = tmp; } if (m0 % m1 == 0) { if (r0 % m1 != r1) { return new long[] { 0, 0 }; } continue; } long[] ig = invGcd(m0, m1); long g = ig[0], im = ig[1]; long u1 = m1 / g; if ((r1 - r0) % g != 0) { return new long[] { 0, 0 }; } long x = (r1 - r0) / g % u1 * im % u1; r0 += x * m0; m0 *= u1; if (r0 < 0) { r0 += m0; } } return new long[] { r0, m0 }; } private static long[] invGcd(long a, long b) { a = a % b; if (a < 0) { a += b; } if (a == 0) { return new long[] { b, 0 }; } long s = b, t = a; long m0 = 0, m1 = 1; while (t > 0) { long u = s / t; s -= t * u; m0 -= m1 * u; long tmp = s; s = t; t = tmp; tmp = m0; m0 = m1; m1 = tmp; } if (m0 < 0) { m0 += b / s; } return new long[] { s, m0 }; } }