'''
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineering College

'''
from os import path
import sys
from heapq import heappush,heappop
from functools import cmp_to_key as ctk
from collections import deque,defaultdict as dd 
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input().rstrip()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
mod=1000000007
# mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]

def bo(i):
    return ord(i)-ord('a')

file = 1





def solve():

    
    prime = [1]*1001
    prime[0],prime[1]=0,0
    for i in range(2,1001):
        if prime[i]:
            for j in range(i*i,1001,i):
                prime[j]=0


    # x = []

    # for i in range(2,int(sqrt(10000))+1):
    #     if prime[i]:
    #         x.append(i)

    for _ in range(ii()):
    
        n = ii()
        a = li()
        mx = max(a)
        ans = []
        m = [0]*n

        while(1):
            ind = -1
            mx1 = 0
            for j in range(n):
                if m[j]==1:
                    continue

                x1 = gcd(mx,a[j])
                if (x1>mx1):
                    mx1 = x1
                    ind = j
            if ind != -1:
                ans.append(a[ind])
                m[ind] = 1
            mx = mx1
            if mx1<=1:
                break
        for i in range(n):
            if m[i]==0:
                ans.append(a[i])
        print(*ans)



        













        

    




        
if __name__ =="__main__":

    if(file):

        if path.exists('input.txt'):
            sys.stdin=open('input.txt', 'r')
            sys.stdout=open('output.txt','w')
        else:
            input=sys.stdin.readline

    # for _ in range(ii()):
    solve()
