def get_factors(n):
    factors = []
    for i in range(1, int(n ** 0.5) + 1):
        if n % i == 0:
            factors.append(i)
            factors.append(n // i)
    factors.sort(reverse = True)
    return factors

def solve(n, a):
    Mx = 0
    for i in range(1, n):
        if a[i] > a[Mx]:
            Mx = i
    M = a.pop(Mx)
    factors = get_factors(M)
    z = 0
    l = []
    for factor in factors:
        bad_factor = False
        for el in l:
            if el % factor != 0:
                bad_factor = True
                break
        if bad_factor:
            continue
        z = 0
        while True:
            if z >= len(a):
                break
            if a[z] % factor == 0:
                l.append(a.pop(z))
            else:
                z += 1
    l.insert(0, M)
    return l

def main():
    t = int(input())
    for _ in range(t):
        n = int(input())
        a = list(map(int, input().split()))
        print(*solve(n, a))
main()