```go
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
in := bufio.NewReader(os.Stdin)
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
var t int
fmt.Fscan(in, &t)
for i := 0; i < t; i++ {
var s string
fmt.Fscan(in, &s)
b := []byte(s)
// The number of "ab" substrings equals the number of "ba" substrings
// if and only if the string starts and ends with the same character.
// If they differ, changing the last character to the first character
// (or vice-versa) equalizes the counts with 1 step, which is minimum.
if b[0] != b[len(b)-1] {
b[len(b)-1] = b[0]
}
fmt.Fprintln(out, string(b))
}
}
```