#include #include #include #include #include #include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; template vector> vec2d(int n, int m, T v){ return vector>(n, vector(m, v)); } template vector>> vec3d(int n, int m, int k, T v){ return vector>>(n, vector>(m, vector(k, v))); } template void print_vector(vector v, char delimiter=' '){ if(v.empty()) { cout << endl; return; } for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter; cout << v.back() << endl; } void solve(){ ll m, a, b, k; cin >> m >> a >> b >> k; ll g = gcd(a, b); if(k%g != 0){ cout << 0 << endl; return; } m /= g; a /= g; b /= g; k /= g; if(k > b) { cout << 0 << endl; return; } if(k == b){ cout << 0 << endl; return; } if(k > a){ cout << 0 << endl; return; } ll l = a*b; ll iter = m/l; ll rem = m-(iter*l); if(k == a){ // 頑張る... // aの倍数 ll last_a = (m/a)*a; ll last_b = (m/b)*a; ll cnt_a = (m/a); ll cnt_b = (m/b); ll cnt_only_b = cnt_b-(m/(a*b)); ll ans = m/a-1-cnt_only_b; if(last_b > last_a) ans--; cout << ans << endl; return; } ll ans = iter*2; // a-bのパターン { auto [r, lc] = atcoder::crt({0ll, k}, {a, b}); if(r <= rem){ ans++; } } // b-aのパターン { auto [r, lc] = atcoder::crt({0ll, k}, {b, a}); if(r <= rem){ ans++; } } cout << ans << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; int t; cin >> t; while(t--) solve(); }