import math
for test in range(int(input())):
	pocet = int(input())
	vstup = list(map(int, input().split(" ")))
	maximum = max(vstup)
	output = str(maximum)
	b = []
	b.append(str(maximum))
	vstup.remove(max(vstup))
	maxgcd = int(b[0])
	#print(vstup)
	while len(vstup)>0:
		divisors = []
		for i in vstup:
			divisors.append(math.gcd(maxgcd,i))
		maxgcd = max(divisors)
		#print("divisors"+str(divisors))
		indices = [i for i, x in enumerate(divisors) if x == maxgcd]
		#print(indices)
		for i in indices:
			b.append(str(vstup[i]))
		indices.reverse()
		for i in indices:
			del vstup[i]
		#print(vstup)
	print(" ".join(b))