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 ans = M // P t = (f * pow(x,-1,P)) % P if f > 0 and t <= M % P: ans += 1 print(ans)