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