def gcd(a,b):
    if a<0 or b<0:
        return -1
    if b==0:
        return a
    if a<b:
        a,b=b,a
    return gcd(b,a%b)


t=int(input())
for _ in range(t):
    n=int(input())
    arr=list(map(int,input().split()))
    temp=[0]*n
    maxx=max(arr)
    for i in range(n):
        temp[i]=gcd(maxx,arr[i])
    index=0
    ans=[]
    used=[1]*n
    var=maxx
    for i in range(n):
        val=0
        for j in range(n):
            if gcd(var,temp[j])>val and used[j]:
                index=j
                val=gcd(var,temp[j])
        var=val
        used[index]=0
        ans.append(index)
    for i in ans:
        print(arr[i],end=' ')
    print()
    
    