import math

def main():
    n = int(input())
    lst = list(map(int, input().split()))
    cur = max(lst)
    res = [lst.index(cur)]
    st = set()
    st.add(lst.index(cur))
    for i in range(n - 1):
        el = -1
        mx = 0

        for k in range(n):
            if k in st:
                pass
            elif math.gcd(cur, lst[k]) > mx:
                mx= math.gcd(cur, lst[k])
                el = k
        st.add(el)
        res.append(el)
        cur = mx
    for i in res:
        print(lst[i], end=" ")
    print()
if __name__ == '__main__':
    t = int(input())
    for i in range(t):
        main()

"""
60, 61
"""
"""
"""
