from itertools import permutations
def GCD(x, y): 

	while(y): 
		x, y = y, x % y 

	return x 
	
def solve(n,arr):
	p = permutations(arr)
	ans = [0]*n
	for i in list(p):
		lst = list(i)
		c = [arr[0]]
		for i in range(1, n):
			gcd = GCD(arr[i],c[-1])
			c.append(gcd)
			if c == c.sort(reverse=True):
				if ans[0] > c[0]:
				    ans = c
	return ans


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = solve(n,arr)
	print(*ans)