def divisor(x): ans = [] for i in range(1, int(x**0.5) + 1): if x % i == 0: ans.append(i) if i * i != x: ans.append(x / i) return ans s = int(input()) assert(1 <= s <= 10000) for _ in range(s): x, y = map(int, input().split()) assert(1 <= x <= 500000000) assert(1 <= y <= 500000000) if x == y: print(len(divisor(x)) + x + x % 2 - 3) continue ans = 0 if x < y: x, y = y, x n = x - y for d in divisor(n): a = n // d + 1 ans += y > d and not (y - d) % (a + 1) print(ans)