N,M,P,Q = map(int,input().split()) ANS = [] for lp in range(Q): x,f = map(int,input().split()) y = f * pow(x,P-2,P) % P if y == 0: ANS.append( (M-y) // P ) else: ANS.append( (M-y) // P + 1 ) print (*ANS,sep="\n")