import sys
input = sys.stdin.readline
from math import gcd
t = int(input())
for _ in range(t):
    n = int(input())
    l = list(map(int, input().split()))
    prev = max(l)
    out = []
    while l:
        nex = max((gcd(l[i], prev), i) for i in range(len(l)))
        out.append(l.pop(nex[1]))
        prev = nex[0]
    print (' '.join(map(str, out)))
