def pfac(n):
	i = 2
	arr = []
	while i <= n:
		if n%i == 0:
			n = n//i
			arr.append(i)
		else:
			i += 1
	return arr
def gcd(a1,a2,n1,n2):
	g = 1
	i = 0
	j = 0
	while i < n1 and j < n2:
		if a1[i] == a2[j]:
			g *= (a1[i])
			i+=1
			j+=1
		elif a1[i] > a2[j]:
			j+=1
		else:
			i+=1
	return g

t = int(input())
for i in range(t):
	n = int(input())
	arr = [int(j) for j in input().split()]
	arr.sort(reverse=True)
	pf = [pfac(j) for j in arr]
	ans = [arr[0]]
	grr = pf[0]
	del pf[0]
	del arr[0]
	while len(pf) > 0:
		maxi = 0
		ind = 0
		for k in range(len(pf)):
			var = gcd(grr,pf[k],len(grr),len(pf[k]))
			if var > maxi:
				maxi = var
				ind = k
		ans.append(arr[ind])
		grr = pfac(maxi)
		del pf[ind]
		del arr[ind]
	for j in ans:
		print(j,end=' ')
	print()



