def friend(x, f, P, M):
  count = 0
  for i in range(1, M + 1):
    if (x * i - f) % P == 0:
      count += 1
  return count

N, M, P, Q = map(int, input().split())

for i in range(Q):
  x, f = map(int, input().split())
  print(friend(x, f, P, M))