def inp():
    return list(map(int, input().split()))


def get_gcd(a, b):
    if a == 0:
        return b

    return get_gcd(b % a, a)


def get_divs(n):
    cnt = 1
    res = []
    while cnt ** 2 <= n:
        if n%cnt == 0:
            res.append(cnt)
            res.append(n//cnt)

        cnt += 1

    return res

def get_val(val, arr):
    gcd_= 0
    ind = 0
    for i in range(len(arr)):
        nw = get_gcd(a[i], val)
        if nw > gcd_:
            ind = i
            gcd_ = nw

    ret = gcd_
    vl = a[ind]
    a.pop(ind)
    return ret, a, vl


for t in range(int(input())):
    n = int(input())
    a = inp()

    mx = max(a)

    res = []

    while len(res) < n:
        mx, a, vl= get_val(mx, a)
        res.append(vl)

    print(*res)