#include using namespace std; using ll = long long; using pint = pair; #include using namespace atcoder; //#include"debug.hpp" int main() { ll N, M, P, Q; cin >> N >> M >> P >> Q; for(int i = 0; i < Q; i++) { ll x, f; cin >> x >> f; ll y = inv_mod(x, P) * f; y %= P; // print(y); if(y == 0) { cout << M / P << endl; } else if(y > M) cout << 0 << endl; else cout << (M - y) / P + 1 << endl;; } }