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) X:=NewInt(x) F:=NewInt(f) X.ModInverse(X,P) F.Mul(F,X).Mod(F,P) f=F.Int64() r:=m/p if f>0&& f<=m%p { r++ } Println(r) } }