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)
    gcd_arr=[]
    for i in range(n):
        gcd=math.gcd(arr[i],maxim)
        gcd_arr.append([gcd,arr[i]])
    gcd_arr.sort(reverse=True)
    #print(gcd_arr)
    ans=[]
    for i,j in gcd_arr:
        ans.append(j)
    print(*ans)