def gcd(a,b):
    while a % b != 0:
        a,b = b, a % b
    return b 

def bigGCD(a):
    maxGcd = max(a)
    b=[]
    while maxGcd > 1:
        if maxGcd in a:
            b.append(maxGcd)
            a.remove(maxGcd)
        maxNow = 1
        best = 0
        for i in a:
            if gcd(maxGcd,i) > maxNow:
                maxNow = gcd(maxGcd,i)
                best = i
        if best != 0:
            b.append(best)
            a.remove(best)
        maxGcd = maxNow
    while a:
        b.append(a.pop(0))
    return b


if __name__ == '__main__':
    cases = int(input())
    for i in range(cases):
        n = int(input())
        a = list(map(int,input().strip().split()))
        ans = bigGCD(a)
        print(f"{' '.join(map(str,ans))}")