from math import gcd


def solve(n, a):
    imax = 0
    for i in range(1, n):
        if a[i] > a[imax]:
            imax = i
    notwas = [True] * n
    notwas[imax] = False
    prevgcd = a[imax]
    ans = [a[imax]]
    for i in range(n - 1):
        max1 = 0
        max_now = 0
        for j in range(n):
            if notwas[j]:
                if gcd(a[j], prevgcd) > max1:
                    max_now = j
                    max1 = gcd(a[max_now], prevgcd)
        ans.append(a[max_now])
        notwas[max_now] = False
        prevgcd = max1
    return ans


t = int(input())
for i in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    print(*solve(n, a))