from math import gcd
from collections import defaultdict

t = int(input())

for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    d = defaultdict(int)
    
    for i in a:
        d[i] += 1
    
    new_seq = [max(a)]
    gcd_so_far = new_seq[0]
    d[gcd_so_far] -= 1
    
    while len(new_seq) != n:
        this_gcd = 0
        element = None
        
        for i in range(n):
            if gcd(gcd_so_far, a[i]) > this_gcd and d[a[i]]:
                this_gcd = gcd(gcd_so_far, a[i])
                element = a[i]
                
        d[element] -= 1
        new_seq.append(element)
        gcd_so_far = gcd(gcd_so_far, element)
        
    print(*new_seq)