import math
for _ in range(int(input())):
    n=int(input())
    arr=list(map(int,input().split()))
    arr.sort()
    out=[]
    for i in range(arr.count(arr[-1])):
        out.append(arr.pop())
    p=out[-1]
    while len(arr)!=0:
        g=1
        todel=[]
        for i in range(len(arr)):
            if math.gcd(p,arr[i])>g:
                g=math.gcd(p,arr[i])
                todel=[i]
            elif math.gcd(p,arr[i])==g:
                todel.append(i)
        todel.reverse()
        p=g
        for i in todel:
            out.append(arr.pop(i))
    print(*out)