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)
        res = []
        for ai in a:
            tmp = math.gcd(m, ai)
            res.append((tmp, ai))
        res.sort(key=lambda x: x[1])
        res.sort(key=lambda x: x[0], reverse=True)
        ans = []
        for x, y in res:
            ans.append(y)
        print(*ans, sep=" ")


if __name__ == '__main__':
    main()
