from math import sqrt
def gcd(a,b): 
    if (b == 0): 
         return a 
    return gcd(b, a%b)
    
for _ in range (int(input())):
    n = int(input())
    a = [int(i) for i in input().split()]
    b = [max(a)]*(n)
    ind = a.index(max(a))
    ans = [a[ind]]
    a[ind]=-1
    prevgcd = ans[-1]
    for j in range (n-1):
        ma = 0
        mi = 0
        for i in range (n):
            if a[i]!=-1:
                cur = gcd(a[i],prevgcd)
                if cur<b[i]:
                    b[i]=cur
                cur = b[i]
                if ma<cur:
                    mi = i
                    ma = cur
        ans.append(a[mi])
        prevgcd = gcd(prevgcd,a[mi])
        a[mi]=-1
    print(*ans)