
import itertools
import math
import sys
from collections import defaultdict

 
def stdinWrapper():
    data = '''7
2
2 5
4
1 8 2 3
3
3 8 9
5
64 25 75 100 50
1
42
6
96 128 88 80 52 7
5
2 4 8 16 17
'''
    
    for line in data.split('\n'):
        yield line
 
if '--debug' not in sys.argv:
    def stdinWrapper():
        while True:
            yield input()
 
inputs = stdinWrapper()
 
def inputWrapper():
    return next(inputs)
 
def getType(_type):
    return _type(inputWrapper())
 
def getArray(_type):
    return [_type(x) for x in inputWrapper().split()]
 
 
 
''' Solution '''

def solve(a):
    res = []
    _max = max(a)

    for i in range(len(a)):
        if a[i] == _max:
            res.append(a.pop(i))
            break

    _current_gcd = res[0]

    while a and _current_gcd > 1:
        _max_gcd = 0
        _i = -1
        for i in range(len(a)):
            _cur = math.gcd(_current_gcd, a[i])
            if _cur > _max_gcd:
                _max_gcd = _cur
                _i = i

        
        res.append(a.pop(_i))
        _current_gcd = _max_gcd


    res += a

    return res


t = getType(int)

for _ in range(t):
    n = getType(int)
    a = getArray(int)
    arr = solve(a)
    print(' '.join(list(map(str, arr))))
