import sys

# import math
# from collections import deque

# import heapq

# from math import inf
from math import gcd

# print(help(deque))
# 26
pprint = lambda s: print(' '.join(map(lambda x: str(x), s)))
input = lambda: sys.stdin.readline().strip()
ipnut = input
for i in range(int(ipnut())):
    n = int(ipnut())
    # n, m = map(int, input().split())
    a = list(map(int, input().split()))
    g = 0
    ans = []
    for i in range(n):
        mi = 0
        mo = 0
        for j in range(len(a)):
            if mo<gcd(g,a[j]):
                mo = gcd(g,a[j])
                mi = j
        g = mo
        ans.append(a[mi])
        del a[mi]
    pprint(ans)
# c = list(map(lambda x: int(x)-1, input().split()))
