#約数全部 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()) P=set(map(lambda x:x-1,Divisors(X+Y))) Q=set(map(lambda x:x+1,Divisors(X-Y))) R=P&Q K=0 for a in R: B=(a*X-Y)//(a*a-1) C=(a*Y-X)//(a*a-1) K+=(B>0 and C>0 and B*(a*a-1)==a*X-Y and C*(a*a-1)==a*Y-X) L[i]=K print("\n".join(map(str,L)))