from math import gcd

for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    t = max(a)
    b = []
    c = []
    for i in a:
        b.append([gcd(t, i), i])
    b.sort(reverse=True)
    for k, v in b:
        if not t % k:
            print(v, end=' ')
            t = k
        else:
            c.append(str(v) + ' ')
    print(*c)
