N, M, P, Q = map(int, input().split()) counts = [0] * P for y in range(1, M + 1): counts[y % P] += 1 for _ in range(Q): x, f = map(int, input().split()) print(counts[(f * pow(x, P - 2, P)) % P])