import sys

#Edge cases handled
# One of the value is zero
# One or both the values are 1
def gcd(a,b):
    # making a the larger and b the smaller number 
    a , b = max(a,b) , min(a,b)
    if b == 0:
        return a
    else :
        return gcd(a%b,b)

def input():
    return sys.stdin.readline().rstrip()

def deleteValAndRecalculate(arr, i, gcd_0toi,gcd_iton):
    arr.pop(i)
    for j in range(i,len(arr)):
        gcd_iton[j] = gcd_iton[j+1]
        gcd_0toi[j] = gcd(arr[j],gcd_0toi[j-1])

    for j in range(i-1,-1,-1):
        gcd_iton[j] = gcd(arr[j],gcd_iton[j+1])

t = int(input())
for i in range(t):
    n = int(input())
    arr = list(map(int,input().split()))

    ans = []
    first_val = max(arr)
    ans.append(first_val)
    present_gcd = first_val
    arr.remove(first_val)

    while len(arr) > 0:
        max_gcd = gcd(arr[0],present_gcd)
        max_val_pos = 0
        for j in range(1,len(arr)):
            temp_gcd = gcd(present_gcd, arr[j])
            if temp_gcd > max_gcd:
                max_val_pos = j
                max_gcd = temp_gcd
        ans.append(arr[max_val_pos])
        arr.pop(max_val_pos)
        present_gcd = max_gcd

    print(" ".join(map(str,ans)))




    # gcd_0toi = [1] * n
    # gcd_iton = [1] * n
    # gcd_0toi[0] = arr[0]
    # gcd_iton[n-1] = arr[n-1]
    # for j in range(1,n):
    #     gcd_0toi[j] = gcd(gcd_0toi[j-1],arr[j])
    # for j in range(n-2,-1,-1):
    #     gcd_iton[j] = gcd(gcd_iton[j+1],arr[j])

    # ans = ''
    # for j in range(1,n):
    #     valueRemovePos = 0
    #     maxGCD = 0
    #     for k in range(0,len(arr)):
    #         temp_gcd = 0
    #         if k == 0:
    #             temp_gcd = gcd_iton[1]
    #         elif k == len(arr)-1:
    #             temp_gcd = gcd_0toi[k-1]
    #         else :
    #             temp_gcd = gcd(gcd_0toi[k-1],gcd_iton[k+1])
    #         if temp_gcd > maxGCD:
    #             valueRemovePos = k
    #             maxGCD = temp_gcd
    #     ans = str(arr[valueRemovePos]) + " " + ans
    #     deleteValAndRecalculate(arr,valueRemovePos,gcd_0toi,gcd_iton)
    # ans = str(arr[0]) + " " + ans
    # print(ans)




    
