import math,sys
from sys import stdin,stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda:int(input())
li = lambda:list(map(int,input().split()))

def solve():
    n=I()
    a=li()
    p=max(a)
    ans=[]
    q=deque()
    a.sort(reverse=True)
    ans=[a[0]]
    p=a[0]
    visited=[0]*n
    visited[0]=1
    while(len(ans)!=n):
        p=math.gcd(p,ans[-1])
        m=0
        key=-1
        for i in range(n):
            if(visited[i]):
                continue 
            g=math.gcd(p,a[i])
            if(g>m):
                m=g 
                key=i
        if(key!=-1):
            ans.append(a[key])
            visited[key]=1
    print(*ans)

for _ in range(I()):
    solve()