# def element(a, s):
#     z = 0
#     x = 0
#     for i in range(len(s)):
#         d = math.gcd(a,s[i])
#         if d>z:
#             z = d
#             x = s[i]
#     return x




# import math
# t = int(input())
# for h in range(t):
#     n1 = int(input())
#     n = list(map(int, input().split()))
#     s = n[:]
#     new = []
#     new.append(max(n))
#     s.remove(new[0])
#     for i in range(n1-1):
#         x = element(new[-1], s)
#         new.append(x)
#         s.remove(x)
#     print(*new)
    

def element(a, s):
    z = 0
    x = 0
    for i in range(len(s)):
        d = math.gcd(a,s[i])
        if d>z:
            z = d
            x = s[i]
    return x , z



import math
t = int(input())
for h in range(t):
    n1 = int(input())
    n = list(map(int, input().split()))
    s = n[:]
    new = []
    new.append(max(n))
    s.remove(new[0])
    if n1 >= 2:
        x,z = element(new[-1], s)
        new.append(x)
        s.remove(x)
    for i in range(n1-2):
        x, z = element(z, s)
        new.append(x)
        s.remove(x)
    print(*new)
    