import math
 
def solve(n, a):
        b = [max(a)]
        c = [max(a)]
        a.remove(max(a))
        while(len(a) > 0):
                best = 0
                for i in range(1, len(a)):
                        if math.gcd(c[-1], a[i]) > math.gcd(c[-1], a[best]):
                                best = i
                b.append(a[best])
                c.append(math.gcd(c[-1], a[best]))
                a.pop(best)
        return " ".join(str(b[i]) for i in range(n))
        

t = int(input())
for t in range(t):
        n = int(input())
        a = [int(s) for s in input().split()]
 
        result = solve(n, a)
        print(result)
