N, M, P, Q = map(int, input().split()) for i in range(Q): x, f = map(int, input().split()) ans = 0 for j in range(1, M + 1): if x * j % P == f: ans += 1 print(ans)