from itertools import product, groupby def fast_prime_factorization_many(lst): # 素因数分解(ロー法、複数) from subprocess import Popen, PIPE res = Popen(["factor"] + list(map(str, lst)), stdout=PIPE).communicate()[0].split(b"\n")[:-1] return [list(map(int, r.split()[1:])) for r in res] def main(): S = int(input()) XY = [list(map(int, input().split())) for _ in range(S)] Factors = fast_prime_factorization_many([x+y for x, y in XY]) Ans = [] for (x, y), factors in zip(XY, Factors): ans = 0 if x == y: ans += x - 1 fs = [] ranges = [] for f, gr in groupby(factors): fs.append(f) ranges.append(range(len(list(gr))+1)) for t in product(*ranges): divisor = 1 for n, f in zip(t, fs): divisor *= f ** n if divisor <= 2: continue a = divisor - 1 denom = a * a - 1 b, bm = divmod(a * x - y, denom) c, cm = divmod(a * y - x, denom) if bm == cm == 0 and b > 0 and c > 0: ans += 1 Ans.append(ans) print("\n".join(map(str, Ans))) main()