import sys
import math
# import numpy as np
fast_reader=sys.stdin.readline
fast_writer=sys.stdout.write
 
def input():
    return fast_reader().strip()
 
# def print(*argv):
#     fast_writer(' '.join((str(i)) for i in argv))
#     fast_writer('\n') 
#############################################################################################
for T in range(int(input())):
    n=int(input())
    a = list(map(int,input().split()))
    m = max(a);count=1
    ans = [m]
    used = [False for i in range(n)]
    for i in range(n):
        if(a[i]==m):
            used[i]=True
            break
    for k in range(1,n):
        mx=1;ind = 0
        for i in range(n):
            if(used[i]==False and math.gcd(a[i],m)>=mx):
                mx = math.gcd(a[i],m)
                el=a[i]
                count = 1
                ind = i
        ans.append(el)
        used[ind]=True
        m = mx
    # print(ans)
    for i in range(n):
        print(ans[i],end = " ")
    print()