import math
import sys
input = sys.stdin.readline

T = int(input())

for t in range(T):
    N = int(input())

    A = [int(_) for _ in input().split()]

    if N == 1:
        print(A[0])
        continue

    maxel = -1
    maxi = -1
    for i, el in enumerate(A):
        if el > maxel:
            maxel = el
            maxi = i

    inds = set()
    for i in range(N):
        if i != maxi:
            inds.add(i)

    res = [maxel]
    current_gcd = maxel

    for i in range(N-1):
        current_max = -1
        best_ind = -1
        for ind in inds:
            current = math.gcd(A[ind], current_gcd)
            if current > current_max:
                current_max = current
                best_ind = ind

        inds.remove(best_ind)
        res.append(A[best_ind])
        current_gcd = current_max

    print(' '.join(map(str, res)))
