def swap(arr,i,j):
    temp = arr[i]
    arr[i] = arr[j]
    arr[j] = temp

def gcd(a,b): 
    if (b == 0): 
         return a 
    return gcd(b, a%b) 

t = int(input())

for i in range(t):
    size = int(input())
    arr = [int(n) for n in input().split()]
    maxi = arr[0]
    idx = 0
    
    for j in range(1,size):
        if maxi<arr[j]:
            maxi  = arr[j]
            idx = j
    
    swap(arr,idx,0)
    old_gcd = arr[0]
    
    for j in range(size-1):
        maxi = -1
        idx = -1
        
        for k in range(j+1,size):
            div = gcd(old_gcd,arr[k])
            
            if maxi == -1 or maxi < div:
                maxi = div
                idx = k
        
        swap(arr,j+1,idx)
        old_gcd = maxi

    print(" ".join(map(str,arr)))