Tim likes Math. He likes it so much that he always brings his tablets with him and reads math e-books everywhere, even during parties.

Tim found an interesting exercise in one of the e-books he is reading. But you want him to join the party, so you decide to answer the question for him.

The problem is: Given ${D}$ and ${P}$, how many ordered pairs of integers are there whose absolute difference is ${D}$ and whose product is ${P}$? In other words, how many pairs of integers $(A,B)$ are there such that:

$|A-B|=D$
$A\times B=P$

Input Format

The first line of input contains ${T}$, the number of test cases. The next ${T}$ lines describe the test cases.

Each test case consists of a single line containing two integers ${D}$ and ${P}$ separated by a single space.

Output Format

For each test case, output a single line containing a single integer which is the answer for that test case.

Constraints  

$1\leq T\leq20000$ 

$|D|\leq10^9$ 

$|P|\leq10^9$  

Sample Input
3
1 2
0 4
-1 1

Sample Output
4
2
0

Explanation

Case 1: There are four pairs of integers with absolute difference ${1}$ and product $2$, namely $(1,2)$, $(2,1)$, $(-1,-2)$, $(-2,-1)$.

Case 2: There are two pairs of integers with absolute difference ${0}$ and product ${4}$, namely $(2,2)$, $(-2,-2)$.

Case 3: There are no pairs of integers with absolute difference $-1$, because the absolute value is never negative.

Difficulty: EASY

Solutions:
from math import sqrt
t = int(input())

def f(d, p):
	if d < 0:
		return 0
	ret = set()
	D1 = d * d + 4 * p
	if D1 >= 0:
		a = round((d - sqrt(D1)) / 2)
		if a * (d - a) == -p:
			ret.add((a, d - a))
		a = round((d + sqrt(D1)) / 2)
		if a * (d - a) == -p:
			ret.add((a, d - a))
		a = round((-d - sqrt(D1)) / 2)
		if a * (-d - a) == -p:
			ret.add((a, -d - a))
		a = round((-d + sqrt(D1)) / 2)
		if a * (-d - a) == -p:
			ret.add((a, -d - a))
	return len(ret)
for _ in range(t):
	(d, p) = map(int, input().split())
	print(f(d, p))


import sys
from math import sqrt

def isqrt(n):
	if n <= 0:
		return 0
	x = int(sqrt(n) * (1 + 1e-12))
	while True:
		y = x + n // x >> 1
		if y >= x:
			return x
		x = y

def solve():
	stdin = sys.stdin
	write = sys.stdout.write
	t = int(stdin.readline())
	for _ in range(t):
		(D, P) = map(int, stdin.readline().split())
		ans = set()
		if D >= 0:
			r = D * D + 4 * P
			if r >= 0:
				rt = isqrt(r)
				if rt * rt == r:
					num = -D + rt
					if not num & 1:
						b = num // 2
						a = D + b
						ans.add((a, b))
						b = (-D - rt) // 2
						a = D + b
						ans.add((a, b))
					num = D + rt
					if not num & 1:
						b = num // 2
						a = -D + b
						ans.add((a, b))
						b = (D - rt) // 2
						a = -D + b
						ans.add((a, b))
		write('%d\n' % len(ans))
solve()


import math

def issq(r):
	if r < 0:
		return False
	else:
		s = int(math.sqrt(r))
		if r == s * s:
			return True
		else:
			return False
T = int(input())
for t in range(T):
	(d, p) = list(map(int, input().split()))
	if d < 0:
		print(0)
	elif d == 0:
		if p == 0:
			print(1)
		elif issq(p):
			print(2)
		else:
			print(0)
	elif issq(d * d + 4 * p):
		if d * d + 4 * p == 0:
			print(2)
		else:
			print(4)
	else:
		print(0)


from math import floor, ceil, sqrt

def is_integer(n):
	return floor(n) == ceil(n)
for t in range(int(input())):
	(d, p) = [int(i) for i in input().split()]
	discriminant = d ** 2 + 4 * p
	if discriminant < 0 or not is_integer(sqrt(discriminant)):
		print(0)
	else:
		a1 = (-d + int(sqrt(discriminant))) // 2
		a2 = (-d - int(sqrt(discriminant))) // 2
		a3 = (d + int(sqrt(discriminant))) // 2
		a4 = (d - int(sqrt(discriminant))) // 2
		b1 = a1 + d
		b2 = a2 + d
		b3 = a3 - d
		b4 = a4 - d
		st = set()
		if abs(a1 - b1) == d and a1 * b1 == p:
			st.add((a1, b1))
		if abs(a2 - b2) == d and a2 * b2 == p:
			st.add((a2, b2))
		if abs(a3 - b3) == d and a3 * b3 == p:
			st.add((a3, b3))
		if abs(a4 - b4) == d and a4 * b4 == p:
			st.add((a4, b4))
		print(len(st))


def is_perfect_square_with_hint(x, a, b):
	if b == a:
		return False
	elif b == a + 1:
		return a * a == x
	else:
		diff = b - a
		c = a + diff // 2
		if x < c * c:
			return is_perfect_square_with_hint(x, a, c)
		else:
			return is_perfect_square_with_hint(x, c, b)

def is_perfect_square(x):
	if x < 0:
		return False
	else:
		return is_perfect_square_with_hint(x, 0, x + 1)

def get_num_solutions(D, P):
	if D < 0:
		return 0
	elif D == 0:
		if P == 0:
			return 1
		elif is_perfect_square(P):
			return 2
		else:
			return 0
	else:
		discriminant = D * D + 4 * P
		if discriminant == 0:
			return 2
		elif is_perfect_square(discriminant):
			return 4
		else:
			return 0
num_test_cases = int(input())
for c in range(num_test_cases):
	line = input().split()
	D = int(line[0])
	P = int(line[1])
	print(get_num_solutions(D, P))


import math
t = int(input())
for i in range(0, t):
	ip = input().split(' ')
	d = int(ip[0])
	p = int(ip[1])
	if d < 0:
		print('0')
	elif d == 0:
		if p > 0:
			tmp = int(math.sqrt(p))
			if tmp * tmp == p:
				print('2')
			else:
				print('0')
		elif p == 0:
			print('1')
		else:
			print('0')
	elif d * d + 4 * p < 0:
		print('0')
	else:
		check = int(math.sqrt(d * d + 4 * p))
		if check * check != d * d + 4 * p or (d + check) % 2 != 0:
			print('0')
		else:
			sol1 = (d + check) / 2
			sol2 = sol1 - d
			if sol1 > 0 and sol2 < 0 and (sol1 == sol2 * -1):
				print('2')
			else:
				print('4')


import math
t = int(input())
for _ in range(t):
	(d, p) = [int(x) for x in input().split()]
	sol = set([])
	try:
		b = (-d + math.sqrt(d ** 2 + 4 * p)) // 2
		a = d + b
		if abs(a - b) == d and a * b == p:
			sol.add((a, b))
			sol.add((b, a))
		b = (-d - math.sqrt(d ** 2 + 4 * p)) // 2
		a = d + b
		if abs(a - b) == d and a * b == p:
			sol.add((a, b))
			sol.add((b, a))
		print(len(sol))
	except:
		print(0)


