import math
t = int(input())
ans = []
def solve(n,arr):
    ans = [max(arr)]
    gcd = max(arr)
    arr.remove(max(arr))
    while arr != []:
        gcds = []
        for ele in arr:
            gcds.append(math.gcd(ele,gcd))
        gcd = max(gcds)
        element_index = gcds.index(max(gcds))
        ans.append(arr[element_index])
        arr.remove(arr[element_index])
        #print(arr)
    return ans
    pass

for i in range(t):
    n = int(input())
    arr = [int(x) for x in input().split()]
    ans.append(solve(n,arr))

#print(ans)
for test in ans:
    for ele in test:
        print(ele, end=" ")
    print()