

def computeGCD(x, y): 
  
   while(y): 
       x, y = y, x % y 
  
   return x


def lexo(alist):
	gcd=max(alist)
	flist=[]
	flist.append(gcd)
	alist.remove(gcd)
	while(len(alist)):
		oval=0
		val=0
		for a in alist:
			g=computeGCD(a,gcd)
			if g>val:
				val=g
				oval=a
		gcd=val
		flist.append(oval)
		alist.remove(oval)
	print(*flist)

t=int(input())
for it in range(t):
	n=int(input())
	x=input()
	alist = [int(i) for i in x.split()]
	lexo(alist)






