def INT(): return int(input()) def MI(): return map(int, input().split()) def LI(): return list(map(int, input().split())) import math N, M, P, Q = MI() for i in range(Q): x, f = MI() x_inv = pow(x, P - 2, P) y_min = ((f % P) * x_inv) % P if y_min == 0: y_min += P if x % P == 0: if f == 0: print(M) continue else: print(0) continue if M < y_min: print(0) else: print(math.ceil((M - y_min + 1) / P))