def read(datatype=int):
    return datatype(input())
 
 
def readl(datatype=int):
    return list(map(datatype, input().split()))
 
 
def printl(data):
    string = ''
    for d in data:
        string += str(d) + ' '
    print(string)


def gcd(x, y): 
   while(y): 
       x, y = y, x % y 
   return x 


def best_gcd(old, arr, used_arr):
    index = 0
    max_gcd = 0
    for i in range(len(arr)):
        this_gcd = gcd(old, arr[i])
        if this_gcd >= max_gcd and not used_arr[i]:
            max_gcd = this_gcd
            index = i
    return max_gcd, index

 
def testcase():
 
    # --------------------- start testcase ---------------------
 
    n = read()
    arr = readl()

    used_arr = [False for _ in arr]

    max_index = 0
    for i in range(1, n):
        if arr[i] > arr[max_index]:
            max_index = i
    used_arr[max_index] = True

    brr = [arr[max_index]]
    crr = [arr[max_index]]

    for _ in range(n - 1):
        max_gcd, index = best_gcd(crr[-1], arr, used_arr)
        brr.append(arr[index])
        crr.append(max_gcd)
        used_arr[index] = True

    printl(brr)

    # --------------------- end testcase -----------------------
    return
 
 
t = read()
for _ in range(t):
    testcase()