#include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); long long n, m, p; int q; cin >> n >> m >> p >> q; vector> query(q); for (int i=0;i> query[i].first >> query[i].second; /** * query[i].first * y (mod p) == query[i].second * 1 <= y <= m */ /** * x * y = cp + f * x * y'= c'p+ f * x * (y-y') = (c-c') * p * * if (x == d * p) => d * (y-y') = (c-c') * else => x * (p *(a-a')) = (c-c') * p */ for (int i=0;i>= 1) { if (ct & 1) y *= d; y %= p; d *= d; d %= p; } y *= f; y %= p; //cout << y << ':'; long long ans {}; if (y <= m) { ans = (m - y + p)/p; if (f == 0) --ans; } cout << ans << '\n'; } } }