from math import gcd
def main():
    t = int(input())
    for _ in range(t):
        n = int(input())
        A = [int(x) for x in input().split()]
        B = []
        x = max(A)
        B.append(x)
        A.remove(x)
        gcd_max = x
        n -= 1
        while(n>1):
            if(gcd_max == 1):
                break
            else:
                gcd_old = 1
                for ele in A:
                    gcd_curr = gcd(gcd_max,ele)
                    if(gcd_curr>=gcd_old):
                        gcd_old = gcd_curr
                        x = ele
                gcd_max = gcd_old
                B.append(x)
                A.remove(x)
            n -=1
        for ele in A:
            B.append(ele)
        print(*B, sep = ' ')
if __name__ == '__main__':
    main()