import math
t = int(input())
while t > 0:
	t -= 1
	l = []
	(d, p) = map(int, input().split())
	if d < 0:
		print(0)
		continue
	else:
		k = d ** 2 + 4 * p
		if k >= 0:
			a1 = math.sqrt(k)
		if k >= 0 and int(int(a1) * int(a1)) == k:
			l.append([float(d + k ** 0.5) / 2, d + float(d + k ** 0.5) / 2])
			l.append([float(d - k ** 0.5) / 2, d + float(d - k ** 0.5) / 2])
			l.append([float(-1 * d + k ** 0.5) / 2, -1 * d + float(-1 * d + k ** 0.5) / 2])
			l.append([float(-1 * d - k ** 0.5) / 2, -1 * d + float(-1 * d - k ** 0.5) / 2])
			cou = 0
			for i in range(0, len(l)):
				flag = 0
				for j in range(0, i):
					if l[i] == l[j]:
						flag = 1
						break
				if flag == 1:
					continue
				elif l[i][0].is_integer() and l[i][1].is_integer():
					cou += 1
			print(cou)
		else:
			print(0)


from math import sqrt
for _ in range(int(input())):
	(D, P) = (int(x) for x in input().split())
	if D < 0:
		print('0')
		continue
	Disc = D ** 2 + 4 * P
	if D == 0 and P == 0:
		print('1')
		continue
	if Disc < 0:
		print('0')
	else:
		square = int(sqrt(Disc)) ** 2
		if square == Disc:
			if D == 0 or Disc == 0:
				print('2')
			else:
				print('4')
		else:
			print('0')


from math import sqrt

def cond_sqrt(n):
	if n < 0:
		return None
	c_sqrt = int(sqrt(n + 0.5))
	return c_sqrt if c_sqrt ** 2 == n else None

def num_solns(d, p):
	if d < 0:
		return 0
	elif d == 0:
		if p == 0:
			return 1
		elif cond_sqrt(p):
			return 2
		else:
			return 0
	else:
		c_sqrt = cond_sqrt(d ** 2 + 4 * p)
		if c_sqrt and (c_sqrt + d) % 2 == 0:
			return 4
		elif c_sqrt == 0:
			return 2
		else:
			return 0
for i in range(int(input())):
	(d, p) = map(int, input().split())
	print(num_solns(d, p))


from math import sqrt
for _ in range(int(input())):
	(D, P) = [int(ns) for ns in input().split()]
	if D < 0:
		print(0)
		continue
	if P == 0:
		if D == 0:
			print(1)
		else:
			print(4)
		continue
	z = D ** 2 + 4 * P
	if z < 0:
		print(0)
		continue
	if int(z) != int(int(sqrt(z)) ** 2):
		print(0)
		continue
	x = (D + sqrt(z)) / 2
	if x == int(x):
		if x > abs(P):
			print(0)
		elif D == 0 or int(x) == D // 2:
			print(2)
		else:
			print(4)
	else:
		print(0)


from math import sqrt
T = int(input())
for cntT in range(T):
	[D, P] = [int(x) for x in input().split()]
	if D < 0:
		print(0)
	elif D == 0:
		if P < 0:
			print(0)
		elif P == 0:
			print(1)
		else:
			sq = round(sqrt(P))
			if sq * sq == P:
				print(2)
			else:
				print(0)
	else:
		delta = D * D + 4 * P
		if delta < 0:
			print(0)
		elif delta == 0:
			if D & 1 == 0:
				print(2)
			else:
				print(0)
		else:
			sq = round(sqrt(delta))
			if sq * sq != delta:
				print(0)
			elif sq - D & 1 == 0:
				print(4)
			else:
				print(0)


nTst = int(input())
for t1 in range(nTst):
	(d, p) = list(map(int, input().split()))
	if d < 0:
		print(0)
	elif d == 0:
		if p < 0:
			print(0)
		elif p == 0:
			print(1)
		else:
			a1 = int(p ** (1 / 2))
			if a1 * a1 == p:
				print(2)
			else:
				print(0)
	else:
		discr = d * d + 4 * p
		if discr < 0:
			print(0)
		elif discr == 0:
			print(2)
		else:
			a1 = int(discr ** (1 / 2))
			if a1 * a1 == discr:
				print(4)
			else:
				print(0)


from sys import stdin
from copy import deepcopy as dcop
from random import randint
from collections import Counter
from functools import reduce
from itertools import accumulate, combinations as cmb, takewhile, product, groupby as grpb
from itertools import repeat as rep
from os.path import basename, exists, expanduser, splitext, isfile
from os import getcwd
from math import sqrt
from array import array as ar
lolPath = getcwd()
trace = 0
prof = 0
tst = 0
randid = '\\qosdjfoiz.txt'
home = isfile(lolPath + randid)
filou = 'D:\\ZeDaddy\\Documents\\Hack\\Algebra\\dandp_02.txt'
if home:
	print('home')
modo = 10 ** 9 + 7
if home:
	from mesures import ch
if home and filou:
	rd = open(filou, 'r')
else:
	rd = stdin

def initPrim(mp):
	ps = [2, 3, 5, 7, 11]
	top = int(sqrt(mp))
	at = 'b'
	ba = ar(at, [0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0])
	c = 1
	while c < 1 + mp // 30:
		ba = ba + ba
		c *= 2
	ba = ar(at, [0, 0, 1, 1, 0, 1]) + ba
	ba = ba[:mp]
	ixp = 3
	ixba = 13
	p = ps[ixp]
	while p <= top:
		for i in range(p * p, mp + 1, 2 * p):
			ba[i] = 0
		ixp += 1
		p = ps[ixp]
		tc = p * p
		for j in range(ixba, min(tc, mp), 2):
			if ba[j]:
				ps += [j]
		ixba = tc
	return (ba, ps)

def factoriserExt(n):
	d = []
	for p in ps:
		if n == p:
			return d + [[n, 1]]
		(q, r) = divmod(n, p)
		po = 0
		while r == 0:
			po += 1
			if q == p:
				return d + [[p, po + 1]]
			n = q
			(q, r) = divmod(q, p)
		if po:
			d = d + [[p, po]]
		elif q <= p:
			return d + [[n, 1]]
	if n > p * p:
		if millRab(n):
			return d + [[-1, 1]]
		if n == p ** 3:
			return d + [[p, 3]]
		if n > p ** 3:
			return d + [[]]
		rt = sqrtNewt(n, p)
		if rt:
			return d + [[rt, 2]]
		return d + [[-1, 1], [-2, 1]]
	return d + [[n, 1]]

def factoriser(n):
	if n == 0:
		return None
	if n == 2:
		return [[2, 1]]
	d = []
	for p in ps:
		if n == p:
			return d + [[n, 1]]
		(q, r) = divmod(n, p)
		po = 0
		while r == 0:
			po += 1
			if q == p:
				return d + [[p, po + 1]]
			n = q
			(q, r) = divmod(q, p)
		if po:
			d = d + [[p, po]]
		elif q <= p:
			return d + [[n, 1]]
	if n > p * p:
		return None
	return d + [[n, 1]]

def genListeDivs(N):
	listeDivs = [1]
	for i in factoriser(N):
		listeDivs = list((pow(i[0], p) * d for p in range(i[1] + 1) for d in listeDivs[:]))
	return listeDivs

def faire(D, P):
	if trace & 1:
		print('faire', D, P)
	if D < 0:
		return 0
	if P == 0:
		if D == 0:
			sols = [[0, 0]]
		else:
			sols = [[0, D], [D, 0], [0, -D], [-D, 0]]
	else:
		sols = []
		for seur in genListeDivs(abs(P)):
			quo = P // seur
			for q in (quo, -quo):
				for s in (seur, -seur):
					if abs(q - s) == D and [q, s] not in sols and (q * s == P):
						sols += [[q, s]]
	if trace & 8:
		print(D, P, sols)
	return len(sols)
