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