def gcd(a,b):
    while a*b:a%=b;a,b=b,a
    return a
for _ in range(int(input())):
    n=int(input());a=list(map(int,input().split()));d=[]
    for i in range(n):a[i]=[a[i]]*2
    for i in range(n):
        m=c=0
        for j in range(n):
            if a[j][0]>m:m=a[j][0];c=j
        d.append(a[c][1]);a[c][0]=0
        for j in range(n):
            if a[j][0]:a[j][0]=gcd(m,a[j][0])
    print(' '.join(map(str,d)))