from math import gcd
import sys;input=sys.stdin.readline
T, = map(int, input().split())
for i in range(T):
    N, = map(int, input().split())
    X = list(map(int, input().split()))
    ns = set(range(N))
    Y = []
    g = max(X)
    for i in range(N):
        mx = 0
        for j in ns:
            gg = gcd(X[j], g)
            if gg > mx:
                t, mx = j, gg
        g = mx
#        print(g)
        Y.append(X[t])
        ns.remove(t)
    print(*Y)
