from bisect import bisect_left T = int(input()) for _ in range(T): a,b = map(int,input().split()) n = b-a if n==0: print(0) else: A = set() for i in range(1,n+1): if i*i>n:break if n%i==0: A.add(i) A.add(n//i) A = sorted(list(A)) ind = bisect_left(A,a) if ind==len(A): print(-1) else: print(A[ind]-a)