from math import gcd
for t in range(int(input())):
    n=int(input())
    l=[int(i) for i in input().split()]
    d={}
    ele=max(l)
    ans=[ele]
    l.remove(ele)
    for i in l:
        temp=gcd(i,ele)
        if temp in d.keys():
            d[temp].append(i)
        else:
            d[temp]=[i]
    ll=list(d.keys())
    ll.sort(reverse=True)
    for i in ll:
        for j in d[i]:
            ans.append(j)
    print(*ans,sep=" ")