from collections import defaultdict
import math
t = int(input())
while t:
    t -= 1
    n = int(input())
    a = list(map(int, input().split()))
    freq = defaultdict(int)
    maxi = float("-inf")

    for x in a:
        freq[x] += 1
        maxi = max(maxi, x)

    ans = [maxi]
    i = 1
    freq[maxi] -= 1
    while i < n:
        curr_max = 0
        next = None
        for num in a:
            if freq[num]:
                if math.gcd(maxi, num) > curr_max:
                    curr_max = math.gcd(maxi, num)
                    next = num
        freq[next]-=1
        ans.append(next)
        maxi = curr_max
        i+=1
    print(*ans)
