from math import gcd
T = int(input())
for t in range(T):
    n = int(input())
    A = list(map(int, input().split()))
    maxx = max(A)
    curr_gcd = maxx
    res = [maxx]
    A.remove(maxx)


    for j in range(n-1):
        temp = 1
        elem = 0
        for i in range(len(A)):
            temp2 = gcd(curr_gcd, A[i])
            if  temp2 > temp:
                temp = temp2
                elem = i
        res.append(A[elem])
        curr_gcd = temp
        A = A[:elem] + A[elem+1:]
    for x in res:
        print(x, end=" ")
    print()
        
        
