import sys
from math import gcd
input = lambda: sys.stdin.readline().rstrip('\r\n')

def solve():
    n = int(input())
    a = list(map(int, input().split()))
    b, g = [], 0
    while a:
        take = max(a, key = lambda i: gcd(i, g))
        b.append(take)
        g = gcd(g, take)
        a.remove(take)
    print(*b)

for _ in range(int(input())):
    solve()