from math import gcd
t=int(input())
for _ in range(t):
    n=int(input())
    a=[int(i) for i in input().split()]
    l=[max(a)]
    a.remove(max(a))
    while len(l)!=n:
        l1=[]
        for e in a:
            x=gcd(l[-1],e)
            l1.append((x,e))
        te=max(l1,key=lambda x:x[0])
        l.append(te[1])
        a.remove(te[1])
    print(*l)