#include #include using namespace std; using namespace chrono; #define rep(i,n) for(int i = 0 ; i < (n) ; i++) #define rep1(i,n) for(int i = 1 ; i <= (n) ; i++) #define rrep(i,n) for(int i = (n) - 1 ; i >= 0 ; i--) #define rrep1(i,n) for(int i = (n) ; i > 0 ; i--) using ll = int64_t; using P = pair; using PL = pair; using PD = pair; using ld = long double; using vi = vector; using vvi = vector>; using vl = vector; using vvl = vector>; using vP = vector

; template using v = vector; template using vv = vector>; template using vvv = vector>>; template using p_q = priority_queue, greater>; const int INF = 1001001001; const int INTMAX = (1ll<<31)-1; const ll LINF = (1ll<<62); const ll LLMAX = (1ll<<62) + ((1ll<<62)-1); #define line cout << "================================" << endl; #define Yn(x) ((x) ? "Yes" : "No") #define yn(x) ((x) ? "yes" : "no") #define debug(x) cout << #x" : " << (x) << endl; #define output(x) cout << (x) << endl; #define outs(x) cout << #x << endl; #define mod(n) %(n) #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() #define m_p(a, b) make_pair(a, b) #define set_time(x) steady_clock::time_point (x) = steady_clock::now() #define p_time(x) duration_cast(x).count() #define show_time(s, e) cout << "実行時間" << duration_cast(e-s).count() << "ms\n" template bool chmin(T& a, T b){if(a>b){a=b;return 1;}return 0;} template bool chmax(T& a, T b){if(a>i;} void vin(vl& a){for(auto& i:a)cin>>i;} void vin(vP& a){for(auto& x:a)cin>>x.first>>x.second;} void vin1(vi& a){rep1(i,a.size()-1)cin>>a[i];} void vvin(vvi& a){for(auto& i:a)for(auto& j:i)cin>>j;} void vvin(vvl& a){for(auto& i:a)for(auto& j:i)cin>>j;} void vvdes(vvi& a){for(auto& i:a){for(auto& j:i){ cout << j << " ";}puts(""); }} void vvdes(vvl& a){for(auto& i:a){for(auto& j:i){ cout << j << " ";}puts(""); }} template void vvd(vv& a){rep(i,a.size()){rep(j,a[i].size()){ cout << a[i][j] << " ";}cout<(e - s).count() >= time){puts("TLE");return true;} return false; } ll rui(ll n, int r, ll m = LLMAX) { n %= m; ll res = (r < 2 ? 1 : n); ll cnt = 1; if(r >= 2)for( ; cnt *2 <= r ; cnt *= 2) { res *= res; res %= m; } else cnt = 0; for(int i = 0 ; i < (r - cnt) ; i++) {res *= n; res %= m;} return res; } ll kai(ll n) { //O(N) ll res = 1; for(int i = 2 ; i <= n ; i++) res *= i; return res; } }; const int MOD = 1000000007; ll modint(ll x, int r, int mod = MOD) { ll res = 1; while(r) { if(r&1) res *= x; x *= x; res%= mod; x%= mod; r /= 2; } return res; } int main(){ //set_time(__start__); alucrex al; int n, m, p, Q; cin >> n >> m >> p >> Q; map mp; rep(i, p) mp[i] = m/p; rep1(i, m%p) mp[i]++; rep(q, Q) { int x, f; cin >> x >> f; if(x%p) { int fx = modint(x, p-2, p)*f; fx %= p; cout << mp[fx] << endl; } else { cout << (f==0)*m << endl; } } //set_time(__end__); //show_time(__start__ , __end__); }