import math
import collections
for t in range(int(input())):
    n=int(input())
    num=list(map(int,input().split()))
    val=max(num)
    k=collections.defaultdict(list)
    for i in num:
        v=math.gcd(val,i)
        k[v].append(i)
    out=[]
    for i in sorted(k.keys(),reverse=True):
        out+=k[i]
    print(*out)