import sys
from collections import defaultdict as dd
from collections import deque
from functools import *
from fractions import Fraction as f
from copy import *
from bisect import *	
from heapq import *
from math import *
from itertools import permutations ,product
 
def eprint(*args):
    print(*args, file=sys.stderr)
zz=1
 
#sys.setrecursionlimit(10**6)
if zz:
	input=sys.stdin.readline
else:	
	sys.stdin=open('input.txt', 'r')
	sys.stdout=open('all.txt','w')
def inc(d,c,x=1):
	d[c]=d[c]+x if c in d else x
def bo(i):
	return ord(i)-ord('A')	
def li():
	return [int(xx) for xx in input().split()]
def fli():
	return [float(x) for x in input().split()]	
def comp(a,b):
	if(a>b):
		return 2
	return 2 if a==b else 0		
def gi():	
	return [xx for xx in input().split()]
def fi():
	return int(input())
def pro(a): 
	return reduce(lambda a,b:a*b,a)		
def swap(a,i,j): 
	a[i],a[j]=a[j],a[i]	
def si():
	return list(input().rstrip())	
def mi():
	return 	map(int,input().split())			
def gh():
	sys.stdout.flush()
def isvalid(i,j):
	return 0<=i<n and 0<=j<n	
def bo(i):
	return ord(i)-ord('a')	
def graph(n,m):
	for i in range(m):
		x,y=mi()
		a[x].append(y)
		a[y].append(x)

t=fi()
while t>0:
	t-=1
	n=fi()
	a=li()
	g=10**18
	for i in range(n):
		for j in range(i+1,n):
			p=gcd(a[i],a[j])
			if p<g:
				g=p
				r=[i,j]
	if g==1:
		print(a[r[0]],a[r[1]],end=" ")
		for i in range(n):
			if i not in r:
				print(a[i],end=" ")
		print()		
	else:
		i=0
		j=n-1
		b=[]
		for i in range(n):
			b.append([a[i]//g,a[i]])
		b.sort()
		#print(b)
		for j in range(n):
			print(b[j][1],end=" ")	
		print()		


