t=int(input())
import math
while t:
    t-=1
    n=int(input())
    a=[int(i) for i in input().split()]
    a.sort()
    ans=[]
    ans.append(a[n-1])
    del a[n-1]
    curg=ans[0]
    while len(a):
        ma=0
        inl=0
        flag=0
        mi=1
        for i in range(len(a)):
            if math.gcd(curg,a[i])>mi:
                
                ma=a[i]
                inl=i
                mi=math.gcd(curg,a[i])
                flag+=1
        if flag==0:
            break
        else:
            ans.append(ma)
            del a[inl]
            curg=math.gcd(curg,ans[-1])
    ans+=a
    print(*ans,sep=" ")


