from sys import stdin, stdout
input = stdin.readline
print = lambda x:stdout.write(str(x)+'\n')

from math import gcd
for _ in range(int(input())):
    n = int(input())
    a = list(map(int,input().split()))
    ans = [max(a)]
    
    taken = set(range(n))
    taken.remove(a.index(ans[0]))
    
    curr = ans[0]
    
    
    while len(ans)!=n:
        mx = 0
        for i in taken:
            g = gcd(curr, a[i])
            if mx<g:
                mx = g
                idx = i
        curr = mx
        taken.remove(idx)
        ans.append(a[idx])
    
    ans = ' '.join(list(map(str, ans)))
    
    print(ans)
    