def g_c_d(a,b):
    if a==0:
        return b
    return g_c_d(b%a,a)
t=int(input())
for i in range(t):
    n=int(input())
    l=list(map(int,input().split()))
    b=[]
    b.append(max(l))
    l.remove(max(l))
    num=b[0]
    for j in range(n-1):
        gcd=0
        for k in range(n-j-1):
            a=g_c_d(num,l[k])
            if a>gcd:
                x=l[k]
                gcd=a
        num=gcd
        l.remove(x)
        b.append(x)
        if x==1:
        	b+=l
        	break
    print(*b)