import sys

sys.setrecursionlimit(10**5)
int1 = lambda x: int(x)-1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def gcd(a,b):
    while b:a,b=b,a%b
    return a

for _ in range(II()):
    n=II()
    aa=LI()
    mx=max(aa)
    i=aa.index(mx)
    ans=[mx]
    aa[i]=-1

    now=mx
    while len(ans)<n:
        mx = -1
        mxi = []
        for i,a in enumerate(aa):
            if a==-1:continue
            g=gcd(now,a)
            if g==mx:mxi.append(i)
            elif g>mx:
                mx=g
                mxi=[i]
        for i in mxi:
            ans.append(aa[i])
            aa[i]=-1
    print(*ans)
