#include #include using namespace std; using ll = long long; #define rep(i, s, t) for (ll i = s; i < (ll)(t); i++) #define all(x) begin(x), end(x) template bool chmin(T& x, T y) { return x > y ? (x = y, true) : false; } template bool chmax(T& x, T y) { return x < y ? (x = y, true) : false; } struct io_setup { io_setup() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); } } io_setup; using mint = atcoder::modint998244353; void solve() { int n; string s; cin >> n >> s; vector> vp; vp.push_back({s[0], 0}); for (char c : s) { if (vp.back().first == c) vp.back().second++; else vp.push_back({c, 1}); } if (vp.size() == 1) { if (vp.front().first == 'A' && n > 2) { cout << "BB" << string(n - 2, 'A') << '\n'; } else { cout << string(n, 'B') << '\n'; } return; } if (vp.front().first == 'A') { if (vp.front().second == 1) { if (vp[1].second > 1) { vp.front().first = 'B'; vp.front().second = 2; vp[1].first = 'A'; vp[1].second--; } else { if (vp.size() >= 4) { vp[3].first = 'A'; } vp.front().first = 'B'; } } else if (vp.front().second == 2) { vp.front().first = 'B'; vp[1].first = 'A'; } else { vp.front().second -= 2; vp.insert(vp.begin(), {'B', 2}); } } else { if (vp.front().second == 1) { if (vp[1].second == 1) { if (vp.size() == 2) { vp[1].first = 'B'; } else { vp[2].first = 'A'; vp[1].first = 'B'; } } else { vp[1].second--; vp.front().second++; } } } for (auto [c, k] : vp) cout << string(k, c); cout << '\n'; } int main() { int t = 1; cin >> t; while (t--) solve(); }