#約数全部 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)[1:] K=0 for a in D: if a==2: K+=(X==Y) else: p=a-1 B=(p*X-Y) C=(-X+p*Y) if B>0 and C>0 and B%(p*p-1)==C%(p*p-1)==0: K+=1 L[i]=K print("\n".join(map(str,L)))