for _ in range(int(input())):
n, m = map(int, input().split())
print(sum((a ** 2 + b ** 2 + m) % (a * b) == 0 for a in range(1, n) for b in range(a + 1, n)))
for _ in range(int(input())):
n, m = map(int, input().split())
print(sum((a ** 2 + b ** 2 + m) % (a * b) == 0 for a in range(1, n) for b in range(a + 1, n)))