for(1..<>){ <>; $_=<>; s/^BB/AB/ || s/^B?AA?B\KB+/$&=~y,B,A,r/e || s/^B?AA?BA+\KB+/$&=~y,B,A,r/e || s/AA/AB/; s/A+/$&=~y,A,B,r/e; print; }