#*****DO NOT COPY*****
#F
#U       ___                     ___                         ___           ___           ___           ___          _____
#C      /  /\      ___          /  /\          ___          /__/\         /  /\         /  /\         /  /\        /  /::\
#K     /  /::\    /  /\        /  /:/_        /  /\         \  \:\       /  /::\       /  /::\       /  /:/_      /  /:/\:\
#     /  /:/\:\  /  /:/       /  /:/ /\      /  /::\         \  \:\     /  /:/\:\     /  /:/\:\     /  /:/ /\    /  /:/  \:\
#Y   /  /:/~/:/ /__/::\      /  /:/ /::\    /  /:/\:\    ___  \  \:\   /  /:/~/::\   /  /:/~/:/    /  /:/ /:/_  /__/:/ \__\:|
#O  /__/:/ /:/  \__\/\:\__  /__/:/ /:/\:\  /  /:/~/::\  /__/\  \__\:\ /__/:/ /:/\:\ /__/:/ /:/___ /__/:/ /:/ /\ \  \:\ /  /:/
#U  \  \:\/:/      \  \:\/\ \  \:\/:/~/:/ /__/:/ /:/\:\ \  \:\ /  /:/ \  \:\/:/__\/ \  \:\/:::::/ \  \:\/:/ /:/  \  \:\  /:/
#    \  \::/        \__\::/  \  \::/ /:/  \  \:\/:/__\/  \  \:\  /:/   \  \::/       \  \::/~~~~   \  \::/ /:/    \  \:\/:/
#A    \  \:\        /__/:/    \__\/ /:/    \  \::/        \  \:\/:/     \  \:\        \  \:\        \  \:\/:/      \  \::/
#L     \  \:\       \__\/       /__/:/      \__\/          \  \::/       \  \:\        \  \:\        \  \::/        \__\/
#L      \__\/                   \__\/                       \__\/         \__\/         \__\/         \__\/
#
#
#*****DO NOT COPY******

def gcd(a , b):
    # if a > b:
    #     a , b =  b , a
    if a == 0:
        return b

    return gcd(b % a, a)

def solve(n , arr):

    output = []

    output.append(max(arr))
    arr.remove(output[0])
    gcd__ = output[0]

    while len(arr) > 0:
        #print('output' , output)
        maxx = 0
        current_ind = 0
        i = 0
        while i < len(arr):

            gcd_ = gcd(gcd__ , arr[i])
            if gcd_ > maxx:
                maxx = gcd_
                current_ind = i
            elif gcd_ == maxx:
                if arr[i] < arr[current_ind]:
                    current_ind = i
            i += 1
        gcd__ = maxx
        output.append(arr[current_ind])
        arr.remove(arr[current_ind])

    output = map(str , output)
    return " ".join(output)
for _ in range(int(input())):
    n = int(input())
    arr = list(map(int , input().split()))
    print(solve(n , arr))
