import math
from collections import Counter 
t=int(input())
for i in range(t):
    n=int(input())
    l=list(map(int,input().split()))
    maa=max(l)
    c=Counter(l)
    l=list(set(l))
    a=l.index(maa)
    l[a]=0 
    gc=maa 
    for i in range(c[gc]):
        print(gc,end=" ")
    m=0
    e=0
    ind=-1
    f=0
    cn=0
    while(1):
        for i in range(len(l)):
            if(l[i]!=0):
                k=math.gcd(l[i],gc)
                if(k>m):
                    m=k 
                    e=l[i]
                    ind=i
        if(m==1):
            f=1 
            break 
        else:
            for i in range(c[e]):
                print(e,end=" ")
            cn+=1
            if(cn==len(l)):
                break
            gc=m 
            l[ind]=0 
            ind=-1
            m=0
            e=0
    if(f==1):
        for i in range(len(l)):
            #print(l[i])
            if(l[i]!=0):
                #print(l[i])
                for j in range(c[l[i]]):
                    print(l[i],end=" ")
        print()
    else: 
        print()
        
        
                
