def gcd(x, y):
    while (y):
        x, y = y, x % y

    return x

def func2(arr,n):
    arr2=[]
    for i in range(0,n):
        arr2.append(0)
    arr3=[]
    maxe=0
    maxi=0
    i=0
    for e in arr:
        if maxe<e:
            maxe=e
            maxi=i
        i+=1
    arr3.append(maxe)
    arr2[maxi]=1
    ogcd=arr3[-1]
    first=arr3[-1]
    while len(arr3)!=n:
        count=0
        for i in range(0,n):
            if arr2[i]!=1:
                if count == 0:
                    fgcd = gcd(ogcd, arr[i])
                    j=i
                    count+=1
                gcd1=gcd(ogcd,arr[i])
                if gcd1>fgcd:
                    fgcd=gcd1
                    j=i
        if fgcd==1:
            for i in range(0,n):
                if arr2[i]!=1:
                    arr3.append(arr[i])
        else:
            arr3.append(arr[j])
            arr2[j]=1
            ogcd=fgcd

    for e in arr3:
        print(e,end=" ")
    print()
    return

if __name__=="__main__":
    for _ in range(int(input())):
        n = int(input())
        arr = [int(x) for x in input().split()]
        func2(arr,n)
