import sys
from math import gcd


#def solve(a,b,c,d):
def solve(a):
    n = len(a)
    #print(a)
    #gcds = [ [0]*n for i in range(n)]
    #for i in range(n):
        #for j in range(i,n):
            #gcds[i][j] = gcd(a[i],a[j])
            #gcds[j][i] = gcd(a[i],a[j])

    #remaining = [i for i in range(n)]
    #while len(remaining) > 0:
    #mx = (-1,-1,-1)
    #for i in remaining:
        #for j in [x for x in remaining if x > i]:
            #if gcds[i][j] > mx[0]:
                #mx = (gcds[i][j],i,j)

    #to_place = [mx[1],mx[2]]
    #while len(remaining) > 0:
        #determine which has the largest gcd
    def largest_in_row( a, ignore_cols ):
        mx = (-1,-1)

        for i,v in enumerate(a):
            if i in ignore_cols:
                continue
            if v > mx[0]:
                mx = (v,i)
        return mx

    b = [0]*n
    maxval,ind = largest_in_row(a, {})
    b[0] = a[ind]
    last_ind,cum_gcd = ind,maxval
    ignore_cols = {ind:True}

    for i in range(1,n):
        #print(b)
        gcd_row = [gcd(cum_gcd,val) for val in a]
        maxgcd,ind = largest_in_row( gcd_row, ignore_cols )
        b[i] = a[ind]
        ignore_cols[ind] = True
        last_ind,cum_gcd = ind, gcd(cum_gcd,maxgcd)
        
        
        
                    
    


    return b


if __name__ == '__main__':
    IN = [x.strip() for x in sys.stdin.readlines()]

    T = int(IN[0])
    cur = 1
    for ti in range(T):
        n = int(IN[cur])
        #a = [int(x) for x in IN[cur].split(' ')]
        a = [int(x) for x in IN[cur+1].split(' ')]
        res = solve(a)
        print(' '.join([str(x) for x in res]))
        cur += 2

