from math import gcd
t=int(input())
for i in range(t):
    n=int(input())
    string=input()
    numbers=string.split()
    visited=[False]*n
    maximum=0
    for j in range(n):
        if int(numbers[j])>maximum:
            index=j
            maximum=int(numbers[j])
    visited[index]=True
    value=maximum
    print(maximum, end=' ')
    for k in range(1,n):
        maximum=0
        for j in range(n):
            if  not visited[j] and gcd(int(numbers[j]),value)>maximum:
                index=j
                maximum=gcd(int(numbers[j]),value)
        visited[index]=True
        value=maximum
        print(int(numbers[index]), end=' ')
