import sys
import math
ii = lambda: sys.stdin.readline().strip()
idata = lambda: [int(x) for x in ii().split()]

def solve():
    n = int(ii())
    data = idata()
    ans = []
    mx = max(data)
    count = 0
    a = [1] * n
    while count < n:
        mx2 = 0
        for i in range(n):
            d = math.gcd(data[i], mx)
            if a[i] and mx == d:
                count += 1
                ans += [data[i]]
                a[i] = 0
            elif a[i] and mx2 < d:
                mx2 = d
        mx = mx2
    print(*ans)


    return

for t in range(int(ii())):
    solve()
