import sys

def gcd(a,b):
	x=min(a,b)
	y=max(a,b)
	if x==0:
		return y
	else:
		return gcd(y%x, x)

t = int(input())
while t>0:
	t-=1
	n = int(sys.stdin.readline())
	input=sys.stdin.readline()
	arr = list(map(int, input.split()))
	arr.sort(reverse=True)
	cur_gcd = arr[0]
	index=1
	while cur_gcd>1 and index < n:
		max_gcd = gcd(cur_gcd, arr[index])
		pos=index
		for i in range(index+1, n):
			value = gcd(cur_gcd, arr[i])
			if value>max_gcd:
				max_gcd = value
				pos = i
		cur_gcd = max_gcd
		# print(index, pos)
		arr[index], arr[pos] = arr[pos], arr[index]
		index+=1
		
	print(*arr)


