from math import gcd

for t in range(int(input())):
	n = int(input())
	a = dict()
	for i in map(int, input().split()):
		if i in a:
			a[i] += 1
		else:
			a[i] = 1
	
	gcd_prev = max(a)
	b = [gcd_prev] * a[gcd_prev]
	del a[gcd_prev]

	while gcd_prev != 1:
		max_gcd = max([gcd(i, gcd_prev) for i in a] + [1])
		for i in list(a):
			if not i % max_gcd:
				b += [i] * a[i]
				del a[i]
		gcd_prev = max_gcd

	for i in list(a):
		b += [i] * a[i]
		del a[i]
	print(*b)