#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 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; fof(i,0,Q) { ll a,f; cin>>a>>f; if(M%P>=(f*modinv(a,P)%P+(f*modinv(a,P)%P==0?P:0))) ln(M/P+1); else ln(M/P); } }