import math

t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int,input().split(" ")))
	s = dict()
	for i in a:
		s[i] = 0
		
	for i in a:
		s[i] += 1
	
	a = list(set(a))
	max_a = max(a)
	
	ans = [max_a]*s[max_a]
	visited = [False]*len(a)
	
	visited[a.index(max_a)] = True
	temp = max_a
	
	for i in range(len(a)):	
		max_ = 1
		x = ans[-1]
		for j in range(len(a)):
			if not visited[j] and max_ < math.gcd(temp,a[j]):
				max_ = math.gcd(temp,a[j])
				y = a[j]
				idx = j
		if max_ != 1:
			ans += [y]*s[y]
			visited[idx] = True
			temp = max_
		else:
			break
	
	for i in range(len(a)):
		if not visited[i]:
			ans += [a[i]]*s[a[i]]
	for i in ans:		
		print(i,end=" ")
	print()
		
			
