#include #define chmin(x,y) (x) = min((x),(y)) #define chmax(x,y) (x) = max((x),(y)) using namespace std; using ll = long long; const ll mod = 998244353; const vector dx = {1,0,-1,0}, dy = {0,1,0,-1}; using Graph = vector>; // https://qiita.com/drken/items/3b4fdf0a78e7a138cd9a // a^n mod を計算する long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } // a^{-1} mod を計算する long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); } int main(){ // input ll N,M,P,Q; cin >> N >> M >> P >> Q; // solve while(Q--){ ll x,f; cin >> x >> f; ll y = (modinv(x,P) * f) % P; if(f == 0) y += P; cout << M / P + (M % P >= y) << endl; } // output }