import sys, math

tc = int(sys.stdin.readline())
for _ in range(tc):
    n = int(sys.stdin.readline())
    arr = list(map(int, sys.stdin.readline().split()))
    check = [False] * n
    ans = [0] * n
    ans[0] = max(arr)
    check[arr.index(max(arr))] = True
    now = ans[0]
    for i in range(1, n):
        temp = []
        for j in range(n):
            if not check[j]:
                temp.append((math.gcd(now, arr[j]), j))
        temp.sort(key=lambda x : -x[0])
        ans[i] = arr[temp[0][1]]
        check[temp[0][1]] = True
        now = temp[0][0]
    print(' '.join(map(str, ans)))





