from math import gcd
def arrgcd(a):
    s=gcd(a[0],a[1])
    for i in range(2,len(a)):
        S=gcd(a[i],s)
    return s
def main():
    for _ in range(int(input())):
        n=int(input())
        a=list(map(int,input().split()))
        odd,even=list(filter(lambda x: x%2==1,a)),list(filter(lambda x: x%2==0,a))
        if(1 in a):
            print(*sorted(a))
        elif(len(even)>0 and len(odd)>0):
            l=[min(a)]
            a.remove(min(a))
            if(l[-1]%2==1):
                l.append(min(even))
                a.remove(min(even))
            else:
                l.append(min(odd))
                a.remove(min(odd))
            l.extend(sorted(a))
            print(*l)
        else:
            if(len(a)==1):
                print(a[0])
            else:
                l=[]
                if(arrgcd(a)==1):
                    l.append(min(a))
                    a.remove(min(a))
                    x=-1
                    for i in range(len(a)):
                        if(gcd(l[-1],a[i])==1):
                            x=a[i]
                            break
                    l.append(x)
                    a.remove(x)
                    l.extend(sorted(a))
                    print(*a)
                else:
                    w=arrgcd(a)
                    for i in range(len(a)):
                        a[i]//=w
                    l.append(min(a))
                    a.remove(min(a))
                    x=-1
                    for i in range(len(a)):
                        if(gcd(l[-1],a[i])==1):
                            x=a[i]
                            break
                    l.append(x)
                    a.remove(x)
                    l.extend(sorted(a))
                    for i in range(n):
                        l[i]*=w
                    print(*l)
                    
                    
main()