#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")

using namespace std;

int p2[26];

vector < int > solve(vector < int > c) {
    p2[0] = 1;

    int cnt[26]; memset(cnt, 0, sizeof(cnt));

    for (int i = 1; i <= 25; i++) p2[i] = p2[i - 1] * 2;
    for (int i = 0; i < (int)c.size(); i++) c[i] = __builtin_ctzl(c[i]), cnt[c[i]]++;
    for (int i = 1; i <= 25; i++) cnt[i] += cnt[i - 1];


    int a[(int)c.size() * 2 + 5];
    for (int i = 0; i < (int)c.size(); i++) a[--cnt[c[i]]] = i;


    for (int i = 0; i < (int)c.size(); i++) a[(int)c.size() + i] = a[i];

    int mx = (int)c.size() + 1, n = (int)c.size();
    long long S = (accumulate(c.begin(), c.end(), 0ll) + 1) / 2, e = 0;


    long long su = 0, ind = 0;

    for (int q = 0; q <= n / 2 + 1; q++) {
        while (su < S) su += p2[c[a[ind]]], ind++;
        int ee = abs(n - 2 * (ind - q));

        if (mx >= ee) {
            mx = ee;
            e = q;

            if (ee == (n & 1)) break;
        } else break;

        su -= c[a[q]];
    }

    vector < int > ans;
    for (int i = e; i < n; i++) ans.push_back(a[i]);
    for (int i = 0; i < e; i++) ans.push_back(a[i]);

    return ans;

}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin >> t ;
    for (int test=1; test<=t; test++) {
        int n;
        cin >> n ;
        vector <int> a(n);
        for (int i=0; i<n; i++) {
            cin >> a[i] ;
        }

        for (auto ind : solve(a)) {
            cout << ind << " ";
        }
        cout << endl ;
    }
    cout << endl ;
    return 0;
}
