import java.util.*;
import java.util.stream.Collectors;

public class Vova {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Collection<Sample> samples = new ArrayList<>(n);

        for (int idx = 0; idx < n; idx++) {
            samples.add(new Sample(in));
        }

        for (Sample sample : samples) {
            sample.solve();
        }
    }

    private static class State {
        private List<Integer> data;
        private List<Integer> remains;
        private List<Integer> gcds;

        public State(List<Integer> init, int startIdx) {
            data = new ArrayList<>(init.size());
            remains = new ArrayList<>(init);
            gcds = new ArrayList<>(init.size());

            data.add(init.get(startIdx));
            gcds.add(init.get(startIdx));
            remains.set(startIdx, -1);
        }

        public String toText() {
            return gcds.stream().map(Object::toString).collect(Collectors.joining());
        }

        public void iterate() {
            Integer prevGcd = gcds.get(gcds.size() - 1);

            int resultIdx = 0;
            int result = remains.get(resultIdx);
            int gcd = this.gcd(result, prevGcd);

            if (prevGcd != 1) {
                for (int idx = 0; idx < remains.size(); idx++) {
                    int variant = remains.get(idx);
                    if (variant > 0) {
                        int variantGcd = this.gcd(variant, prevGcd);

                        if (variantGcd > gcd || result < 0) {
                            resultIdx = idx;
                            result = variant;
                            gcd = variantGcd;
                        }
                    }
                }
            } else {
                int idx = 0;
                while (remains.get(idx) < 0) {
                    idx++;
                }

                resultIdx = idx;
                result = remains.get(resultIdx);
                gcd = this.gcd(result, prevGcd);
            }

            data.add(result);
            gcds.add(gcd);
            remains.set(resultIdx, -1);
        }

        public int gcd(int a, int b) {
            int variant = 1;
            int gcd = 1;
            int barrier = Math.min(a,b);

            while (variant <= barrier) {
                if (a % variant == 0 && b % variant == 0) {
                    gcd = variant;
                }
                variant++;
            }

            return gcd;
        }

        public void print() {
            System.out.println(data);
            System.out.println(gcds);
            System.out.println("-------------");
        }

        public int compare(State other) {
            int minLength = Math.min(gcds.size(), other.gcds.size());

            for (int idx = 0; idx < minLength; idx++) {
                int a = this.gcds.get(idx);
                int b = other.gcds.get(idx);
                if (a != b) {
                    return Integer.compare(a, b);
                }
            }
            if (this.gcds.size() == other.gcds.size()) {
                return 0;
            }

            return this.gcds.size() < other.gcds.size() ? -1 : 1;
        }
    }

    private static class Sample {
        private int size;
        private List<Integer> data;

        public Sample(Scanner in) {
            size = in.nextInt();
            data = new ArrayList<>(size);
            for (int idx = 0; idx < size; idx++) {
                data.add(in.nextInt());
            }
        }

        public void solve() {
            int maxIdx = 0;
            int max = data.get(maxIdx);

            for (int idx = 0; idx < size; idx++) {
                if (data.get(idx) > max) {
                    maxIdx = idx;
                    max = data.get(maxIdx);
                }
            }

            State result = optimize(maxIdx);
//
//            for (int idx = 0; idx < size; idx++) {
//                State candidate = optimize(idx);
//                if (result == null || (candidate.compare(result) > 0)) {
//                    result = candidate;
//                }
//            }

            print(result);
        }

        private State optimize(int startIdx) {
            State state = new State(data, startIdx);
            while (state.data.size() < data.size()) {
                state.iterate();
            }
            return state;
        }

        private void print(State state) {
            for (Integer val : state.data) {
                System.out.print(val);
                System.out.print(" ");
            }

            System.out.println();
        }
    }
}
