from math import gcd as bltin_gcd

def coprime2(a, b):
    return bltin_gcd(a, b)

t = int(input())
from collections import defaultdict
for i in range(t):
    n = int(input())
    arr = list(map(int,input().strip().split()))[:n]

    visited = defaultdict(int)
    ans = [0]*n
    cd = 0

    for i in range(n):
        temp = 0;val = 0
        for j in range(n):
            #print(num,cd,coprime2(cd,num))
            if visited[j] == False and coprime2(cd,arr[j]) > temp:
                #print(cd,num,coprime2(cd,num))
                temp = coprime2(cd,arr[j])
                val = j

        visited[val] = True
        cd = temp
        ans[i] = arr[val]

    for num in ans:
        print(num,end=" ")

    print()
