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 = f x_inv = pow(x, P - 2, P) y_min = (a * x_inv) % P if y_min == 0: y_min += P if M < y_min: print(0) elif M == y_min: print(1) else: print((M - y_min) // P + 1)