from sys import stdin
    
class Input:
    def readline(self):
        return stdin.readline().strip()
    
    def read_list(self):
        return self.readline().split()
    
    def read_ints(self):
        return list(map(int, self.read_list()))
    
    def known_tests(self):
        cases, = self.read_ints()
    
        for case in range(cases):
            yield self

    def single_test(self):
        yield self

class Problem:
    def __init__(self, input):
        n, = input.read_ints()
        a = input.read_ints()
        g = [0 for i in range(n)]

        a.sort(reverse=True)
        g[0] = a[0]
        le = 1
        while le < n:
            for ri in range(le, n):
                g[ri] = self.gcd(g[le - 1], a[ri])
            
            mx = max(g[le:])
            l = le
            ri = le
            while ri < n:
                if mx == g[ri]:
                    a[le], a[ri] = a[ri], a[le]
                    g[le], g[ri] = g[ri], g[le]
                    le += 1
                ri += 1

            a[l:le] = sorted(a[l:le])
            #print(a[l:le])
            #print(g[l:le])

        print(*tuple(a))
    
    def gcd(self, a, b):
        return a if b == 0 else self.gcd(b, a % b)
    
for test_input in Input().known_tests():
    Problem(test_input)