n, m, p, q = map(int, input().split()) for i in range(q): x, f = map(int, input().split()) x %= p if f == 0: if x == 0: print(m) else: print(m // p) else: if x == 0: print(0) continue t = pow(x, p-2, p) * f % p print((m+p-t)//p)