from sys import stdin, stdout
import heapq
import cProfile
from collections import Counter, defaultdict, deque
from functools import reduce
import math


def get_int():return int(stdin.readline().strip())
def get_tuple():return map(int, stdin.readline().split())
def get_list():return list(map(int, stdin.readline().split()))

def prime_factors(n):  # n**0.5 complex
    factors = dict()
    for i in range(2, math.ceil(math.sqrt(n)) + 1):
        while n % i == 0:
            if i in factors:
                factors[i] += 1
            else:
                factors[i] = 1
            n = n // i
    if n > 2:
        factors[n] = 1
    return (factors)

def solve():
    n = get_int()
    ls = get_list()
    start = max(ls)
    ans = [start]
    st = [i for i in range(n)]
    st.remove(ls.index(start))
    n -= 1
    while n:
        till_max = 0
        new_ind = 0
        for i in st:
            gcd = math.gcd(start,ls[i])
            if gcd>till_max:
                till_max = gcd
                new_ind = i
        ans.append(ls[new_ind])
        start = till_max
        st.remove(new_ind)
        n -= 1
    print(*ans)







def main():
    solve()


TestCases = True

if TestCases:
    for i in range(get_int()):
        main()
else:
    main()
