Chef is playing a game with his brother Chefu. He asked Chefu to choose a positive integer $N$, multiply it by a given integer $A$, then choose a divisor of $N$ (possibly $N$ itself) and add it to the product. Let's denote the resulting integer by $M$; more formally, $M = A \cdot N + d$, where $d$ is some divisor of $N$.
Chefu told Chef the value of $M$ and now, Chef should guess $N$. Help him find all values of $N$ which Chefu could have chosen.

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains two space-separated integers $A$ and $M$.

-----Output-----
For each test case, print two lines. The first line should contain a single integer $C$ denoting the number of possible values of $N$. The second line should contain $C$ space-separated integers denoting all possible values of $N$ in increasing order.
It is guaranteed that the sum of $C$ over all test cases does not exceed $10^7$.

-----Constraints-----
- $1 \le T \le 100$
- $2 \le M \le 10^{10}$
- $1 \le A < M$

-----Subtasks-----
Subtask #1 (50 points):
- $M \le 10^6$
Subtask #2 (50 points): original constraints

-----Example Input-----
3
3 35
5 50
4 65

-----Example Output-----
1
10
0

3
13 15 16

Difficulty: MEDIUM_HARD
Tags: Number theory

Mathematics

Starter code:

Solutions:
import math

def getDivisors(n):
	i = 1
	arr = []
	while i <= math.sqrt(n):
		if n % i == 0:
			if n / i == i:
				arr.append(i)
			else:
				arr.append(i)
				arr.append(n / i)
		i += 1
	return arr
t = int(input())
for _ in range(t):
	(a, m) = list(map(int, input().split()))
	ans = []
	counter = 0
	fac = getDivisors(m)
	for f in fac:
		if (f - 1) % a == 0 and f != 1:
			q = (f - 1) // a
			d = m // f
			N = q * d
			ans.append(int(N))
			counter += 1
	ans.sort()
	print(counter)
	for i in range(len(ans)):
		print(ans[i], end=' ')
	print()


