n, m, p, q = map(int, input().split()) assert 1 <= n <= 10 ** 9 assert 1 <= m <= 10 ** 9 assert 2 <= p <= 10 ** 9 assert 1 <= q <= 10 ** 5 for _ in range(q): x, f = map(int, input().split()) assert 1 <= x <= n assert 0 <= f <= p - 1 if x % p == 0: print(0 if f else m) continue y = f * pow(x, p-2, p) % p print(m // p if y == 0 or y > m % p else m // p + 1)