import math
import sys
import collections
import bisect
import time
def get_ints():return map(int, sys.stdin.readline().strip().split())
def get_list():return list(map(int, sys.stdin.readline().strip().split()))
def get_string():return sys.stdin.readline().strip()
for t in range(int(input())):
    n=int(input())
    arr=get_list()
    maxim=max(arr)
    ans=[maxim]
    prev=maxim
    visted=[False]*n
    pos=arr.index(maxim)
    visted[pos]=True
    while len(ans)<n:
        pos=0
        maxim_gcd=1
        for i in range(n):
            if visted[i]==True:
                continue
            gcd=math.gcd(prev,arr[i])
            if gcd>maxim_gcd:
                maxim_gcd=gcd
                pos=i
        if maxim_gcd==1:
            break
        ans.append(arr[pos])
        prev=math.gcd(arr[pos],prev)
        visted[pos]=True
    #print(visted)
    #print(ans)
    for i in range(n):
        if visted[i]==False:
            ans.append(arr[i])
    print(*ans)