import heapq
import collections


def gcd(a, b):
    if a < b:
        return gcd(b, a)
    if a % b == 0:
        return b
    return gcd(b, a % b)


def solve(n, arr):
    if n == 1:
        return arr
    curr_gcd = max(arr)
    ans = [curr_gcd]
    counter = collections.Counter(arr)
    counter[curr_gcd] -= 1
    for _ in range(n - 1):
        gcd_max = -1
        for num in counter:
            if counter[num] > 0:
                gcd_max = max(gcd_max, gcd(num, curr_gcd))

        next_num = -1
        for num in counter:
            if counter[num] > 0 and gcd(num, curr_gcd) == gcd_max:
                next_num = num
                break
        ans.append(next_num)
        counter[next_num] -= 1
        curr_gcd = gcd_max
    return ans


if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        n = int(input())
        arr = [*map(int, input().split())]
        print(" ".join(map(str, solve(n, arr))))
