import math

def solve(n, arr):
    b = [max(arr)] # first element is max of arr
    arr.remove(max(arr))
    # print(arr)
    currGCD = b[0]
    while arr:
        factors = [math.gcd(x, currGCD) for x in arr]
        currGCD = max(factors)
        next_b = arr[factors.index(currGCD)]
        b.append(next_b)
        arr.remove(next_b)
    
    return b

def main():
    t = int(input())
    for _ in range(t):
        n = int(input())
        arr = [int(x) for x in input().split()]

        assert (n == len(arr))

        answer = " ".join([str(x) for x in solve(n, arr)])
        print(answer)

main()