#include using namespace std; using i64 = int64_t; int main() { i64 N, M, P, Q; cin >> N >> M >> P >> Q; for (i64 _ = 0; _ < Q; _++) { i64 x, f; cin >> x >> f; i64 begin = -1; for (i64 i = 0; P * i + f <= M * x; i++) { if (P * i + f != 0 && (P * i + f) % x == 0) { begin = (P * i + f) / x; break; } } if (begin == -1) { cout << 0 << endl; } else { cout << (M - begin) / P + 1 << endl; } } return 0; }