#約数全部 def Divisors(N): N=abs(N) L,U=[],[] k=1 while k*k <=N: if N%k== 0: L.append(k) if k!=N//k: U.append(N//k) k+=1 return L+U[::-1] #==================================== S=int(input()) L=[0]*S for i in range(S): X,Y=map(int,input().split()) D=Divisors(X+Y) K=0 for d in D: if d==1: continue elif d==2: if X==Y: K+=X-1 else: A=d-1 B2=A*X-Y C2=-X+A*Y if B2>0 and C2>0 and B2%(A*A-1)==C2%(A*A-1)==0: K+=1 L[i]=K print("\n".join(map(str,L)))