from math import gcd
from sys import stdin, gettrace
from collections import Counter

if gettrace():
    inputi = input
else:
    def input():
        return next(stdin)[:-1]


    def inputi():
        return stdin.buffer.readline()


def solve():
    n = int(inputi())
    aa = [int(a) for a in inputi().split()]
    aac = Counter(aa)
    i = max(range(n), key = lambda i:aa[i])
    bb = [aa[i]]
    g = aa[i]
    aa[i] = 0
    while True:
        bg = g
        try:
            i = max((j for j in range(n) if aa[j] != 0), key = lambda i: gcd(aa[i],g))
        except:
            break
        g = gcd(g,aa[i])
        bb.append(aa[i])
        aa[i] = 0
    print(' '.join(map(str, bb)))


def main():
    t = int(inputi())
    for _ in range(t):
        solve()


if __name__ == "__main__":
    main()
