n,m,p,q=gets.split.map(&:to_i) q.times do x,f=gets.split.map(&:to_i) y=f*x.pow(p-2,p)%p puts (m-y)/p-(1-y)/p-((1-y)%p==0 ? 0 : 1)+1 end