from math import gcd
for j in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    s=[0]*n
    l=0
    for i in range(n):
        if a[i]>a[l]:
            l=i
    ans=[]
    ans.append(a[l])
    s[l]=1
    u=ans[0]
    y=0
    for i in range(1,n):
        while s[y]==1 and y<n:
            y+=1
        h=y
        for z in range(h+1,n):
            if gcd(a[h],u)<gcd(a[z],u) and s[z]==0:
                h=z
        s[h]=1
        ans.append(a[h])
        u=gcd(u,a[h])
    print(*ans)