import math
def answer(n,A):
    A.sort()
    vis=[0]*n
    arr=[A[n-1]]
    vis[n-1]=1
    g=A[n-1]
    while len(arr)<n:
        index=-1
        maxi=0
        for i in range(n):
            if vis[i]==0:
                x=math.gcd(g,A[i])
                if x>maxi:
                    maxi=x
                    index=i
        if maxi>1:
            vis[index]=1
            arr.append(A[index])
            g=maxi
        else:
            break
            
    for i in range(n):
        if vis[i]==0:
            arr.append(A[i])
    return arr
            

t=int(input())
for i in range(t):
    n=int(input())
    arr=list(map(int,input().split()))
    
    b=answer(n,arr)
    print(*b)