import math

t = int(input())
for c in range(t):
    n = int(input())
    arr = [int(x) for x in input().split(" ")]
    result = []
    cur_gcd = max(arr)
    while(len(arr) > 0):
        max_index = 0
        max_gcd = 1
        for i in range(len(arr)):
            new_gcd = math.gcd(cur_gcd, arr[i])
            if new_gcd >= max_gcd:
                max_gcd = new_gcd
                max_index = i
        cur_gcd = max_gcd
        result.append(arr.pop(max_index))
    for i in result:
        print(i, end = " ")
    print()