#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; rep(t, q) { int x, f; cin >> x >> f; int ans = cy; if(x == 0) { if(f == 0) { ans++; } } else { if(f == 0) { cout << ans << endl; continue; } else { limy = f; limy /= x; if(limy.val() <= y) ans++; } } cout << ans << endl; } return 0; }