#include #include #include #include #include #include #include #include #include #include using ll = long long; using namespace std; int main() { ll n, m, p, q; cin >> n >> m >> p >> q; vector yaki(p); for (int i = 0; i < q; i++){ ll x, f, cnt = 0; cin >> x >> f; for (int j = 0; j < p; j++){ if ((x * j)%p == f){ cnt += (m/p); if(m%p >= j) cnt++; if (j == 0) cnt--; } } cout << cnt << endl; } }