import math
t=int(input())
for i in range(t):
    n=int(input())
    arr=list(map(int,input().split()))
    a=max(arr)
    ans=[a]
    arr.remove(a)
    l=n-1
    GCD=[a]
    for k in range(n-1):
        maxi=-1
        for j in range(l):
            temp=math.gcd(GCD[-1],arr[j])
            if temp>maxi:
                maxi=temp
                temp1=arr[j]
        GCD.append(math.gcd(temp1,GCD[-1]))
        ans.append(temp1)
        arr.remove(temp1)
        l-=1
    print(" ".join(str(x) for x in ans))