#include using namespace std; using ll = long long; bool chmin(auto &a, auto b) { return a > b ? a = b, true : false; } bool chmax(auto &a, auto b) { return a < b ? a = b, true : false; } template vector> run_length_encoding(const vector &v) { vector> res; for (const auto &x : v) { if (res.empty() || res.back().first != x) { res.emplace_back(x, 0); } res.back().second++; } return res; } vector> run_length_encoding(const string &s) { return run_length_encoding(vector(s.begin(), s.end())); } void solve() { int N; string S; cin >> N >> S; auto flip = [&] (int l, int r) -> void { for (int i = l; i < r; i++) S[i] ^= 'A' ^ 'B'; }; if (N <= 2) { cout << string(N, 'B') << '\n'; return; } auto P = run_length_encoding(S); if (P.size() == 1) { if (P[0].first == 'A') { cout << "BB" + string(N - 2, 'A') << '\n'; } else { cout << string(N, 'B') << '\n'; } return; } if (P[0].first == 'A') { if (P[0].second == 1) { if (P[1].second == 1) { if (P.size() >= 4) { int l = P[0].second + P[1].second + P[2].second; int r = l + P[3].second; flip(l, r); } } else { flip(2, P[0].second + P[1].second); } } else if (P[0].second == 2) { flip(1, P[0].second + P[1].second); } else { flip(1, 2); } flip(0, 1); } else { if (P[0].second == 1) { if (P[1].second == 1) { flip(0, P[0].second + P[1].second + P[2].second); } else { flip(0, 2); } flip(0, 1); } } cout << S << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) solve(); }