#include #include using namespace std; using namespace atcoder; typedef modint998244353 mint; typedef long long ll; ll mod_pow(ll x, ll m, int P){ ll ret = 1; ll tmp = x; while (m > 0){ if (m&1){ ret = ret * tmp % P; } m >>= 1; tmp = tmp * tmp % P; } return ret; } int main(){ ll n, m, p; cin >> n >> m >> p; int q; cin >> q; ll base = m / p; ll amari = m % p; for (int i=0; i> x >> f; if (x % p == 0){ if (f == 0){ cout << m << "\n"; }else{ cout << 0 << "\n"; } }else{ ll tar = mod_pow(x % p, p-2, (int)p) * f % p; if ((tar - 1 + p) % p <= amari){ cout << base + 1 << "\n"; }else{ cout << base << "\n"; } } } }