#include #include #define rep(i, n) for(int i = 0; i < (int)(n); i++) #define rep2(i, s, n) for(int i = (s); i < (int)(n); i++) #define printYN(check) cout << ((check)? "Yes" : "No") << endl #define NUM 998244353 #define IMAX INT_MAX #define LMAX LONG_MAX using namespace std; using namespace atcoder; using mint = modint; using vi = vector; using vvi = vector; using ll = long long; using vl = vector; using vvl = vector; using vs = vector; using vb = vector; using vvb = vector; using pii = pair; using vpii = vector; using tii = tuple; using mii = map; using Graph = vvi; // Graph graph(n); // vi dx = {-1, 0, 1, -1, 1, -1, 0, 1}; // vi dy = { 1, 1, 1, 0, 0, -1, -1,-1}; // snippet -> {UnionFind, dfs_reculsive, dfs_stack, is_prime, mypow, // bfs_queue, digsum, binary_search, gcd, Dijkstra, bitsearch} // ------------------------------------------------------------------------- int main() { int n, m, p, q; cin >> n >> m >> p >> q; int cy = m / p; // cycle y int y = m % p; mint::set_mod(p); mint limy; // cerr << y << endl; rep(t, q) { int x, f; cin >> x >> f; ll ans = (ll)cy; if(f != 0) { limy = (f % p); if(limy.val() < 0) limy += p; limy /= (x % p); if(limy.val() < 0) limy += p; // cerr << "limy " << limy.val() << endl; if(limy.val() <= y) { ans++; } } // cerr << limy.val() << endl; // cerr << "ans > "; cout << ans << endl; } return 0; }