package main import . "fmt" import . "math/big" import . "os" import bf "bufio" func main() { rd:=bf.NewReader(Stdin) var n,m,p,q int64 Fscan(rd,&n,&m,&p,&q) P:=NewInt(p) for ;q>0;q-- { var x,f int64 Fscan(rd,&x,&f) if x%p==0&&f==0 { Println(m) continue } X:=NewInt(x) F:=NewInt(f) X.ModInverse(X,P) F.Mul(F,X).Mod(F,P) f=F.Int64() r:=m/p if m%p>0&&f>0&& f<=m%p { r++ } Println(r) } }