#_______________________________________________________________#
def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x-1)
def lower_bound(li, num): 
	answer = -1
	start = 0
	end = len(li)-1

	while(start <= end):
		middle = (end+start)//2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer #index where x is not less than num
def upper_bound(li, num): 
	answer = -1
	start = 0
	end = len(li)-1

	while(start <= end):
		middle = (end+start)//2

		if li[middle] <= num:
			answer = middle
			start = middle + 1
		
		else:
			end = middle - 1
	return answer #index where x is not greater than num

def abs(x):
	return x if x >=0 else -x
def binary_search(li, val, lb, ub): 
	ans = 0
	while(lb <= ub):
		mid = (lb+ub)//2
		#print(mid, li[mid])
		if li[mid] > val:
			ub = mid-1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans
def kadane(x): #maximum sum contiguous subarray
	sum_so_far = 0
	current_sum = 0
	for i in x:
		current_sum += i
		if current_sum < 0:
			current_sum = 0
		else:
			sum_so_far = mpos(sum_so_far,current_sum)
	return sum_so_far
def pref(li):
	pref_sum = [0]
	for i in li:
		pref_sum.append(pref_sum[-1] + i)
	return pref_sum
def graph(n,m):
	adj = dict()
	for i in range(1,n+1):
		adj.setdefault(i,0)
	for i in range(m):
		a,b = map(int,input().split())
		adj[a] += 1
		adj[b] += 1
	return adj

#_______________________________________________________________#
'''
      ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
   ▄███████▀▀▀▀▀▀███████▄
░▐████▀▒▒Aestroix▒▒▀██████
░███▀▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▀████
░▐██▒▒▒▒▒KARMANYA▒▒▒▒▒▒████▌         ________________
░▐█▌▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒████▌  ? ?   |▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒|
░░█▒▒▄▀▀▀▀▀▄▒▒▄▀▀▀▀▀▄▒▒▐███▌   ?    |___CM ONE DAY___|
░░░▐░░░▄▄░░▌▐░░░▄▄░░▌▒▐███▌     ? ? |▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒|
░▄▀▌░░░▀▀░░▌▐░░░▀▀░░▌▒▀▒█▌    ? ?    
░▌▒▀▄░░░░▄▀▒▒▀▄░░░▄▀▒▒▄▀▒▌      ? 
░▀▄▐▒▀▀▀▀▒▒▒▒▒▒▀▀▀▒▒▒▒▒▒█     ? ? 
░░░▀▌▒▄██▄▄▄▄████▄▒▒▒▒█▀       ? 
░░░░▄█████████ ████=========█▒▒▐▌
░░░▀███▀▀████▀█████▀▒▌
░░░░░▌▒▒▒▄▒▒▒▄▒▒▒▒▒▒▐
░░░░░▌▒▒▒▒▀▀▀▒▒▒▒▒▒▒▐
░░░░░████████████████
'''
import sys
import threading
from math import *
sys.setrecursionlimit(300000)
threading.stack_size(10**8)

def pow_of_3(x):
	ans = 0
	while(x%3 == 0):
		ans += 1
		x //= 3
	return ans

#def main():
for _ in range(int(input())):
#for _ in range(1):
	n = int(input())
	#n,m = map(int,input().split())
	#n, s = input().split()
	#s = int(s)
	#a = [int(x) for x in a]
	#b = list(input())
	a = list(map(int,input().split()))
	#b = list(map(int,input().split()))
	#adj = graph(n,m)
	'''if len(set(a)) == 1:
		print(n)
		print(*a)
	else:
		zero = []
		cnt = 0
		for i in range(n):
			if a[i] == 0:
				cnt += 1
			else:
				zero.append(cnt)
		zero.append(cnt)
		one = a.count(1)
		ev,od = one//2,one//2
		if ev+od >= n//2:
			print(ev+od)
			print(1*(ev+od))
		else:
			adz = min((n//2)-ev-od,zero[0])
'''
	if n == 1:
		print(*a)
	else:
		a.sort()
		ans = [a[-1]]
		gg = a[-1]
		vis  = [0]*n
		vis[-1] = 1
		#print(a)
		ind = n-1
		for i in range(n-1,-1,-1):
			if vis.count(1) == n:
				break
			li = [0]*(n)
			for j in range(n-1,-1,-1):
				li[j] = gcd(gg,a[j])
				if vis[j] == 1:
					li[j] = 0
			gg =  max(li)
			ind = li.index(gg)
			#print(li)
			ans.append(a[ind])
			vis[ind] = 1
			#print(ans)
			#print(vis)
			#print('')
		print(*ans)






	











































'''		
t = threading.Thread(target=main)
t.start()
t.join()
'''









	




