import sys
import math
ii = lambda: sys.stdin.readline().strip()
idata = lambda: [int(x) for x in ii().split()]

def solve():
    n = int(ii())
    data = idata()
    ans = [0] * n
    mx = max(data)
    data = sorted(data)[::-1]
    slov = {}
    for i in range(n):
        ans[i] = math.gcd(mx, data[i])
        if ans[i] in slov:
            slov[ans[i]] += [data[i]]
        else:
            slov[ans[i]] = [data[i]]
    ans = sorted(list(set(ans)))[::-1]
    for i in range(len(ans)):
        for j in range(len(slov[ans[i]])):
            print(slov[ans[i]][j], end=' ')
    print()
    return

for t in range(int(ii())):
    solve()
