from sys import stdin,stdout
input=stdin.readline
from math import gcd
for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    b=[0]*n
    c=[0]
    d=[]
    for i in range(n):
        l=c[-1]
        for j in range(n):
            if b[j]==0:
                k=gcd(l,a[j])
                break
        for j in range(n):
            if b[j]==0:
                k=max(k,gcd(l,a[j]))
        for j in range(n):
            if b[j]==0 :
                r=gcd(c[-1],a[j])
                if r==k:
                    c.append(k)
                    d.append(a[j])
                    b[j]=1
                    break
    stdout.write(' '.join(str(i) for i in d)+'\n')
                
        
                
                
            
    
            