from math import gcd
for _ in range(int(input())):
    n = int(input())
    l = list(map(int,input().split()))
    x = l.copy()
    maxGcd,nums =0,[0,0]
    for i in range(n-1):
        for j in range(i+1,n):
            if gcd(l[i],l[j])>=maxGcd:
                maxGcd=gcd(l[i],l[j])
                nums = [l[i],l[j]]
    if nums!=[0,0]:
        x.remove(nums[0])
        x.remove(nums[1])
        fin = nums+sorted(x)
        if fin==l:
            fin = nums[::-1]+sorted(x)
        print(*fin)
    else:
        print(*l)
