#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; long long r = f * inv_mod(x, p) % p; // count x % p = r in [1, m] long long cnt = (m + 1) / p; cnt += (m % p >= r); if (r == 0) cnt--; cout << cnt << "\n"; } }