#include using namespace std; using int64 = long long; // a^e mod mod int64 mod_pow(int64 a, int64 e, int64 mod) { int64 r = 1 % mod; while (e > 0) { if (e & 1) r = (r * a) % mod; a = (a * a) % mod; e >>= 1; } return r; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int64 N, M, P; int Q; cin >> N >> M >> P >> Q; while (Q--) { int64 x, f; cin >> x >> f; int64 xm = x % P; // x ≡ 0 (mod P) if (xm == 0) { if (f == 0) cout << M << '\n'; else cout << 0 << '\n'; continue; } // y ≡ f * x^{-1} (mod P) int64 inv = mod_pow(xm, P - 2, P); // フェルマーの小定理 int64 r = (f * inv) % P; int64 ans = 0; if (r == 0) { ans = M / P; } else { if (r <= M) ans = (M - r) / P + 1; else ans = 0; } cout << ans << '\n'; } return 0; }