#include #include using namespace std; using namespace atcoder; #define ll long long /* hi, I'm rintaro. AC is right AC count is my life But don't solve too many easy plobems! */ #define g(n,m) get(m) #define fof(i,o,n) for (ll i = o; i < (ll)n; i++) #define fof_(dontuseit,a) for(auto &dontuseit : a) #define vcin(a) fof_(i,a) cin>>i #define all(o) o.begin(),o.end() #define double long double #define pii pair #define vec vector #define vi vec #define vs vec #define vc vec #define vvi vec #define vpii vec #define igpqueue priority_queue> #define re return #define r0 re 0 #define ln(n) cout << n << endl #define fix(n) << fixed << setprecision(n) #define out_(n) << n << " " #define Out cout #define out(n) << n #define End << endl #define imap map #define smap map #define gmap map> #define m9 998244353 #define m7 1000000007 #define lny ln("Yes") #define lnn ln("No") long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1LL) 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() { ll N, M, P, Q; cin >> N >> M >> P >> Q; fof(i,0,Q) { ll a,f; cin>>a>>f; if(M%P>=((f%P)*(modinv(a,P)%P)%P+((f%P)*(modinv(a,P)%P)%P==0?P:0))) ln(M/P+1); else ln(M/P); } }