import math

T = int(input())

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

    maxind = 0
    oldmaxgcd = a[0]

    for i in range(1, n):
        if a[i] > oldmaxgcd:
            maxind = i
            oldmaxgcd = a[i]

    print(a[maxind], end=' ')
    del a[maxind]
    n -= 1

    while a:
        maxind = 0
        maxgcd = math.gcd(oldmaxgcd, a[0])

        for i in range(1, n):
            gcd = math.gcd(oldmaxgcd, a[i])

            if gcd > maxgcd:
                maxind = i
                maxgcd = gcd

        print(a[maxind], end=' ')
        del a[maxind]
        n -= 1
        oldmaxgcd = maxgcd

    print()
