N, M, P, Q = map(int, input().split()) ymodP = [y % P for y in range(1, M+1)] for _ in range(Q): xi, fi = map(int, input().split()) count = 0 for ymodp in ymodP: if (xi * ymodp) % P == fi: count += 1 print(count)