#!/usr/bin/env python3
import sys
input = sys.stdin.readline
from fractions import gcd

t = int(input())

for _ in range(t):
    n = int(input())
    a = [int(item) for item in input().split()]
    gcd_val = max(a)
    ans = [gcd_val]
    a.remove(gcd_val)
    while a:
        maxs = []
        rest = []
        ngcd = 0
        for item in a:
            g = gcd(item, gcd_val)
            if g > ngcd:
                rest += maxs
                maxs = [item]
                ngcd = gcd(item, gcd_val)
            elif g == ngcd:
                maxs.append(item)
            else:
                rest.append(item)
        gcd_val = ngcd
        ans += maxs
        a = rest[:]
    print(*ans)
