N,M,P,Q = map(int,input().split()) q = M // P r = M % P for _ in range(Q): x,f = map(int,input().split()) if x % P == 0: if f == 0: print(M) else: print(0) continue y = f * pow(x,P-2,P) % P if y == 0: print(q) continue if y <= r: print(q+1) else: print(q)