func_code_string: #include <bits/stdc++.h>   #include <algorithm>   #include <unordered_map>   using namespace std;      typedef long long ll;   typedef vector<long long int> vll;   typedef vector<int> vi;   typedef unordered_map<int, int> umap;   typedef vector<bool> vb;   typedef pair<int, int> pii;   typedef vector<pii> vpii;   typedef vector<pair<int, vi>> vpiv;   typedef unordered_map<ll, vll> ull;   typedef set<ll> sll;      #define min(a, b) ((a > b) ? b : a)   #define max(a, b) ((a > b) ? a : b)   #define run(i, n) for (int i = 0; i < n; i++)   #define rund(i, n) for (int i = n - 1; i >= 0; i--)   #define in(v) insert(v)   #define pb(v) push_back(v)   #define mp(v, u) make_pair(v, u)   #define MOD 1000000007   #define lim 10000000   #define INF 1000000009   #define f first   #define s second   #define yes cout <<  YES  << endl   #define no cout <<  NO  << endl   //-------------------Number theory------------   // generate prime numbers till n   vb seive(int n)   {       vb prime(n + 1, true);       prime[0] = false;       prime[1] = false;       for (ll i = 2; i * i <= n; i++)       {           if (prime[i])               for (ll j = i * i; j <= n; j += i)               {                   prime[j] = false;               }       }       return prime;   }      //euler totient function   vll totient(ll n)   {       vll tot(n + 1);       run(i, n + 1)       {           tot[i] = i;       }       for (int i = 2; i * i <= n; i++)       {           if (tot[i] == i)           {               for (int j = i; j <= n; j += i)               {                   tot[j] /= i;                   tot[j] *= i - 1;               }           }       }       return tot;   }   //modular exponentiation   ll modExp(ll a, ll n, ll p)   {       if (n == 1)       {           return a % p;       }       if (n % 2)       {           ll t = modExp(a, n / 2, p);           return (a * t % p * t) % p;       }       else       {           ll t = modExp(a, n / 2, p);           return t % p * t % p;       }   }   //a inverse mod p, where a and p are coprime   ll modInv(ll a, ll p, vb prime)   {       // we know if a and p are coprime then a^totient(p) = 1 mod p => a^{totient(p)-1}=a^-1 mod p       vll tmp = totient(p);       ll t = tmp[t];       return modExp(a, t, p);   }      //----------------Segment Tree -SUM -----------------------------------------------   void buildSeg(int *a, int *t, int v, int l, int r)   {       //initially v should be passed as 1       //declare t in main method       if (l == r)       {           t[v] = a[l];           return;       }       int m = (l + r) / 2;       buildSeg(a, t, 2 * v, l, m);         //build Left sub-tree       buildSeg(a, t, 2 * v + 1, m + 1, r); //build Right sub-tree       // store the sum of left and right sub-segments in the present node       t[v] = t[2 * v] + t[2 * v + 1];   }      ll sumQuery(int *t, int v, int tl, int tr, int l, int r)   {       // start = 0 and end = n-1 initially .       // v initially should be 1 .       if (l > r)       {           return 0;       }       if (tl == l && tr == r)       {           return t[v];       }       else       {           int tm = (tl + tr) / 2;           if (tl <= l && l <= tm && tl <= r && r <= tm)           {               // the query range is completely in left sub-segment               return sumQuery(t, 2 * v, tl, tm, l, r);           }           else if (tm < l && l <= tr && tm < r && r <= tr)           {               return sumQuery(t, 2 * v + 1, tm + 1, tr, l, r);           }           else           {               ll s1 = sumQuery(t, 2 * v, tl, tm, l, tm);               ll s2 = sumQuery(t, 2 * v + 1, tm + 1, tr, tm + 1, r);               return s1 + s2;           }       }   }      // few helpers   void print(vll a)   {       for (int i = 0; i < a.size(); i++)       {           cout << a[i] <<    ;       }       cout << endl;   }      void inp(vll &a)   {       int i = 0;       for (auto &it : a)       {           int t;           cin >> t;           it = t;       }   }   class myComparator   {   public:       bool operator()(pair<ll, ll> &p1, pair<ll, ll> &p2)       {           if (p1.first < p2.first)               return true;           else if (p1.first == p2.first)               return p1.second > p2.second;           else               return false;       }   };   class four   {   public:       ll x1, y1, x2, y2;   };   int p1 = 1000000007;   int p2 = 1824236251;   ll p = 998244353;      //----------------MAIN METHOD (Code)------------------------------------------------   ll merge(vll& arr, vll& left, vll& right) {       ll i = 0, j = 0, count = 0;       while (i < left.size() || j < right.size()) {           if (i == left.size()) {               arr[i+j] = right[j];               j++;           } else if (j == right.size()) {               arr[i+j] = left[i];               i++;           } else if (left[i] <= right[j]) {               arr[i+j] = left[i];               i++;                           } else {               arr[i+j] = right[j];               count += left.size()-i;               j++;           }       }       return count;   }      ll getInvCount(vll& arr) {       if (arr.size() < 2)           return 0;          int m = (arr.size() + 1) / 2;       vll left(arr.begin() , arr.begin()+m);       vll right(arr.begin()+ m, arr.end());          return getInvCount(left) + getInvCount(right) + merge(arr, left, right);   }   ll findInv(string &tmp , string&s , unordered_map<char,vector<ll>>& t){       int inv = 0;       int c1 =0,c2 =0 , c3 = 0 , c4 =0;       vector<ll> id;       for(auto i:tmp ){           if(i== A ){               id.pb(t[ A ][c1++]);           }           if(i== N ){               id.pb(t[ N ][c2++]);           }           if(i== O ){               id.pb(t[ O ][c3++]);           }           if(i== T ){               id.pb(t[ T ][c4++]);           }       }       // print(id);       return getInvCount(id);   }      int main()   {       ios::sync_with_stdio(0);       cin.tie(0);       cout.tie(0);       int t ; cin>>t;       while(t--){           string s;cin>>s;           unordered_map<char,int> freq;           unordered_map<char,vll> rec;           string ans =  ;           ll inversion_number = 0;           vector<char> letter = { A , N , O , T };           for(int i=0;i<s.length();i++){               freq[s[i]] ++;               rec[s[i]].pb(i);           }           int i = 1;           do{               string tmp =  ;               for(auto c : letter){                   run(i,freq[c]){                       tmp+=c;                   }               }               // cout<<tmp<<endl;               ll temp = findInv(tmp,s,rec);               if(temp >= inversion_number){                   inversion_number = temp;                   ans = tmp;               }                          }while( next_permutation(letter.begin(),letter.end()));              cout<<ans<<endl;       }          }