def gcd(a,b):
    #assuming a>b
    while(a%b!=0):
        w = a%b
        a = b
        b = w

    return(b)

def ans(a, n):
    occupied = [0]*n
    e = max(a)
    answer = [e]
    occupied[a.index(e)] = 1

    for i in range(1,n):
        temp = 0
        ind = -1
        for j in range(n):
            if gcd(answer[-1], a[j])>temp and occupied[j]==0:
                temp = gcd(answer[-1], a[j])
                ind = j
        occupied[ind] = 1
        answer.append(a[ind])
    
            
    s = ''
    for i in answer:
        s = s+str(i)+' '

    return(s)

m = int(input())
for i in range(m):
    n = int(input())
    arr = input().split()

    a = []
    for i in arr:
        a.append(int(i))

    print(ans(a, n))
