from functools import lru_cache
from sys import stdin, stdout
import sys
from math import *

# from collections import deque
# sys.setrecursionlimit(int(2e5+10))
input = stdin.readline
# print = stdout.write
# dp=[-1]*100000

for __ in range(int(input())):
	n=int(input())
	ar=list(map(int,input().split()))
	m=max(ar)
	ans=[m]
	no=[]
	yes=[]
	for i in range(n):
		if(gcd(m,ar[i])==1):
			no.append(ar[i])
		else:
			yes.append(ar[i])
	g=m
	yes.sort()
	yes.pop()
	yes=yes[::-1]
	temp=[0]*len(yes)
	for i in range(len(yes)):
		t=1
		for j in range(len(yes)):
			if(temp[j]==0):
				ind=j
		for j in range(len(yes)):
			if(gcd(g,yes[j])>t and temp[j]==0):
				ind=j
				t=gcd(g,yes[j])
		ans.append(yes[ind])
		g=t
		temp[ind]=1
	print(*(ans+no))
