from math import gcd

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