N, M, P, Q = map(int, input().split()) def mod_div(a, b, p): return (a * pow(b, p - 2, p)) % p ans = [] for _ in range(Q): x, f = map(int, input().split()) y = mod_div(f, x, P) if y == 0: y = P ok = 0 ng = M while ng - ok > 1: mid = (ok + ng) // 2 if P * mid + y <= M: ok = mid else: ng = mid ans.append(ok + 1) print(*ans, sep="\n")