def gcd(x,y):
    while(y):
        x, y = y, x % y
    return x 
    
for t in range(int(input())):
    n=int(input())
    l=list(map(int,input().split()))
    res=[]
    egcd=0
    i=0
    maxgcd=0
    while(i<n):
        maxgcd=0
        for j in range(n):
            if l[j]!=-1:
                x=gcd(egcd,l[j])
                if maxgcd<x:
                    loc=j
                    maxgcd=x
        res.append(l[loc])
        l[loc]=-1
        egcd=maxgcd
        i=i+1
    for i in res:
        print(i,end=" ")
    print()