import math

t = int(input())
for _ in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	ans = []
	vis = [0]*n
	b = [0]*n

	d = 0
	for i in range(n):
		maxi = 0
		ind = 0
		for j in range(n):
			if vis[j] == 0 and math.gcd(d, x[j]) > maxi:
				maxi = math.gcd(d, x[j])
				ind = j

		d = maxi
		vis[ind] = 1
		b[i] = x[ind]

	print(*b)




