import math
test_cases=int(input())
for test_case in range(test_cases):
    n=int(input())
    a=list(map(int,input().split()))
    gcd=0
    a.sort(reverse=True)
    ans=[a[0]]
    a.pop(0)
    x=1
    i = ans[-1]
    while True:
        max=0
        for j in a:
            if math.gcd(i,j)>=max:
                max=math.gcd(i,j)
                x=j
        if len(a)==0:
            break
        ans.append(x)
        a.remove(x)
        i=max
        if i==1:
            ans.extend(a)
            break

    print(*ans)



