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 * x**(P-2) % P #print(f"{y=}") assert x * y % P == f if y == 0: y = P print((M-y)//P +1)