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