#!/usr/bin/env python3
import functools


def gcd(a, b):
    return gcd(b, a % b) if b != 0 else a


def solve_seq(seq, numbers):
    if not numbers:
        return seq
    seqgcd = functools.reduce(gcd, seq)
    maxarg, maxgcd = -1, -1
    for n in numbers:
        curgcd = gcd(n, seqgcd)
        if curgcd > maxgcd:
            maxgcd = curgcd
            maxarg = n
    numbers.remove(maxarg)
    return solve_seq(seq + [maxarg], numbers)



def solve_numbers(numbers):
    numbers = list(reversed(sorted(numbers)))
    solved = solve_seq([numbers[0]], numbers[1:])
    print(" ".join(map(str, solved)))


def solve():
    count = int(input())
    for i in range(count):
        _ = input()
        numbers = list(map(int, input().split()))
        solve_numbers(numbers)


if __name__ == '__main__':
    solve()
