t = int(input())

def gcd(a:int,b:int):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)

def getseq(n:int,arr:list):

    seq_b = []
    curr_gcd = 0
    maximum_gcd = curr_gcd
    max_index = 0
    visited = [False for i in range(n)]
    for i in range(n):
        max_index = 0
        maximum_gcd = 0
        for j in range(n):
            if not visited[j] and gcd(arr[j],curr_gcd) > maximum_gcd:
                maximum_gcd = gcd(arr[j],curr_gcd)
                max_index = j


        curr_gcd = maximum_gcd
        visited[max_index] = True
        seq_b.append(arr[max_index])
    for i in seq_b:
        print(i,end=' ')


for _ in range(t):
    n = int(input())
    seq = list(map(int,input().split(' ')))
    getseq(n,seq)
    print('')
