import sys
import math


input = sys.stdin.readline


def bigvova():
    n = int(input())
    nums = list(map(int, input().split()))
    used = [False for _ in range(n)]
    
    max_num = 0
    max_idx = 0
    for i in range(n):
        if nums[i] > max_num:
            max_num = nums[i]
            max_idx = i
    
    seq = [max_num]
    g = max_num
    used[max_idx] = True
    
    for _ in range(n-1):
        now_gcd = 0
        now_num = 0
        now_idx = 0
        
        for i in range(n):
            if used[i]: continue
            
            if math.gcd(g, nums[i]) > now_gcd:
                now_gcd = math.gcd(g, nums[i])
                now_num = nums[i]
                now_idx = i
        
        used[now_idx] = True
        seq.append(now_num)
        g = now_gcd
    
    for num in seq:
        print(num, end=" ")
    print()


if __name__ == "__main__":
    for _ in range(int(input())):
        bigvova()
