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 = []
    num = max(arr)
    visited[num] = True
    ans.append(num)
    cd = num

    while len(ans) < n:
        temp = 1;val = -1
        for num in arr:
            #print(num,cd,coprime2(cd,num))
            if visited[num] == False and coprime2(cd,num) >= temp:
                #print(cd,num,coprime2(cd,num))
                temp = coprime2(cd,num)
                val = num

        visited[val] = True
        cd = temp
        ans.append(val)

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

    print()
