from math import gcd
for _ in range(int(input())):
    n = int(input())
    l = list(map(int,input().split()))
    ans = [max(l)]
    ind = l.index(max(l))
    l = l[:ind]+l[ind+1:]
    cnt = 1
    g = ans[0]
    while cnt < n:
        ind = -1
        temp = -float("inf")
        for i in range(n-cnt):
            t = gcd(g,l[i])
            if t > temp:
                temp = t
                ind = i
        g = temp
        cnt+=1
        ans.append(l[ind])
        l = l[:ind]+l[ind+1:]
    print(*ans)
