from math import gcd
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = list()
	ans.append(max(a))
	cg = max(a)
	a[a.index(max(a))] = 0
	na = 0
	z = -1
	for i in range(n):
		na = 0
		for j in range(n):
			if i == j:
				continue
			elif not a[i]:
				break
			elif not a[j]:
				continue

			if gcd(cg, a[j]) > 1:
				na = a[j]
				z = j

		if not na:
			break
		else:
			ans.append(a[z])
			a[z] = 0


	for i in range(n):
		if a[i]:
			ans.append(a[i])

	print(*ans)
