#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) { if (f == 0) cout << m << "\n"; else cout << 0 << "\n"; continue; } long long r = f * inv_mod(x, p) % p; if (r == 0) { cout << m / p << "\n"; } else { cout << (m - r + p) / p << "\n"; } } }