from math import gcd
import sys
input=sys.stdin.readline
t=int(input())
for i in range(t):
    n=int(input())
    a=[int(i) for i in input().split() if i!='\n']
    maxa=max(a)
    ans=[]
    ind=[0]*(n)
    for i in range(n):
        tmp,store=0,0
        for j in range(n):
            if ind[j]==0:
                gcd_=gcd(maxa,a[j])
                #print(gcd_,store,ind,a[i])
                if gcd_>store:
                    store=gcd_
                    tmp=j
        ind[tmp]=1
        maxa=store
        ans.append(a[tmp])
    ans=' '.join(map(str,ans))
    sys.stdout.write(ans+'\n')
        
        
    
