from fractions import Fraction
import bisect
import os
import io
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate
from queue import Queue


# sys.setrecursionlimit(200000)
# input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline


def input():
    return sys.stdin.readline().strip()


def iinput():
    return int(input())


def tinput():
    return input().split()


def rinput():
    return map(int, tinput())


def rlinput():
    return list(rinput())


mod = int(1e9)+7


def factors(n):
    return set(reduce(list.__add__,
                      ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))


# ----------------------------------------------------
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')

# print("HELLO")



t = 1
t = iinput()
for i in range(t):
    # n = iinput()
    # a = rlinput()
    # temp = n // 2
    # while temp:
    #     even = 0
    #     odd = 0
    #     for i in range(len(a)):
    #         if a[i]:
    #             if i % 2:
    #                 odd += 1
    #             else:
    #                 even += 1
    #     if even == odd:
    #         break
    #     a.remove(1)
    #     temp -= 1
    # print(len(a))
    # print(*a)
    n = iinput()
    a = rlinput()
    done = [False] * n
    maxx = max(a)
    less = float('inf')
    lst = []
    while len(lst) != n:
        curridx = -1
        currmax = 0
        currgcd = 1
        for i in range(n):
            gcd = math.gcd(a[i], maxx) 
            if gcd >= currgcd and not done[i]:
                currmax = a[i]
                curridx = i
                currgcd = gcd
        # print(currmax,currgcd)
        lst.append(currmax)
        done[curridx] = True
        maxx=currgcd
    print(*lst)
        

