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