N,M,P,Q = map(int, input().split()) # (x * y) % P = f # n^(P-1) mod P = 1 # given x and f, y = ? # x * x^(P-2) mod P = 1 # x * f * x^(P-2) mod P = f for _ in range(Q): x,f = map(int, input().split()) y = (f * pow(x,P-2,P)) % P #print(f"{y=}") #assert x * y % P == f if y == 0: y = P """ count = 0 while True: #print(f"{y=}") if y > M:break count += 1 y += P #print(f"{count=}") print(count) """ #print(f"{y=}") if x % P == 0: if f == 0: print(M) else: print(0) elif y > M: print(0) else: print((M-y)//P +1)