#include using namespace std; #define rep(i,n) for(int i=0; i; using TU = tuple; using vint = vector; using vvint = vector; using vvvint = vector; using vll = vector; using vvll = vector; using vvvll = vector; template istream &operator>>(istream &is,vector &v){for(T &in:v){is>>in;}return is;} template ostream &operator<<(ostream &os,vector &v){for(int i=0;i<(int)v.size();i++){os< istream &operator>>(istream &is,vector> &v){for(vector &in:v){is>>in;}return is;} template ostream &operator<<(ostream &os,vector> &v){for(vector &out:v){os<>n>>m>>p>>q; rep(_,q){ ll x,f;cin>>x>>f; x%=p; ll val=f*modinv(x,p); val%=p; // y mod p == val int res=m/p; if(val && val<=m%p) res++; cout<< res <<'\n'; } return 0; }