#include #include using namespace std; using namespace atcoder; using ll = int_fast64_t; using ull = uint_fast64_t; using mint = modint998244353; constexpr ll INF = 1ll << 60; template void printelem(const T& container, const D& divide){ for(const auto& i : container) cout << i << divide; cout << endl; } template void printelem(const T& container) { printelem(container, ", "); } int main(){ //インプット ll N, M, P, Q; cin >> N >> M >> P >> Q; for(ll q = 0; q < Q; q++){ ll x, f; cin >> x >> f; ll first = -1; for(ll i = x; i <= x*M; i += x){ if(i % P == f){ first = i; break; } } // ll ans = 0; // for(ll i = first; i <= x*M; i += x*P){ // ans++; // } // if(first > 0) cout << ans << endl; // else cout << 0 << endl; if(first > 0) cout << (x*M-first)/(x*P) + 1 << endl; else cout << 0 << endl; } return 0; }