import sys
input = sys.stdin.readline
from math import gcd

def f():
    n = int(input())
    A = list(map(int, input().split()))
    C = [0]*n
    now = 0
    L = []
    for _ in range(n):
        idx = -1
        temp = -1
        for i, a in enumerate(A):
            if not C[i]:
                g = gcd(a, now)
                if g > temp:
                    temp = g
                    idx = i
        C[idx] = 1
        now = temp
        L.append(A[idx])
    print(*L)

t = int(input())
for x in range(t):
    f()