def nod(a, b):
    while b!=0 :
        a, b = b, a%b
    return a

t = int(input())
for sdfhyujkl in range(t):
    n = int(input())
    l = list(map(int, input().split()))
    m = max(l)  
    l.pop(l.index(m))
    answer = [m]
    fifa = m
    for i in range(1, n):
        r = 0
        ind = 0
        for j in range(len(l)):
            e = nod(l[j], fifa)
            if e > r:
                r = e
                ind = j
        if r == 1:
            break
        fifa = r
        answer.append(l.pop(ind))
        
    answer += l
    print(*answer)
                
                
                
        
     