def extgcd(a, b): # ax + by = gcd(a, b) となる(x, y, gcd(a, b)) if a == 0: return (0, 1, b) (X, Y, G) = extgcd(b % a, a) return (Y - b // a * X, X, G) T = int(input()) for case in range(T): P = list(map(int, input().split())) Y = P[3] P = P[:3] P.sort(reverse = True) ans = 0 for i in range((Y // P[0]) + 1): R = Y - i * P[0] x, y, g = extgcd(P[1], P[2]) if R % g != 0: continue K = P[1] H = P[2] K //= g H //= g R //= g x *= R y *= R if x >= 0 and y >= 0: ans += 1 if x * y != 0: ans += min(H // x, K // y) else: if x * y == 0: if x + y < 0: continue ans += 1 else: ans += 1 + max(abs(x) // H, abs(y) // K) - min((abs(x) + H - 1) // H, (abs(y) + K - 1) // K) print(ans)