def divisor(n): cd = [] i = 1 while i*i <= n: if n%i==0: cd.append(i) i += 1 return cd def divisor2(n): cd = [] i = 1 while i*i <= n: if n%i==0: cd.append(i) if i!=n//i: cd.append(n//i) i += 1 return cd def check(bpc,bmc): if (bpc+bmc)%2==1: return False if bpc-bmc<=0: return False return True def solve(): S = int(input()) ans = [0]*S for s in range(S): X, Y = map(int, input().split()) q = abs(X-Y) p = X+Y if q==0: if p%2==0: ans[s] += p//2 - 1 ans[s] += len(divisor2(p//2))-1 if p%4==0: ans[s] -= 1 continue divs = divisor(q) for div in divs: div2 = q//div if p%(div+2)==0: bpc = p//(div+2) bmc = div2 if check(bpc,bmc): ans[s] += 1 if p%(div2+2)==0: bpc = p//(div2+2) bmc = div if check(bpc,bmc): ans[s] += 1 return ans print(*solve(),sep='\n')