from math import gcd
t = int(input())
for _ in range(t):
    n = int(input())
    arr = list(map(int,input().strip().split()))[:n]
    #arr = sorted(arr,reverse=True)
    maxi_index = None
    maxi = -float('inf')
    for i in range(n):
        if arr[i] > maxi:
            maxi = arr[i]
            maxi_index = i
    (arr[0],arr[maxi_index]) = (arr[maxi_index],arr[0])
    curr_gcd = arr[0]
    for i in range(n-1):
        max_gcd = 0
        max_index = None
        for j in range(i+1,n):
            index_gcd = gcd(curr_gcd,arr[j])
            #print(index_gcd,'index gcd deb')
            if index_gcd > max_gcd:
                max_index = j
                max_gcd = index_gcd
        #print(max_gcd,'max gcd deb')
        curr_gcd = max_gcd
        (arr[i+1],arr[max_index]) = (arr[max_index],arr[i+1])
    for i in arr:
        print(i,end=' ')
    print()

