def main(): N, M, P, Q = map(int, input().split()) for i in range(Q): x, f = map(int, input().split()) if x % P == 0: print(M if f == 0 else 0) else: z = pow(x, -1, P) * f % P if z == 0: z = P print((M - z + P) // P) if __name__ == '__main__': main()