
from math import gcd

t = int(input())

for i in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	used = [False for i in range(n)]
	current_gcd = 0
	B = []
	for i in range(n):
		max_new_gcd = 0
		index_achieving_max = 0
		for j in range(n):
			if not used[j]:
				val = gcd(current_gcd, A[j])
				if val > max_new_gcd:
					max_new_gcd = val
					index_achieving_max = j
		used[index_achieving_max] = True
		B.append(A[index_achieving_max])
		current_gcd = max_new_gcd
	print(" ".join(map(str, B)))