import math
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
    gcdvalue=res[ind]
    #print(gcdvalue)
    while len(res)!=n:
        mv=0
        #print(l)
        for i in range(len(l)):
            #print("l[i]",l[i])
            temp=math.gcd(gcdvalue,l[i])
            #print("temp",temp)
            #print("___________________")
            #print("res",res[ind])
            #print(temp)
            if temp>mv:
                mv=temp
                value=l[i]
        #print("-------------------------------------------------------------------------")
        gcdvalue=mv
        res.append(value)
        ind+=1
        l.remove(value)
    print(*res)