import sys def search_devisors(n): res = [] for a in range(1, int(n ** .5) + 2): if n % a == 0: if a == n // a: res.append(a) else: res.append(a) res.append(n // a) res.sort() return res input = sys.stdin.readline s = int(input()) for _ in range(s): x, y = map(int, input().split()) x, y = max(x, y), min(x, y) if x == y: d = search_devisors(x) print(x - 1 + len(d) - 2) else: d = search_devisors(x - y) ans = 0 for k in d: t = (x - y) // k a = k + 1 if (x - a * t) % (a + 1) == 0: c = (x - a * t) // (a + 1) b = t + c if c > 0 and y == a * c + b: ans += 1 print(ans)