s = int(input()) def make_divisors(n): divisors = [] for i in range(1, int(n**0.5)+1): if n % i == 0: divisors.append(i) if i != n // i: divisors.append(n//i) #divisors.sort(reverse=True) return divisors for _ in range(s): x, y = map(int, input().split()) t = x-y if t < 0: print(0) elif t == 0: ans = x-1 D = make_divisors(x) if x%2 == 0: for d in D: if d >= 3: ans += 1 else: for d in D: if d >= 2: ans += 1 print(ans) else: D = make_divisors(t) ans = 0 for d in D: a = d+1 if (a*y-x)>0 and (a*y-x)%(a**2-1) == 0 and (a*x-y)>0 and (a*x-y)%(a**2-1) == 0: ans += 1 print(ans)