#include #include #include #include #include #include #include #include #include #include #include using namespace std; using namespace atcoder; typedef long long ll; #define rep(i,n) for (int i = 0; i < int(n);i++) int main(){ ll n,m,p,q; cin >> n >> m >> p >> q; for (int i = 0; i < q;i++){ ll x,f; cin >> x >> f; ll inv = pow_mod(x,p-2,p); ll y = (inv*f)%p; //cout << "y:" << y << endl; ll r = m%p; ll ans = m/p + (y <= r); if (y == 0) ans--; cout << ans << endl; //cout << m/p << endl; } return 0; }