import sys

def gcd(a,b):

	while(b!=0):
		a,b = b,a%b

	return a

t=int(sys.stdin.readline())

for i in range(t):
	n=int(sys.stdin.readline())

	a=[int(j) for j in sys.stdin.readline().split()]

	arr=[]
	for p in range(n):
		arr.append(False)

	ans=[]
	ans.append(max(a))
	
	val=max(a)
	for h in range(n):
		if(val==a[h]):
			arr[h]=True
			break

	cur=ans[0]

	for k in range(1,n):
		
		cur=gcd(cur,ans[-1])
		mx=0
		ctr=0
		ind=-2

		for f in range(n):
			tmp = gcd(cur,a[f])
			if(tmp>mx and arr[f]==False):
				mx=tmp
				ind=f

		ans.append(a[ind])
		arr[ind]=True

	for p in range(n):
		print(ans[p],end=" ")
	print()
