import math
import collections

def solution(n, a):
    max_ = max(a)
    max_i = a.index(max_)
    x = [max_]
    del a[max_i]
    gcd, out = 0, collections.defaultdict(list)
    for e in a:
        out[math.gcd(x[-1], e)].append(e)
    temp = list(out.keys())
    temp.sort(reverse = True)
    for e in temp:
        x.extend(out.pop(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))
