def GCD(a, b):
    if a == b:
        return a
    else: 
        return GCD(max(a,b) - min(a,b), min(a,b))

def lexicoMax(arr):
    first = max(arr)
    arr_gcd = first
    result = [first]
    arr.remove(first)
    while len(arr) > 0:
        max_gcd = 1
        for num in arr:
            if GCD(arr_gcd, num) > max_gcd:
                max_gcd = GCD(arr_gcd, num)
                next_num = num
                # print(result)
                # print(max_gcd, next_num)
        if max_gcd == 1:
            return result + arr
        else:
            result.append(next_num)
            arr.remove(next_num)
            arr_gcd = max_gcd
    return result

t = int(input())
test_cases = []
for _ in range(t):
    n = int(input())
    arr = list(map(int,input().split()))
    test_cases.append((n, arr))

for case in test_cases:
    n, arr = case
    print(*lexicoMax(arr))