def gcd(a, b):  
    if a == 0 : 
        return b  
      
    return gcd(b%a, a)
t=int(input())
while(t>0):
    t=t-1
    n=int(input())
    a=[int(j) for j in input().split()]
    maxgcd = max(a)
    b=[max(a)]
    a.remove(max(a))
    n=n-1
    
    while(n>0):
        tempgcd =0
        tempele = 0
        for i in range(len(a)):
            currgcd =  gcd(maxgcd,a[i])
            if(currgcd>=tempgcd):
                tempgcd=currgcd
                tempele= a[i]

        b.append(tempele)
        a.remove(tempele)
        maxgcd= tempgcd
        n=n-1

    for i in b:
        print(i,end=" ")
    print()
        
    
     
