n, m, p, q = map(int, input().split()) c, r = divmod(m, p) for _ in range(q): x, f = map(int, input().split()) if x % p == 0: if f % p != 0: print(0) else: print(m) continue y = f * pow(x, p - 2, p) % p if y <= r: ans = c + 1 else: ans = c if y == 0: ans -= 1 print(ans)