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 ANS = [] T = int(input()) for t in range(T): a, b = map(int, input().split()) dv = divisors(b-a) res = f(dv, a) if a == b: ANS.append(0) elif res == -1: ANS.append(-1) # print(-1) else: ANS.append(res - a) # print(res - a) for ans in ANS: print(ans) # print(*ANS)