def INT(): return int(input()) def MI(): return map(int, input().split()) def LI(): return list(map(int, input().split())) N, M, P, Q = MI() for i in range(Q): x, f = MI() a = P + f x_inv = pow(x, P - 2, P) y_min = (a * x_inv) % P if y_min == 0: y_min += P ans = 0 if M > y_min: ans = (M - y_min) // P + int((M - y_min) % P != 0) if M == y_min: ans += 1 print(ans)