import math
c = int(input())

for _ in range(c):
    length =  int(input())
    arr = [int(item) for item in input().split()]
    new_arr = []

    for index,val in enumerate(arr):
        new_arr.append((val,index))


    def reArrange(arr):
        n = len(arr)
        if n == 0:
            return []
    
        g_list = sorted(arr,reverse=True)
        # meaningless weight -> 'heap'

        top = g_list[0][0]

        stop_index = 0

        result_list = []

        while len(g_list) != 0:
            while stop_index + 1 <= len(g_list) - 1 and g_list[stop_index][0] == g_list[stop_index+1][0]:
                stop_index += 1 

            result_list += sorted([arr[i[1]][0] for i in g_list[:stop_index+1]],reverse=True)

            g_list = g_list[stop_index+1:]

            if len(g_list) == 0:
                break
    
            t_list = []

            for count in range(len(g_list)):
                p = math.gcd(top,g_list[count][0])
                t_list.append((p,g_list[count][1]))

            g_list = sorted(t_list,reverse=True)

            top = g_list[0][0]

            stop_index = 0

        return result_list
        
    print(*reArrange(new_arr))





            



