# -*- coding: utf-8 -*-
# import bisect
# import heapq
# import math
# import random
# from collections import Counter, defaultdict, deque
# from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
# from fractions import Fraction
# from functools import lru_cache, reduce
# from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
# from operator import add, mul, sub, itemgetter, attrgetter


import sys
# sys.setrecursionlimit(10**6)
# readline = sys.stdin.buffer.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(readline())


def read_int_n():
    return list(map(int, readline().split()))


def read_float():
    return float(readline())


def read_float_n():
    return list(map(float, readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()


def ep(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.perf_counter()
        ret = f(*args, **kwargs)
        e = time.perf_counter()

        ep(e - s, 'sec')
        return ret

    return wrap


import math
from itertools import combinations
from collections import Counter

# @mt
def slv(N, A):
    if N == 1:
        return A

    ans = []

    ac = Counter(A)
    g = 0
    for _ in range(N):
        # print(ac)
        mg = (-1, -1)
        for k, v in ac.items():
            if v == 0:
                continue
            mg = max(mg, (math.gcd(g, k), k))
        ans.append(mg[1])
        ac[mg[1]] -= 1
        g = mg[0]


    return ans


def main():
    for _ in range(read_int()):
        N = read_int()
        A = read_int_n()
        print(*slv(N, A))


if __name__ == '__main__':
    main()
