def g(a,b):
    if b==0:
        return a
    return g(b,a%b)
for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    i=0
    m=-1
    j=0
    while i<n:
        if a[i]>m:
            j=i
            m=a[i]
        i+=1
    b=[j]
    ans=a[j]
    for _ in range(n-1):
        m=-1
        j=0
        for i in range(n):
            if i not in b:
                if g(ans,a[i])>m:
                    j=i
                    m=g(ans,a[i])
        b.append(j)
        ans=g(ans,a[j])
    c=[]
    for i in range(n):
        c.append(a[b[i]])
    print(*c)
    
    