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