#include using namespace std; long long modinv(long long a, long long mod) { long long b = mod, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= mod; if (u < 0) u += mod; return u; } int main() { int T; cin >> T; for (int i = 0; i < T; i++) { long long M, A, B, K; cin >> M >> A >> B >> K; if (K < A) { long long ans = (M / (A * B)) * 2; long long a = (K * modinv(B, A)) % A; long long b = A - a; if (B * a + (M / (A * B)) * A * B <= M) ans++; if (B * b + (M / (A * B)) * A * B + K <= M) ans++; cout << ans << endl; } else if (K == A) { long long ans = M / A + M / B - M / (A * B) - 1; if ((M / A) * A >= (M / B) * B) ans -= 2 * (M / B - M / (A * B)); else ans -= 2 * (M / B - M / (A * B)) - 1; cout << ans << endl; } else { cout << 0 << endl; } } }