def gcd(a,b):
	if b==0:
		return a
	return gcd(b, a%b)

test = int(input())
for t in range(test):
	n = int(input())
	l = list(map(int, input().split()))
	contin = 1
	for i in range(n):
		m = 1
		ind = 0		
		for j in range(len(l)):
			if i==0:
				temp = gcd(l[j],l[j])
			else:
				temp = gcd(contin, l[j])
			if temp>m:
				m = temp
				ind = j
			#dprint(m, temp)
		print(l[ind], end=' ')
		contin = m
		del l[ind]

	print()

