#include<bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin>>t;

    while(t--){
        int n;
        cin>>n;

        vector<long long> b,c,a,d;

        for(int i=0;i<n;i++){
            long long x;
            cin>>x;

            bool two=(x%2==0);
            bool three=(x%3==0);

            if(two&&three) a.push_back(x);
            else if(two) b.push_back(x);
            else if(three) c.push_back(x);
            else d.push_back(x);
        }

        vector<long long> ans;

        if((int)b.size()>=(int)c.size()){
            for(auto x:d) ans.push_back(x);
            for(auto x:b) ans.push_back(x);
            for(auto x:a) ans.push_back(x);
            for(auto x:c) ans.push_back(x);
        }
        else{
            for(auto x:d) ans.push_back(x);
            for(auto x:c) ans.push_back(x);
            for(auto x:a) ans.push_back(x);
            for(auto x:b) ans.push_back(x);
        }

        for(int i=0;i<n;i++){
            if(i) cout<<" ";
            cout<<ans[i];
        }
        cout<<endl;
    }

    return 0;
}