from math import gcd

for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    a.sort(reverse=True)
    
    di = {}
    for i in a:
        di[i] = di.get(i, 0) + 1

    ans = [a[0]] * di[a[0]]
    val = a[0]
    while di[val]:
        a.remove(val)
        di[val] -= 1
    g = 1
    temp = val
    for i in a:
        if gcd(val, i) > g:
            temp = i
            g = gcd(val, i)

    val = temp
    ans += [val] * di[val]
    while di[val]:
        a.remove(val)
        di[val] -= 1

    if val == -1 or g == 1:
        print(*(ans + a))
    else:
        val = g
        g = 1
        while a:
            temp = val
            for i in a:
                if gcd(val, i) > g:
                    temp = i
                    g = gcd(val, i)
            
            val = temp
            if g == 1:
                break

            ans += [val] * di[val]
            while di[val]:
                a.remove(val)
                di[val] -= 1

            val = g
            g = 1

        ans += a
        print(*ans)