from math import gcd

for _ in range(int(input())):
    n = int(input())
    arr = list(map(int, input().split()))
    res = []

    max_num = max(arr)
    res.append(max_num)
    ind = arr.index(max_num)
    arr[ind] = 0
    # if len(arr) == 1:
    #     print(max_num)

    prev_gcd = max_num
    for _ in range(n - 1):
        max_num, max_gcd, max_num_ind = 0, 0, 0
        for ind, num in enumerate(arr):
            if num == 0:
                continue
            else:
                gcd_x = gcd(prev_gcd, num)
                if gcd_x > max_gcd:
                    max_gcd = gcd_x
                    max_num = num
                    max_num_ind = ind
        res.append(max_num)
        arr[max_num_ind] = 0
        prev_gcd = max_gcd

    temp = list(map(str, res))
    print(" ".join(temp))
