import math
for i in range(int(input())):
    n=int(input())
    arr=[int(item) for item in input().split()]
    maxi=0
    d=[False]*n
    j=-1
    for i in range(n):
        if(arr[i]>maxi):
            maxi=arr[i]
            j=i
    brr=[]
    brr.append(maxi)
    d[j]=True
    g=maxi
    e=-1
    while(len(brr)<n):
        j=-1
        for i in range(n):
            if(d[i]==False):
                r=math.gcd(g,arr[i])
                if(r>e):
                    e=r
                    j=i
        brr.append(arr[j])
        d[j]=True
        g=e
        e=-1
    print(*brr)  
  
                