def gcd(a,b):
    if a==0:
        return b 
    if b==0:
        return a 
    return gcd(b,a%b)

for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    g=0 
    vis=[0]*n 
    for i in range(n):
        cg,mg=0,0
        index=-1 
        for j in range(n):
            if vis[j]:
                continue 
            cg=gcd(g,a[j])
            if cg>=mg:
                mg=cg 
                index=j
        vis[index]=1
        g=mg
        print(a[index],end=' ')
                
    print()
