import math
t= int(input())
for i in range(t):
    n= int(input())
    a= list(map(int,input().split()))
   
    max111= max(a)
    ans=list()
    for j in range(n):
        if(max111==1):
            break
        besg= math.gcd(max111,a[0])
        mark=0
        while(False):
            break
        for k in range(n):
            if(math.gcd(max111,a[k])>besg):
                besg=math.gcd(max111,a[k])
                mark=k
        ans.append(a[mark])
        a[mark]=-1
        max111=besg
    for j in a:
        if j!=-1:
            ans.append(j)
    xx=0
    for cc in ans:
        if(cc==-1):
            continue
        print(cc,end=' ')
        
    print("")    
            
