from collections import defaultdict
import math
t = int(input())
for _ in range(t):
    n = int(input())
    l = list(map(int,input().split()))
    dic = defaultdict(lambda:0)
    for i in range(n):
        dic[l[i]]+=1
    ans = [max(l)]
    GCD = max(l)
    dic[max(l)]-=1
    prev_gcd = GCD
    for i in range(n-1):
        max_gcd = 1
        m_i = n-1
        for j in range(n):
            if dic[l[j]]:
                if math.gcd(prev_gcd,l[j])>=max_gcd:
                    max_gcd  = math.gcd(prev_gcd,l[j])
#                    print(max_gcd)
                    m_i = j
        dic[l[m_i]]-=1
        ans.append(l[m_i])
        prev_gcd = math.gcd(prev_gcd,ans[-1])
    print(*ans)
    