import math  
def printDivisors(n) : 
    i = 1
    l=[]
    while i <= math.sqrt(n): 
        if (n % i == 0) : 
            if (n / i == i) : 
                l.append(i) 
            else : 
                l.append(i)
                l.append(n//i)
        i = i + 1
    l.remove(1)
    return l
t=int(input())
for _ in range(t):
    n=int(input())
    l=list(map(int, input().split()))
    l1=printDivisors(max(l))
    l1.sort(reverse=True)
    ans=[]
    for i in l1:
        l2=[]
        for j in l:
            if j%i==0:
                ans.append(j)
                l2.append(j)
        for z in l2:
            l.remove(z)
    for i in l:
        ans.append(i)
    for i in ans:
        print(i,end=" ")
    print()
    