import math

t = int(input())

while (t):

	n = int(input())

	a = list(map(int,input().split()))

	g = 0
	for i in range(n):
		mj = 0
		for j in range(n-i):
			if (math.gcd(g,a[j]) > math.gcd(g,a[mj])):
				mj = j
		g = math.gcd(g,a[mj])
		print(a[mj],end=' ')
		a = a[:mj] + a[mj+1:]
				
	print()
		
	

	t -= 1
	