from functools import reduce
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase
input = lambda: sys.stdin.readline().rstrip("\r\n")
def value(): return tuple(map(int, input().split())) # multiple values
def arr(): return [int(i) for i in input().split()] # array input
def sarr(): return [int(i) for i in input()] #array from string
def starr(): return [str(x) for x in input().split()] #string array
def inn(): return int(input()) # integer input
def svalue(): return tuple(map(str, input().split())) #multiple string values
def parr(): return [(value()) for i in range(n)] # array of pairs
mo = 1000000007
# ----------------------------CODE------------------------------#
def gcd(a, b):
    while b:
        a, b = b, a%b
    return a
for _ in range(inn()):
    n=inn()
    a=arr()
    a.sort(reverse=True)
    ans=[]
    ans+=[a[0]]
    l=[]
    for i in range(1,n):
        res=gcd(a[0],a[i])
        if(res>1):
            l+=[a[i]]
            a[i]=-1
    if(len(l)==0):
        print(*a)
        continue
    res=a[0]
    for i in range(len(l)):
        mx=-1
        digit=-1
        id=-1
        for j in range(len(l)):
            if(l[j]!=-1):
                val=gcd(res,l[j])
                if(val>mx):
                    mx=val
                    digit=l[j]
                    id=j
        if(mx!=-1):
            ans+=[digit]
            res=mx
            l[id] = -1
    a[0]=-1
    for i in range(len(l)):
        if(l[i]!=-1):
            ans+=[l[i]]
    for i in range(n):
        if(a[i]!=-1):
            ans+=[a[i]]
    print(*ans)



