from sys import stdin,stdout
from math import gcd
for _ in " "*int(stdin.readline()):
    a=int(stdin.readline())
    b=list(map(int,stdin.readline().split()))
    t=0;z=[]
    for i in range(a):
        p=0;k=0
        for j in range(len(b)):
           if gcd(b[j],t)>k:p=j;k=gcd(b[j],t)
        t=gcd(t,b[p]);z.append(b.pop(p))
    print(*z)