import sys
import math
input = sys.stdin.readline

if __name__ == '__main__':
    for _ in range(int(input())):
        n = int(input())
        a = list(map(int, input().rstrip().split()))
        lg = 0
        ans = []
        for t in range(n):
            l, li, g = 0, None, None
            for i, v in enumerate(a):
                if v == 0:
                    continue
                g = math.gcd(lg, v)
                if g > l:
                    l, li = g, i
            lg = l
            ans.append(a[li])
            a[li] = 0
        print(' '.join(map(str,ans)))