entree = rd.read(10000000)
Pars = entree.splitlines()
if home:
	cr = ch()
r = []
(cb, ps) = initPrim(10 ** 5)
if home:
	print(ps[:20])
for ix in range(1, int(Pars[0].strip()) + 1):
	(D, P) = list((int(i.strip()) for i in Pars[ix].strip().split()))
	if home:
		cr = ch()
	r += [faire(D, P)]
if home:
	print(cr.lap())
	print(r[:50])
else:
	for v in r:
		print(v)


import math

def read_ints():
	return [int(x) for x in input().strip().split(' ')]

def read_int():
	return int(input().strip())

def compute_num_sol(D, P):
	if D < 0:
		return 0
	if D == 0:
		if P == 0:
			return 1
		if P < 0:
			return 0
		sr = int(math.sqrt(P))
		if sr * sr != P:
			return 0
		return 2
	if P == 0:
		return 4
	delta = D * D + 4 * P
	if delta < 0:
		return 0
	sd = int(math.sqrt(delta))
	b1 = (-D + sd) // 2
	b2 = (-D - sd) // 2
	a1 = D + b1
	a2 = D + b2
	solutions = set()
	for a in [a1, a2, -a1, -a2]:
		for b in [b1, b2, -b1, -b2]:
			if abs(a - b) == D and a * b == P:
				solutions.add((a, b))
	return len(solutions)
T = read_int()
for t in range(T):
	(D, P) = read_ints()
	num_sol = compute_num_sol(D, P)
	print(num_sol)


from math import sqrt

def is_square(D, P):
	xx = D ** 2 + 4 * P
	if xx < 0:
		return (False, -1)
	else:
		x = int(sqrt(xx))
		if x * x == xx:
			return (True, x)
		else:
			return (False, x)
T = int(input().strip())
for t in range(T):
	(D, P) = [int(x) for x in input().strip().split(' ')]
	if D < 0:
		print(0)
	elif P == 0 and D == 0:
		print(1)
	else:
		(flag, x) = is_square(D, P)
		if flag:
			n = 0
			if (x - D) % 2 == 0:
				n += 2
			if D != 0 and x != 0 and ((x + D) % 2 == 0):
				n += 2
			print(n)
		else:
			print(0)


for _ in range(int(input())):
	(d, p) = map(int, input().split())
	delta = d ** 2 + 4 * p
	print(0 if d < 0 or delta < 0 or int(delta ** 0.5) ** 2 != delta or (d + int(delta ** 0.5)) % 2 else (1 + (delta != 0)) * (1 + (d != 0)))


for i in range(int(input())):
	(d, p) = list(map(int, input().split()))
	if d < 0:
		print(0)
	elif p == 0 and d == 0:
		print(1)
	elif p == 1:
		if d == 0:
			print(2)
		else:
			print(0)
	elif p == -1:
		if d == 2:
			print(2)
		else:
			print(0)
	elif d ** 2 + 4 * p < 0:
		print(0)
	else:
		b1 = (-d + (d ** 2 + 4 * p) ** 0.5) / 2
		b2 = (-d - (d ** 2 + 4 * p) ** 0.5) / 2
		if (b1 + d) * b1 != p:
			print(0)
		elif b1 % 1 == 0:
			if abs(b1) == abs(b2):
				print(2)
			else:
				print(4)
		else:
			print(0)


def is_square(x):
	return int(x ** 0.5) ** 2 == x
T = int(input())
for t in range(T):
	(D, P) = input().split()
	(D, P) = [int(D), int(P)]
	if D < 0:
		print(0)
	elif D == 0:
		if P < 0:
			print(0)
		elif P == 0:
			print(1)
		elif is_square(P):
			print(2)
		else:
			print(0)
	else:
		delta = D ** 2 + 4 * P
		if delta < 0:
			print(0)
		elif delta == 0:
			if D % 2:
				print(0)
			else:
				print(2)
		elif is_square(delta):
			if (D - int(delta ** 0.5)) % 2 == 0:
				print(4)
		else:
			print(0)


