#include using namespace std; int main() { int n, m, p, q; cin >> n >> m >> p >> q; vector>a(n+1, vector(m+1)); for(int i=1; i<=n; i++){ for(int j=1; j<=m; j++){ a[i][j] = i*j%p; } } for(int i=1; i<=q; i++){ int x, f; cin >> x >> f; int ans = 0; for(int j=1; j<=m; j++){ if(a[x][j] == f) ans++; } cout << ans << endl; } }