import sys input = sys.stdin.readline T=int(input()) for tests in range(T): A,B=map(int,input().split()) if A==B: print(0) continue u=B-A L=[] for i in range(1,10**5): if u%i==0: L.append(i) L.append(u//i) ANS=1<<63 for l in L: if l>=A: ANS=min(ANS,l-A) if ANS==1<<63: ANS=-1 print(ANS) else: print(ANS)