def gcd(x, y):
    while y:
        x, y = y, x % y
    return x


t = int(input())
for _ in range(t):
    n = int(input())
    a = [int(x) for x in input().split()]
    a.sort(reverse=True)
    g = a[0]
    for i in range(1, n):

        def func(x):
            return gcd(g, x)

        a[i:n] = sorted(a[i:n], reverse=True, key=func)
        g = gcd(g, a[i])
    print(' '.join([str(x) for x in a]))
