def divisors(n): res_low, res_high = [], [] i = 1 while i * i <= n: if n % i == 0: res_low.append(i) if i != n // i: res_high.append(n // i) i += 1 return res_low + res_high[::-1] def f(dv, a): for x in dv: if a <= x: return x return -1 T = int(input()) for t in range(T): a, b = map(int, input().split()) dv = divisors(b-a) res = f(dv, a) if res == -1: print(-1) else: print(res - a)