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