import math
import collections

def solution(n, a):
    max_ = max(a)
    max_i = a.index(max_)
    x = [max_]
    del a[max_i]
    while a != []:
        out = collections.defaultdict(list)
        for e in a:
            out[math.gcd(x[-1], e)].append(e)
        temp = out.pop(max(list(out.keys())))
        x.extend(temp)
        for e in temp:
            a.remove(e)
    return x


if __name__ == "__main__":
    t = int(input())    

    for _ in range(t):
        n = int(input())
        a = list(map(int, input().split(' ')))
        print(*solution(n, a))
