#include using namespace std; using ll=long long; long long modinv(long long a, long long m) { long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } int main(){ ll n,m,p,q; cin>>n>>m>>p>>q; while(q--){ ll x,f; cin>>x>>f; ll inv=modinv(x,p); ll y=f*inv%p; ll cnt=(m-y)/p; if(y<=m){ cnt++; } if(y==0){ cnt--; } cout<