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