t = int(input())

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

    return hcf(b%a, a)

while t:
    t -= 1
    n = int(input())

    a = [int(ai) for ai in input().split()]

    b = []
    b.append(max(a))

    for i in range(0, n):
        if a[i] == b[0]:
            a.pop(i)
            break


    temp = -1
    ind = -1
    gcd = b[0]

    for i in range(1, n):
        for j in range(0, len(a)):
            ans = hcf(gcd, a[j])

            if ans > temp:
                temp = ans
                ind = j

            if ans == temp:
                if a[j] > a[ind]:
                    ind = j

        b.append(a[ind])
        a.pop(ind)
        temp = -1
        ind = -1
        gcd = hcf(gcd, b[i])

    for bi in b:
        print(bi, end=" ")
    print()

