#include using namespace std; void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); } long long pow_mod(long long x, long long n, long long p) { long long res = 1; while (n > 0) { if (n % 2 == 1) { res = (res * x) % p; } x = (x * x) % p; n /= 2; } return res; } long long inv_mod(long long x, long long p) { return pow_mod(x, p - 2, p); } int main() { fast_io(); int n, m, p, q; cin >> n >> m >> p >> q; for (; q--;) { int x, f; cin >> x >> f; if (x % p == 0) { cout << m << "\n"; continue; } long long r = f * inv_mod(x, p) % p; // count x % p = r in [1, m] long long ans = m / p; if (m % p >= r) { ans++; } cout << ans << "\n"; } }