#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
char missingChar(char a, char b) {
// 对于两个不同字符 a,b 返回第三个字符
if ((a=='L' && b=='I') || (a=='I' && b=='L')) return 'T';
if ((a=='L' && b=='T') || (a=='T' && b=='L')) return 'I';
if ((a=='I' && b=='T') || (a=='T' && b=='I')) return 'L';
return '?'; // 不会出现
}
bool isBalanced(const string &s) {
int cntL=0, cntI=0, cntT=0;
for(char c : s) {
if(c=='L') cntL++;
else if(c=='I') cntI++;
else if(c=='T') cntT++;
}
return (cntL==cntI && cntI==cntT);
}
// 模拟函数:给定目标 k、扫描顺序参数 order:
// order = 0 表示从左到右扫描,order = 1 表示从右到左扫描。
// 若能完成恰好 m = 3*k - n 次操作且最终平衡,则返回操作序列;否则返回空序列表示失败。
vector<int> simulate(int k, const string &s_init, int n, int order) {
// 计算初始计数
int cntL=0, cntI=0, cntT=0;
for(char c : s_init) {
if(c=='L') cntL++;
else if(c=='I') cntI++;
else if(c=='T') cntT++;
}
int dL = k - cntL, dI = k - cntI, dT = k - cntT;
int m = 3*k - n;
string s = s_init;
vector<int> ops; // 记录操作,下标为当前状态下插入位置(1-indexed)
// 模拟最多 m 次操作
while((int)ops.size() < m) {
bool inserted = false;
if(order==0) { // 从左到右扫描
for (int i = 0; i < (int)s.size()-1; i++) {
if(s[i] == s[i+1]) continue;
char x = missingChar(s[i], s[i+1]);
// 若该字母还需要插入,则在此位置插入
if((x=='L' && dL>0) || (x=='I' && dI>0) || (x=='T' && dT>0)) {
ops.push_back(i+1); // 输出 1-indexed位置,即在 s[i] 与 s[i+1] 间插入
s.insert(s.begin() + i + 1, x);
if(x=='L') dL--;
else if(x=='I') dI--;
else if(x=='T') dT--;
inserted = true;
break; // 进行一次操作后重头扫描
}
}
} else { // 从右到左扫描
for (int i = s.size()-2; i >= 0; i--) {
if(s[i] == s[i+1]) continue;
char x = missingChar(s[i], s[i+1]);
if((x=='L' && dL>0) || (x=='I' && dI>0) || (x=='T' && dT>0)) {
ops.push_back(i+1);
s.insert(s.begin() + i + 1, x);
if(x=='L') dL--;
else if(x=='I') dI--;
else if(x=='T') dT--;
inserted = true;
break;
}
}
}
if(!inserted) break;
}
// 检查是否完成 m 次操作且最终串平衡
if((int)ops.size() == m && isBalanced(s)) return ops;
return vector<int>(); // 返回空表示失败
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while(t--){
int n;
cin >> n;
string s;
cin >> s;
// 统计初始各字母个数
int cntL=0, cntI=0, cntT=0;
for(char c: s){
if(c=='L') cntL++;
else if(c=='I') cntI++;
else if(c=='T') cntT++;
}
// 若初始串已平衡,直接输出 0
if(cntL==cntI && cntI==cntT){
cout << 0 << "\n";
continue;
}
bool solved = false;
vector<int> ansOps;
// 枚举目标 k,从 max(c_L, c_I, c_T) 到 n
int startK = max({cntL, cntI, cntT});
for (int k = startK; k <= n; k++){
int m = 3*k - n;
if(m > 2*n) continue; // 超过操作限制
// 尝试两种扫描顺序
vector<int> ops = simulate(k, s, n, 0);
if(ops.empty()) ops = simulate(k, s, n, 1);
if(!ops.empty()){
ansOps = ops;
solved = true;
break;
}
}
if(!solved)
cout << -1 << "\n";
else {
cout << ansOps.size() << "\n";
for (int op : ansOps)
cout << op << "\n";
}
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8YWxnb3JpdGhtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY2hhciBtaXNzaW5nQ2hhcihjaGFyIGEsIGNoYXIgYikgewogICAgLy8g5a+55LqO5Lik5Liq5LiN5ZCM5a2X56ymIGEsYiDov5Tlm57nrKzkuInkuKrlrZfnrKYKICAgIGlmICgoYT09J0wnICYmIGI9PSdJJykgfHwgKGE9PSdJJyAmJiBiPT0nTCcpKSByZXR1cm4gJ1QnOwogICAgaWYgKChhPT0nTCcgJiYgYj09J1QnKSB8fCAoYT09J1QnICYmIGI9PSdMJykpIHJldHVybiAnSSc7CiAgICBpZiAoKGE9PSdJJyAmJiBiPT0nVCcpIHx8IChhPT0nVCcgJiYgYj09J0knKSkgcmV0dXJuICdMJzsKICAgIHJldHVybiAnPyc7IC8vIOS4jeS8muWHuueOsAp9Cgpib29sIGlzQmFsYW5jZWQoY29uc3Qgc3RyaW5nICZzKSB7CiAgICBpbnQgY250TD0wLCBjbnRJPTAsIGNudFQ9MDsKICAgIGZvcihjaGFyIGMgOiBzKSB7CiAgICAgICAgaWYoYz09J0wnKSBjbnRMKys7CiAgICAgICAgZWxzZSBpZihjPT0nSScpIGNudEkrKzsKICAgICAgICBlbHNlIGlmKGM9PSdUJykgY250VCsrOwogICAgfQogICAgcmV0dXJuIChjbnRMPT1jbnRJICYmIGNudEk9PWNudFQpOwp9CgovLyDmqKHmi5/lh73mlbDvvJrnu5nlrprnm67moIcga+OAgeaJq+aPj+mhuuW6j+WPguaVsCBvcmRlcjoKLy8gb3JkZXIgPSAwIOihqOekuuS7juW3puWIsOWPs+aJq+aPj++8jG9yZGVyID0gMSDooajnpLrku47lj7PliLDlt6bmiavmj4/jgIIKLy8g6Iul6IO95a6M5oiQ5oGw5aW9IG0gPSAzKmsgLSBuIOasoeaTjeS9nOS4lOacgOe7iOW5s+ihoe+8jOWImei/lOWbnuaTjeS9nOW6j+WIl++8m+WQpuWImei/lOWbnuepuuW6j+WIl+ihqOekuuWksei0peOAggp2ZWN0b3I8aW50PiBzaW11bGF0ZShpbnQgaywgY29uc3Qgc3RyaW5nICZzX2luaXQsIGludCBuLCBpbnQgb3JkZXIpIHsKICAgIC8vIOiuoeeul+WIneWni+iuoeaVsAogICAgaW50IGNudEw9MCwgY250ST0wLCBjbnRUPTA7CiAgICBmb3IoY2hhciBjIDogc19pbml0KSB7CiAgICAgICAgaWYoYz09J0wnKSBjbnRMKys7CiAgICAgICAgZWxzZSBpZihjPT0nSScpIGNudEkrKzsKICAgICAgICBlbHNlIGlmKGM9PSdUJykgY250VCsrOwogICAgfQogICAgaW50IGRMID0gayAtIGNudEwsIGRJID0gayAtIGNudEksIGRUID0gayAtIGNudFQ7CiAgICBpbnQgbSA9IDMqayAtIG47CiAgICBzdHJpbmcgcyA9IHNfaW5pdDsKICAgIHZlY3RvcjxpbnQ+IG9wczsgLy8g6K6w5b2V5pON5L2c77yM5LiL5qCH5Li65b2T5YmN54q25oCB5LiL5o+S5YWl5L2N572u77yIMS1pbmRleGVk77yJCiAgICAKICAgIC8vIOaooeaLn+acgOWkmiBtIOasoeaTjeS9nAogICAgd2hpbGUoKGludClvcHMuc2l6ZSgpIDwgbSkgewogICAgICAgIGJvb2wgaW5zZXJ0ZWQgPSBmYWxzZTsKICAgICAgICBpZihvcmRlcj09MCkgeyAvLyDku47lt6bliLDlj7Pmiavmj48KICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCAoaW50KXMuc2l6ZSgpLTE7IGkrKykgewogICAgICAgICAgICAgICAgaWYoc1tpXSA9PSBzW2krMV0pIGNvbnRpbnVlOwogICAgICAgICAgICAgICAgY2hhciB4ID0gbWlzc2luZ0NoYXIoc1tpXSwgc1tpKzFdKTsKICAgICAgICAgICAgICAgIC8vIOiLpeivpeWtl+avjei/mOmcgOimgeaPkuWFpe+8jOWImeWcqOatpOS9jee9ruaPkuWFpQogICAgICAgICAgICAgICAgaWYoKHg9PSdMJyAmJiBkTD4wKSB8fCAoeD09J0knICYmIGRJPjApIHx8ICh4PT0nVCcgJiYgZFQ+MCkpIHsKICAgICAgICAgICAgICAgICAgICBvcHMucHVzaF9iYWNrKGkrMSk7IC8vIOi+k+WHuiAxLWluZGV4ZWTkvY3nva7vvIzljbPlnKggc1tpXSDkuI4gc1tpKzFdIOmXtOaPkuWFpQogICAgICAgICAgICAgICAgICAgIHMuaW5zZXJ0KHMuYmVnaW4oKSArIGkgKyAxLCB4KTsKICAgICAgICAgICAgICAgICAgICBpZih4PT0nTCcpIGRMLS07CiAgICAgICAgICAgICAgICAgICAgZWxzZSBpZih4PT0nSScpIGRJLS07CiAgICAgICAgICAgICAgICAgICAgZWxzZSBpZih4PT0nVCcpIGRULS07CiAgICAgICAgICAgICAgICAgICAgaW5zZXJ0ZWQgPSB0cnVlOwogICAgICAgICAgICAgICAgICAgIGJyZWFrOyAvLyDov5vooYzkuIDmrKHmk43kvZzlkI7ph43lpLTmiavmj48KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0gZWxzZSB7IC8vIOS7juWPs+WIsOW3puaJq+aPjwogICAgICAgICAgICBmb3IgKGludCBpID0gcy5zaXplKCktMjsgaSA+PSAwOyBpLS0pIHsKICAgICAgICAgICAgICAgIGlmKHNbaV0gPT0gc1tpKzFdKSBjb250aW51ZTsKICAgICAgICAgICAgICAgIGNoYXIgeCA9IG1pc3NpbmdDaGFyKHNbaV0sIHNbaSsxXSk7CiAgICAgICAgICAgICAgICBpZigoeD09J0wnICYmIGRMPjApIHx8ICh4PT0nSScgJiYgZEk+MCkgfHwgKHg9PSdUJyAmJiBkVD4wKSkgewogICAgICAgICAgICAgICAgICAgIG9wcy5wdXNoX2JhY2soaSsxKTsKICAgICAgICAgICAgICAgICAgICBzLmluc2VydChzLmJlZ2luKCkgKyBpICsgMSwgeCk7CiAgICAgICAgICAgICAgICAgICAgaWYoeD09J0wnKSBkTC0tOwogICAgICAgICAgICAgICAgICAgIGVsc2UgaWYoeD09J0knKSBkSS0tOwogICAgICAgICAgICAgICAgICAgIGVsc2UgaWYoeD09J1QnKSBkVC0tOwogICAgICAgICAgICAgICAgICAgIGluc2VydGVkID0gdHJ1ZTsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZighaW5zZXJ0ZWQpIGJyZWFrOwogICAgfQogICAgLy8g5qOA5p+l5piv5ZCm5a6M5oiQIG0g5qyh5pON5L2c5LiU5pyA57uI5Liy5bmz6KGhCiAgICBpZigoaW50KW9wcy5zaXplKCkgPT0gbSAmJiBpc0JhbGFuY2VkKHMpKSByZXR1cm4gb3BzOwogICAgcmV0dXJuIHZlY3RvcjxpbnQ+KCk7IC8vIOi/lOWbnuepuuihqOekuuWksei0pQp9CiAKaW50IG1haW4oKXsKICAgIGlvczo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGNpbi50aWUobnVsbHB0cik7CiAgICAKICAgIGludCB0OwogICAgY2luID4+IHQ7CiAgICB3aGlsZSh0LS0pewogICAgICAgIGludCBuOwogICAgICAgIGNpbiA+PiBuOwogICAgICAgIHN0cmluZyBzOwogICAgICAgIGNpbiA+PiBzOwogICAgICAgIC8vIOe7n+iuoeWIneWni+WQhOWtl+avjeS4quaVsAogICAgICAgIGludCBjbnRMPTAsIGNudEk9MCwgY250VD0wOwogICAgICAgIGZvcihjaGFyIGM6IHMpewogICAgICAgICAgICBpZihjPT0nTCcpIGNudEwrKzsKICAgICAgICAgICAgZWxzZSBpZihjPT0nSScpIGNudEkrKzsKICAgICAgICAgICAgZWxzZSBpZihjPT0nVCcpIGNudFQrKzsKICAgICAgICB9CiAgICAgICAgLy8g6Iul5Yid5aeL5Liy5bey5bmz6KGh77yM55u05o6l6L6T5Ye6IDAKICAgICAgICBpZihjbnRMPT1jbnRJICYmIGNudEk9PWNudFQpewogICAgICAgICAgICBjb3V0IDw8IDAgPDwgIlxuIjsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgfQogICAgICAgIGJvb2wgc29sdmVkID0gZmFsc2U7CiAgICAgICAgdmVjdG9yPGludD4gYW5zT3BzOwogICAgICAgIC8vIOaemuS4vuebruaghyBr77yM5LuOIG1heChjX0wsIGNfSSwgY19UKSDliLAgbgogICAgICAgIGludCBzdGFydEsgPSBtYXgoe2NudEwsIGNudEksIGNudFR9KTsKICAgICAgICBmb3IgKGludCBrID0gc3RhcnRLOyBrIDw9IG47IGsrKyl7CiAgICAgICAgICAgIGludCBtID0gMyprIC0gbjsKICAgICAgICAgICAgaWYobSA+IDIqbikgY29udGludWU7IC8vIOi2hei/h+aTjeS9nOmZkOWItgogICAgICAgICAgICAvLyDlsJ3or5XkuKTnp43miavmj4/pobrluo8KICAgICAgICAgICAgdmVjdG9yPGludD4gb3BzID0gc2ltdWxhdGUoaywgcywgbiwgMCk7CiAgICAgICAgICAgIGlmKG9wcy5lbXB0eSgpKSBvcHMgPSBzaW11bGF0ZShrLCBzLCBuLCAxKTsKICAgICAgICAgICAgaWYoIW9wcy5lbXB0eSgpKXsKICAgICAgICAgICAgICAgIGFuc09wcyA9IG9wczsKICAgICAgICAgICAgICAgIHNvbHZlZCA9IHRydWU7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZighc29sdmVkKQogICAgICAgICAgICBjb3V0IDw8IC0xIDw8ICJcbiI7CiAgICAgICAgZWxzZSB7CiAgICAgICAgICAgIGNvdXQgPDwgYW5zT3BzLnNpemUoKSA8PCAiXG4iOwogICAgICAgICAgICBmb3IgKGludCBvcCA6IGFuc09wcykKICAgICAgICAgICAgICAgIGNvdXQgPDwgb3AgPDwgIlxuIjsKICAgICAgICB9CiAgICB9CiAgICAKICAgIHJldHVybiAwOwp9