#include<algorithm> #include<cstring> #include<iostream> #include<string> #include<vector> usingnamespace std; constint N = 100004; string s; int t; int tmp[N]; longlong qres; int a[N]; voidmsort(int l, int r){ if (l >= r) return; int m = l + r >> 1; msort(l, m); msort(m + 1, r); int i = l, j = m + 1; int k = 0; while (i <= m && j <= r) { if (a[i] <= a[j]) tmp[k++] = a[i++]; else { tmp[k++] = a[j++]; qres += 1ll * m - i + 1ll; } } while (i <= m) tmp[k++] = a[i++]; while (j <= r) tmp[k++] = a[j++]; for (i = l, k = 0; i <= r; i++, k++) a[i] = tmp[k]; } intmain(){ cin >> t; auto turn = [&](char c) -> int { if (c == 'A') { return1; } if (c == 'N') { return2; } if (c == 'O') { return3; } if (c == 'T') { return4; } }; while (t--) { cin >> s; vector<int> v[5]; for (int i = 0; i < s.length(); i++) { v[turn(s[i])].push_back(i + 1); } char chr[] = {'A', 'N', 'O', 'T'}; string ans = ""; longlong mmax = 0; do { qres = 0; string res = ""; int cnt = 0; for (int i = 0; i < 4; i++) { for (auto p : v[turn(chr[i])]) { res += chr[i]; a[++cnt] = p; } } msort(1, cnt); if (qres >= mmax) { mmax = qres; ans = res; } } while (next_permutation(chr, chr + 4)); cout << ans << endl; } return0; } // Asusetic eru quionours