from collections import defaultdict
import math
for _ in range(int(input())):
    n=int(input())
    l=list(map(int,input().split()))
    d=defaultdict(int)
    ans=[max(l)]
    gcd=max(l)
    for i in l:
        d[i]+=1
    d[max(l)]-=1
    curr=1
    for i in range(n-1):
        ele=1
        curr=1
        for j in l:
            if d[j]>0:
                x=math.gcd(gcd,j)
                if x>=curr:
                    curr=x
                    ele=j
        d[ele]-=1
        gcd=curr
        ans.append(ele)
        if gcd==1:
            break
    if gcd==1:
        for i in l:
            if d[i]>0:
                ans.append(i)
                d[i]-=1
    print(*ans)
    
            