import math

def maxAndInde(a):
    inde = 0
    maxim = a[0]
    for i in range(len(a)):
        if maxim < a[i]:
            inde = i
            maxim = a[i]
    return maxim,inde


T = int(input())

ret = []
for t in range(T):
    n = int(input())
    a = list(map(int,input().split()))
    raw = [a[i] for i in range(len(a))]
    gc = 0
    ans = []
    while len(a)>0:
        maxim,inde = maxAndInde(a)
        ans.append(raw[inde])
        a.pop(inde)
        raw.pop(inde)
        gc = math.gcd(gc,maxim)
        for i in range(len(a)):
            a[i]=math.gcd(gc,a[i])
    ret.append(ans)

for t in range(T):
    print(" ".join(list(map(str,ret[t]))))
