def solve(g):
    m=0
    s=0
    for i in range(n):
        if li[i]!=-1:
                if gcd(li[i],g)>m:
                    s=i
                m=max(m,gcd(g,li[i]))
    r=li[s]            
    g=gcd(m,g)
    li[s]=-1
    return [r,g]
def gcd(x, y): 
  
   while(y): 
       x, y = y, x % y 
  
   return x 
t=int(input())
li=[]
for _ in range(t):
    n=int(input())
    li=list(map(int, input().split()))
    a1=max(li)
    g1=a1
    ans=[a1]
    li[li.index(a1)]=-1
    for i in range(1,n):
        [r1,g1]=solve(g1)
        ans.append(r1)
    print(*ans)        
  