import math

def crypto(n,a):
    first = max(a)
    ans = [first]
    old = first
    a.remove(first)    
    while a:
        gcds = [math.gcd(x,old) for x in a]
        ind = gcds.index(max(gcds))
        ans.append(a[ind])
        old = gcds[ind]
        del a[ind]
    return ans

t = int(input())
for i in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    print(*crypto(n,a))