from sys import stdin, stdout

def GCD(a, b):  
    if a == 0 : 
        return b  
    return GCD(b%a, a) 

def find(arr,N):
    G=0
    A=[]
    while(arr):
        K=[GCD(G,i) for i in arr]
        x=K.index(max(K))
        A.append(arr[x])
        G=max(K)
        del arr[x]
    return " ".join(map(str,A))




def main():
    for _ in range(int(stdin.readline())):
        N=int(stdin.readline())
        arr=list(map(int, stdin.readline().split()))
        print(find(arr,N))
main()