import sys # sys.setrecursionlimit(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = (1 << 63)-1 # inf = (1 << 31)-1 md = 10**9+7 # md = 998244353 a_max = 500000005 r = round(a_max**0.5) while r**2 < a_max: r += 1 isp, pp = [1]*(r+1), [] for a in range(2, r+1): if isp[a]: pp.append(a) for b in range(a+a, r+1, a): isp[b] = 0 def factors(a): ff = [1] for p in pp: if a == 1: break c = 0 if p**2 > a: p, a, c = a, 1, 1 else: while a%p == 0: c, a = c+1, a//p if c == 0: continue w, nf = 1, [] for _ in range(c): w *= p for f in ff: nf.append(f*w) ff += nf return ff # ab+c=b+ac # (a-1)b-(a-1)c=0 # (a-1)(b-c)=0 # ab+ c=x # b+ac=y # (a-1)(b-c)=x-y # ab+c=x # b-c=z # (a+1)b=x+z # b=(x+z)/(a+1) def solve(): x, y = LI() ans = 0 if x == y: ans = len(factors(x))+x-2 else: if x < y: x, y = y, x for a1 in factors(x-y): a = a1+1 z = (x-y)//a1 if (x+z)%(a+1) == 0 and (x+z)//(a+1)-z > 0: ans += 1 print(ans) for _ in range(II()): solve()