import math

T = int(input())
for _ in range(T):
    n = int(input())
    x = list(map(int, input().split()))

    l = list()
    gcd = 0
    vis = [-1 for k in range(n)]
    for i in range(len(x)):
        ind = 0
        maxi = 0
        for j in range(len(x)):
            if vis[j] != 1 and math.gcd(gcd, x[j]) > maxi:
                maxi = math.gcd(gcd, x[j])
                ind = j
        gcd = maxi
        vis[ind] = 1
        l.append(x[ind])

    st = ''
    for i in l:
        st += str(i) + ' '

    print(st)