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