t=int(input())
def gcd(m,n):
    x=max(m,n)
    y=min(m,n)
    r=x%y
    if(r==0):
        return y
    else:
        return gcd(r,y)
    
def maxgcd(x,a=[]):
    ind=0
    maxm=0
    for i in range(len(a)):
        g=gcd(x,a[i])
        if(g>=maxm):
            maxm=g
            ind=i
    return [maxm,ind]

for i in range(t):
    a=[]
    n=int(input())
    a=[int(a) for a in input().split()]
    res=[]
    b=a.copy()
    g=max(b)
    res.append(max(b))
    del b[b.index(max(b))]
    while(len(b)!=0):
        v=maxgcd(g,b)
        g=v[0]
        res.append(b[v[1]])
        del b[v[1]]
    v=[print(x,end=' ') for x in res]
    print('')