def main(): N, M, P, Q = map(int, input().split()) XF = [list(map(int, input().split())) for _ in range(Q)] for i in range(Q): x, f = XF[i] y_0 = (f * pow(x, P-2, P)) % P if y_0 == 0: print((M - y_0) // P) else: print((M - y_0) // P + 1) return 0 main()