import math
import re
import itertools
import copy
import time
import sys
import functools


def input_linear(lamb=lambda x: int(x), seperator=" "):
    return map(lamb, input().split(seperator))


def input_array(lamb=lambda x: int(x), seperator=" "):
    return list(map(lamb, input().split(seperator)))


def input_matrix(rows, lamb=lambda x: int(x), seperator=" "):
    tmp_matrix = []
    for i in range(rows):
        tmp_matrix.append(input_array(lamb, seperator))
    return tmp_matrix


def input_graph_al_pair(vertices, lines, lamb=lambda x: int(x), seperator=" ", directional=False, remove_dup=False):
    temp_matrix = [x[:] for x in [[]]*vertices]
    if not directional:
        if not remove_dup:
            for i in range(lines):
                ina, inb = input_linear(lamb, seperator)
                temp_matrix[ina].append(inb)
                temp_matrix[inb].append(ina)
        else:
            for i in range(lines):
                ina, inb = input_linear(lamb, seperator)
                if inb not in temp_matrix[ina]: temp_matrix[ina].append(inb)
                if ina not in temp_matrix[inb]: temp_matrix[inb].append(ina)
        return temp_matrix
    else:
        if not remove_dup:
            for i in range(lines):
                ina, inb = input_linear(lamb, seperator)
                temp_matrix[ina].append(inb)
        else:
            for i in range(lines):
                ina, inb = input_linear(lamb, seperator)
                if inb not in temp_matrix[ina]: temp_matrix[ina].append(inb)
        return temp_matrix




"""
[*open(0)] -- Input, with \n at the end of each line
new_list = filter(lambda item: condition(item), old_list) 
"""

# MAIN
if __name__ == '__main__':
    for _ in range(int(input())):
        n = int(input())
        a = input_array()
        last = max(a)
        for i in range(n):
            if i != 0:
                print("", end=" ")
            curr = max(a, key=lambda x: math.gcd(x, last))
            print(curr, end="")
            a.remove(curr)
            last = math.gcd(curr, last)
        print("")
