from math import gcd


def rear(a, pgcd):
    if len(a) <= 1:
        return a
    i = a.index(max(a, key=lambda x: gcd(pgcd, x)))
    a[i], a[0] = a[0], a[i]
    f = []
    o = []
    pgcd = gcd(pgcd, a[0])
    [f.append(x) if gcd(x, a[0]) > 1 else o.append(x) for x in a]
    return a[0:1] + rear(f[1:], pgcd) + o


"""
a = [1, 2, 5, 3, 4, 5, 10]
a = list(map(int, '115 14 932 665 528 396 633 293 948 428'.split()))
ret = rear(a)
gcd1 = 948
print(gcd1, end=' ')
for x in ret[1:]:
    gcd1 = gcd(gcd1, x)
    print(gcd1, end=' ')
print()
print(ret)
"""

for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))

    ret = rear(a, max(a))
    gcd1 = ret[0]
    # print('\t', gcd1, end=' ')
    # for x in ret[1:]:
    #     gcd1 = gcd(gcd1, x)
    #     print(gcd1, end=' ')
    # print()
    print(*ret)
