#include using namespace std; using ll=long long; using vl=vector; using vvl=vector>; using vvvl=vector>>; using pl=pair; using vpl=vector>; #define fi first #define se second #define all(x) (x).begin(),(x).end() #define _overload3(_1,_2,_3,name,...) name #define _rep(i,n) repi(i,0,n) #define repi(i,a,b) for(long long i=(long long)(a);i<(long long)(b);++i) #define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__) #define pb push_back #define lb lower_bound #define ub upper_bound #include using namespace atcoder; long long modpow(long long a, long long n, long long mo){long long res=1;while(n>0){if(n&1){res=res*a%mo;}a=a*a%mo;n>>=1;}return res;} long long Pow(long long a, long long n){long long res=1;while(n>0){if(n&1){res=res*a;}a=a*a;n>>=1;}return res;} const ll MOD=998244353; const ll INF=(1ll<<60); int main(){ ll N,M,P,Q; cin>>N>>M>>P>>Q; rep(i,Q){ ll x,f; cin>>x>>f; ll y=(inv_mod(x%P,P)*f)%P; if(f==0) y=P; cout<<(M-y+P)/P<