testcases = int(input())
def computeGCD(x, y): 
  
   while(y): 
       x, y = y, x % y 
  
   return x 
   
for testcase in range(testcases):
    n = int(input())
    temparr = input()
    temparr = temparr.split()
    arr = []
    for i in temparr:
        arr.append(int(i))
    ans = []
    tempgcd = []
    ans.append(max(arr))
    tempgcd.append(max(arr))
    x = ans[-1]
    arr.pop(arr.index(max(ans)))
    arr1 = arr
    nlen = len(arr1)
    while nlen:
        curelem = tempgcd[-1]
        curbest = 0
        ans1 = 0 
        for i in range(nlen):
            curb = computeGCD(curelem, arr1[i])
            if curb >= curbest:
                ans1 = arr1[i]
                curbest = curb 
        ans.append(ans1)
        tempgcd.append(curbest)
        flag = 0
        arr2 = []
        for i in arr1:
            if i == ans1 and flag == 0:
                flag = 1 
            else:
                arr2.append(i)
        arr1 = arr2[::]
        nlen -= 1 
    ansstr = []
    for i in ans:
        ansstr.append(str(i))
    print(" ".join(ansstr))