def gcd(a,b): 
    if (b == 0): 
         return a 
    return gcd(b, a%b) 

def ans():
    n = int(input())
    a = list(map(int, input().split()))
    d = 0
    b = [0 for i in range(n)]
    vis = set()
    for i in range(n):
        maxi = 0
        ind = 0
        for j in range(n):
            if j not in vis and gcd(d, a[j])>maxi:
                maxi = gcd(d, a[j])
                ind = j
        d = maxi
        vis.add(ind)
        b[i] = a[ind]
    print(*b)

def solve():
    for _ in range(int(input())):
        ans()
solve()