mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.buffer.readline def divisors(n): ret = [] for d in range(1, n+1): if d * d > n: break if n%d == 0: ret.append(d) ret.append(n//d) if ret[-1] == ret[-2]: ret.pop() return ret for _ in range(int(input())): x, y = map(int, input().split()) if x < y: x, y = y, x xy = x+y div = divisors(xy) ans = 0 for d in div: a = d - 1 if a == 0: continue bc = xy // (a+1) if a == 1: if x == y: ans += x-1 continue else: continue if (x-y) % (a-1) != 0: continue b_c = (x-y) // (a-1) if bc%2 == b_c%2: b = (bc + b_c)//2 c = (bc - b_c)//2 if b > 0 and c > 0: ans += 1 print(ans) if __name__ == '__main__': main()