//practiceee;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class B {
    private static FastScanner fs = new FastScanner();
    public static void main(String[] args) {
        int t = fs.nextInt();
        while(t-->0)
        {
            int n = fs.nextInt();
            int arr[] = fs.readArray(n);
            int vis[] = new int[n];
            int gcd =0;
            for(int i=0;i<n;i++)
            {
                int targ =-1;
                for(int j=0;j<n;j++)
                {
                    if(vis[j]==0)
                    {
                        if (targ == -1 || fs.gcd(gcd,arr[j])> fs.gcd(gcd,arr[targ]))  targ = j;

                    }
                }
                gcd = fs.gcd(gcd,arr[targ]);
                vis[targ] =1;
                System.out.print(arr[targ] +" ");
            }

            System.out.println();
        }

    }





    static class FastScanner {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");

        String next() {
            while (!st.hasMoreTokens())
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        int[] readArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) a[i] = nextInt();
            return a;
        }

        long nextLong() {
            return Long.parseLong(next());
        }
        int  gcd(int a,int b)
        {
            while (b > 0)
            {
                int  temp = b;
                b = a % b;
                a = temp;
            }
            return a;
        }
    }
}
