#include #include using ll = long long; #define MOD 1000000007 #define Mod 998244353 const int MAX = 1000000005; const long long INF = 1000000000000000005LL; using namespace std; using namespace atcoder; ll modpow(ll a, ll n, ll m) { ll res = 1; while (n > 0) { if (n & 1) res = res * a % m; a = a * a % m; n >>= 1; } return res; } ll modinv(ll a, ll m) { return modpow(a, m-2, m); } int main() { ios::sync_with_stdio(0);cin.tie(); int N, M, P, Q; cin >> N >> M >> P >> Q; while (Q--) { int x, f; cin >> x >> f; int a = modinv(x, P) * f % P; if (M < a) { cout << 0 << endl; } else { cout << (M - a)/P + 1 - (a == 0) << endl; } } }