import sys
import math
import collections
import bisect
import itertools
import decimal
import copy
import heapq

# import numpy as np

# sys.setrecursionlimit(10 ** 6)
INF = 10 ** 20
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline().rstrip())
ns = lambda: map(int, sys.stdin.readline().rstrip().split())
na = lambda: list(map(int, sys.stdin.readline().rstrip().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().rstrip().split()))


# ===CODE===
def main():
    t = ni()
    for _ in range(t):
        n = ni()
        a = na()
        m = max(a)
        c = collections.Counter(a)
        res = []

        max_gcd = m
        while len(c):
            t_m = 0
            t_max_gcd = 0
            for k in c.keys():
                t_gcd = math.gcd(max_gcd, k)
                if t_gcd > t_max_gcd:
                    t_max_gcd = t_gcd
                    t_m = k
            max_gcd = t_max_gcd
            m = t_m
            for __ in range(c[m]):
                res.append(m)
            del c[m]

        print(*res, sep=" ")


if __name__ == '__main__':
    main()
