import math
for t in range(int(input())):
    n=int(input())
    num=list(map(int,input().split()))
    val=max(num)
    k={}
    for i in num:
        k[i]=math.gcd(val,i)
    out=sorted(k.keys(),key=lambda x: k[x],reverse=True)
    print(*out)