import math
for _ in range(int(input())):
    n = int(input())
    arr = [int(x) for x in input().split()]
    m = max(arr)
    arr.remove(m)
    gcd = m
    res = [m]
    num = 0
    while len(arr) > 0:
        tem = 0
        for i in range(len(arr)):
            if math.gcd(gcd, arr[i]) > tem:
                tem = math.gcd(gcd, arr[i])
                num = i
        res.append(arr[num])
        gcd = tem
        del arr[num]
    print(*res)