from math import sqrt
T = int(input())
for i in range(T):
	(D, P) = (int(j) for j in input().split())
	d1 = D * D + 4 * P
	if d1 < 0 or D < 0:
		print(0)
		continue
	d1 = sqrt(d1)
	if d1 % 1 != 0:
		print(0)
		continue
	d1 = int(d1)
	if (d1 + D) % 2 != 0 or d1 * d1 != D * D + 4 * P:
		print(0)
		continue
	res = len(set([((-D - d1) // 2 + D, (-D - d1) // 2), ((-D + d1) // 2 + D, (-D + d1) // 2), ((-D + d1) // 2, (-D + d1) // 2 + D), ((-D - d1) // 2, (-D - d1) // 2 + D)]))
	print(res)


import math

def isPerfectSquare(n):
	return round(math.sqrt(n)) ** 2 == n
T = int(input())
for t in range(T):
	(D, P) = (int(j) for j in input().split(' '))
	if D < 0:
		print(0)
	elif D == 0:
		if P == 0:
			print(1)
		elif P > 0 and isPerfectSquare(P):
			print(2)
		else:
			print(0)
	else:
		disc = D ** 2 + 4 * P
		if disc < 0:
			print(0)
		elif disc == 0:
			print(2)
		elif isPerfectSquare(disc):
			print(4)
		else:
			print(0)


def is_square(x):
	r = int(x ** 0.5)
	return r * r == x
for _ in range(int(input())):
	(d, p) = [int(a) for a in input().split()]
	if d < 0:
		print(0)
	elif d == 0:
		if p < 0 or not is_square(p):
			print(0)
		elif p == 0:
			print(1)
		else:
			print(2)
	else:
		disc = d * d + 4 * p
		if disc < 0 or not is_square(disc):
			print(0)
		elif disc == 0:
			print(2)
		else:
			print(4)


import math
t = int(input())
for x in range(t):
	[d, p] = [int(y) for y in input().strip().split(' ')]
	if d < 0:
		print(0)
	elif d == 0:
		if p == 0:
			print(1)
		elif p < 0:
			print(0)
		else:
			sq = math.sqrt(p)
			if sq == int(sq):
				print(2)
			else:
				print(0)
	else:
		ret = 0
		if d * d + 4 * p >= 0:
			square = math.sqrt(d * d + 4 * p)
			if square != int(square):
				print(0)
				continue
			di = {}
			a1 = (d + math.sqrt(d * d + 4 * p)) / 2
			a2 = (d - math.sqrt(d * d + 4 * p)) / 2
			b1 = a1 - d
			b2 = a2 - d
			a3 = (-d + math.sqrt(d * d + 4 * p)) / 2
			a4 = (-d - math.sqrt(d * d + 4 * p)) / 2
			b3 = a3 + d
			b4 = a4 + d
			if a1 * b1 == p and abs(int(round(a1, 0)) - a1) < 1e-11:
				ret += 1
				di[int(round(a1, 0)), b1] = 1
			if a2 * b2 == p and abs(int(round(a2, 0)) - a2) < 1e-11:
				if not (int(round(a2, 0)), b2) in di:
					ret += 1
					di[int(round(a2, 0)), b2] = 1
			if a3 * b3 == p and abs(int(round(a3, 0)) - a3) < 1e-11:
				if not (int(round(a3, 0)), b3) in di:
					ret += 1
					di[int(round(a3, 0)), b3] = 1
			if a4 * b4 == p and abs(int(round(a4, 0)) - a4) < 1e-11:
				if not (int(round(a4, 0)), b4) in di:
					ret += 1
					di[int(round(a4, 0)), b4] = 1
			print(ret)
		else:
			print(0)


import math
T = int(input())
for _ in range(T):
	(D, P) = list(map(int, input().split()))
	if D < 0:
		print(0)
		continue
	count = 0
	if P > 0:
		end = round(math.sqrt(P)) + 1
		start = max(P // (end + D), 1)
		for B in range(start, end):
			A = B + D
			if A * B > P:
				break
			if A * B == P:
				if A == B:
					count += 2
				else:
					count += 4
	elif P < 0:
		end = round(math.sqrt(-P)) + 1
		start = max(-P // (end + D), 1)
		for A in range(start, end):
			if A > D:
				break
			B = A - D
			if A * B < P:
				break
			if A * B == P:
				if A == -B:
					count += 2
				else:
					count += 4
	elif D == 0:
		count += 1
	else:
		count += 4
	print(count)


def f(D, P):
	if D < 0:
		return 0
	elif D == 0 and P < 0:
		return 0
	elif D == 0 and P == 0:
		return 1
	else:
		d = D * D + 4 * P
		if d < 0:
			return 0
		elif d == 0:
			if D % 2 == 0:
				return 2
			else:
				return 0
		else:
			sqrt = int(round(d ** 0.5))
			if sqrt ** 2 == d:
				if D == 0:
					return 2
				else:
					return 4
			else:
				return 0
T = int(input())
for i in range(T):
	(D, P) = map(int, input().split(' '))
	print(f(D, P))


from decimal import *
T = int(input())
for t in range(T):
	(D, P) = list(map(int, input().split()))
	if D < 0:
		print(0)
		continue
	if D == 0 and P == 0:
		print(1)
		continue
	x = 4 * P + D ** 2
	if x < 0:
		print(0)
		continue
	x = Decimal(x) ** Decimal(0.5)
	if type(x) == complex:
		print(0)
		continue
	if not x % 1 == 0:
		print(0)
		continue
	x = int(x) * D
	B1 = ((2 * P + D ** 2 + x) / 2) ** 0.5
	B2 = ((2 * P + D ** 2 - x) / 2) ** 0.5
	if type(B1) != complex and type(B2) != complex:
		if B1 % 1 == 0 and B2 % 1 == 0:
			if B1 == B2:
				print(2)
			else:
				print(4)
			continue
	if type(B1) != complex:
		if B1 % 1 == 0:
			print(2)
			continue
	if type(B2) != complex:
		if B2 % 1 == 0:
			print(2)
			continue
	print(0)


from math import sqrt

def is_perfect_square(y):
	if y < 0:
		return False
	x = int(sqrt(y))
	while x * x < y:
		x += 1
	while x * x > y:
		x -= 1
	return x * x == y

def f(d, p):
	y = d * d + 4 * p
	if d < 0 or not is_perfect_square(y):
		return 0
	return 1 << (d > 0) + (y > 0)
a = []
for cas in range(int(input())):
	(d, p) = list(map(int, input().strip().split()))
	a.append(f(d, p))
for i in range(len(a)):
	print(a[i])


def issquare(x):
	if x == 1:
		return True
	if x == 2:
		return False
	if x == 3:
		return False
	a = x // 2
	alreadyseen = set([x])
	while a ** 2 != x:
		alreadyseen.add(a)
		if a == 0:
			return False
		a = (a + x // a) // 2
		if a in alreadyseen:
			return False
	return True
T = int(input())
for i in range(T):
	inpt = list(map(int, input().split()))
	D = inpt[0]
	P = inpt[1]
	numpairs = 0
	if D < 0:
		print(0)
		continue
	if P == 0:
		if D == 0:
			numpairs += 1
		else:
			numpairs += 4
	elif D ** 2 + 4 * P >= 0:
		disc = D ** 2 + 4 * P
		if issquare(disc):
			if D != 0 and disc != 0:
				numpairs += 4
			else:
				numpairs += 2
	print(numpairs)


import math

def is_square(n):
	r = int(math.sqrt(n))
	if n == r * r:
		return True
	else:
		return False
t = int(input())
for i in range(0, t):
	case = input().split()
	d = int(case[0])
	p = int(case[1])
	if d < 0:
		sol = 0
	elif d == 0 and p == 0:
		sol = 1
	elif d == 0 and p < 0:
		sol = 0
	elif d == 0 and p > 0:
		if is_square(p):
			sol = 2
		else:
			sol = 0
	elif d > 0 and p == 0:
		sol = 4
	elif d > 0 and p != 0:
		if d ** 2 + 4 * p < 0:
			sol = 0
		elif d ** 2 + 4 * p == 0:
			sol = 2
		elif d ** 2 + 4 * p > 0:
			if is_square(d ** 2 + 4 * p):
				sol = 4
			else:
				sol = 0
	print(sol)


t = int(input())
for _ in range(t):
	(d, p) = [int(x) for x in input().split(' ')]
	if d < 0:
		print(0)
		continue
	if d == 0 and p <= 0:
		print(1 if p == 0 else 0)
		continue
	if d == 0:
		y = int(p ** 0.5 + 0.1)
		print(2 if y * y == p else 0)
		continue
	x = d * d + 4 * p
	if x < 0:
		print(0)
		continue
	r = 0
	y = int(x ** 0.5 + 0.1)
	if (y - d) * (y + d) == 4 * p:
		r = 2 if y == 0 else 4
	print(r)


def ps(x):
	sq = int(pow(x, 0.5) + 0.001)
	if sq * sq == x:
		return sq
	else:
		return -1
for t in range(int(input())):
	(d, p) = map(int, input().split())
	D = d * d + 4 * p
	ans = 1
	if d == 0 and p == 0:
		ans = 1
	elif D < 0 or d < 0:
		ans = 0
	elif D == 0:
		if d % 2 == 0:
			ans = 2
	elif ps(D) > 0:
		sq = ps(D)
		if d == 0:
			ans = 2
		elif sq % 2 == d % 2:
			ans = 4
		else:
			ans = 0
	else:
		ans = 0
	print(ans)


from math import sqrt
from decimal import Decimal
T = int(input())
for i in range(T):
	(D, P) = list(map(int, input().split()))
	c = 0
	k = D ** 2 + 4 * P
	if D >= 0 and k >= 0 and (int(Decimal(k).sqrt()) == Decimal(k).sqrt()):
		(a1, a2) = ((D + Decimal(k).sqrt()) // 2, (D - Decimal(k).sqrt()) // 2)
		if a1 == a2:
			a = a1
			b = a1 - D
			if a == b:
				c = 1
			else:
				c = 2
		else:
			b1 = a1 - D
			b2 = a2 - D
			if a1 == b1:
				c += 1
			else:
				c += 2
			if a2 == b2:
				c += 1
			else:
				c += 2
	print(c)


import sys
import math
res = []
for a0 in range(int(input())):
	(D, P) = input().strip().split(' ')
	(D, P) = [int(D), int(P)]
	ans = 0
	if D < 0:
		ans = 0
	elif P == 0 and D == 0:
		ans = 1
	else:
		Roots = D ** 2 + 4 * P
		if Roots < 0:
			ans = 0
		elif Roots == 0:
			ans = 2
		elif Roots == int(math.sqrt(Roots)) ** 2:
			ans = 4
			if D == 0:
				ans = ans - 2
	print(ans)


from math import sqrt
t = int(input())

def f(d, p):
	if d < 0:
		return 0
	ret = set()
	D1 = d * d + 4 * p
	if D1 >= 0:
		a = round((d - sqrt(D1)) / 2)
		if a * (d - a) == -p:
			ret.add((a, d - a))
		a = round((d + sqrt(D1)) / 2)
		if a * (d - a) == -p:
			ret.add((a, d - a))
		a = round((-d - sqrt(D1)) / 2)
		if a * (-d - a) == -p:
			ret.add((a, -d - a))
		a = round((-d + sqrt(D1)) / 2)
		if a * (-d - a) == -p:
			ret.add((a, -d - a))
	return len(ret)
for _ in range(t):
	(d, p) = map(int, input().split())
	print(f(d, p))


from math import *
for t in range(int(input())):
	(d, p) = map(int, input().split())
	pair = 0
	if d == 0 and p == 0:
		pair = 1
	elif d < 0:
		pair = 0
	else:
		c = d * d + 4 * p
		if c == 0:
			pair = 2
		elif c < 0:
			pair = 0
		elif int(sqrt(c)) ** 2 == c:
			pair = 4
			if d == 0:
				pair = 2
	print(pair)


def getRoot(a, b, c):
	root = b ** 2 - 4 * a * c
	if root < 0:
		return None
	sqroot = int(root ** 0.5)
	if sqroot ** 2 == root and (-b + sqroot) % 2 == 0:
		return ((-b + sqroot) // 2, (-b - sqroot) // 2)
	return None
for _ in range(int(input())):
	(d, p) = map(int, input().split())
	root = getRoot(1, -d, -p)
	if root == None or d < 0:
		print(0)
	else:
		(x1, x2) = root
		res = set()
		if x1 != 0:
			res.add((x1, p / x1))
			res.add((-x1, p / -x1))
		else:
			res.add((x1, d))
			res.add((x1, -d))
		(x1, x2) = (x2, x1)
		if x1 != 0:
			res.add((x1, p / x1))
			res.add((-x1, p / -x1))
		else:
			res.add((x1, d))
			res.add((x1, -d))
		print(len(res))


t = int(input())

def sqroot(sq):
	if sq < 2:
		return sq
	head = 0
	tail = sq
	while tail - head > 1:
		mid = (tail + head) // 2
		tmp = mid ** 2
		if tmp == sq:
			return mid
		elif tmp > sq:
			tail = mid
		else:
			head = mid
	return -1
while t > 0:
	t -= 1
	(d, p) = [int(x) for x in input().split()]
	if d < 0:
		print(0)
		continue
	if p == 0:
		print(1 if d == 0 else 4)
		continue
	a_plus_b_sq = d ** 2 + 4 * p
	if a_plus_b_sq < 0:
		print(0)
		continue
	if a_plus_b_sq == 0:
		print(2)
		continue
	a_plus_b = sqroot(a_plus_b_sq)
	if a_plus_b < 0:
		print(0)
	else:
		print(2 if d == 0 else 4)


import math

def solve(d, p):
	x = d * d + 4 * p
	if d < 0 or x < 0:
		return 0
	y = int(math.sqrt(x))
	if y * y != x or d + y & 1:
		return 0
	if d and y:
		return 4
	if d or y:
		return 2
	return 1
T = int(input())
for t in range(T):
	(D, P) = map(int, input().split())
	print(solve(D, P))


t = int(input().strip())
for _ in range(t):
	(d, p) = map(int, input().strip().split())
	cont = 0
	if d < 0:
		print(0)
		continue
	Det = d ** 2 + 4 * p
	if Det < 0:
		print(0)
		continue
	rD = int(Det ** 0.5)
	if rD * rD != Det or (rD - d) % 2 != 0:
		print(0)
		continue
	i = (-d + rD) // 2
	j = i + d
	soln = {(-i, -j), (i, j), (-j, -i), (j, i)}
	print(len(soln))


for i in range(int(input())):
	(d, p) = input().strip().split()
	(d, p) = [int(d), int(p)]
	delta = d ** 2 + 4 * p
	if d < 0:
		print(0)
	elif delta < 0:
		print(0)
	elif delta > 0:
		if delta == round(delta ** 0.5) ** 2:
			if d == 0:
				print(2)
			else:
				print(4)
		else:
			print(0)
	elif d == 0:
		print(1)
	else:
		print(2)


import math

def num_pairs(d, p):
	if d < 0:
		return 0
	x = d * d + 4 * p
	if x < 0:
		return 0
	x_sqrt = int(math.sqrt(x))
	if x_sqrt * x_sqrt != x:
		return 0
	if x == 0:
		if d == 0:
			return 1
		else:
			return 2
	if d == 0:
		return 2
	return 4
num_probs = int(input())
for prob in range(num_probs):
	(d, p) = (int(x) for x in input().split())
	print(num_pairs(d, p))


def solve(D, P):
	if D < 0:
		return 0
	if D * D + 4 * P < 0:
		return 0
	r = int((D * D + 4 * P) ** 0.5)
	if r * r != D * D + 4 * P:
		return 0
	if (r - D) % 2 != 0:
		return 0
	S = set()
	for a in ((r - D) // 2, (-r - D) // 2):
		if a == 0:
			b = D
		else:
			if P % a != 0:
				continue
			b = P // a
		S |= {(a, b), (b, a), (-a, -b), (-b, -a)}
	return len(S)
T = int(input())
for _ in range(T):
	(D, P) = map(int, input().split())
	print(solve(D, P))


import sys, math
T = int(sys.stdin.readline())
for _ in range(T):
	(D, P) = map(int, sys.stdin.readline().strip().split())
	if D < 0:
		print(0)
		continue
	if not P:
		if D > 0:
			print(4)
		elif not D:
			print(1)
		continue
	if not D:
		print(0 if P <= 0 or P != int(math.sqrt(P)) ** 2 else 2)
		continue
	rr = 4 * P + D * D
	if not rr:
		print(0 if D % 2 else 2)
		continue
	if rr < 0:
		print(0)
		continue
	r = math.sqrt(rr)
	if int(r) ** 2 != rr:
		print(0)
		continue
	if not (D + int(r)) % 2:
		print(4)


import math

def solution(a, b, c):

	def int_sqrt(n):
		if n >= 0:
			s = int(math.sqrt(n))
			if s * s == n:
				return s
		return None
	result = set()
	if d < 0:
		return result
	delta = b * b - 4 * a * c
	sqrt_delta = int_sqrt(delta)
	if sqrt_delta is None:
		return result

	def add_solution(double_x):
		if double_x % (2 * a) == 0:
			x = double_x // (2 * a)
			result.add(x)
	if sqrt_delta >= 0:
		add_solution(-b - sqrt_delta)
		add_solution(-b + sqrt_delta)
	return result
for _ in range(int(input())):
	(d, p) = map(int, input().split())
	a_set = solution(1, -d, -p) | solution(1, d, -p)
	s = set()
	for a in a_set:
		if a != 0:
			if p % a == 0:
				s.add((a, p // a))
		else:
			s.add((0, d))
			s.add((0, -d))
	print(len(s))


from math import sqrt
cases = int(input())
for i in range(0, cases):
	(d, p) = [int(num) for num in input().split()]
	if d ** 2 + 4 * p < 0:
		print(0)
		continue
	sr = int(sqrt(d ** 2 + 4 * p))
	if d < 0:
		print(0)
	elif d == 0:
		if p == 0:
			print(1)
			continue
		elif int(sqrt(p)) ** 2 == p:
			print(2)
			continue
		else:
			print(0)
	elif p == 0:
		print(4)
	elif sr ** 2 == d ** 2 + 4 * p and (d + sr) % 2 == 0 and (2 * p % (d + sr) == 0):
		if d ** 2 + 4 * p == 0:
			print(2)
		else:
			print(4)
	else:
		print(0)


T = int(input().strip())
sqrt = lambda x: x ** 0.5

def solution(D, P):
	result = 0
	seen = 0
	if D < 0:
		return 0
	if D == 0 and P == 0:
		return 1
	discriminant = sqrt(D ** 2 + 4 * P)
	if isinstance(discriminant, complex):
		return 0
	A = [(D + discriminant) // 2, (D - discriminant) // 2, (-D + discriminant) // 2, (-D - discriminant) // 2]
	B = [A[0] - D, A[1] - D, A[2] + D, A[3] + D]
	for (a, b) in set(zip(A, B)):
		if abs(a - b) == D and a * b == P:
			result += 1
	return result
for _ in range(T):
	(D, P) = [int(x) for x in input().strip().split(' ')]
	print(solution(D, P))


def bitize(x):
	L = []
	while x > 0:
		L.append(x % 2)
		x //= 2
	if not L:
		L.append(0)
	return L

def gcd_matrix(a, b):
	M = [[1, 0, a], [0, 1, b]]
	if a > 0 and b > 0:
		while M[0][2] != 0 and M[1][2] != 0:
			if M[0][2] > M[1][2]:
				multiple = M[0][2] // M[1][2]
				for i in range(0, 3):
					M[0][i] -= multiple * M[1][i]
			elif M[0][2] < M[1][2]:
				multiple = M[1][2] // M[0][2]
				for i in range(0, 3):
					M[1][i] -= multiple * M[0][i]
			elif M[0][2] == M[1][2]:
				return M[0][2]
		if M[0][2] == 0:
			return M[1][2]
		else:
			return M[0][2]
	elif a != 0 and b != 0:
		return gcd_matrix(abs(a), abs(b))
	else:
		return 0

def is_prime(x, L=[]):
	sqrt = int(pow(x, 0.5)) + 1
	PL = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 16, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
	if not L:
		if x < 210:
			if x in PL:
				return True
			else:
				return False
		else:
			for p in PL:
				if x % p == 0:
					return False
			for i in range(1, sqrt // 210 + 1):
				for j in [1, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 121, 127, 131, 137, 139, 143, 149, 151, 157, 163, 167, 169, 173, 179, 181, 187, 191, 193, 197, 199, 209]:
					if x % (210 * i + j) == 0:
						return False
		return True
	elif x != 0 and x != 1:
		j = 0
		while j < len(L) and L[j] <= sqrt:
			if x % L[j] == 0:
				return False
			j += 1
	else:
		return False
	return True

def prime_list(n):
	L = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
	for k in range(1, n // 210):
		for j in [1, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 121, 127, 131, 137, 139, 143, 149, 151, 157, 163, 167, 169, 173, 179, 181, 187, 191, 193, 197, 199, 209]:
			if is_prime(210 * k + j, L):
				L.append(210 * k + j)
	k = n // 210
	for j in [1, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 121, 127, 131, 137, 139, 143, 149, 151, 157, 163, 167, 169, 173, 179, 181, 187, 191, 193, 197, 199, 209]:
		if is_prime(210 * k + j, L) and 210 * k + j <= n:
			L.append(210 * k + j)
	return L

def pow_mod(a, b, m):
	b_bits = bitize(b)
	a_powers = [0 for x in b_bits]
	a_powers[0] = a % m
	if b_bits[0]:
		total = a % m
	else:
		total = 1
	for i in range(1, len(b_bits)):
		a_powers[i] = a_powers[i - 1] * a_powers[i - 1] % m
		if b_bits[i]:
			total *= a_powers[i]
			total %= m
	return total % m

def is_miller_rabin(x, num_tests=3):
	k = x - 1
	e = 0
	while k % 2 == 0:
		e += 1
		k //= 2
	tests = [False for _ in range(0, num_tests)]
	A = [2, 3, 5]
	for j in range(3, num_tests):
		A.append(random.randint(7, x - 1))
	if x < 2047:
		num_tests = 1
	elif x < 1373653:
		num_tests = 2
	elif x < 25326001:
		num_tests = 3
	for i in range(0, num_tests):
		ak = pow_mod(A[i], k, x)
		if ak == 1:
			tests[i] = True
		else:
			for j in range(0, e):
				if ak == x - 1:
					tests[i] = True
				ak = ak * ak % x
	for i in range(0, num_tests):
		if not tests[i]:
			return False
	return True

def factor(n, PL):
	sqrt = int(pow(n, 0.5)) + 1
	factor_list = []
	if not PL:
		PL = prime_list(sqrt)
	for p in PL:
		if n % p == 0:
			elet = [p, 0]
			while not n % p:
				n //= p
				elet[1] += 1
			factor_list.append(elet)
	if n != 1:
		if is_miller_rabin(n):
			factor_list.append([n, 1])
	return factor_list

def euler_totient_with_factors(n, factors):
	for ar_elet in factors:
		n //= ar_elet[0]
		n *= ar_elet[0] - 1
	return n

def compute_factor_partitions(factors, D, P, pos, index=0, currentA=1):
	prime_power = 1
	total = 0
	if index < len(factors):
		for i in range(0, factors[index][1] + 1):
			total += compute_factor_partitions(factors, D, P, pos, index + 1, currentA * prime_power)
			prime_power *= factors[index][0]
	elif D == abs(currentA - pos * P // currentA):
		return 2
	else:
		return 0
	return total

def number_of_difference_products(D, P, PL):
	p_pos = 1
	if D < 0:
		return 0
	if P < 0:
		P *= -1
		p_pos = -1
	elif P == 0 and D != 0:
		return 4
	elif P == 0 and D == 0:
		return 1
	factors = factor(P, PL)
	return compute_factor_partitions(factors, D, P, p_pos)
PL = prime_list(32000)
Q = int(input())
for _ in range(0, Q):
	(D, P) = [int(a) for a in input().split()]
	print(number_of_difference_products(D, P, PL))


import math

def perfectSquare(n):
	sr = math.floor(math.sqrt(n) + 0.5)
	if n == sr * sr:
		return (True, sr)
	else:
		return (False, None)
nt = int(input())
for _ in range(nt):
	(d, p) = map(int, input().strip().split())
	if d < 0:
		np = 0
	elif p == 0:
		np = 1 if d == 0 else 4
	elif p == 1:
		np = 2 if d == 0 else 0
	elif p == -1:
		np = 2 if d == 2 else 0
	elif d == 0:
		if p < 0:
			np = 0
		else:
			(bl, rt) = perfectSquare(p)
			np = 2 if bl else 0
	else:
		disc = d * d + 4 * p
		if disc < 0:
			np = 0
		elif disc == 0:
			np = 0 if d % 2 else 2
		else:
			(ps, rt) = perfectSquare(disc)
			if ps:
				np = 0 if (d + rt) % 2 else 4
			else:
				np = 0
	print(np)


import sys

def mult_gen(n):
	if n < 1:
		raise Exception('n<1')
	r = int(pow(n, 0.5))
	for u in range(1, r + 1):
		if n % u == 0:
			yield (u, n // u)

def uv_gen(d, p):
	(A, B, C) = (1, d, -p)
	print('A=%d B=%d C=%d' % (A, B, C), file=sys.stderr)
	D = B * B - 4 * A * C
	print('D=%d' % (D,), file=sys.stderr)
	if D < 0:
		return
	r = int(pow(D, 0.5))
	if r * r != D:
		return
	if (-B - r) % 2 == 1:
		return
	(u1, u2) = ((-B - r) // 2, (-B + r) // 2)
	ss = set()
	for u in (u1, u2):
		v = u + d
		ss.add((u, v))
		ss.add((v, u))
	yield from ss

def solve(d, p):
	print('\nd=%d p=%d' % (d, p), file=sys.stderr)
	if d < 0:
		return 0
	cnt = 0
	for (u, v) in uv_gen(d, p):
		cnt += 1
		print('u=%d v=%d' % (u, v), file=sys.stderr)
	return cnt
t = int(input())
for _ in range(t):
	(d, p) = map(int, input().split())
	print(solve(d, p))


import math
for _ in range(int(input())):
	(D, P) = [int(x) for x in input().split(' ')]
	if D < 0:
		print(0)
	elif P == 1:
		if D == 0:
			print(2)
		else:
			print(0)
	elif P == -1:
		if D == 2:
			print(2)
		else:
			print(0)
	elif D == 0:
		if P < 0:
			print(0)
		elif P == 0:
			print(1)
		else:
			sqrt = math.sqrt(P)
			int_sqrt = int(sqrt)
			if int_sqrt ** 2 == P:
				print(2)
			else:
				print(0)
	else:
		disc = D ** 2 + 4 * P
		if disc < 0:
			print(0)
			continue
		sqrt = math.sqrt(disc)
		if int(sqrt) ** 2 != disc:
			print(0)
		elif disc == 0:
			if D % 2 == 0:
				print(2)
			else:
				print(0)
		elif (D - math.sqrt(disc)) % 2 == 0:
			print(4)
		else:
			print(0)


t = int(input())
for _ in range(t):
	(d, p) = map(int, input().split())
	if d < 0:
		print(0)
		continue
	if p == 0:
		if d == 0:
			print(1)
		else:
			print(4)
		continue
	discriminant = d ** 2 + 4 * p
	if discriminant < 0:
		print(0)
		continue
	discriminant_root = int((d ** 2 + 4 * p) ** 0.5)
	if discriminant_root ** 2 != discriminant or (d + discriminant_root) % 2 == 1:
		print(0)
		continue
	solutions = set()
	b = (-d - discriminant_root) // 2
	if p % b == 0 and p // b - b >= 0:
		solutions.add(b)
	b = (-d + discriminant_root) // 2
	if p % b == 0 and p // b - b >= 0:
		solutions.add(b)
	b = (d - discriminant_root) // 2
	if p % b == 0 and p // b - b < 0:
		solutions.add(b)
	b = (d + discriminant_root) // 2
	if p % b == 0 and p // b - b < 0:
		solutions.add(b)
	print(len(solutions))


def issqr(n):
	if n < 0:
		return False
	else:
		return int(n ** 0.5 + 0.5) ** 2 == n

def calc(d, p):
	if d < 0:
		return 0
	elif d == 0:
		if p < 0:
			return 0
		elif p == 0:
			return 1
		elif issqr(p):
			return 2
		else:
			return 0
	else:
		delta = d * d + 4 * p
		if delta < 0:
			return 0
		elif delta == 0:
			if d % 2 == 0:
				return 2
			else:
				return 0
		elif issqr(delta):
			m = int(delta ** 0.5 + 0.5)
			if (d + m) % 2 == 1:
				return 0
			else:
				return 4
		else:
			return 0
T = int(input())
for i in range(T):
	[D, P] = input().split()
	[D, P] = [int(D), int(P)]
	print(calc(D, P))


from decimal import *
getcontext().prec = 21
T = int(input().strip())
for _ in range(T):
	(D, P) = input().strip().split(' ')
	(D, P) = [int(D), int(P)]
	if D < 0:
		print(0)
	elif D == 0:
		if P == 0:
			print(1)
		elif P < 0:
			print(0)
		else:
			E = P ** 0.5
			if E == int(E):
				print(2)
			else:
				print(0)
	elif P == 0:
		print(4)
	else:
		E2 = D ** 2 + 4 * P
		if E2 < 0:
			print(0)
		else:
			E = Decimal(E2) ** Decimal(0.5)
			if E != int(E):
				print(0)
			else:
				(rE, rD) = [int(E) % 2, D % 2]
				if rE != rD:
					print(0)
				elif E == 0:
					print(2)
				else:
					B = -D + E
					A = D + E
					if A == -B:
						print(2)
					else:
						print(4)


from decimal import *
getcontext().prec = 21

def solve_quadratic(b, c):
	determinant = b ** 2 - 4 * c
	if determinant < 0:
		return (complex(0, 1), complex(0, 1))
	D = Decimal(b ** 2 - 4 * c) ** Decimal(0.5)
	if determinant < 0:
		return (complex(0, 1), complex(0, 1))
	return (Decimal((-b + D) / Decimal(2)), Decimal((-b - D) / Decimal(2)))
T = int(input())
for _ in range(T):
	[D, P] = input().split()
	[D, P] = [int(D), int(P)]
	if D < 0:
		print(0)
		continue
	(x1, x2) = solve_quadratic(D, -1 * P)
	if type(x1) == complex:
		print(0)
	elif int(x1) != x1 or int(x2) != x2:
		print(0)
	else:
		x1 = int(x1)
		x2 = int(x2)
		solutions = set()
		solutions.add((x1, x2))
		solutions.add((x2, x1))
		solutions.add((-1 * x1, -1 * x2))
		solutions.add((-1 * x2, -1 * x1))
		print(len(solutions))


from math import sqrt
for _ in range(int(input())):
	(d, p) = map(int, input().split())
	if d < 0:
		print(0)
		continue
	nb = 0
	D = d * d + 4 * p
	if D < 0:
		print(0)
		continue
	b = int((-d - sqrt(D)) / 2)
	a = d + b
	if a * b == p:
		nb += 1
		if a != b:
			nb += 1
	if D != 0:
		b = int((-d + sqrt(D)) / 2)
		a = d + b
		if a * b == p:
			nb += 1
			if a != b:
				nb += 1
	print(nb)


import sys
from math import sqrt

def isqrt(n):
	if n <= 0:
		return 0
	x = int(sqrt(n) * (1 + 1e-12))
	while True:
		y = x + n // x >> 1
		if y >= x:
			return x
		x = y

def solve():
	stdin = sys.stdin
	write = sys.stdout.write
	t = int(stdin.readline())
	for _ in range(t):
		(D, P) = map(int, stdin.readline().split())
		ans = set()
		if D >= 0:
			r = D * D + 4 * P
			if r >= 0:
				rt = isqrt(r)
				if rt * rt == r:
					num = -D + rt
					if not num & 1:
						b = num // 2
						a = D + b
						ans.add((a, b))
						b = (-D - rt) // 2
						a = D + b
						ans.add((a, b))
					num = D + rt
					if not num & 1:
						b = num // 2
						a = -D + b
						ans.add((a, b))
						b = (D - rt) // 2
						a = -D + b
						ans.add((a, b))
		write('%d\n' % len(ans))
solve()


def isqrt(n):
	x = n
	y = (x + 1) // 2
	while y < x:
		x = y
		y = (x + n // x) // 2
	return x
for _ in range(int(input())):
	ans = 0
	(d, p) = map(int, input().split())
	disc4 = d * d + 4 * p
	if d >= 0 and disc4 >= 0:
		isq2 = isqrt(disc4)
		x0 = (isq2 - d) // 2
		x1 = (-isq2 - d) // 2
		if x0 * (x0 + d) == p:
			ans += 1 + (d > 0)
		if x0 != x1 and x1 * (x1 + d) == p:
			ans += 1 + (d > 0)
	print(ans)


import math

def pr(x):
	if x < 0:
		return 0
	sr = math.sqrt(x)
	ss = math.floor(sr)
	if sr - math.floor(sr) == 0 and ss ** 2 == x:
		return 1
	else:
		return 0
t = int(input())
while t > 0:
	(d, p) = [int(i) for i in input().split()]
	dd = d * d + 4 * p
	if d < 0 or pr(dd) == 0:
		print(0)
	elif d == 0:
		if p == 0:
			print(1)
		else:
			print(2)
	elif dd == 0:
		print(2)
	else:
		print(4)
	t -= 1


import math
t = int(input())
while t > 0:
	(D, P) = [int(x) for x in input().split()]
	if D < 0:
		print(0)
	else:
		import math
		dis = D ** 2 + 4 * P
		if dis < 0:
			print(0)
		else:
			rt = math.sqrt(dis)
			(a, b, c, d) = ((D + rt) / 2, (D - rt) / 2, (-D + rt) / 2, (-D - rt) / 2)
			myset = set()
			if a - int(a) == 0:
				myset.add(a)
			if b - int(b) == 0:
				myset.add(b)
			if c - int(c) == 0:
				myset.add(c)
			if d - int(d) == 0:
				myset.add(d)
			mypair = set()
			for x in myset:
				(b1, b2) = (x - D, x + D)
				if b1 - int(b1 == 0):
					mypair.add((x, b1))
				if b2 - int(b2) == 0:
					mypair.add((x, b2))
			count = 0
			for x in mypair:
				(a, b) = (x[0], x[1])
				if a * b == P:
					count += 1
			print(count)
	t -= 1


import math
t = int(input())
for i in range(t):
	(d, p) = map(int, input().split())
	if d < 0:
		ans = 0
	elif d == 0 and p == 0:
		ans = 1
	elif d == 0:
		if p < 0:
			ans = 0
		else:
			k = int(math.sqrt(p))
			if k * k == p:
				ans = 2
			else:
				ans = 0
	elif p == 0:
		ans = 4
	else:
		ans = 0
		D = d * d + 4 * p
		if D < 0:
			ans = 0
		elif D == 0:
			if d % 2 == 0:
				ans = 2
			else:
				ans = 0
		else:
			k = int(math.sqrt(D))
			if k * k != D:
				ans = 0
			elif (d + k) % 2 == 0:
				ans = 4
			else:
				ans = 0
	print(ans)


from math import sqrt
for _ in range(int(input())):
	(d, p) = map(int, input().split())
	if d < 0:
		print(0)
		continue
	nb = 0
	D = d * d + 4 * p
	if D < 0:
		print(0)
		continue
	b = int((-d - sqrt(D)) / 2)
	a = d + b
	if a * b == p:
		nb += 1
		if a != b:
			nb += 1
	if D != 0:
		b = int((-d + sqrt(D)) / 2)
		a = d + b
		if a * b == p:
			nb += 1
			if a != b:
				nb += 1
	print(nb)


import os
import sys
from math import sqrt
for _ in range(int(input())):
	(d, p) = map(int, input().split())
	if d < 0:
		print(0)
		continue
	nb = 0
	D = d * d + 4 * p
	if D < 0:
		print(0)
		continue
	b = int((-d - sqrt(D)) / 2)
	a = d + b
	if a * b == p:
		nb += 1
		if a != b:
			nb += 1
	if D != 0:
		b = int((-d + sqrt(D)) / 2)
		a = d + b
		if a * b == p:
			nb += 1
			if a != b:
				nb += 1
	print(nb)


import os
import sys
import math

def is_a_sq(number):
	root = math.sqrt(number)
	if int(root + 0.5) ** 2 == number:
		return 1
	else:
		return 0

def solve(d, p):
	if d < 0:
		return 0
	elif d == 0:
		if p > 0:
			if is_a_sq(p) == 0:
				return 0
			else:
				return 2
		elif p == 0:
			return 1
		else:
			return 0
	else:
		s = pow(d, 2) + 4 * p
		if s < 0:
			return 0
		elif s == 0:
			return 2
		elif is_a_sq(s) == 0:
			return 0
		else:
			return 4
t = int(input())
for t_itr in range(t):
	dp = input().split()
	d = int(dp[0])
	p = int(dp[1])
	result = solve(d, p)
	print(result)


from math import sqrt

def solve(d, p):
	if d < 0:
		return 0
	if p == 0:
		return d == 0 and 1 or 4
	if d * d + 4 * p >= 0:
		div = round((-d + sqrt(d * d + 4 * p)) / 2)
		if div and p % div == 0 and (abs(p // div - div) == d):
			return p // div == abs(div) and 2 or 4
	return 0
for _ in range(int(input())):
	print(solve(*map(int, input().split())))


from math import sqrt
for _ in range(int(input())):
	(d, p) = map(int, input().split())
	if d < 0:
		print(0)
		continue
	nb = 0
	D = d * d + 4 * p
	if D < 0:
		print(0)
		continue
	b = int((-d - sqrt(D)) / 2)
	a = d + b
	if a * b == p:
		nb += 1
		if a != b:
			nb += 1
	if D != 0:
		b = int((-d + sqrt(D)) / 2)
		a = d + b
		if a * b == p:
			nb += 1
			if a != b:
				nb += 1
	print(nb)


import os
import sys
from math import sqrt
for _ in range(int(input())):
	(d, p) = map(int, input().split())
	if d < 0:
		print(0)
		continue
	nb = 0
	D = d * d + 4 * p
	if D < 0:
		print(0)
		continue
	b = int((-d - sqrt(D)) / 2)
	a = d + b
	if a * b == p:
		nb += 1
		if a != b:
			nb += 1
	if D != 0:
		b = int((-d + sqrt(D)) / 2)
		a = d + b
		if a * b == p:
			nb += 1
			if a != b:
				nb += 1
	print(nb)


from math import sqrt
for _ in range(int(input())):
	(d, p) = map(int, input().split(' '))
	if d < 0:
		print(0)
	elif d == 0:
		if p == 0:
			print(1)
		else:
			print(2 if p > 0 and int(sqrt(p) + 0.5) ** 2 == p else 0)
	else:
		x = d ** 2 + 4 * p
		if x == 0:
			print(2)
		elif x < 0:
			print(0)
		else:
			xx = int(sqrt(x) + 0.5)
			print(2 * (int(((d + xx) / 2).is_integer()) + int(((d - xx) / 2).is_integer())) if xx ** 2 == x else 0)