import sys
from math import ceil, factorial, gcd
#from math import comb, perm 	only in python3
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right 
from heapq import heappop, heappush, heapify
MOD = 10**9 + 7
INF = float('inf')

rl = lambda : list(map(int, sys.stdin.readline().split()))
rs = lambda : sys.stdin.readline().strip()

for _ in range(int(input())):
	n = int(input())
	A = rl()
	used = [False] * n
	ans = [max(A)]
	used[A.index(max(A))] = True
	g = ans[0]
	for _ in range(n-1):
		idx = -1
		largest = -INF
		for i in range(n):
			if not used[i]:
				res = gcd(g, A[i])
				largest = max(largest, res)
				if largest == res:
					idx = i

		g = gcd(g, A[idx])
		used[idx] = True
		ans.append(A[idx])

	print(*ans)
				





