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