from math import gcd


def find_max(pre):
    g, to_ans = 1, [1, arr[0]]
    for b in arr:
        tem_g = gcd(pre, b)
        if tem_g > g:
            g, to_ans = tem_g, [tem_g, b]
    return to_ans


for _ in range(int(input())):
    n = int(input())
    arr = list(map(int, input().split()))
    ma = max(arr)
    ans_gcd, ans, ans_res = [ma], [], [ma]
    arr.remove(ma)
    for m in range(n-1):
        rep = find_max(ans_gcd[-1])
        ans_gcd.append(rep[0])
        arr.remove(rep[1])
        ans_res.append(rep[1])
    print(' '.join([str(x) for x in ans_res]))
