import sys
input = sys.stdin.readline
from math import gcd

t=int(input())
for tests in range(t):
    n=int(input())
    A=list(map(int,input().split()))

    NOW=0
    ANS=[]

    while A:
        MAX=-1
        NEXT=-1
        NEXTIND=-1
        for i in range(len(A)):
            a=A[i]
            if gcd(NOW,a)>MAX:
                MAX=gcd(NOW,a)
                NEXT=a
                NEXTIND=i

        if MAX!=1:                  
            ANS.append(NEXT)
            A.pop(NEXTIND)
            NOW=gcd(NOW,NEXT)
            #print(ANS,A,MAX)
        else:
            ANS+=A
            break

    print(*ANS)
    
        
        

    
