def Proverka_numbers(x):
    list_1 = [i for i in range(1,round(x ** 0.5) + 1)]
    list_of_numbers = []
    for i in list_1:
        if x % i == 0:
            k = x // i
            list_of_numbers.append(i)
            list_of_numbers.append(k)
    list_of_numbers.sort()
    LOL = set(list_of_numbers)
    list_of_numbers = list(LOL)
    list_of_numbers.sort()
    return list_of_numbers
mass = []

def kek(mass,x):
    _kek_ = mass
    _kek_.sort()
    _kek_ = _kek_[::-1]
    for i in _kek_:
        if x % i != 0:
            del mass[mass.index(i)]
    return mass
def GCD(_dul_,x,n):
    ''' наибольший общий делитель '''
    _dul_ = kek(_dul_,x)
    return _dul_
def mass(x):
    _MUX_ = max(x)
    _del_ = Proverka_numbers(_MUX_)
    mass_b = [_MUX_]
    del x[x.index(max(x))]
    for n in range(0,len(x)):
        _max_ = 0
        for i in x:
            _kok_ = GCD(_del_[:],i,len(mass_b + [i]))
            _number_ = max(_kok_)

            if _number_ > _max_:
                max_i = i
                _max_ =_number_
        else:
            mass_b += [max_i]
            _del_ = _kok_
            iindex = x.index(max_i)

            del x[iindex]
    return mass_b
n = int(input())
_MASS_ = []
for i in range(n):
    K = input()
    a = input().split()
    a = list(map(int,a))
    _MASS_.append(mass(a))
for i in _MASS_:
    for j in i:
        print(j,end=' ')
    print()