import sys

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

    

T=int(sys.stdin.readline())
for _ in range(T):
    n = int(sys.stdin.readline())
    list_num = list(map(int, sys.stdin.readline().split()))
    res = [0]*n
    nm, ni = 0, 0

    for i in range(n):
        if list_num[i] > nm:
            nm = list_num[i]
            ni=i

    list_num[ni] = -1
    res[0] = nm
    
    for i in range(1, n):
        #print(list_num)
        nn = 0
        for j in range(n):
            if list_num[j] == -1:
                continue
            v = gcd(nm, list_num[j])
            if v > nn:
                nn = v
                ni = j
        res[i] = list_num[ni]
        list_num[ni]=-1
        nm = nn
    print(*res)
