# from math import factorial

# ans=  0
# for x in range(1,5):
# 	for y in range(1,5-x+1):
# 		# print(x,y,5-x-y)
# 		ans += pow(12,x)*pow(33,y)*pow(52,5-x-y)*factorial(5)//(factorial(x)*factorial(y)*factorial(5-x-y))
# print(ans*52)
from math import gcd
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int,input().split()))
	maxx = max(arr)
	g = maxx
	ans = []
	vis = [False]*n
	while len(ans)<n:
		t = -1
		m = -1
		for i in range(n):
			if not vis[i]:
				if gcd(g,arr[i])>m:
					t = i
					m = gcd(g,arr[i])
		ans.append(arr[t])
		vis[t] = True
		g = m
	print(*ans)

