from math import gcd
for i in range(int(input())):
    n=int(input())
    l=[int(x) for x in input().split()]
    res=[]
    res.append(max(l))
    l.remove(max(l))
    ind=0
    while len(res)!=n:
        mv=0
        l.sort()
        #print(l)
        for i in range(len(l)):
            temp=gcd(res[ind],l[i])
            #print("res",res[ind])
            #print(temp)
            if temp>=mv:
                mv=temp
                value=l[i]
        res.append(value)
        ind+=1
        l.remove(value)
    print(*res)