from math import gcd
for _ in range(int(input())):
    n = int(input())
    a = list(map(int,input().split()))
    l = sorted(a,reverse=True)
    for i in range(n-1):
        curr,maxGcd=i+1,gcd(l[i],l[i+1])
        for j in range(i+1,n):
            if gcd(l[i],l[j])>=maxGcd:
                maxGcd=gcd(l[j],l[i])
                curr = j
        l[i+1],l[curr]=l[curr],l[i+1]
    if a==l and n!=1:
        for i in range(n-1,0,-1):
            if l[i]!=l[i-1] and l[i]>a[i-1]:
                l[i],l[i-1]=l[i-1],l[i]
                break
    print(*l)
