from collections import *
import sys  

# "". join(strings)  
#	print(guess, flush= True)

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

def ri():
    return int(input())
 
def rl():
    return list(map(int, input().split()))
    

t =ri()

for _ in range(t):
	n = ri()
	aa = rl()
	bb = []
	gcd = max(aa)
	bb.append(max(aa))
	aa.remove((max(aa)))

	while (aa):
		if gcd == 1:
			for ai in aa:
				bb.append(ai)
			break
		mini = 1
		for ai in aa:
			temp = computeGCD(gcd, ai)
			if temp >= mini:
				mini = temp
				curr = ai
		gcd = mini
		bb.append(curr)
		aa.remove(curr)
	print(*bb)




	