from sys import stdin,stdout
from math import gcd
for _ in range(int(stdin.readline())):
    n=int(stdin.readline())
    a=list(map(int,stdin.readline().split()))
    h=max(a);ans=[]
    while 1:
        maxh=0
        for j in range(n):
            if a[j]==-1:continue
            h1=gcd(a[j],h)
            if h1>maxh:
                maxh=h1
                ind=j
        if maxh==0:break
        ans+=[a[ind]]
        a[ind]=-1
        h=maxh
    print(*ans)