#include using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) using ll = unsigned long long; using Graph = vector>; int main() { int n, m, p, q; cin >> n >> m >> p >> q; rep(i, q) { int x, f; cin >> x >> f; ll ans = 0; for (int j = 1; j < m + 1; j++) { ll xy = x * j; if (xy % p == f) ans++; } cout << ans << endl; } }