from collections import defaultdict
from math import gcd
t = int(input())

for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    # delims = defaultdict(lambda x: set())
    # for k, elem in enumerate(a):
    #     for i in range(1, round(elem ** 0.5) + 1):
    #         if elem % i == 0:
    #             delims[(elem, k)].add(i)

    num = max(a)
    indices = set([a.index(num)])
    print(num, end=" ")
    for __ in range(1, len(a)):
        cur_gcd = -1
        cur_num = 1001
        cur_ind = -1
        for j, elem in enumerate(a):
            if j not in indices:
                g = gcd(num, a[j])
                if g > cur_gcd or g == cur_gcd and a[j] < cur_num:
                    cur_gcd = g
                    cur_num = a[j]
                    cur_ind = j
        indices.add(cur_ind)
        print(cur_num, end= " ")
        num = gcd(num, cur_num)
    print()
