import math
def ans():
    N=int(input())
    for i in range(N):
        A=int(input())
        arr=sorted(list(map(int,input().split())))
        c=[]
        arr=arr[::-1]
        c+=[int(arr[0])]
        while len(arr)>0:
            a=0
            e=0
            for j in range(len(arr)):
                if math.gcd(arr[j],c[-1])>a:
                    a=math.gcd(arr[j],c[-1])
                    e=j
            c+=[arr[e]]
            if len(arr)!=1:
                print(arr[e],end=' ')
            else:
                print(arr[e])
            arr.remove(arr[e])
        
            
     
        

ans()