# not my code
# https://codeforces.com/contest/1407/submission/92244789
 
from __future__ import division, print_function
 
import sys
import math
 
input = sys.stdin.buffer.readline

t = int(input())
for _ in range(t):
    n = int(input())
    a = [int(ai) for ai in input().split()]

    m = max(a)

    used = [0] * n
    sol = [0] * n
    cur = m

    for i in range(n):
        idx = -1
        best = 0

        for j in range(n):
            if not used[j]:
                g = math.gcd(cur, a[j])
                if best < g:
                    best = g
                    idx = j

        used[idx] = 1
        sol[i] = a[idx]
        cur = math.gcd(sol[i], cur)

    print(*sol)
