import sys
from math import sqrt
from collections import defaultdict
inpt = sys.stdin.readline

def inp():
    return(int(inpt()))

def inlist():
    return(list(map(int,inpt().split())))

def instr():
    s = inpt()
    return(list(s[:len(s)-1]))

def inbitstr():
    s = inpt()
    return([int(i) for i in list(s[:len(s)-1])])

def invr():
    return(map(int,inpt().split()))

def printlist(a):
    for i in range(len(a)):
        print(a[i],end=' ')
    print()

class Graph:

    def __init__(self,n):
        self.n = n
        self.graph = defaultdict(list)
        self.marked = defaultdict(int)
        self.wt = defaultdict(int)
    
    def addEdge(self,u,v,wt):
        self.graph[u].append(v)
        self.graph[v].append(u)
        self.wt[n*u+v] = wt

    def cleanup(self):
        self.marked = defaultdict(int)

    def dfs(self,u):

        self.marked[u] = 1

        max,maxnode = 0,u
        for i in self.graph[u]:
            cur,node = 0,0
            if(self.marked[i] == 0):
                cur,node = self.dfs(i)
            if(cur>max):
                max = cur
                maxnode = node
        
        return 1+max,maxnode

    def lngdfs(self,u,v):

        self.marked[u] = 1

        max = self.n
        for i in self.graph[u]:
            if(i == v):
                return 1
            cur = self.n
            if(self.marked[i] == 0):
                cur = self.lngdfs(i,v)
            if(cur<max):
                max = cur
        
        return 1+max

def gcd(x,y):

    rem = 0
    for i in range(1,min(x,y)+1):
        if(x%i == 0 and y%i == 0):
            rem = i

    return rem

def main():
    
    t = inp()
 
    for l in range(t):
        n = inp()
        a = inlist()
 
        a = sorted(a,reverse=True)
 
        curgcd = a[0]
        for i in range(n):
            curgcd = gcd(curgcd,a[i])
            for j in range(i+2,n):
                if(gcd(a[j],curgcd) > gcd(a[i+1],curgcd)):
                    temp = a[i+1]
                    a[i+1] = a[j]
                    a[j] = temp
        
        printlist(a)

main() 

