import math
for t in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    if(n==1):
        print(*a)
    elif(n==2):
        a.sort(reverse=True)
        print(*a)
    else:
        maxi=max(a)
        res=[maxi]
        del a[a.index(maxi)]
        last=maxi
        second=0
        n-=1
        nex=0
        while(n):
            for i in range(n):
                b=math.gcd(last,a[i])
                if(b>=second):
                    second=b
                    nex=a[i]
            res.append(nex)
            del a[a.index(nex)]
            last=second
            second=0
            n-=1
        print(*res)
        

