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])]
        d=[]
        for j in range(1,A):
            c+=[math.gcd(int(c[0]),int(arr[j]))]
        c=sorted(c)    
        c=c[::-1]
        for k in range(len(c)):
            for m in range(len(arr)):
                if math.gcd(arr[m],c[k])==c[k]:
                    if k!=len(c)-1:
                        print(arr[m],end=' ')
                    else:
                        print(arr[m])
                    arr.remove(arr[m])
                    break
                
        
            

ans()