#to run: ctrl+alt+b
#del window: ctrl+w
from math import gcd
for _ in range(int(input())):
	n=int(input())
	arr=list(map(int,input().split()))
	c=[0 for i in range(n)]
	b=[0 for i in range(n)]

	c[0]=max(arr)
	b[0]=max(arr)
	arr[arr.index(max(arr))]=-1
	for i in range(n-1):
		ind=0
		m=-1
		for j in range(len(arr)):
			
		
			if arr[j]!=-1:
				g=gcd(arr[j],c[i])
				
				if g>m:
					c[i+1]=g
					b[i+1]=arr[j]
					ind=j
					m=g
		arr[ind]=-1
	print(*b)