from math import sqrt
t = int(input())
for jack in range(t):
	(a, m) = map(int, input().split())
	fac = [1]
	n = []
	for i in range(2, int(sqrt(m)) + 1):
		if m % i == 0:
			fac.append(i)
			if i != m // i:
				fac.append(m // i)
	fac.sort(reverse=True)
	for i in range(len(fac)):
		if (m // fac[i] - 1) % a == 0:
			x = (m // fac[i] - 1) // a
			n.append(x * fac[i])
	print(len(n))
	for i in n:
		print(i, end=' ')
	print()


t = int(input())
for _ in range(t):
	(a, m) = map(int, input().split())
	l = []
	for i in range(1, int(pow(m, 0.5)) + 1):
		if not m % i:
			l.append(i)
			if i != m // i:
				l.append(m // i)
	k = []
	for i in l:
		x = m - i
		x = x / a
		if x == int(x) and x != 0:
			if not x % i:
				k.append(int(x))
	print(len(k))
	k.sort()
	print(*k)


import math

def divisors(m):
	l1 = []
	for i in range(1, int(math.sqrt(m)) + 1):
		if m % i == 0:
			if m // i == i:
				l1.append(i)
			else:
				l1.append(i)
				l1.append(m // i)
	return l1

def printDivisors(n):
	list = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				print(i, end=' ')
			else:
				print(i, end=' ')
				list.append(int(n / i))
s = ''
for _ in range(int(input())):
	l1 = [int(y) for y in input().split()]
	a = l1[0]
	m = l1[1]
	l1 = divisors(m)
	l2 = []
	for d in l1:
		n = (m - d) / a
		if n >= d and n % 1 == 0 and (n % d == 0):
			l2.append(int(n))
	l2.sort()
	s = s + str(len(l2)) + '\n'
	for i in l2:
		s = s + str(i) + ' '
	s += '\n'
print(s)


import numbers
import math

def find_factor(n):
	res = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n / i == i:
				res.append(i)
			else:
				res.append(i)
				res.append(int(n / i))
		i = i + 1
	return sorted(res)
for _ in range(int(input())):
	(a, m) = list(map(int, input().split()))
	factors = []
	factors = find_factor(m)
	ans = 0
	ans_list = []
	for i in factors:
		if (i - 1) % a == 0 and i != 1:
			ans = ans + 1
			q = int((i - 1) / a)
			d = int(m / i)
			ans_list.append(q * d)
	print(ans)
	for x in ans_list:
		print(x, end=' ')
	print('')


import math
t = int(input())
for i in range(t):
	(a, m) = map(int, input().split())
	cnt = 0
	arr = []
	factors = []
	for j in range(1, int(math.sqrt(m)) + 1):
		if m % j == 0:
			factors.append(j)
			if m // j != j:
				factors.append(m // j)
	for f in factors:
		if (f - 1) % a == 0 and f != 1:
			q = (f - 1) // a
			d = m // f
			n = q * d
			arr.append(n)
			cnt += 1
	arr.sort()
	print(cnt)
	print(' '.join(map(str, arr)))


import math
for _ in range(int(input())):
	(a, m) = map(int, input().split())
	factors = []
	for i in range(1, int(math.sqrt(m)) + 1):
		if m % i == 0:
			factors.append(i)
			if i != m // i:
				factors.append(m // i)
	N = []
	count = 0
	for f in factors:
		if (f - 1) % a == 0:
			q = (f - 1) // a
			d = m // f
			if q * d != 0:
				N.append(q * d)
				count += 1
	print(count)
	if count == 0:
		print()
	else:
		N.sort()
		print(*N)


def getdiv(m):
	i = 1
	res = []
	while i * i <= m:
		if m % i == 0:
			res.append(i)
			if m // i != i:
				res.append(m // i)
		i += 1
	return res
for _ in range(int(input())):
	(a, m) = map(int, input().split())
	div = getdiv(m)
	res = []
	for i in div:
		n = (m - i) / a
		if n >= i and n % 1 == 0 and (n % i == 0):
			res.append(int(n))
	res.sort()
	print(len(res))
	for i in res:
		print(i, end=' ')
	print('')


t = int(input())
while t > 0:
	(a, m) = list(map(int, input().split()))
	l = m // a
	arr = []
	for i in range(l, 0, -1):
		if m - i * a > i:
			break
		if m - a * i == 0:
			continue
		elif i % (m - a * i) == 0:
			arr.append(i)
	print(len(arr))
	arr = arr[::-1]
	print(' '.join(map(str, arr)))
	t = t - 1


ans = []
for i in range(int(input())):
	lst = []
	l = []
	(a, m) = map(int, input().split())
	for d in range(1, int(m ** 0.5) + 1):
		if m % d == 0:
			r = m / d - 1
			if r % a == 0:
				q = r / a
				if q != 0:
					lst.append(int(d * q))
			rg = d - 1
			if rg % a == 0:
				q = rg / a
				if q != 0:
					lst.append(int(q * m / d))
	if m % (a + 1) == 0:
		lst.append(int(m // (a + 1)))
	for i in lst:
		if i not in l:
			l.append(i)
	ans.append(len(l))
	ans.append(l)
for i in range(0, len(ans), 2):
	print(ans[i])
	ans[i + 1].sort()
	for j in range(0, ans[i]):
		print(ans[i + 1][j], end=' ')
	print()


from functools import reduce

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
for _ in range(int(input())):
	(a, m) = map(int, input().split())
	l = []
	f = list(factors(m))
	f.sort()
	f.pop()
	for i in f:
		if (m - i) / a % i == 0:
			l.append(int((m - i) / a))
	print(len(l))
	l.sort()
	for i in l:
		print(i, end=' ')
	print('')


for _ in range(int(input())):
	(a, m) = map(int, input().split())
	d = set()
	sqm = int(m ** 0.5 + 1)
	for i in range(1, sqm):
		if m % i == 0:
			d.add(i)
			d.add(int(m / i))
	n = set()
	for dd in d:
		q = (m / dd - 1) / a
		if int(q) == q:
			n.add(int(q) * dd)
	n.remove(0)
	s = sorted(n)
	ans = ''
	print(len(s))
	for x in s:
		print(x, end=' ')
	print()


import math

def divisors(m):
	l1 = []
	for i in range(1, int(math.sqrt(m)) + 1):
		if m % i == 0:
			if m // i == i:
				l1.append(i)
			else:
				l1.append(i)
				l1.append(m // i)
	return l1
s = ''
test = int(input())
while test != 0:
	(a, m) = list(map(int, input().split()))
	l = divisors(m)
	ans = []
	for d in l:
		n = (m - d) / a
		if n >= d and n % 1 == 0 and (n % d == 0):
			ans.append(int(n))
	ans.sort()
	s = s + str(len(ans)) + '\n'
	for i in ans:
		s = s + str(i) + ' '
	s += '\n'
	test = test - 1
print(s)


import math
t = int(input())
while t > 0:
	(a, m) = list(map(int, input().split()))
	factors = []
	l = int(math.sqrt(m))
	for i in range(1, l + 1):
		if m % i == 0:
			if i == m // i:
				factors.append(i)
			else:
				factors.append(i)
				factors.append(m // i)
	potential = set()
	for i in factors:
		r = m // i - 1
		if r % a == 0:
			q = r // a
			pot = q * i
			if pot != 0:
				if pot not in potential:
					potential.add(pot)
	ans = []
	for i in potential:
		ans.append(i)
	ans.sort()
	print(len(ans))
	for i in ans:
		print(i, end=' ')
	print()
	t -= 1


from math import ceil

def solve():
	(a, m) = list(map(int, input().split()))
	n = ceil(m / a)
	all_vals = []
	while a * n + n >= m:
		if m - a * n > 0 and a * n + (m - a * n) * (n % (m - a * n) == 0) == m:
			all_vals.append(n)
		n -= 1
	print(len(all_vals))
	if len(all_vals) != 0:
		print(*all_vals[::-1])
for T in range(int(input())):
	solve()


import math
t = int(input())
while t > 0:
	t -= 1
	(a, m) = list(map(int, input().split()))
	d = 1
	ans = set()
	limit = int(math.sqrt(m) + 1)
	while d < limit + 1:
		if m % d == 0:
			num = m // d - 1
			if num % a == 0:
				ans.add(num // a * d)
			d2 = m // d
			num = m // d2 - 1
			if num > 0 and num % a == 0:
				ans.add(num // a * d2)
		d += 1
	print(len(ans))
	print(*sorted(ans))


t = int(input())
for i in range(t):
	am = input().split()
	a = int(am[0])
	m = int(am[1])
	l = list()
	c = 0
	indx = m // a
	for i in range(indx, 0, -1):
		diff = m - i * a
		if diff > i:
			break
		if diff != 0:
			if i % diff == 0:
				c += 1
				l.append(i)
	l.sort()
	res = str()
	for i in range(len(l)):
		res = res + str(l[i]) + ' '
	res.rstrip()
	print(c)
	print(res)


import math
for t in range(int(input())):
	l1 = [int(y) for y in input().split()]
	a = l1[0]
	m = l1[1]
	s = ''
	l1 = []
	for i in range(1, int(math.sqrt(m)) + 1):
		if m % i == 0:
			if m // i == i:
				l1.append(i)
			else:
				l1.append(i)
				l1.append(m // i)
	l2 = []
	for d in l1:
		n = (m - d) / a
		if n >= d and n % 1 == 0 and (n % d == 0):
			l2.append(int(n))
	l2.sort()
	s = s + str(len(l2)) + '\n'
	for i in l2:
		s = s + str(i) + ' '
	print(s)


def factors(m):
	k = []
	for i in range(2, int(m ** 0.5) + 1):
		if m % i == 0:
			k.append(i)
			k.append(m // i)
	k.append(m)
	return k
t = int(input())
for _ in range(t):
	(a, m) = map(int, input().split())
	k = factors(m)
	c = 0
	h = []
	for f in k:
		if (f - 1) % a == 0:
			q = (f - 1) // a
			d = m // f
			n = q * d
			h.append(n)
			c = c + 1
	h.sort()
	print(c)
	print(*h)


import math

def printDivisors(n):
	i = 1
	f = []
	while i <= math.sqrt(n):
		if n % i == 0:
			if n / i == i:
				f.append(i)
			else:
				f.append(i)
				f.append(int(n / i))
		i = i + 1
	return f
for _ in range(int(input())):
	d = []
	(a, b) = map(int, input().split())
	c = printDivisors(b)
	for i in range(len(c)):
		if (b // c[i] - 1) % a == 0:
			d.append(b // a - c[i] // a)
	d.remove(0)
	d.sort()
	print(len(d))
	print(*d)


t = int(input())
for j in range(t):
	(a, m) = [int(x) for x in input().split()]
	count = 0
	li = []
	for i in range(1, int(m ** 0.5) + 1):
		if m % i == 0:
			li.append(i)
			li.append(m // i)
	l = []
	for i in li:
		if (m // i - 1) % a == 0:
			if (m // i - 1) // a * i == 0:
				continue
			l.append((m // i - 1) // a * i)
			count += 1
	print(count)
	for i in sorted(l):
		print(i, end=' ')
	print()


import math
t = int(input())
while t > 0:
	(a, m) = map(int, input().split(' '))
	n = int(math.sqrt(m))
	count = 0
	v = []
	for i in range(1, n + 1):
		if m % i == 0:
			if m // i == i:
				d = i
				s = (m / d - 1) % a
				r = (m / d - 1) // a
				if s == 0:
					if r != 0:
						count += 1
						v.append(r * d)
			else:
				d = i
				s = (m // d - 1) % a
				r = (m // d - 1) // a
				if s == 0:
					if r != 0:
						count += 1
						v.append(r * d)
				d = m // i
				s = (m // d - 1) % a
				r = (m // d - 1) // a
				if s == 0:
					if r != 0:
						count += 1
						v.append(r * d)
	print(count)
	v.sort()
	if count == 0:
		print()
	else:
		for i in range(count):
			print(v[i], end=' ')
		print()
	t -= 1


t = int(input())
for _ in range(t):
	d = []
	jj = 0
	(a, m) = list(map(int, input().split()))
	p = m // (a + 1)
	q = m // a
	for i in range(p, q + 1):
		if i != m / a:
			if i % (m - a * i) == 0:
				d.append(i)
	print(len(d))
	d.sort()
	for u in d:
		print(u, end=' ')
	print('\n', end='')


try:
	num = int(input())
	from functools import reduce

	def fact(n):
		return list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))
	while num > 0:
		arr = list(map(int, input().split()))
		a = arr[0]
		m = arr[1]
		factors = fact(m)
		ans = []
		for j in factors:
			if (j - 1) % a == 0:
				q = (j - 1) / a
				d = m / j
				ans.append(q * d)
		ans.sort()
		ans = ans[1:]
		size = len(ans)
		print(size)
		i = 0
		while i < size:
			print(int(ans[i]), end=' ')
			i += 1
		print('\n', end='')
		num -= 1
except:
	pass


T = int(input())
import math
for _ in range(T):
	(a, m) = (int(x) for x in input().split(' '))
	fac = []
	N = []
	count = 0
	i = 1
	while i <= math.sqrt(m):
		if m % i == 0:
			if m // i == i:
				fac.append(i)
			else:
				fac.append(i)
				fac.append(m // i)
		i = i + 1
	fac.remove(1)
	for f in fac:
		if (f - 1) % a == 0:
			q = (f - 1) // a
			n = q * (m // f)
			N.append(n)
			count += 1
	N.sort()
	print(count)
	print(*N)


from math import sqrt

def find_factors(m):
	factors = []
	for i in range(1, int(sqrt(m)) + 1):
		if m % i == 0:
			factors.append(i)
			if m / i != i:
				factors.append(m / i)
	return factors
for _ in range(int(input())):
	(a, m) = [int(x) for x in input().split()]
	factors = find_factors(m)[1:]
	sol = []
	for i in factors:
		if (i - 1) % a == 0:
			q = (i - 1) / a
			d = m / i
			sol.append(int(q * d))
	sol.sort()
	print(len(sol))
	for i in sol:
		print(i, end=' ')
	print()


import sys
import math
from collections import defaultdict, Counter
t = int(input())
for i in range(t):
	(a, m) = map(int, input().split())
	r = (m - 1) // a
	l = []
	for j in range(1, r + 1):
		if m % (a * j + 1) == 0:
			d = m // (a * j + 1)
			l.append(d * j)
	print(len(l))
	print(*l)


T = int(input())
import math
for i in range(T):
	list = []
	(A, M) = input().split()
	A = int(A)
	M = int(M)
	L1 = M // A
	L2 = math.ceil(M / (A + 1))
	while L2 <= L1:
		N = L2
		if M - N * A != 0 and N % (M - N * A) == 0:
			list.append(L2)
		L2 += 1
	print(len(list))
	if len(list) > 0:
		for j in range(len(list) - 1):
			print(list[j], end=' ')
		print(list[len(list) - 1])
	else:
		print(' ')


import math
t = int(input())
for _ in range(t):
	ans = []
	(A, m) = map(int, input().split())
	s = math.ceil(m ** (1 / 2))
	for i in range(2, s):
		if m % i == 0:
			if (i - 1) % A == 0:
				x = (i - 1) // A
				ans.append(x * m // i)
			j = m // i
			if j != i:
				if (j - 1) % A == 0:
					x = (j - 1) // A
					ans.append(x * m // j)
	i = m
	if (i - 1) % A == 0:
		x = (i - 1) // A
		ans.append(x * m // i)
	print(len(ans))
	ans.sort()
	print(*ans)


import math
t = int(input())
for _ in range(t):
	(a, m) = map(int, input().split())
	l = list()
	cnt = 0
	for i in range(1, int(math.sqrt(m)) + 1):
		x = m % i
		if x == 0:
			sh = (i - 1) % a
			req = (i - 1) // a
			if sh == 0:
				cnt += 1
				if int(m / i) * req != 0:
					l.append(int(m / i) * req)
			re = int(m / i)
			sh = (re - 1) % a
			req = (re - 1) // a
			if sh == 0:
				cnt += 1
				if int(m / re) * req != 0:
					l.append(int(m / re) * req)
	l.sort()
	print(len(l))
	for i in range(len(l)):
		print(l[i], end=' ')
	print(' ')


from math import sqrt

def solve():
	(a, m) = list(map(int, input().split()))
	all_vals = []
	for i in range(1, int(sqrt(m)) + 1):
		if m % i == 0:
			if (m - i) % a == 0 and (m - i) // a % i == 0:
				all_vals.append((m - i) // a)
			i = m // i
			if i != m and (m - i) % a == 0 and ((m - i) // a % i == 0):
				all_vals.append((m - i) // a)
	print(len(all_vals))
	if len(all_vals) != 0:
		all_vals.sort()
		print(*all_vals)
for T in range(int(input())):
	solve()


import sys

def input():
	return sys.stdin.readline().strip()
t = int(input())
while t:
	t -= 1
	(a, m) = map(int, input().split())
	possible = []
	for i in range(2, int(m ** 0.5) + 1):
		if not m % i:
			d = m // i
			if not (m - d) % (a * d):
				possible.append((m - d) // a)
			d = m // d
			if not (m - d) % (a * d):
				possible.append((m - d) // a)
	if not (m - 1) % a:
		possible.append((m - 1) // a)
	possible.sort()
	print(len(possible))
	for i in possible:
		print(i, end=' ')
	print()


from functools import reduce
import math

def factors(n):
	divisors = []
	for i in range(1, int(math.sqrt(n)) + 1):
		if n % i is 0:
			if n // i is i:
				divisors.append(i)
			else:
				divisors.append(i)
				divisors.append(n // i)
	divisors.append(n)
	return sorted(list(set(divisors)))
for t in range(int(input())):
	(a, m) = map(int, input().split())
	s = list(factors(m))
	count = 0
	res = []
	for i in s:
		k = (m - i) / a
		z = (m - i) % a
		if k >= i and k % 1 == 0 and (k % i == 0):
			count = count + 1
			res.append(int(k))
	print(len(res))
	for i in sorted(res):
		print(i, end=' ')
	print()


import math

def divisors(m):
	l1 = []
	for i in range(1, int(math.sqrt(m)) + 1):
		if m % i == 0:
			if m // i == i:
				l1.append(i)
			else:
				l1.append(i)
				l1.append(m // i)
	return l1
t = int(input())
while t > 0:
	(a, m) = map(int, input().split())
	fac = divisors(m)
	l = []
	for i in fac:
		n = (m - i) / a
		if n >= i and n % i == 0 and (n % 1 == 0):
			l.append(int(n))
	l.sort()
	print(len(l))
	for i in l:
		print(i, end=' ')
	print()
	t -= 1


import math

def divisors(n):
	arr = []
	for i in range(1, 1 + int(math.ceil(math.sqrt(n)))):
		if n % i == 0:
			arr.append(i)
			arr.append(n // i)
	arr = list(sorted(set(arr)))
	return arr
try:
	t = int(input())
	while t:
		t -= 1
		(a, m) = map(int, input().split())
		divs = divisors(m)
		ans = []
		for d in divs:
			q = (m // d - 1) / a
			if q % 1 == 0 and q > 0:
				ans.append(int(d * q))
		ans.sort()
		print(len(ans))
		for i in ans:
			print(i, end=' ')
		print()
except:
	pass


t = int(input())
for T in range(t):
	(a, m) = map(int, input().split())
	ar = []
	factors = []
	for i in range(1, int(m ** 0.5) + 1):
		if m % i == 0:
			factors.append(i)
			factors.append(m // i)
	n = m // a
	for i in factors:
		if i != 1 and (i - 1) % a == 0:
			ar.append((i - 1) * m // (i * a))
	print(len(ar))
	ar.sort()
	for i in range(len(ar)):
		print(int(ar[i]), end=' ')
	print()


for i in range(int(input())):
	(a, m) = [int(i) for i in input().split()]
	arr = []
	for i in range(1, int(m ** 0.5) + 1):
		if m % i == 0:
			p = m - i
			if p % a == 0 and p // a % i == 0:
				arr.append(p // a)
		if i < m // i:
			i = m // i
			if m % i == 0 and i < m:
				p = m - i
				if p % a == 0:
					n = p // a
					if n % i == 0:
						arr.append(p // a)
	print(len(arr))
	arr.sort()
	for i in arr:
		print(i, '', end='')
	print()


def factors(n):
	f = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			f.append(i)
			f.append(n // i)
	return set(f)
for _ in range(int(input())):
	(a, m) = map(int, input().split())
	r = []
	c = 0
	f = factors(m)
	for i in f:
		if (m / i - 1) % a == 0:
			c += 1
			n = (m - i) // a
			r.append(n)
	print(c - 1)
	r.sort()
	r = r[1:]
	print(*r)


import math

def printDivisors(n):
	i = 1
	l = []
	while i <= math.sqrt(n):
		if n % i == 0:
			if n / i == i:
				l.append(i)
			else:
				l += [i, n // i]
		i = i + 1
	return l

def guess(a, m):
	l1 = printDivisors(m)
	l2 = []
	for i in l1:
		n = (m - i) / a
		if n < m and n % i == 0 and (n != 0):
			l2.append(n)
	print(len(l2))
	l2.sort()
	for i in l2:
		print(int(i), end=' ')
	print()
t = int(input())
for i in range(t):
	l = list(map(int, input().split()))
	a = l[0]
	m = l[1]
	guess(a, m)


import math
t = int(input())
for _ in range(t):
	(A, M) = map(int, input().split())
	i = 2
	divisors = [M]
	while i <= int(math.sqrt(M)):
		if M % i == 0:
			divisors.append(i)
			if M // i != i:
				divisors.append(M // i)
		i += 1
	expr = lambda divisor: M // divisor * (divisor - 1) // A
	res = [expr(divisor) for divisor in divisors if (divisor - 1) % A == 0]
	res.sort()
	print(len(res))
	if res:
		print(*res)
	else:
		print()


import math

def get_divisiors(n):
	divisors = []
	for i in range(1, int(math.sqrt(n)) + 1):
		if n % i is 0:
			if n // i is i:
				divisors.append(i)
			else:
				divisors.append(i)
				divisors.append(n // i)
	divisors.append(n)
	return sorted(list(set(divisors)))

def solve():
	for _ in range(int(input())):
		(a, m) = list(map(int, input().split()))
		divisor_m = get_divisiors(m)
		n_s = []
		n = 0
		for d in divisor_m:
			n = (m - d) / a
			if n >= d and n % 1 == 0 and (n % d == 0):
				n_s.append(int(n))
		print(len(n_s))
		for y in sorted(n_s):
			print(y, end=' ')
		print()
solve()


import math
for _ in range(int(input())):
	(a, m) = map(int, input().split())
	c = 0
	l = set()
	h = []
	for i in range(2, int(math.sqrt(m)) + 1):
		if m % i == 0:
			l.add(i)
			l.add(m // i)
	l.add(m)
	for i in l:
		if (i - 1) % a == 0:
			h.append(m // i * ((i - 1) // a))
	h.sort()
	print(len(h))
	for i in h:
		print(i, end=' ')
	print()


import math
from itertools import product

def gen_primes():
	D = {}
	q = 2
	while True:
		if q not in D:
			yield q
			D[q * q] = [q]
		else:
			for p in D[q]:
				D.setdefault(p + q, []).append(p)
			del D[q]
		q += 1

def primes(gen=gen_primes(), mem=[]):
	for p in mem:
		yield p
	for p in gen:
		mem.append(p)
		yield p

def decompose(n):
	root = math.sqrt(n)
	for p in primes():
		if p > root:
			return
		(d, m) = divmod(n, p)
		k = 0
		while m == 0:
			k += 1
			n = d
			(d, m) = divmod(n, p)
		if k:
			yield (p, k)
			if n == 1:
				return

def divisors(n):
	dn = list(decompose(n))
	if not dn:
		return [1]
	(ps, ks) = zip(*dn)
	res = set()
	for u in product(*[range(k + 1) for k in ks]):
		prod = 1
		for (p, k) in zip(ps, u):
			prod *= p ** k
		res.add(prod)
		res.add(n // prod)
	return res

def sol(a, m):
	for d in divisors(m):
		(dd, r) = divmod(m - d, a)
		if r == 0 and dd > 0 and (dd % d == 0):
			yield dd

def main():
	(t,) = [int(x) for x in input().split()]
	for _ in range(t):
		(a, m) = [int(x) for x in input().split()]
		res = sorted(sol(a, m))
		print(len(res))
		print(' '.join((str(x) for x in res)))
main()


import math as ma
t = int(input())
for _ in range(t):
	fact = []
	(a, m) = map(int, input().split())
	for i in range(2, ma.ceil(ma.sqrt(m))):
		if m % i == 0:
			fact.append(i)
			fact.append(m // i)
	if ma.sqrt(m) == int(ma.sqrt(m)):
		fact.append(int(ma.sqrt(m)))
	fact.append(m)
	fact.sort()
	ans = []
	for f in fact:
		if (f - 1) % a == 0:
			q = (f - 1) // a
			d = m // f
			n = q * d
			ans.append(n)
	print(len(ans))
	if len(ans) == 0:
		print(' ')
	else:
		print(*ans)


import math

def divisor(M):
	l1 = []
	for i in range(1, int(math.sqrt(M)) + 1):
		if M % i == 0:
			if M / i == i:
				l1.append(i)
			else:
				l1.append(i)
				l1.append(M // i)
	return l1
for _ in range(int(input())):
	(A, M) = map(int, input().split())
	my_array = []
	l1 = divisor(M)
	l1.remove(M)
	for i in l1:
		if (M - i) % A == 0 and (M - i) // A % i == 0:
			my_array.append((M - i) // A)
	my_array.sort()
	print(len(my_array))
	print(*my_array)


import math
T = int(input())
for i in range(T):
	(a, m) = list(map(int, input().split()))
	f = {1}
	for i in range(2, int(math.sqrt(m)) + 1):
		if m % i == 0:
			f.add(i)
			f.add(m // i)
	total = 0
	l = []
	for i in f:
		p = m - i
		if p % a == 0:
			q = p // a
			if q % i == 0:
				l.append(q)
				total += 1
	print(total)
	if total > 0:
		l.sort()
		print(*l, end=' ')
		print('')
	else:
		print(' ')


import math

def search(l):
	a = l[0]
	n = l[1]
	g = []
	r = []
	e = []
	t = []
	z = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n / i == i:
				g.append(i)
			else:
				g.append(i)
				g.append(int(n / i))
		i = i + 1
	g.sort()
	for i in g:
		k = int(n / i) - 1
		e.append(k)
	for j in e:
		if j % a == 0:
			p = int(j / a)
			ind = e.index(j)
			f = g[ind] * p
			z.append(f)
			t.append(p)
	z.sort()
	if len(z) == 1 and z[0] == 0:
		print('0')
	else:
		z.remove(0)
		print(len(z))
		print(*z, sep=' ')
t = int(input())
for i in range(t):
	l = list(map(int, input().split()))
	search(l)


import math

def printDivisors(n):
	i = 1
	factor = []
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				factor.append(i)
			else:
				factor.append(i)
				factor.append(n // i)
		i = i + 1
	return factor
t = int(input())
for _ in range(t):
	(a, m) = map(int, input().split())
	factor = printDivisors(m)
	aa = []
	for i in range(len(factor)):
		r = m // factor[i] - 1
		if r % a == 0:
			aa.append(r // a * factor[i])
	aa.sort()
	aa.remove(0)
	ans = set(aa)
	ans = list(ans)
	ans.sort()
	print(len(ans))
	print(*ans)


from math import sqrt
t = int(input())
while t > 0:
	(a, m) = [int(x) for x in input().split()]
	div = []
	n = []
	for i in range(1, int(sqrt(m)) + 1):
		if m % i == 0:
			if m // i == i:
				div.append(i)
			else:
				div.append(i)
				div.append(m // i)
	div.sort()
	div = div[:-1]
	for i in div:
		if (m // i - 1) % a == 0:
			n.append(i * ((m // i - 1) // a))
	s = set(n)
	n = list(s)
	n.sort()
	print(len(n))
	for i in n:
		print(i, end=' ')
	print()
	t -= 1


for _ in range(int(input())):
	(a, m) = map(int, input().split())
	divisors = []
	for i in range(1, int(m ** 0.5 + 1)):
		if m % i == 0:
			divisors.append(i)
		if i != m / i:
			divisors.append(m / i)
	p = []
	for h in divisors:
		if (m - h) / a % 1 == 0 and (m - h) / a != 0:
			if int((m - h) / a) % h == 0:
				p.append(int((m - h) / a))
	print(len(p))
	p.sort()
	for j in p:
		print(j, end=' ')
	print(' ')


import math
for _ in range(int(input())):
	(a, m) = [int(i) for i in input().split()]
	(l, d, div) = ([], 1, [])
	for d in range(1, int(math.sqrt(m)) + 1):
		if m % d == 0:
			(div.append(d), div.append(m // d))
	for d in div:
		if (m // d - 1) % a == 0:
			l.append((m // d - 1) // a * d)
	l = sorted(list(set(l)))
	l = [str(e) for e in l if e != 0]
	print(len(l))
	print(' '.join(l))


from math import sqrt

def findN(a, m, d, ans):
	r = m // d - 1
	if r % a == 0:
		q = r // a
		n = q * d
		if n > 0:
			ans.add(n)
t = int(input())
for _ in range(t):
	(a, m) = map(int, input().split(' '))
	if m % 2 == 0:
		step = 1
	else:
		step = 2
	x = int(sqrt(m))
	ans = set()
	for d1 in range(1, x + 1, step):
		if m % d1 == 0:
			d2 = m // d1
			findN(a, m, d1, ans)
			findN(a, m, d2, ans)
	c = len(ans)
	ans_sorted = sorted(ans)
	print(c)
	print(*ans_sorted)


import math
t = int(input())
for i in range(t):
	d = []
	n = []
	(a, m) = map(int, input().split())
	for i in range(1, int(math.sqrt(m)) + 1):
		if m % i == 0:
			if m // i == i:
				d.append(i)
			else:
				d.append(i)
				d.append(m // i)
	for i in d:
		if (m - i) % a == 0:
			l = (m - i) // a
			if l != 0 and l % i == 0:
				n.append(l)
	n = sorted(n)
	if len(n) > 0:
		print(len(n))
		for i in n:
			print(i, end=' ')
		print()
	else:
		print(0)
		print(' ')
		print()


def findFactors(N):
	factors = set()
	factors.add(1)
	factors.add(N)
	factors.add(N)
	for i in range(2, int((N + 1) ** 0.5)):
		if N % i == 0:
			factors.add(i)
			factors.add(N // i)
	return factors

def testcase():
	(a, m) = [int(i) for i in input().split()]
	n = []
	mfactors = findFactors(m)
	for d in mfactors:
		qa = m // d - 1
		if qa % a == 0:
			q = qa // a
			if q * d == 0:
				continue
			else:
				n.append(q * d)
	n.sort()
	print(len(n))
	print(' '.join([str(i) for i in n]))
t = int(input())
for test in range(t):
	testcase()


import math
for _ in range(int(input())):
	(a, m) = map(int, input().split())
	km = m
	nl = []
	i = 1
	fact = []
	while i <= math.sqrt(m):
		if m % i == 0:
			fact.append(i)
			if m // i != i:
				fact.append(m // i)
		i += 1
	for i in fact:
		x = i
		if i > 1 and (x - 1) % a == 0:
			k = (x - 1) // a
			d = m // x
			nl.append(k * d)
	nl.sort()
	print(len(nl))
	for i in nl:
		print(i, end=' ')
	print()


import math

def getFactors(m):
	factors = []
	for i in range(1, int(math.sqrt(m)) + 1):
		if m % i == 0:
			factors.append(i)
			if m / i != i:
				factors.append(m / i)
	return factors
t = int(input())
for _ in range(t):
	(a, m) = map(int, input().split())
	nValues = []
	xValues = getFactors(m)
	for x in xValues:
		if x > 1 and (x - 1) % a == 0:
			k = (x - 1) / a
			d = m / x
			nValues.append(int(k * d))
	nValues.sort()
	print(len(nValues))
	print(*nValues)


t = int(input())
while t:
	t -= 1
	l = list(map(int, input().split()))
	a = l[0]
	m = l[1]
	d = []
	for i in range(1, int(m ** 0.5) + 1):
		if m % i == 0:
			if m // i == i:
				d.append(i)
			else:
				d.append(i)
				d.append(m // i)
	x = len(d)
	l1 = []
	for i in range(x):
		n = (m - d[i]) / a
		if n % d[i] == 0 and n != 0:
			l1.append(int(n))
	l1.sort()
	print(len(l1))
	print(*l1)


import math
for _ in range(int(input())):
	(a, m) = map(int, input().split())
	l = []
	for i in range(1, int(math.sqrt(m) + 1)):
		if m % i == 0:
			l.append(i)
			if i != m // i:
				l.append(m // i)
	l1 = list(set(l))
	l2 = []
	for i in range(len(l1)):
		if m // l1[i] > 1:
			if (m // l1[i] - 1) % a == 0:
				l2.append(l1[i] * ((m // l1[i] - 1) // a))
	l3 = list(set(l2))
	l3.sort()
	print(len(l3))
	for i in l3:
		print(i, end=' ')
	print()


import math

def divisors(n):
	res = []
	for i in range(1, int(math.sqrt(n)) + 1):
		if n % i == 0:
			res.append(i)
			if n // i != i:
				res.append(n // i)
	return res
t = int(input())
for i in range(0, t):
	(a, m) = map(int, input().split())
	res = divisors(m)
	ans = []
	for i in res:
		if (i - 1) % a == 0 and i > 1:
			q = (i - 1) // a
			d = m // i
			n = q * d
			ans.append(n)
	ans.sort()
	print(len(ans))
	print(*ans)


from math import sqrt

def printDivisors(n):
	i = 1
	divisor_list = []
	while i <= sqrt(n):
		if n % i == 0:
			if n / i == i:
				divisor_list.append(i)
			else:
				divisor_list.append(i)
				divisor_list.append(n // i)
		i += 1
	return divisor_list
for test_case in range(int(input())):
	(A, M) = map(int, input().split())
	divisor_list = printDivisors(M)
	divisor_list.remove(M)
	result_array = []
	for i in divisor_list:
		if (M - i) % A == 0 and (M - i) // A % i == 0:
			result_array.append((M - i) // A)
	result_array.sort()
	print(len(result_array))
	print(*result_array)


import sys
import math
from collections import defaultdict, Counter
t = int(input())
for i in range(t):
	(a, m) = map(int, input().split())
	r = (m - 1) // a
	l = []
	l2 = []
	for j in range(1, int(math.sqrt(m)) + 1):
		if m % j == 0:
			if j % a == 1 and 1 <= j // a <= r or (a == 1 and 2 <= j // a <= r + 1):
				d = m // j
				if a != 1:
					l.append(d * (j // a))
				else:
					l.append(d * (j // a - 1))
			if m // j != j and (m // j % a == 1 and 1 <= m // j // a <= r or (a == 1 and 2 <= m // j // a <= r + 1)):
				d = m // (m // j)
				if a != 1:
					l2 = [d * (m // j // a)] + l2
				else:
					l2 = [d * (m // j // a - 1)] + l2
	l.extend(l2)
	print(len(l))
	print(*l)


import math

def printDivisors(a, n):
	i = 1
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				a.append(i)
			else:
				a.append(i)
				a.append(n // i)
	return a
x = int(input())
c = []
o = []
o1 = []
for i in range(x):
	l1 = [int(y) for y in input().split()]
	a = l1[0]
	m = l1[1]
	b = printDivisors(c, m)
	for d in b:
		n = (m - d) / a
		if n >= d and n % 1 == 0 and (n % d == 0):
			o1.append(int(n))
	c = []
	o1 = sorted(o1)
	o.append(o1)
	o1 = []
for i in range(len(o)):
	print(len(o[i]))
	for j in o[i]:
		print(j, end=' ')
	print()


def factorize(m: int) -> list:
	root = m ** 0.5
	i = 1
	factors = []
	while i < root + 1:
		if m % i == 0:
			factors.append((i, m // i))
			factors.append((m // i, i))
		i += 1
	return factors

def possible_n(factors: list, a: int) -> list:
	ns = []
	for (fi, fj) in factors:
		if fj == 1 or (fj - 1) % a != 0:
			continue
		n = fi * ((fj - 1) // a)
		ns.append(n)
	return sorted(set(ns))

def solve(a: int, m: int) -> list:
	factors = factorize(m)
	return possible_n(factors, a)

def main():
	t = int(input().strip())
	for _ in range(t):
		(a, m) = map(int, input().strip().split())
		ns = solve(a, m)
		print(len(ns))
		if ns:
			print(' '.join(map(str, ns)))
main()


def solve():
	(a, m) = list(map(int, input().split()))
	n = m // (a + 1)
	all_vals = []
	while a * n < m:
		if a * n + (m - a * n) * (n % (m - a * n) == 0) == m:
			all_vals.append(n)
		n += 1
	print(len(all_vals))
	if len(all_vals) != 0:
		print(*all_vals)
for T in range(int(input())):
	solve()


from math import sqrt

def div(m):
	dv = []
	for i in range(1, int(sqrt(m)) + 1):
		if m % i == 0:
			if m // i == i:
				dv.append(i)
			else:
				dv.append(i)
				dv.append(m // i)
	return dv
for _ in range(int(input())):
	(a, m) = map(int, input().split())
	dv = div(m)
	out = []
	for d in dv:
		n = (m - d) / a
		if d <= n and n % d == 0 and (n % 1 == 0):
			out.append(int(n))
	out.sort()
	print(len(out))
	out = list((str(j) for j in out))
	print(' '.join(out))


import math

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n)) + 1):
		if n % i == 0:
			if n / i == i:
				l.append(i)
			else:
				l.append(n // i)
				l.append(i)
	return l
from sys import stdin, stdout
for _ in range(int(input())):
	(a, m) = map(int, input().split())
	div = Divisors(m)
	ans = set()
	for i in div:
		if (m // i - 1) % a == 0:
			ans.add((m // i - 1) // a * i)
	ans = list(ans)[1:]
	ans.sort()
	print(len(ans))
	print(*ans)


from math import sqrt
for _ in range(int(input())):
	(A, M) = list(map(int, input().split()))
	divisors = set()
	for i in range(1, int(sqrt(M)) + 1):
		if M % i == 0:
			divisors.add(i)
			divisors.add(M // i)
	answers = []
	for i in divisors:
		ans = (M - i) / A
		if ans % i == 0 and ans != 0:
			answers.append(int(ans))
	print(len(answers))
	print(' '.join(list(map(str, sorted(answers)))))


import math

def divisors(n, l):
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
		i = i + 1
	return l
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	l = []
	divisors(m, l)
	l.remove(1)
	ll = []
	c = 0
	for i in l:
		if (i - 1) % n == 0:
			q = (i - 1) // n
			d = m // (q * n + 1)
			ll.append(q * d)
			c += 1
	print(c)
	print(*sorted(ll))


for i in range(int(input())):
	(a, m) = map(int, input().split())
	j = 1
	l = []
	while j * j <= m:
		if m % j == 0:
			if j != m // j:
				l.append(j)
				l.append(m // j)
			else:
				l.append(j)
		j += 1
	l2 = []
	j = 0
	for i in range(len(l)):
		t = (m - l[i]) / a
		if t >= l[i] and t % 1 == 0 and (t % l[i] == 0):
			l2.append(t)
			j += 1
	l2.sort()
	print(j)
	for i in range(j):
		print(int(l2[i]), end=' ')
	print()


t = int(input())
for _ in range(t):
	(a, m) = map(int, input().split())
	count = 0
	factors = []
	list1 = []
	for i in range(1, int(m ** 0.5) + 1):
		if m % i == 0:
			factors.append(i)
			if i != m // i:
				factors.append(m // i)
	for f in factors:
		if (f - 1) % a == 0:
			q = (f - 1) // a
			d = m // f
			if q * d != 0:
				count += 1
				list1.append(q * d)
	list1 = sorted(list1)
	print(count)
	for i in list1:
		print(i, end=' ')
	print()


import math
t = int(input())
while t:
	(a, m) = map(int, input().split())
	fac_m = []
	for i in range(1, int(math.sqrt(m)) + 1):
		if m % i == 0:
			fac_m.extend([i, m // i])
	li_a = []
	for i in fac_m:
		r = m / i - 1
		if r % a == 0:
			q = r // a
			li_a.append(q * i)
	li_a = sorted(set(li_a))
	if 0 in li_a:
		li_a.remove(0)
	print(len(li_a))
	for i in li_a:
		print(int(i), end=' ')
	print()
	t -= 1


T = int(input())
for X in range(T):
	(A, M) = map(int, input().split())
	S = set()
	N = set()
	for D in range(1, int(M ** 0.5) + 1):
		if M % D == 0:
			S.add(D)
			S.add(M // D)
	for I in S:
		if (M - I) / A % I == 0:
			N.add((M - I) // A)
	N = list(N)
	N.sort()
	print(len(N) - 1)
	for I in N:
		if I != 0:
			print(I, end=' ')
	print()


import functools
import math

def factors(n):
	step = 2 if n % 2 else 1
	return set(functools.reduce(list.__add__, ([i, n // i] for i in range(1, int(math.sqrt(n)) + 1, step) if n % i == 0)))
T = int(input())
for t in range(T):
	(A, M) = [int(i) for i in input().split()]
	M_factors = factors(M)
	M_factors.remove(M)
	N_list = []
	for d in M_factors:
		diff = M - d
		N = diff // A
		if diff % A == 0 and N % d == 0:
			N_list.append(N)
	if len(N_list) == 0:
		print(0)
		print('')
	else:
		N_list.sort()
		s = ''
		for N in N_list:
			s += str(N) + ' '
		print(len(N_list))
		print(s.strip())


from collections import defaultdict
from heapq import *
import itertools, math

def parse_integer_list():
	return [int(x) for x in input().split()]

def handle_input(M, A):
	res = []
	high_candidates = []
	for i in range(1, math.floor(math.sqrt(M) + 1)):
		if M % i == 0:
			if (M - i) % A == 0 and (M - i) // A % i == 0:
				res.append((M - i) // A)
			if i != 1:
				high_candidates.append(M // i)
	for i in reversed(high_candidates):
		if (M - i) % A == 0 and (M - i) // A % i == 0:
			res.append((M - i) // A)
	print(len(res))
	print(' '.join([str(x) for x in reversed(res)]))
T = int(input())
for i in range(T):
	(A, M) = parse_integer_list()
	handle_input(M, A)


for _ in range(int(input())):
	(a, m) = map(int, input().split())
	l = set()
	ans = 0
	i = 1
	ans = set()
	while i * i <= m:
		if m % i == 0:
			l.add(i)
			l.add(m // i)
		i += 1
	for i in l:
		temp = m // i - 1
		if temp % a:
			continue
		else:
			ans.add(temp // a * i)
	ans.discard(0)
	ans = list(ans)
	ans.sort()
	print(len(ans))
	for i in ans:
		print(i, end=' ')
	print()


for _ in range(int(input())):
	(a, m) = map(int, input().split())
	ans = []
	mx = min(int(m ** 0.5), (m - 1) // a)
	for d in range(1, mx + 1):
		if (m - d) % a == 0:
			n = (m - d) // a
			if n % d == 0:
				ans.append(n)
		if m % d == 0 and d != 1:
			dd = m // d
			if (m - dd) % a == 0:
				n = (m - dd) // a
				if n % dd == 0:
					ans.append(n)
	print(len(ans))
	print(' '.join(map(str, sorted(ans))))


import sys
from math import sqrt

def get(m, a):
	d = []
	for i in range(1, int(sqrt(m)) + 1):
		if m % i == 0:
			d.append(i)
			if m // i != i:
				d.append(m // i)
	ans = []
	for i in d:
		if (m - i) % a == 0 and (m - i) // a != 0 and ((m - i) // a % i == 0):
			ans.append((m - i) // a)
	return ans
t = int(sys.stdin.readline())
for _ in range(t):
	(a, m) = map(int, sys.stdin.readline().split())
	ans = get(m, a)
	print(len(ans))
	ans.sort()
	print(*ans)


t = int(input())
for q in range(t):
	(a, m) = map(int, input().split())
	l = []
	i = 1
	while i < m ** 0.5 + 1:
		if m % i == 0:
			if i not in l:
				l.append(i)
			if m // i not in l:
				l.append(m // i)
		i += 1
	l.sort()
	l2 = []
	for d in range(len(l) - 1):
		if (m - l[d]) % (a * l[d]) == 0:
			l2.append((m - l[d]) // a)
	print(len(l2))
	for i in range(len(l2) - 1, -1, -1):
		print(l2[i], end=' ')
	print()
	del l
	del l2


from math import *
for _ in range(int(input())):
	(a, m) = [int(i) for i in input().split()]
	l = []
	d = 1
	div = []
	while d * d <= m:
		if m % d == 0:
			div.append(d)
			div.append(m // d)
		d += 1
	for d in div:
		f = m // d
		f -= 1
		if f % a == 0:
			p = f // a
			l.append(p * d)
	l = set(l)
	l = list(l)
	l.sort()
	l = [str(e) for e in l if e != 0]
	print(len(l))
	print(' '.join(l))


T = int(input())
while T:
	(A, M) = map(int, input().split())
	ls = []
	for i in range(1, int(M ** 0.5) + 2):
		if M % i == 0:
			d = i
			if (M - d) % A == 0:
				N = (M - d) // A
				if N % d == 0 and N > 0:
					ls.append(N)
			d = M // i
			if (M - d) % A == 0:
				N = (M - d) // A
				if N % d == 0 and N > 0:
					ls.append(N)
	ls = list(set(ls))
	print(len(ls))
	ls.sort()
	print(*ls)
	T -= 1


from math import sqrt
for _ in range(int(input())):
	(a, m) = map(int, input().split())
	l = []
	for i in range(1, int(sqrt(m) + 1)):
		if m % i == 0:
			if i == m // i:
				l.append(i)
			else:
				l.append(i)
				l.append(m // i)
	l1 = []
	for i in l:
		if (i - 1) % a == 0 and i > 1:
			q = (i - 1) // a
			d = m // i
			l1.append(q * d)
	print(len(l1))
	l1.sort()
	print(*l1)


def factors(n):
	List = set()
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			List.add(i)
			List.add(int(n / i))
	return list(List)
for _ in range(int(input())):
	(A, M) = [int(x) for x in input().split()]
	List = factors(M)
	count = 0
	Numbers = []
	for i in List:
		if (i - 1) % A == 0:
			q = int((i - 1) / A)
			d = int(M / i)
			N = q * d
			count += 1
			Numbers.append(N)
	print(len(Numbers) - 1)
	Numbers.sort()
	Numbers.remove(0)
	print(*Numbers)


import math

def fun(a, m):
	q = int(math.sqrt(m))
	l = []
	for i in range(1, q + 1):
		if i * i != m:
			if m % i == 0:
				d = m // i
				if (d - 1) % a == 0:
					k = (d - 1) // a
					n = i * k
					l.append(n)
				if (i - 1) % a == 0:
					if i == 1:
						continue
					k = (i - 1) // a
					n = d * k
					l.append(n)
		elif (i - 1) % a == 0:
			k = (i - 1) // a
			n = d * k
			l.append(n)
	if len(l) == 0:
		print(0)
	else:
		l.sort()
		print(len(l))
		l = list(map(str, l))
		s = ' '
		s = s.join(l)
		print(s)
for _ in range(int(input())):
	(a, m) = list(map(int, input().split()))
	fun(a, m)


import math
for _ in range(int(input())):
	(a, m) = map(int, input().split())
	i = 2
	sd = [m]
	while i <= math.sqrt(m):
		if m % i == 0:
			if m // i == i:
				sd.append(i)
			else:
				sd.append(i)
				sd.append(m // i)
		i = i + 1
	ls = []
	cnt = 0
	for i in sd:
		if (i - 1) % a == 0:
			ls.append(m // i * (i - 1) // a)
			cnt += 1
	print(cnt)
	ls.sort()
	for i in range(len(ls)):
		print(ls[i], end=' ')
	print()


from math import sqrt, floor

def find_factors(n):
	end = floor(sqrt(n))
	li = [1]
	for i in range(end, 1, -1):
		if m % i == 0:
			li.append(i)
			li.append(m // i)
	return li
for _ in range(int(input())):
	(a, m) = map(int, input().split())
	l = find_factors(m)
	ans = []
	for i in l:
		x = (m - i) / a
		if x >= i and x % 1 == 0 and (x % i == 0):
			ans.append(int(x))
	print(len(ans))
	print(*sorted(ans))


t = int(input())
for _ in range(t):
	l = []
	(a, m) = [int(j) for j in input().split()]
	for i in range(1, int(m ** 0.5) + 1):
		if m % i == 0:
			l.append(i)
			l.append(m // i)
	ans = []
	for i in l:
		if (m - i) % a != 0:
			continue
		c = (m - i) // a
		if c % i != 0:
			continue
		if i <= c:
			ans.append(c)
	print(len(ans))
	ans.sort()
	for r in ans:
		print(r, end=' ')
	print()


for i in range(int(input())):
	(a, m) = map(int, input().split())
	x = []
	d = []
	j = 1
	while j <= m ** 0.5:
		if m % j == 0:
			if m // j == j:
				d.append(j)
			else:
				d.append(j)
				d.append(m // j)
		j += 1
	d = sorted(d)
	for i in d:
		n = (m - i) / a
		if n >= i and n == int(n) and (int(n) % i == 0):
			x.append(int(n))
	print(len(x))
	x = sorted(x)
	print(*x)