n,m,p,q = map(int, input().split()) for _ in range(q): x,f = map(int, input().split()) if(x%p==0): if(f==0): print(m) else: print(0) continue a = f*pow(x, -1, p) a%=p ans = m//p if(a!=0 and m%p >= a): ans+=1 print(ans)