from math import gcd
def gt():
    return list(map(int, input().split()))
    

t ,= gt()
while t:
    t -= 1
    n ,= gt()
    ar = gt()
    vis = [0 for val in ar]
    g = max(ar)
    while 1:
        cnt = 0
        mx = 0
        for i in range(len(ar)):
            if vis[i]: continue
            tmp = gcd(ar[i], g)
            if tmp == g:
                print(ar[i],end=' ')
                vis[i] = 1
                cnt += 1
                
            if tmp < g:
                mx = max(mx,tmp)
        g = mx
        if mx==0: break
    print('')
