from math import gcd

for _ in range(int(input())):
    n = int(input())
    ar = list(map(int , input().split()))
    mx =0
    id1= 0
    mx1 = 0
    id2 = 0 
    mat = [[0]*n]*n
    for i in range(n):
        if ar[i] > mx1:
            mx1 = ar[i] 
            id2 = i 
    
    dic = {}
    dic[id2] = 1 
    s = ""
    s+= str(ar[id2]) + " "
    gc= ar[id2]
    while(len(dic) < n ):
        for i in range (n):
            if dic.get(i) == None and gcd(gc , ar[i]) > mx:
                mx = gcd(gc , ar[i])
                id1 = i 
                # print(ar[i] , " id2 = " , ar[id2])
        s += str(ar[id1]) +  " "
        gc = gcd(gc , ar[id1])
        id2 = id1 
        mx = 0 
        dic[id2] = 1 

    print(s)
