You are given a string $s$ of length $n$ consisting of characters a and/or b.

Let $\operatorname{AB}(s)$ be the number of occurrences of string ab in $s$ as a substring. Analogically, $\operatorname{BA}(s)$ is the number of occurrences of ba in $s$ as a substring.

In one step, you can choose any index $i$ and replace $s_i$ with character a or b.

What is the minimum number of steps you need to make to achieve $\operatorname{AB}(s) = \operatorname{BA}(s)$?

Reminder:

The number of occurrences of string $d$ in $s$ as substring is the number of indices $i$ ($1 \le i \le |s| - |d| + 1$) such that substring $s_i s_{i + 1} \dots s_{i + |d| - 1}$ is equal to $d$. For example, $\operatorname{AB}($aabbbabaa$) = 2$ since there are two indices $i$: $i = 2$ where aabbbabaa and $i = 6$ where aabbbabaa.


-----Input-----

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). Description of the test cases follows.

The first and only line of each test case contains a single string $s$ ($1 \le |s| \le 100$, where $|s|$ is the length of the string $s$), consisting only of characters a and/or b.


-----Output-----

For each test case, print the resulting string $s$ with $\operatorname{AB}(s) = \operatorname{BA}(s)$ you'll get making the minimum number of steps.

If there are multiple answers, print any of them.


-----Examples-----

Input
4
b
aabbbabaa
abbb
abbaab
Output
b
aabbbabaa
bbbb
abbaaa


-----Note-----

In the first test case, both $\operatorname{AB}(s) = 0$ and $\operatorname{BA}(s) = 0$ (there are no occurrences of ab (ba) in b), so can leave $s$ untouched.

In the second test case, $\operatorname{AB}(s) = 2$ and $\operatorname{BA}(s) = 2$, so you can leave $s$ untouched.

In the third test case, $\operatorname{AB}(s) = 1$ and $\operatorname{BA}(s) = 0$. For example, we can change $s_1$ to b and make both values zero.

In the fourth test case, $\operatorname{AB}(s) = 2$ and $\operatorname{BA}(s) = 1$. For example, we can change $s_6$ to a and make both values equal to $1$.

Difficulty: EASY
Tags: String algorithms

Starter code:

Solutions:
for _ in range(int(input())):
	s = input()
	n = len(s)
	ab = 0
	ba = 0
	for i in range(n - 1):
		if s[i] == 'a' and s[i + 1] == 'b':
			ab += 1
		elif s[i] == 'b' and s[i + 1] == 'a':
			ba += 1
	if ab == ba:
		print(s)
	elif s[0] == 'a':
		print('b' + s[1:])
	else:
		print('a' + s[1:])


from sys import stdin
for _ in range(int(stdin.readline())):
	s = stdin.readline().strip()
	(ab, ba) = (0, 0)
	for i in range(1, len(s)):
		if s[i - 1:i + 1] == 'ab':
			ab += 1
		elif s[i - 1:i + 1] == 'ba':
			ba += 1
	if ab == ba:
		print(s)
	else:
		res = s[:-1]
		res += 'a' if s[-1] == 'b' else 'b'
		print(res)


voc = {'a': 'b', 'b': 'a'}

def ab(s):
	return s.count('ab')

def ba(s):
	return s.count('ba')

def change_first(s):
	return voc[s[0]] + s[1:]

def change_last(s):
	return voc[s[-1]] + s[:-1]
n = int(input())
list = []
for i in range(n):
	list.append(input())
for item in list:
	if ab(item) != ba(item):
		ch = change_first(item)
		if ab(ch) == ba(ch):
			print(ch)
		else:
			print(change_last(item))
	else:
		print(item)


n = int(input())
while n:
	s = input()
	a = s.count('ab')
	b = s.count('ba')
	if a == b:
		print(s)
	else:
		if a > b:
			if s[-1] == 'b':
				t = list(s)
				t[-1] = 'a'
				s = ''.join(t)
				a -= 1
			if a > b:
				if s[0] == 'b':
					t = list(s)
					t[0] = 'a'
					s = ''.join(t)
					a -= 1
		else:
			if s[-1] == 'a':
				t = list(s)
				t[-1] = 'b'
				s = ''.join(t)
				b -= 1
			if b > a:
				if s[0] == 'a':
					t = list(s)
					t[0] = 'b'
					s = ''.join(t)
					b -= 1
		print(s)
	n -= 1


for i in range(int(input())):
	J = input()
	if len(J) == 1:
		print(J)
	else:
		print(J[-1] + J[1:])


t = int(input())
for q in range(t):
	s = input()
	n = len(s)
	print(s[-1] + s[1:])


for _ in range(int(input())):
	s = input()
	ab = s.count('ab')
	ba = s.count('ba')
	s = list(s)
	if ab > ba:
		if s[0] == 'a':
			s[0] = 'b'
		elif s[-1] == 'a':
			s[0] = 'b'
	elif ba > ab:
		if s[0] == 'b':
			s[0] = 'a'
		elif s[-1] == 'b':
			s[0] = 'a'
	print(''.join((i for i in s)))


import sys
input = lambda : sys.stdin.readline().strip()
t = int(input())

def match(s, m):
	i = 0
	res = 0
	while i + len(m) <= len(s):
		if s[i:i + len(m)] == m:
			res += 1
			i = i + len(m)
			continue
		i += 1
	return res
while t:
	t -= 1
	s = list(input())
	a = match(s, list('ab'))
	b = match(s, list('ba'))
	if a == b:
		print(''.join(s))
	elif a > b:
		print(''.join(['b'] + s[1:]))
	else:
		print(''.join(['a'] + s[1:]))


t = int(input())
for _ in range(t):
	s = list(input())
	ab = 0
	ba = 0
	for i in range(1, len(s)):
		if s[i] == 'a' and s[i - 1] == 'b':
			ba += 1
		elif s[i] == 'b' and s[i - 1] == 'a':
			ab += 1
	if ab > ba:
		s[0] = 'b'
	elif ba > ab:
		s[0] = 'a'
	print(*s, sep='')


for _ in range(int(input())):
	line = input()
	k = line.count('ab') - line.count('ba')
	if k == 0:
		print(line)
	elif k == 1:
		print(line[:-1], 'a', sep='')
	else:
		print(line[:-1], 'b', sep='')


for nt in range(int(input())):
	s = input()
	if s[0] == s[-1]:
		print(s)
	elif s[0] == 'a':
		print('b' + s[1:])
	else:
		print('a' + s[1:])


for _ in range(int(input())):
	str = input()
	list1 = list(str)
	x = str.count('ab')
	y = str.count('ba')
	if x == y:
		print(str)
	else:
		if str[0] == 'a':
			list1[0] = 'b'
		else:
			list1[0] = 'a'
		str = ''.join(list1)
		print(str)


import math
from heapq import heappush, heappop, heapify
import random
import string
from collections import deque
from bisect import bisect, bisect_left, bisect_right, insort
import sys
input = sys.stdin.readline
S = lambda : input().rstrip()
I = lambda : int(S())
M = lambda : map(int, S().split())
L = lambda : list(M())
H = 1000000000 + 7
for _ in range(I()):
	s = S()
	n = len(s)
	if s[0] != s[-1]:
		print(s[-1] + s[1:])
	else:
		print(s)


import math
t = int(input())
for _ in range(t):
	s = list(input())
	if s[0] == s[-1]:
		for i in s:
			print(i, end='')
	else:
		s[0] = s[-1]
		for i in s:
			print(i, end='')
	print()


n = int(input())
for i in range(n):
	a = input()
	x = a.count('ab')
	y = a.count('ba')
	if x != y:
		if a[0] == 'a':
			a = 'b' + a[1:]
		else:
			a = 'a' + a[1:]
	print(a)


for _ in range(int(input())):
	s = input()
	if s[0] == s[-1]:
		print(s)
	else:
		print(s[-1] + s[1:])


for _UN_ in range(int(input())):
	a = str(input())
	s = 0
	for i in range(1, len(a)):
		if a[i] == 'b' and a[i - 1] == 'a':
			s += 1
		elif a[i] == 'a' and a[i - 1] == 'b':
			s -= 1
	if s == 0:
		print(a)
	elif s == 1:
		a = a[:len(a) - 1]
		a += 'a'
		print(a)
	else:
		a = a[:len(a) - 1]
		a += 'b'
		print(a)


import io, os, sys
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
output = sys.stdout.write

def solve(S):
	if S[0] == S[-1]:
		return S
	return S[:-1] + S[0]

def main():
	T = int(input())
	for _ in range(T):
		S = input().decode().strip()
		out = solve(S)
		output(str(out) + '\n')
main()


cases = int(input())
while cases:
	original = input()
	modified = original
	l = list(original)
	length = len(original)
	ab = original.count('ab')
	ba = original.count('ba')
	if ab == ba:
		print(original)
	else:
		l[0] = l[length - 1]
		modified = ''.join(l)
		print(modified)
	cases -= 1


t = int(input())
x = 0
c = 0
while t > 0:
	t = t - 1
	l = str(input())
	ab = l.count('ab')
	ba = l.count('ba')
	if ab == ba:
		print(l)
	elif ab > ba:
		print(l[:-1] + 'a')
	elif ab < ba:
		print(l[:-1] + 'b')


T = int(input())
for _ in range(T):
	s = list(input())
	if s[0] == s[-1]:
		pass
	else:
		s[0] = 'b' if s[0] == 'a' else 'a'
	print(''.join(s))


test = int(input())
for t in range(test):
	string = input()
	print(string[:len(string) - 1] + '' + string[0])


t = int(input())
for i in range(t):
	(ab, ba) = (0, 0)
	s = input()
	for i in range(len(s) - 1):
		if s[i] == 'a' and s[i + 1] == 'b':
			ab += 1
		if s[i] == 'b' and s[i + 1] == 'a':
			ba += 1
	if ab == ba:
		print(s)
	elif ab > ba:
		print(s.replace('a', 'b', 1))
	elif ba > ab:
		print(s.replace('b', 'a', 1))


t = int(input())
from collections import defaultdict as dd

def find_change(orig: str, new: str) -> tuple:
	orig_score = (1, 0) if orig == 'ab' else (0, 1) if orig == 'ba' else (0, 0)
	new_score = (1, 0) if new == 'ab' else (0, 1) if new == 'ba' else (0, 0)
	return (new_score[0] - orig_score[0], new_score[1] - orig_score[1])
for _ in range(t):
	s = input().strip()
	(ab, ba) = (0, 0)
	for i in range(len(s) - 1):
		if s[i] == 'a' and s[i + 1] == 'b':
			ab += 1
		elif s[i] == 'b' and s[i + 1] == 'a':
			ba += 1
	for i in range(len(s)):
		if ab == ba:
			break
		change = (0, 0)
		c = s[i]
		flipped = 'a' if c == 'b' else 'b'
		if i > 0:
			change = find_change(s[i - 1:i + 1], s[i - 1] + flipped)
		if i < len(s) - 1:
			tmp = find_change(s[i:i + 2], flipped + s[i + 1])
			change = (change[0] + tmp[0], change[1] + tmp[1])
		new_state = (ab + change[0], ba + change[1])
		if abs(new_state[0] - new_state[1]) < abs(ab - ba):
			ab += change[0]
			ba += change[1]
			s = s[:i] + flipped + s[i + 1:]
	print(s)


t = int(input())
for i in range(t):
	s1 = str(input())
	b = s1.count('ba')
	a = s1.count('ab')
	if a == b:
		print(s1)
	else:
		s = ' '.join(s1).split()
		if a > b:
			for j in range(len(s)):
				if s[j] == 'a':
					s[j] = 'b'
					a = a - 1
				if a == b:
					break
			s1 = ''.join(s)
			print(s1)
		else:
			s = ' '.join(s1).split()
			for j in range(len(s)):
				if s[j] == 'b':
					s[j] = 'a'
					b = b - 1
				if a == b:
					break
			s1 = ''.join(s)
			print(s1)


x = int(input())
ab = lambda x: x.count('ab')
ba = lambda x: x.count('ba')
for _ in range(x):
	x = input()
	if x.count('ab') == x.count('ba'):
		print(x)
		continue
	xx = list(x)
	if xx[0] == 'a':
		xx[0] = 'b'
	else:
		xx[0] = 'a'
	xx = ''.join(xx)
	if xx.count('ab') == xx.count('ba'):
		print(xx)
		continue
	xx = list(x)
	if xx[-1] == 'a':
		xx[-1] = 'b'
	else:
		xx[-1] = 'a'
	xx = ''.join(xx)
	if x.count('ab') == x.count('ba'):
		print(xx)
		continue


from __future__ import division, print_function
import math
import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def inp():
	return int(input())

def inps():
	return input().strip()

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input().strip()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())

def cntab(s):
	cnt = 0
	for i in range(len(s) - 1):
		if s[i] == 'a' and s[i + 1] == 'b':
			cnt += 1
	return cnt

def cntba(s):
	cnt = 0
	for i in range(len(s) - 1):
		if s[i] == 'b' and s[i + 1] == 'a':
			cnt += 1
	return cnt
for _ in range(inp()):
	s = insr()
	if cntab(s) == cntba(s):
		print(''.join(s))
	else:
		cng = {'a': 'b', 'b': 'a'}
		for i in range(len(s)):
			g = s.copy()
			g[i] = cng[g[i]]
			v1 = cntab(g)
			v2 = cntab(g)
			if v1 == v2:
				s = g
				break
		print(''.join(s))


import sys

def rall():
	return sys.stdin.readlines()

def rl():
	return sys.stdin.readline().strip()

def rl_types(types):
	str_list = [x for x in sys.stdin.readline().strip().split(' ')]
	return [types[i](str_list[i]) for i in range(len(str_list))]

def pr(something=''):
	sys.stdout.write(str(something) + '\n')

def pra(array):
	sys.stdout.write(' '.join([str(x) for x in array]) + '\n')

def solve(array):
	return array
NT = int(rl())
for _ in range(NT):
	s = rl()
	(nab, nba) = (0, 0)
	nab = sum([1 if s[i] == 'a' and s[i + 1] == 'b' else 0 for i in range(len(s) - 1)])
	nba = sum([1 if s[i] == 'b' and s[i + 1] == 'a' else 0 for i in range(len(s) - 1)])
	if nab > nba:
		s = 'b' + s[1:]
	elif nba > nab:
		s = 'a' + s[1:]
	nab = sum([1 if s[i] == 'a' and s[i + 1] == 'b' else 0 for i in range(len(s) - 1)])
	nba = sum([1 if s[i] == 'b' and s[i + 1] == 'a' else 0 for i in range(len(s) - 1)])
	pr(s)


def convert(s):
	str1 = ''
	return str1.join(s)
t = int(input())
cases = []
for i in range(0, t):
	cases.append(input())
for case in cases:
	case = list(case)
	case[0] = case[len(case) - 1]
	print(convert(case))


for i in range(int(input())):
	s = input()
	a = []
	b = []
	n = len(s)
	l = list(s)
	if s.count('ab') == s.count('ba'):
		print(s)
	else:
		if s.count('ab') > s.count('ba'):
			l[-1] = 'a'
		else:
			l[-1] = 'b'
		print(''.join(l))


N = int(input())
for _ in range(N):
	s = list(input())
	s[-1] = s[0]
	print(''.join(s))


from collections import deque
for _ in range(int(input())):
	s = input()
	a = s.count('ab')
	b = s.count('ba')
	if a == b:
		print(s)
	else:
		new = ''
		for i in range(len(s)):
			if s[i] == 'a':
				new = s[:i] + 'b' + s[i + 1:]
			else:
				new = s[:i] + 'a' + s[i + 1:]
			a = new.count('ab')
			b = new.count('ba')
			if a == b:
				print(new)
				break


from itertools import product, combinations
from math import sqrt, gcd, ceil
INF = int(1000000000.0 + 1)
t = int(input())
for _ in range(t):
	s = input()
	if s[0] == s[-1]:
		print(s)
	else:
		print(s[-1] + s[1:])


t = int(input())
for _ in range(t):
	string = input()
	n = len(string)
	if string[0] == 'a' and string[n - 1] == 'b':
		ans = 'b' + string[1:n]
	elif string[0] == 'b' and string[n - 1] == 'a':
		ans = 'a' + string[1:n]
	else:
		ans = string
	print(ans)


t = int(input())
for i in range(t):
	s = input()
	count = 0
	add = 0
	x = 0
	y = 0
	for j in range(len(s) - 1):
		if s[j] == 'a' and s[j + 1] == 'b':
			count += 1
			x = j
		elif s[j] == 'b' and s[j + 1] == 'a':
			add += 1
			y = j
	if count == add:
		print(s)
	elif count > add:
		s = s.replace(s[x], 'b', 1)
		print(s)
	else:
		s = s.replace(s[y], 'a', 1)
		print(s)


for _ in range(int(input())):
	s = list(input())
	abs = 0
	bas = 0
	for i in range(len(s) - 1):
		if s[i] == 'a' and s[i + 1] == 'b':
			abs += 1
		if s[i] == 'b' and s[i + 1] == 'a':
			bas += 1
	if abs == bas:
		print(''.join(s))
	else:
		if s[0] == 'a':
			s[0] = 'b'
		else:
			s[0] = 'a'
		print(''.join(s))


num_testcases = int(input(''))
lis = [0] * num_testcases
for j in range(num_testcases):
	lis[j] = input('')

def ab_balance(stra):
	num_ab = 0
	num_ba = 0
	pos_ab = 0
	pos_ba = 0
	boolean1 = True
	boolean2 = True
	n1 = 0
	n2 = 0
	while boolean1:
		if stra.find('ab', pos_ab) != -1 and pos_ab < len(stra) - 1:
			num_ab = num_ab + 1
			pos_ab = stra.find('ab', pos_ab) + 2
		else:
			boolean1 = False
	while boolean2:
		if stra.find('ba', pos_ba) != -1 and pos_ba < len(stra) - 1:
			num_ba = num_ba + 1
			pos_ba = stra.find('ba', pos_ba) + 2
		else:
			boolean2 = False
	if num_ab == num_ba:
		print(stra)
	elif num_ab < num_ba:
		print(stra[:len(stra) - 1] + 'b')
	elif num_ba < num_ab:
		print(stra[:len(stra) - 1] + 'a')
for i in range(num_testcases):
	ab_balance(lis[i])


from sys import stdin
from collections import defaultdict as dd
from collections import deque as dq
from collections import Counter as ctr
from heapq import heapify, heappush as ha, heappop as hp
from math import ceil, floor, gcd, sqrt
from bisect import bisect_left as bsl, bisect_right as bsr
from bisect import insort_left as isl, insort_right as isr
from itertools import permutations, combinations
ip = lambda : int(stdin.readline())
ips = lambda : str(stdin.readline())
ipm = lambda : map(int, stdin.readline().split())
mod = int(1000000000.0) + 7
mod2 = 998244353
for _ in range(ip()):
	s = ips()
	s = s[:len(s) - 1]
	if s[0] != s[len(s) - 1]:
		s = s[len(s) - 1] + s[1:]
	print(s)


from math import *
from operator import *
n = int(input())
s = list()
for _ in range(n):
	s.append(input())
for subs in s:
	ab = subs.count('ab')
	ba = subs.count('ba')
	if ab > ba:
		if subs[0] == 'a':
			subs = 'b' + subs[1:len(subs)]
			print(subs)
		elif subs[len(s) - 1] == 'b':
			subs = subs[0:len(subs) - 1] + 'a'
			print(subs)
	elif ab < ba:
		if subs[0] == 'b':
			subs = 'a' + subs[1:len(subs)]
			print(subs)
		elif subs[len(s) - 1] == 'a':
			subs = subs[0:len(subs) - 1] + 'b'
			print(subs)
	else:
		print(subs)


for _ in range(int(input())):
	s = input()
	print(s[:-1] + s[0])


for _ in range(int(input())):
	p = input()
	if p[0] != p[-1]:
		if p[0] == 'a':
			print('b' + p[1:])
		else:
			print('a' + p[1:])
	else:
		print(p)


import sys
import os.path
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
	sys.stderr = open('error.txt', 'w')
depth = 1000000
mod = 1000000007
lim = mod * mod
sys.setrecursionlimit(depth)
linp = lambda : list(minp())
minp = lambda : map(int, input().split())
from math import inf, ceil, sqrt, log2, gcd
from collections import defaultdict, deque
dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
for _ in range(int(input())):
	s = [c for c in input()]
	if s[0] != s[-1]:
		if s[0] == 'a':
			s[0] = 'b'
		else:
			s[0] = 'a'
	print(''.join(s))


t = int(input())
for i in range(t):
	s = input()
	if s[0] == s[len(s) - 1]:
		print(s)
	else:
		s = s[len(s) - 1] + s[1:]
		print(s)


for _ in range(int(input())):
	s = input()
	n = len(s)
	if n < 2 or s[0] == s[-1]:
		print(s)
		continue
	print(s[-1] + s[1:])


t = int(input())
while t:
	x = input()
	print(x[:len(x) - 1], end='')
	print(x[0])
	t -= 1


from __future__ import division, print_function
import os
import sys
from fileinput import input
from io import BytesIO, IOBase
import math
inf = float('inf')
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def main():

	def solve():
		s = input()
		n = len(s)
		if n == 1:
			print(s)
			return
		ab = s.count('ab')
		ba = s.count('ba')
		if ab == ba:
			print(s)
			return
		if ab > ba:
			print(s[:-1] + 'a')
		else:
			print('a' + s[1:])
	t = int(input())
	for i in range(t):
		solve()
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import os
import sys
from io import BytesIO, IOBase
mod = 10 ** 9 + 7

def main():
	for _ in range(int(input())):
		s = input()
		n = len(s)
		if n == 1:
			print(s[0])
			continue
		ab = 0
		ba = 0
		for i in range(n - 1):
			if s[i:i + 2] == 'ab':
				ab += 1
		for i in range(n - 1):
			if s[i:i + 2] == 'ba':
				ba += 1
		if ab == ba:
			print(s)
		elif ab > ba:
			print('b' + s[1:])
		else:
			print('a' + s[1:])
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from sys import stdin, stdout, setrecursionlimit
from io import BytesIO, IOBase
setrecursionlimit(10000)

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None
		self.BUFSIZE = 8192

	def read(self):
		while True:
			a = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
			if not a:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(a), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			a = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
			self.newlines = a.count(b'\n') + (not a)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(a), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(stdin, stdout) = (IOWrapper(stdin), IOWrapper(stdout))
input = lambda : stdin.readline().rstrip('\r\n')
inp = lambda : int(input())
st = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
int_arr = lambda : list(map(int, input().strip().split()))
str_arr = lambda : list(map(str, input().split()))
get_str = lambda : map(str, input().strip().split())
get_int = lambda : map(int, input().strip().split())
get_float = lambda : map(float, input().strip().split())
mod = 1000000007
for _ in range(inp()):
	s = st()
	ab = s.count('ab')
	ba = s.count('ba')
	lst = list(s)
	if ab == ba:
		print(s)
	else:
		for i in range(len(s)):
			lst[i] = 'a'
			s1 = ''.join(lst)
			if s1.count('ab') == s1.count('ba'):
				print(s1)
				break
			lst[i] = 'b'
			s2 = ''.join(lst)
			if s2.count('ab') == s2.count('ba'):
				print(s2)
				break


for _ in range(int(input())):
	st = input()
	print(st[:-1] + st[0])


import enum
import random
import collections
import string
import math
import copy
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
mo = int(1000000000.0 + 7)

def exgcd(a, b):
	if not b:
		return (1, 0)
	(y, x) = exgcd(b, a % b)
	y -= a // b * x
	return (x, y)

def getinv(a, m):
	(x, y) = exgcd(a, m)
	return -1 if x == 1 else x % m

def comb(n, b):
	res = 1
	b = min(b, n - b)
	for i in range(b):
		res = res * (n - i) / (i + 1)
	return res

def quickpower(a, n):
	res = 1
	while n:
		if n & 1:
			res = res * a % mo
		n >>= 1
		a = a * a % mo
	return res

def dis(a, b):
	return abs(a[0] - b[0]) + abs(a[1] - b[1])

def getpref(x):
	if x > 1:
		return x * (x - 1) >> 1
	else:
		return 0

def orafli(upp):
	primes = []
	marked = [False for i in range(upp + 3)]
	prvs = [i for i in range(upp + 3)]
	for i in range(2, upp):
		if not marked[i]:
			primes.append(i)
		for j in primes:
			if i * j >= upp:
				break
			marked[i * j] = True
			prvs[i * j] = j
			if i % j == 0:
				break
	return (primes, prvs)

def lower_ord(c: str) -> int:
	return ord(c) - 97

def upper_ord(c: str) -> int:
	return ord(c) - 65

def read_list():
	return [int(i) for i in input().split()]

def read_int():
	s = input().split()
	if len(s) == 1:
		return int(s[0])
	else:
		return map(int, s)

def ask(s, i, j):
	print(f'{s} {i} {j}', flush=True)

def answer(x):
	print(f'finish {x}', flush=True)
import functools
import itertools
from fractions import Fraction
import random

def lowbit(x):
	return -x & x

def pref(x):
	return x * (x + 1) >> 1

def cum(sx, mx):
	return (sx + mx) * (mx - sx + 1) >> 1

def solve():
	s = input()
	if len(s) == 1:
		print(s)
	elif len(s) == 2:
		if s[0] != s[1]:
			print('aa')
		else:
			print(s)
	elif len(s) == 3:
		if s[0] != s[1] != s[2]:
			print(s)
		elif s.count('a') > s.count('b'):
			print('aaa')
		else:
			print('bbb')
	else:

		def cntt(s):
			ab = 0
			ba = 0
			for (p, i) in enumerate(s[1:]):
				if s[p] == 'a' and i == 'b':
					ab += 1
				elif s[p] == 'b' and i == 'a':
					ba += 1
			return (ab, ba)
		(ab, ba) = cntt(s)
		if ab == ba:
			print(s)
		elif ab > ba:
			if s[:2] == 'aa':
				print('b' + s[1:])
				return
			if s[:2] == 'ab':
				print('bb' + s[2:])
				return
			if s[-2:] == 'bb':
				print(s[:-1] + 'a')
				return
			if s[-2:] == 'ab':
				print(s[:-2] + 'aa')
				return
			raise NameError
		else:
			if s[:2] == 'bb':
				print('a' + s[1:])
				return
			if s[:2] == 'ba':
				print('aa' + s[2:])
				return
			if s[-2:] == 'aa':
				print(s[:-1] + 'b')
				return
			if s[-2:] == 'ba':
				print(s[:-2] + 'bb')
				return
T = int(input())
for ti in range(T):
	solve()


import math
from collections import OrderedDict
from collections import Counter
from itertools import combinations

def solve(s):
	s = list(s)
	if s[0] != s[len(s) - 1]:
		s[0] = s[len(s) - 1]
	print(''.join(s))
t = int(input(''))
for i in range(t):
	n = input('')
	solve(n)


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
inp_vars_int = lambda : map(int, input().split())
inp_list_int = lambda : list(map(int, input().split()))
t = int(input())
for _ in range(t):
	s = input()
	set_s = set(s)
	if len(set_s) == 0 or s[0] == s[-1]:
		print(s)
		continue
	else:
		print(s[:-1] + s[0])


from math import ceil

def main():
	for _ in range(int(input())):
		s = input()
		if len(s) == 1 or s[:1] == s[-1:]:
			print(s)
			continue
		if s == 'ab' or s == 'ba':
			print('aa')
			continue
		left = s[:1]
		right = s[-1:]
		for i in range(1, ceil(len(s) / 2)):
			if left != s[i - 1:i]:
				print(s=right * i + s[i:])
				break
			elif right != s[-i:-i + 1]:
				print(s[:-i] + left * i)
				break
main()


def abc(s):
	if s.count('ab') == s.count('ba'):
		return True
	return False
for _ in range(int(input())):
	s = input().strip()
	if abc(s):
		print(s)
	else:
		l = len(s)
		for i in range(l):
			if s[i] == 'a':
				d = s[:i] + 'b' + s[i + 1:]
			else:
				d = s[:i] + 'a' + s[i + 1:]
			if abc(d):
				print(d)
				break


from sys import stdin
lst = stdin.read().split()
_s = 0

def inp(n=1):
	global _s
	ret = lst[_s:_s + n]
	_s += n
	return ret

def inp1():
	return inp()[0]
t = int(inp1())
for _ in range(t):
	s = inp1()
	if s[0] != s[-1]:
		s = s[-1] + s[1:]
	print(s)


import sys
for _ in range(int(sys.stdin.readline())):
	s = sys.stdin.readline().strip()
	s = s.replace(s[0], s[-1], 1)
	print(s)


n = int(input())
for _ in range(n):
	s = input()
	c1 = s.count('ab')
	c2 = s.count('ba')
	if c1 == c2:
		print(s)
	elif s[0] == 'a':
		l = s[1:]
		print('b' + l)
	else:
		l = s[1:]
		print('a' + l)


t = int(input())
results = list()
for i in range(0, t):
	s = input()
	numberOfABs = s.count('ab')
	numberOfBAs = s.count('ba')
	if numberOfABs == numberOfBAs:
		results.append(s)
	elif numberOfABs > numberOfBAs:
		while numberOfABs != numberOfBAs:
			s = s.replace('a', 'b', 1)
			numberOfABs = s.count('ab')
			numberOfBAs = s.count('ba')
		results.append(s)
	elif numberOfBAs > numberOfABs:
		while numberOfABs != numberOfBAs:
			s = s.replace('b', 'a', 1)
			numberOfABs = s.count('ab')
			numberOfBAs = s.count('ba')
		results.append(s)
for i in range(0, t):
	print(results[i])


a = int(input())
for x in range(a):
	b = input()
	c = print(b[-1] + b[1:])


from sys import stderr

def is_good(s):
	return s.count('ab') == s.count('ba')
t = int(input())
for _ in range(t):
	s = input()
	if is_good(s):
		print(s)
	else:
		s1 = s
		if s1[0] == 'a':
			s1 = 'b' + s1[1:]
			print(s1, file=stderr)
			if is_good(s1):
				print(s1)
				continue
		elif s1[0] == 'b':
			s1 = 'a' + s1[1:]
			print(s1, file=stderr)
			if is_good(s1):
				print(s1)
				continue
		if s1[-1] == 'a':
			s1 = s1[:-1] + 'b'
			print(s1, file=stderr)
			if is_good(s1):
				print(s1)
				continue
		elif s1[-1] == 'b':
			s1 = s1[:-1] + 'a'
			print(s1, file=stderr)
			if is_good(s1):
				print(s1)
				continue


def make_equal(keep):
	while keep.count('ab') != keep.count('ba'):
		if keep.count('ab') > keep.count('ba'):
			keep = keep.replace('a', 'b', 1)
		else:
			keep = keep.replace('b', 'a', 1)
	return keep
test_case = int(input())
while test_case:
	string = input()
	if string.count('ab') == string.count('ba'):
		print(f'{string}')
	else:
		print(make_equal(string))
	test_case -= 1


for i in range(int(input())):
	s = input()
	if s.count('ab') == s.count('ba'):
		print(s)
	else:
		j = s
		for i in range(len(s)):
			if i > 0:
				j = j[:i] + 'a' + j[i + 1:len(s)]
			else:
				j = 'a' + j[i + 1:len(s)]
			if j.count('ab') == j.count('ba'):
				print(j)
				break
			j = s


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def st():
	return input().rstrip('\n')

def lis():
	return list(map(int, input().split()))

def ma():
	return map(int, input().split())
t = inp()
while t:
	t -= 1
	s = st()
	mo = 0
	for i in range(len(s) - 1):
		if s[i] != s[i + 1]:
			mo += 1
	if mo % 2:
		if s[-1] == 'a':
			s = s[:-1] + 'b'
		else:
			s = s[:-1] + 'a'
	print(s)


for _ in range(int(input())):
	s = input()
	c1 = 0
	c2 = 0
	if 'ab' in s:
		c1 += s.count('ab')
	if 'ba' in s:
		c2 += s.count('ba')
	if c1 == c2:
		print(s)
	else:
		s = list(s)
		if s[-1] == 'b':
			s[-1] = 'a'
		else:
			s[-1] = 'b'
		print(''.join(s))


t = int(input())
for i in range(t):
	s = input()
	if s[0] == s[-1]:
		print(s)
	elif s[0] == 'a':
		print('b' + s[1:])
	else:
		print('a' + s[1:])


for i in range(int(input())):
	s = input()
	cnta = 0
	cntb = 0
	for j in range(len(s) - 1):
		if s[j] == 'a' and s[j + 1] == 'b':
			cnta += 1
		elif s[j] == 'b' and s[j + 1] == 'a':
			cntb += 1
	if cnta != cntb:
		diff = abs(cnta - cntb)
		if cnta > cntb:
			for j in range(len(s) - 1):
				if s[j] == 'a' and diff > 0:
					s = s[:j] + 'b' + s[j + 1:]
					diff -= 1
		else:
			for j in range(len(s) - 1):
				if s[j] == 'b' and diff > 0:
					s = s[:j] + 'a' + s[j + 1:]
					diff -= 1
	print(s)


from itertools import permutations as per
from math import factorial as fact
from difflib import SequenceMatcher
t = int(input())
for _ in range(t):
	s = input()
	(f, l) = (-1, -1)
	for i in range(1, len(s)):
		if s[i - 1] != s[i]:
			l = i - 1
			if f == -1:
				f = i - 1
	if f != -1 and s[f:f + 2] == s[l:l + 2]:
		print(s[:-1] + ('a' if s[-1] == 'b' else 'b'))
	else:
		print(s)


def main():
	n = int(input())
	for i in range(n):
		each1()

def each1():
	s = input()
	if s[0] == s[-1]:
		print(s)
	elif s[0] == 'a':
		print('b' + s[1:])
	else:
		print(s[:-1] + 'b')
main()


x = int(input())
for i in range(x):
	s = input()
	y = s.count('ab') - s.count('ba')
	if abs(y) == 2:
		if s[0] == 'a':
			s = 'b' + s[1:]
		else:
			s = 'a' + s[1:]
		if s[len(s) - 1] == 'a':
			s = s[:-1] + 'b'
		else:
			s = s[:-1] + 'a'
	if y == 1:
		if s[:2] == 'ab':
			s = 'b' + s[1:]
		else:
			s = s[:-1] + 'a'
	if y == -1:
		if s[:2] == 'ba':
			s = 'a' + s[1:]
		else:
			s = s[:-1] + 'b'
	print(s)


def compute(string):
	cnt = 0
	for i in range(len(string) - 1):
		if string[i] == 'a' and string[i + 1] == 'b':
			cnt += 1
		if string[i] == 'b' and string[i + 1] == 'a':
			cnt -= 1
	return cnt
for i in range(int(input())):
	string = input()
	cnt = compute(string)
	if cnt == 0:
		print(string)
		continue
	if cnt == 1:
		string = string[:-1] + 'a'
		print(string)
		continue
	string = string[:-1] + 'b'
	print(string)


n_testcase = int(input())
for i in range(n_testcase):
	s = input()
	temp = s[0]
	if s[0] == s[len(s) - 1]:
		print(s)
	else:
		print(('a' if s[0] == 'b' else 'b') + s[1:])


for T in range(int(input())):
	N = input()
	if N[0] == N[-1]:
		print(N)
	elif N[0] == 'a':
		print('b' + N[1:])
	else:
		print('a' + N[1:])


import sys
sys.setrecursionlimit(100000)

def _r():
	return sys.stdin.buffer.readline()

def rs():
	return _r().decode('ascii').strip()

def rn():
	return int(_r())

def rnt():
	return tuple(map(int, _r().split()))

def rnl():
	return list(rnt())

def solve(s):
	if s[0] == s[-1]:
		return s
	return s[:-1] + s[0]
for _ in range(rn()):
	print(solve(rs()))


t = int(input())
for i in range(0, t):
	a = input()
	if a[0] == a[-1]:
		print(a)
	else:
		b = list(a)
		b[0] = b[-1]
		d = ''.join(b)
		print(d)


for _ in range(int(input())):
	s = input()
	if len(s) == 1:
		print(s)
		continue
	if s[0] == s[len(s) - 1]:
		print(s)
	else:
		ns = ''
		if s[0] == 'a':
			ns = 'b'
		else:
			ns = 'a'
		print(ns + s[1:])


for _ in range(int(input())):
	s = input()
	if s == 'b' * len(s) or s == 'a' * len(s):
		print(s)
		continue
	print(s[:len(s) - 1] + s[0])


for i in range(int(input())):
	s = list(input())
	s[0] = s[-1]
	print(''.join(s))


t = int(input())
for i in range(t):
	s = [i for i in input()]
	n = len(s)
	c1 = 0
	c2 = 0
	for i in range(1, n):
		if s[i] == 'a' and s[i - 1] == 'b':
			c2 += 1
		elif s[i] == 'b' and s[i - 1] == 'a':
			c1 += 1
	if c1 == c2:
		print(''.join(s))
	elif c1 > c2:
		for i in range(n):
			if s[i] == 'a':
				s[i] = 'b'
				break
		print(''.join(s))
	else:
		for i in range(n):
			if s[i] == 'b':
				s[i] = 'a'
				break
		print(''.join(s))


from math import gcd, sqrt
from sys import stdin, stdout
from collections import Counter, deque
from bisect import bisect_left, bisect_right
input = stdin.readline
for _ in range(int(input())):
	s = input()
	l = list(s)
	for i in range(len(s)):
		if s.count('ab') > s.count('ba'):
			if l[i] == 'a':
				l[i] = 'b'
		elif s.count('ab') == s.count('ba'):
			s = s
		elif l[i] == 'b':
			l[i] = 'a'
		print(''.join((str(i) for i in l)))
		break


t = int(input())
for _ in range(t):
	s = input()
	arr = list(s)
	count1 = 0
	count2 = 0
	for i in range(len(s) - 1):
		if s[i:i + 2] == 'ab':
			count1 += 1
		elif s[i:i + 2] == 'ba':
			count2 += 1
	if count2 == count1:
		print(s)
	else:
		if count1 > count2:
			if s[0:2] == 'ab':
				arr[0] = 'b'
			else:
				arr[-1] = 'a'
		elif s[0:2] == 'ba':
			arr[0] = 'a'
		else:
			arr[-1] = 'b'
		print(''.join(arr))


for _ in range(int(input())):
	st = input()
	if st[:1] == st[-1:]:
		print(st)
	elif st[:1] == 'a':
		print('b' + st[1:])
	else:
		print('a' + st[1:])


t = int(input())
for _ in range(t):
	s = input()
	changes = []
	for i in range(1, len(s)):
		if s[i] != s[i - 1]:
			changes.append(i)
	if len(changes) % 2 == 0:
		print(s)
	else:
		c = 'a' if s[0] == 'b' else 'b'
		print(c + s[1:])


t = int(input())
for _ in range(t):
	s = input()
	print(s[-1] + s[1:])


for i in range(int(input())):
	s = input()
	sf = s[0:len(s) - 1] + s[0]
	print(sf)


from itertools import permutations as per
from math import factorial as fact
from difflib import SequenceMatcher
t = int(input())
for _ in range(t):
	s = input()
	print(s[:-1] + s[0])


t = int(input())
for _ in range(t):
	s = input()
	ab = s.count('ab')
	ba = s.count('ba')
	a = list(s)
	while ab > ba:
		idx = a.index('a')
		a[idx] = 'b'
		ab -= 1
	while ab < ba:
		idx = a.index('b')
		a[idx] = 'a'
		ba -= 1
	print(''.join(a))


for i in range(int(input())):
	n = input()
	ab = 0
	ba = 0
	for j in range(len(n) - 1):
		s = n[j] + n[j + 1]
		if s == 'ab':
			ab += 1
		elif s == 'ba':
			ba += 1
	if ab == ba:
		print(n)
	elif ab < ba:
		print('a' + n[1:])
	elif ba < ab:
		print(n[:len(n) - 1] + 'a')


n = int(input())
for i in range(n):
	s = input()
	print(s[-1] + s[1:])


import sys
input = lambda : sys.stdin.readline().strip()

def solve():
	s = input()
	return s[:-1] + s[0]
for _ in range(int(input())):
	print(solve())


def occur(A):
	rev = []
	now = ''
	for i in range(len(A)):
		if A[i] != now:
			rev.append(1)
			now = A[i]
		else:
			rev[-1] += 1
	return rev

def differ(t):
	if t == 'a':
		return 'b'
	if t == 'b':
		return 'a'
T = int(input())
ANS = []
for _ in range(T):
	S = list(input())
	oc = occur(S)
	ans = S
	if len(oc) % 2 == 0:
		ans[0] = differ(S[0])
	ANS.append(''.join(ans))
for ans in ANS:
	print(ans)


test = int(input())
for tes in range(test):
	s = input()
	n = len(s)
	for i in range(n):
		if s.count('ab') == s.count('ba'):
			break
		if s[i] == 'a':
			s = s[0:i] + 'b' + s[i + 1:len(s)]
		else:
			s = s[0:i] + 'a' + s[i + 1:len(s)]
		if s.count('ab') == s.count('ba'):
			break
		if s[i] == 'a':
			s = s[0:i] + 'b' + s[i + 1:len(s)]
		else:
			s = s[0:i] + 'a' + s[i + 1:len(s)]
	print(s)


def main():
	alpha = 'abcdefghijklmnopqrstuvwxyz'
	ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
	inf = 1e+17
	mod = 10 ** 9 + 7

	def factorial(n):
		f = 1
		for i in range(1, n + 1):
			f = f * i % mod
		return f

	def factorial_by(n, by):
		f = 1
		for i in range(1, n + 1):
			if i == by:
				continue
			f = f * i % mod
		return f

	def ncr(n, r):
		num = den = 1
		for i in range(r):
			num = num * (n - i) % mod
			den = den * (i + 1) % mod
		return num * pow(den, mod - 2, mod) % mod

	def primeFactors(num):
		pf = []
		while num % 2 == 0:
			pf.append(2)
			num = num // 2
		for i in range(3, int(math.sqrt(num)) + 1, 2):
			while num % i == 0:
				pf.append(i)
				num = num // i
		if num > 2:
			pf.append(num)
		return pf

	class Node(object):

		def __init__(self, name, d):
			self.d = d
			self.name = name

		def __repr__(self):
			pass

	def solve(s):
		s_str = ''.join(s)
		if s_str.count('ab') > s_str.count('ba'):
			if s[0] == 'a':
				s[0] = 'b'
			else:
				s[-1] = 'b'
		elif s_str.count('ab') < s_str.count('ba'):
			if s[0] == 'b':
				s[0] = 'a'
			else:
				s[-1] = 'a'
		return ''.join(s)
	t = int(input())
	ans = []
	for _ in range(t):
		s = list(input()[:-1])
		ans.append(solve(s))
	p = 1
	for answer in ans:
		print(answer)
		p += 1
import sys, threading
import bisect
import math
import itertools
from sys import stdout
import heapq
from queue import PriorityQueue
from collections import deque
input = sys.stdin.readline
thread = threading.Thread(target=main)
thread.start()
thread.join()


t = int(input())
for _ in range(t):
	s = list(input())
	(abCount, baCount) = (0, 0)
	for i in range(0, len(s) - 1):
		if s[i] == 'a' and s[i + 1] == 'b':
			abCount += 1
		if s[i] == 'b' and s[i + 1] == 'a':
			baCount += 1
	if abCount == baCount:
		print(''.join(s))
	else:
		if s[0] == 'a':
			s[0] = 'b'
		else:
			s[0] = 'a'
		print(''.join(s))


t = int(input())
for i in range(t):
	s = str(input())
	s = list(s)
	if s[0] != s[-1]:
		s[0] = s[-1]
	print(''.join(s))


T = int(input())
for t in range(T):
	s = input().strip()
	(ab, ba) = (s.count('ab'), s.count('ba'))
	if ab == ba:
		print(s)
	elif ab > ba:
		print(s.replace('a', 'b', ab - ba))
	else:
		print(s.replace('b', 'a', ba - ab))


n = int(input())
for _ in range(n):
	s = input()
	print(s[-1] + s[1:])


cases = int(input())
for z in range(0, cases):
	string = input()
	s = string[0]
	w = string[-1]
	if s == w:
		print(string)
	elif s == 'a' and w == 'b':
		letters = list(string)
		letters[0] = 'b'
		print(''.join(letters))
	else:
		letters = list(string)
		letters[0] = 'a'
		print(''.join(letters))


t = int(input())
for _ in range(t):
	s = input()
	f = 0
	for i in range(len(s) - 1):
		if s[i] == 'a' and s[i + 1] == 'b':
			f += 1
		elif s[i] == 'b' and s[i + 1] == 'a':
			f -= 1
	if f == 0:
		print(s)
	elif f == 1:
		print(s[:-1] + 'a')
	else:
		print(s[:-1] + 'b')


n = int(input())
for i in range(n):
	s = input()
	ab = s.count('ab')
	ba = s.count('ba')
	if ab > ba:
		s = s.replace('a', 'b', 1)
	elif ba > ab:
		s = s.replace('b', 'a', 1)
	print(s)


from os import path
from sys import stdin, stdout
if path.exists('tc.txt'):
	stdin = open('tc.txt', 'r')

def gmi():
	return map(int, stdin.readline().strip().split())

def gms():
	return map(str, stdin.readline().strip().split())

def gari():
	return list(map(int, stdin.readline().strip().split()))

def gart():
	return tuple(map(int, stdin.readline().strip().split()))

def gars():
	return list(map(str, stdin.readline().strip().split()))

def gs():
	return stdin.readline().strip()

def gls():
	return list(stdin.readline().strip())

def gi():
	return int(stdin.readline())

def solve():
	s = gs()
	(ab, ba) = (s.count('ab'), s.count('ba'))
	ans = ''
	if ab == ba:
		return s
	elif ab > ba:
		d = ab - ba
		for i in s:
			if i == 'a' and d:
				ans += 'b'
				d -= 1
			else:
				ans += i
	else:
		d = ba - ab
		for i in s:
			if i == 'b' and d:
				ans += 'a'
				d -= 1
			else:
				ans += i
	return ans
tc = gi()
while tc:
	tc -= 1
	ans = solve()
	print(ans)


for t in range(int(input())):
	s = input()
	n = len(s)
	ab = 0
	ba = 0
	for i in range(n - 1):
		if s[i] == 'a' and s[i + 1] == 'b':
			ab += 1
		if s[i] == 'b' and s[i + 1] == 'a':
			ba += 1
	if ab == ba:
		print(s)
	elif ab > ba:
		diff = ab - ba
		ans = list(s)
		i = 0
		while i < len(ans) and diff:
			if ans[i] == 'a':
				ans[i] = 'b'
				diff -= 1
			i += 1
		print(''.join(ans))
	else:
		diff = ba - ab
		ans = list(s)
		i = 0
		while i < len(ans) and diff:
			if ans[i] == 'b':
				ans[i] = 'a'
				diff -= 1
			i += 1
		print(''.join(ans))


for _ in range(int(input())):
	a = input()
	print(a[-1] + a[1:])


def fun():
	n = int(input())
	for i in range(n):
		s = input()
		if s[0] != s[-1]:
			s = s[-1] + s[1:]
		print(s)
fun()


def solve():
	s = list(input())
	n = len(s)
	if len(s) == 1:
		print(*s)
	else:
		sab = 0
		for i in range(n - 1):
			if s[i] == 'a' and s[i + 1] == 'b':
				sab += 1
			elif s[i] == 'b' and s[i + 1] == 'a':
				sab -= 1
		if sab == 0:
			print(''.join(s), sep='')
		elif sab == 1:
			for i in range(n - 1):
				print(s[i], sep='', end='')
			print('a')
		else:
			for i in range(n - 1):
				print(s[i], sep='', end='')
			print('b')
t = int(input())
while t:
	solve()
	t -= 1


import sys
input = sys.stdin.readline

def AB(s):
	cnt = 0
	for i in range(len(s) - 1):
		if s[i:i + 2] == 'ab':
			cnt += 1
	return cnt

def BA(s):
	cnt = 0
	for i in range(len(s) - 1):
		if s[i:i + 2] == 'ba':
			cnt += 1
	return cnt
t = int(input())
for __ in range(t):
	s = input().rstrip()
	ab = AB(s)
	ba = BA(s)
	i = 0
	while ab != ba:
		if i != 0:
			if s[i] == 'a':
				s = s[:i] + 'b' + s[i + 1:]
			else:
				s = s[:i] + 'a' + s[i + 1:]
		if ab > ba:
			s = s[:i] + 'b' + s[i + 1:]
		if ab < ba:
			s = s[:i] + 'a' + s[i + 1:]
		ab = AB(s)
		ba = BA(s)
	print(s)


from sys import stdin, stdout
nmbr = lambda : int(input())
lst = lambda : list(map(int, input().split()))
for _ in range(nmbr()):
	s = input()
	n = len(s)
	if n == 1:
		print(''.join(s))
		continue
	if n == 2:
		print(s[0] * 2)
		continue
	ab = ba = 0
	for i in range(n - 1):
		if s[i:i + 2] == 'ab':
			ab += 1
		elif s[i:i + 2] == 'ba':
			ba += 1
	s = list(s)
	if ab > ba:
		s[0] = 'b'
	elif ba > ab:
		s[0] = 'a'
	print(''.join(s))


import sys
import math
inpu = sys.stdin.readline
prin = sys.stdout.write
t = int(inpu())
for _ in range(t):
	s = inpu().strip()
	if len(s) > 1:
		prin(s[:-1] + s[0])
	else:
		prin(s)
	prin('\n')


def solve(s):
	if s[0] == s[-1]:
		return s
	s = s[-1] + s[1:]
	return s
t = int(input())
for i in range(t):
	s = input()
	print(solve(s))


for _ in range(int(input())):
	s = input()
	res = 0
	for i in range(len(s) - 1):
		if s[i] == 'a' and s[i + 1] == 'b':
			res += 1
		elif s[i] == 'b' and s[i + 1] == 'a':
			res -= 1
	if res == 0:
		print(s)
	elif res == 1:
		for i in range(len(s) - 1):
			print(s[i], end='')
		print('a')
	else:
		for i in range(len(s) - 1):
			print(s[i], end='')
		print('b')


n = int(input())
for i in range(n):
	m = input()
	print(m[:-1] + m[0])


t = int(input())
for _ in range(t):
	s = input()
	ABs = 0
	BAs = 0
	for i in range(1, len(s)):
		if s[i - 1] + s[i] == 'ab':
			ABs += 1
		elif s[i - 1] + s[i] == 'ba':
			BAs += 1
	if ABs == BAs:
		print(s)
	elif ABs > BAs:
		print(s[:-1] + 'a')
	else:
		print(s[:-1] + 'b')


t = int(input())
for i in range(t):
	st = str(input())
	c1 = st.count('ab')
	c2 = st.count('ba')
	if c1 == c2:
		print(st)
	elif c1 > c2:
		lst = list(st)
		c = lst.index('a')
		del lst[c]
		lst.insert(c, 'b')
		st1 = ''
		for x in range(len(lst)):
			st1 = st1 + lst[x]
		print(st1)
	else:
		lst1 = list(st)
		c = lst1.index('b')
		del lst1[c]
		lst1.insert(c, 'a')
		st2 = ''
		for y in range(len(lst1)):
			st2 = st2 + lst1[y]
		print(st2)


t = int(input())
for i in range(t):
	s = input()
	c1 = 0
	c2 = 0
	a = []
	b = []
	for i in range(1, len(s)):
		if s[i - 1] + s[i] == 'ab':
			c1 = c1 + 1
			a.append(i - 1)
		elif s[i - 1] + s[i] == 'ba':
			c2 = c2 + 1
			b.append(i - 1)
	if c1 > c2:
		n = a[len(a) - 1]
		k = s.replace(s[n], 'b', 1)
		print(k)
	elif c1 < c2:
		n = b[len(b) - 1]
		k = s.replace(s[n], 'a', 1)
		print(k)
	else:
		print(s)


def make_equal(string):
	characters = list(string)
	occurance_of_ab = string.count('ab')
	occurance_of_ba = string.count('ba')
	while occurance_of_ab != occurance_of_ba:
		if occurance_of_ab > occurance_of_ba:
			characters[''.join(characters).find('a')] = 'b'
		else:
			characters[''.join(characters).find('b')] = 'a'
		occurance_of_ab = ''.join(characters).count('ab')
		occurance_of_ba = ''.join(characters).count('ba')
	return ''.join(characters)
test_case = int(input())
while test_case:
	string = input()
	if string.count('ab') == string.count('ba'):
		print(string)
	else:
		print(make_equal(string))
	test_case -= 1


for i in range(int(input())):
	s = input()
	cab = 0
	cba = 0
	for j in range(len(s) - 1):
		if s[j] == 'a' and s[j + 1] == 'b':
			cab += 1
		elif s[j] == 'b' and s[j + 1] == 'a':
			cba += 1
	if cab == cba:
		print(s)
	elif cab < cba:
		print('a' + s[1:])
	elif cab > cba:
		print(s[:len(s) - 1] + 'a')


t = int(input())

def solve(s):
	if s[0] == s[-1]:
		return s
	else:
		return s[:-1] + s[0]
for _ in range(t):
	s = input()
	print(solve(s))


t = int(input())
for k in range(t):
	rec = input()
	if rec.count('ab') > rec.count('ba'):
		rec = 'b' + rec[1:]
	elif rec.count('ba') > rec.count('ab'):
		rec = 'a' + rec[1:]
	print(rec)


from collections import deque

def get_count(string, x, y):
	count = 0
	for i in range(len(string) - 1):
		if string[i] == x and string[i + 1] == y:
			count += 1
	return count

def solve():
	string = input()
	(ab, ba) = (get_count(string, 'a', 'b'), get_count(string, 'b', 'a'))
	if ab == ba:
		return string
	(q, visited) = (deque(), set())
	q.append(string)
	while q:
		for _ in range(len(q)):
			cur = q.popleft()
			if cur in visited:
				continue
			else:
				visited.add(cur)
			(ab, ba) = (get_count(cur, 'a', 'b'), get_count(cur, 'b', 'a'))
			if ab == ba:
				return cur
			for i in range(len(cur)):
				char = string[i]
				if char == 'a':
					new_char = 'b'
				else:
					new_char = 'a'
				new_string = string[:i] + new_char + string[i + 1:]
				if new_string in visited:
					continue
				q.append(new_string)
for i in range(int(input())):
	print(solve())


for _ in range(int(input())):
	s = input()
	if s[0] == 'a' and s[-1] == 'b':
		print(s[:-1] + 'a')
	elif s[0] == 'b' and s[-1] == 'a':
		print(s[:-1] + 'b')
	else:
		print(s)


t = int(input())
while t:
	s = list(input())
	if s[0] != s[-1]:
		s[0] = s[-1]
	print(''.join(s))
	t -= 1


for t in range(int(input())):
	string_ab = input()
	if string_ab[0] == string_ab[len(string_ab) - 1]:
		print(string_ab)
	else:
		string_ab_change = ''
		if string_ab[0] == 'a':
			string_ab = 'b' + string_ab[1:len(string_ab)]
		elif string_ab[0] == 'b':
			string_ab = 'a' + string_ab[1:len(string_ab)]
		print(string_ab)


for _ in range(int(input())):
	s = input()
	x = s.count('ab')
	y = s.count('ba')
	if x == y:
		print(s)
	elif x > y:
		i = 0
		z = ''
		while i < len(s):
			if x > y and s[i] == 'a':
				z += 'b'
				i += 1
				x -= 1
			else:
				z += s[i]
				i += 1
		print(z)
	else:
		i = 0
		z = ''
		while i < len(s):
			if y > x and s[i] == 'b':
				z += 'a'
				i += 1
				y -= 1
			else:
				z += s[i]
				i += 1
		print(z)


for _ in range(int(input())):
	a = list(input())
	if a[0] == a[-1]:
		a = a
	elif a[0] == 'b':
		a[0] = 'a'
	else:
		a[-1] = 'a'
	print(''.join(a))


for i in range(int(input())):
	string = input()
	if string[0] != string[-1]:
		string = string[-1] + string[1:]
	print(string)


for _ in range(int(input())):
	f = input()
	x = []
	for i in f:
		x.append(i)
	count1 = 0
	count2 = 0
	if len(x) > 1:
		for i in range(len(x) - 1):
			if x[i] == 'a' and x[i + 1] == 'b':
				count1 += 1
			elif x[i] == 'b' and x[i + 1] == 'a':
				count2 += 1
		if count1 == count2:
			print(f)
		else:
			if count1 > count2:
				for i in range(len(x)):
					if x[i] == 'a':
						x[i] = 'b'
						break
			else:
				for i in range(len(x)):
					if x[i] == 'b':
						x[i] = 'a'
						break
			str = ''
			for i in x:
				str += i
			print(str)
	else:
		print(f)


for _ in range(int(input())):
	s = input()
	x = s.count('ab')
	y = s.count('ba')
	if x == y:
		pass
	elif x > y:
		m = s.index('a')
		s = s[:m] + 'b' + s[m + 1:]
	else:
		m = s.index('b')
		s = s[:m] + 'a' + s[m + 1:]
	print(s)


T = int(input())
for t in range(T):
	s = list(input())
	l = len(s)
	if s[0] != s[l - 1]:
		s[l - 1] = s[0]
	print(''.join(s))


from math import *
from collections import *
import os
from io import BytesIO, IOBase
import sys
sys.setrecursionlimit(10 ** 6)
MOD = 1000000007
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def binpow(a, b, m):
	a %= m
	x = 1
	while b > 0:
		if b & 1:
			x = x * a % m
		a = a * a % m
		b >>= 1
	return x

def sakurajima(n):
	d = defaultdict(list)
	if n < 9:
		n = 10
	l = [1] * n
	for i in range(2, int(n ** 0.5) + 1):
		if l[i - 1] == 1:
			for j in range(2 * i, n + 1, i):
				if j % i == 0:
					l[j - 1] = 0
					d[j].append(i)
	return d

def binser(arr, l, r, x):
	while l < r:
		mid = l + (r - l) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return mid

def lcm(a, b):
	return a * b // gcd(a, b)

def sod(n):
	l = list(str(n))
	s = 0
	for i in l:
		s += int(i)
	return s

def prime_factors(num):
	l = []
	if num % 2:
		l.append(2)
	while num % 2 == 0:
		num = num / 2
	for i in range(3, int(sqrt(num)) + 1, 2):
		if not num % i:
			l.append(i)
		while num % i == 0:
			num = num / i
	if num > 2:
		l.append(num)
	return l

def factmod(n, p):
	f = defaultdict(int)
	f[0] = 1
	for i in range(1, n + 1):
		f[i] = f[i - 1] * i % MOD
	return f

def largestPower(n, p):
	x = 0
	while n:
		n //= p
		x += n
	return x

def modFact(n, p):
	if n >= p:
		return 0
	res = 1
	isPrime = [1] * (n + 1)
	i = 2
	while i * i <= n:
		if isPrime[i]:
			for j in range(2 * i, n, i):
				isPrime[j] = 0
		i += 1
	for i in range(2, n):
		if isPrime[i]:
			k = largestPower(n, i)
			res = res * binpow(i, k, p) % p
	return res

def dfs(ajl, now, vis):
	vis[now] = True
	for i in ajl[now]:
		if not vis[i]:
			dfs(ajl, i, vis)

def panda(s):
	n = len(s)
	(x, y) = (0, 0)
	(la, lb) = ([], [])
	l = list(s)
	i = 0
	while i < n - 1:
		if i + 2 < n:
			if s[i:i + 3] == 'aba' or s[i:i + 3] == 'bab':
				i += 2
				continue
		if s[i:i + 2] == 'ab':
			x += 1
			la.append(i)
		elif s[i:i + 2] == 'ba':
			y += 1
			lb.append(i)
		i += 1
	if x == y:
		return s
	if x > y:
		l[0] = 'b'
	else:
		l[-1] = 'b'
	return ''.join(l)
t = int(input())
for _ in range(t):
	s = input()
	print(panda(s))


t = int(input())
for _ in range(t):
	s = input()
	(c1, c2) = (0, 0)
	for i in range(len(s) - 1):
		if s[i] + s[i + 1] == 'ab':
			c1 += 1
		if s[i] + s[i + 1] == 'ba':
			c2 += 1
	if c1 == c2:
		print(s)
	elif s[0] == 'a':
		print('b' + s[1:])
	else:
		print('a' + s[1:])


import sys
input = sys.stdin.readline

def iinput():
	return int(input())

def sinput():
	return input().rstrip()

def i0input():
	return int(input()) - 1

def linput():
	return list(input().split())

def liinput():
	return list(map(int, input().split()))

def miinput():
	return map(int, input().split())

def li0input():
	return list(map(lambda x: int(x) - 1, input().split()))

def mi0input():
	return map(lambda x: int(x) - 1, input().split())
INF = 10 ** 20
MOD = 1000000007
t = iinput()
for _ in [0] * t:
	s = sinput()
	if s[0] == s[-1]:
		print(s)
		continue
	if s[-1] == 'a':
		print(s[:-1] + 'b')
	else:
		print(s[:-1] + 'a')


def count(s):
	c1 = 0
	c2 = 0
	s = list(s)
	for i in range(len(s) - 1):
		if s[i] == 'a' and s[i + 1] == 'b':
			c1 += 1
		if s[i] == 'b' and s[i + 1] == 'a':
			c2 += 1
	if c1 == c2:
		return 1
	else:
		return 0
for _ in range(int(input())):
	s = input()
	s = list(s)
	n = len(s)
	if count(s) == 1:
		llstr = ''
		for i in s:
			llstr += str(i)
		print(llstr)
	else:
		for i in range(len(s)):
			s1 = s
			if s1[i] == 'a':
				s1[i] = 'b'
			else:
				s1[i] = 'a'
			if count(s1) == 1:
				llstr = ''
				for i in s:
					llstr += str(i)
				print(llstr)
				break


from sys import stdin, stdout
ip = lambda : stdin.readline().rstrip('\r\n')
num = lambda : int(ip())
ips = lambda : ip().split()
mp = lambda : map(int, ips())
ls = lambda : list(mp())
out = lambda x, end='\n': stdout.write(str(x) + end)
from collections import defaultdict, Counter, deque
from math import gcd
for _ in range(num()):
	s = ip()
	if len(s) == 1:
		out(s)
		continue
	x = s.count('ab')
	y = s.count('ba')
	if x > y:
		if s[0] == 'a':
			out('b' + s[1:])
		elif s[-1] == 'b':
			out(s[:-1] + 'a')
	elif x < y:
		if s[0] == 'b':
			out('a' + s[1:])
		elif s[-1] == 'a':
			out(s[:-1] + 'b')
	else:
		out(s)


for _ in range(int(input())):
	s = input()
	ct1 = 0
	ct2 = 0
	for i in range(len(s) - 1):
		if s[i] == 'a' and s[i + 1] == 'b':
			ct1 += 1
		if s[i] == 'b' and s[i + 1] == 'a':
			ct2 += 1
	if len(set(s)) == 1:
		print(s)
	elif ct1 == ct2:
		print(s)
	elif s[-1] == 'a':
		print(s[:-1] + 'b')
	else:
		print(s[:-1] + 'a')


import sys
from collections import deque
import heapq
import bisect
import copy
MOD = 1000000007
NO = lambda : print('NO')
YES = lambda : print('YES')
_1 = lambda : print(-1)
ari = lambda : [int(_) for _ in input().split()]
cin = lambda : int(input())
cis = lambda : input()
show = lambda x: print(x)
fast = lambda : sys.stdin.readline()
test_case = 1
test_case = int(input())
sys.setrecursionlimit(10 ** 6)

def ans():
	s = cis()
	abcount = 0
	bacount = 0
	n = len(s)
	s = list(s)

	def bruteforce(news):
		abcount = 0
		nonlocal n
		bacount = 0
		for i in range(n - 1):
			if s[i] == 'a' and s[i + 1] == 'b':
				abcount += 1
			if s[i] == 'b' and s[i + 1] == 'a':
				bacount += 1
		if abcount == bacount:
			return ''.join(news)
		return False
	ans = bruteforce(s)
	if bruteforce(s) != False:
		print(ans)
		return
	for i in range(n):
		val = s[i]
		if s[i] == 'a':
			s[i] = 'b'
			ans = bruteforce(s)
			if ans != False:
				print(ans)
				return
			s[i] = 'a'
		else:
			s[i] = 'a'
			ans = bruteforce(s)
			if ans != False:
				print(ans)
				return
			s[i] = 'b'
	print(ans)
	return
for _ in range(test_case):
	ans()


t = int(input())
for q in range(t):
	s = input()
	n = len(s)
	if s[0] == s[-1]:
		print(s)
	else:
		print(s[-1] + s[1:])


t = int(input())
for i in range(t):
	inp = input()
	if inp[0] == inp[-1]:
		print(inp)
	else:
		print(inp[-1] + inp[1:])


for _ in range(int(input())):
	s = input()
	x = s.count('ab')
	y = s.count('ba')
	if x or y:
		n = len(s)
		print(s[:n - 1] + s[0])
	else:
		print(s)


a = int(input())
for i in range(a):
	b = input()
	x = []
	for z in range(len(b)):
		x.append(b[z])
	count = 0
	p = []
	q = []
	for i in range(len(b) - 1):
		if b[i] == 'a' and b[i + 1] == 'b':
			count = count + 1
			p.append(i)
		if b[i] == 'b' and b[i + 1] == 'a':
			count = count - 1
			q.append(i)
	if count == 0:
		print(b)
	else:
		if count > 0:
			x[-1] = 'a'
		if count < 0:
			x[-1] = 'b'
		for k in range(len(x)):
			print(x[k], end='')
		print()


a = eval(input())
e = []

def str_count(a, b):
	length_a = len(a)
	length_b = len(b)
	count = 0
	i = 0
	for j in range(length_a):
		if a[j] == b[i]:
			if i == length_b - 1:
				count = count + 1
				i = 0
			else:
				i += 1
				if j < length_a - 1 and a[j + 1] != b[i]:
					i = 0
		else:
			continue
	return count

def judge(j):
	cnt1 = str_count(j, 'ab')
	cnt2 = str_count(j, 'ba')
	while cnt1 != cnt2:
		if cnt1 > cnt2:
			b = j.find('ab', 0, len(j))
			j = list(j)
			if j[len(j) - 1] == 'b':
				j[len(j) - 1] = 'a'
			else:
				j[b] = 'b'
			str1 = ''.join(j)
			cnt1 = str_count(str1, 'ab')
			cnt2 = str_count(str1, 'ba')
			j = str1
		elif cnt2 > cnt1:
			b = j.find('ba', 0, len(j))
			j = list(j)
			if j[len(j) - 1] == 'a':
				j[len(j) - 1] = 'b'
			else:
				j[b] = 'a'
			str1 = ''.join(j)
			cnt1 = str_count(str1, 'ab')
			cnt2 = str_count(str1, 'ba')
			j = str1
	return j
for i in range(a):
	j = input()
	j = judge(j)
	e.append(j)
for i in e:
	print(i)


import os
import sys
from io import BytesIO, IOBase

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()
T = int(input())
for _ in range(T):
	s = list(input())
	n = len(s)
	(x, y) = (0, 0)
	for i in range(n - 1):
		if s[i] == 'a' and s[i + 1] == 'b':
			x = x + 1
		elif s[i] == 'b' and s[i + 1] == 'a':
			y = y + 1
	if x == y:
		print(*s, sep='')
	else:
		flag = 0
		if s[0] == 'a' and flag == 0:
			(x, y) = (0, 0)
			t = ['b'] + s[1:]
			for i in range(n - 1):
				if t[i] == 'a' and t[i + 1] == 'b':
					x = x + 1
				elif t[i] == 'b' and t[i + 1] == 'a':
					y = y + 1
			if x == y:
				flag = 1
				print(*t, sep='')
		if s[-1] == 'a' and flag == 0:
			(x, y) = (0, 0)
			t = s[:n - 1] + ['b']
			for i in range(n - 1):
				if t[i] == 'a' and t[i + 1] == 'b':
					x = x + 1
				elif t[i] == 'b' and t[i + 1] == 'a':
					y = y + 1
			if x == y:
				flag = 1
				print(*t, sep='')
		if s[0] == 'b' and flag == 0:
			(x, y) = (0, 0)
			t = ['a'] + s[1:]
			for i in range(n - 1):
				if t[i] == 'a' and t[i + 1] == 'b':
					x = x + 1
				elif t[i] == 'b' and t[i + 1] == 'a':
					y = y + 1
			if x == y:
				flag = 1
				print(*t, sep='')
		if s[-1] == 'b' and flag == 0:
			(x, y) = (0, 0)
			t = s[:n - 1] + ['a']
			for i in range(n - 1):
				if t[i] == 'a' and t[i + 1] == 'b':
					x = x + 1
				elif t[i] == 'b' and t[i + 1] == 'a':
					y = y + 1
			if x == y:
				flag = 1
				print(*t, sep='')


t = input()
for rrrr in range(int(t)):
	nput = input()
	if nput.endswith(nput[0]):
		print(nput)
	elif nput[0] == 'a':
		print('b' + nput[1:])
	else:
		print('a' + nput[1:])


for i in range(int(input())):
	s = input()
	print(s[:-1], end='')
	print(s[0])


tc = int(input())
dct = {1: 'a', -1: 'b'}

def get_key(val):
	for (key, value) in dct.items():
		if val == value:
			return key
while tc:
	polpo = input()
	a = polpo.count('ab')
	b = polpo.count('ba')
	polpo = list(polpo)
	if a != b:
		if abs(a - b) == 2:
			polpo[0] = dct[-1 * get_key(polpo[0])]
			polpo[-1] = dct[-1 * get_key(polpo[-1])]
			print(''.join(polpo))
		else:
			c = polpo[0]
			polpo[0] = dct[-1 * get_key(polpo[0])]
			if polpo.count('ab') != polpo.count('ba'):
				polpo[0] = c
				polpo[-1] = dct[-1 * get_key(polpo[-1])]
				print(''.join(polpo))
			else:
				print(''.join(polpo))
	else:
		print(''.join(polpo))
	tc -= 1


n = int(input())
for _ in range(n):
	s = str(input())
	x = s[0]
	res = 0
	for c in s:
		res = (res + 1) % 2 if c != x else res
		x = c
	if res == 1:
		s = 'b' + s[1:] if s[0] == 'a' else 'a' + s[1:]
	print(s)


for i in range(int(input())):
	s = input()
	a = 0
	for i in range(len(s) - 1):
		if s[i] == 'a' and s[i + 1] == 'b':
			a += 1
		if s[i] == 'b' and s[i + 1] == 'a':
			a -= 1
	if a == 0:
		print(s)
	else:
		if s[0] == 'a':
			s = 'b' + s[1:]
		else:
			s = 'a' + s[1:]
		a = 0
		for i in range(len(s) - 1):
			if s[i] == 'a' and s[i + 1] == 'b':
				a += 1
			if s[i] == 'b' and s[i + 1] == 'a':
				a -= 1
		if a == 0:
			print(s)
		else:
			if s[0] == 'a':
				s = 'b' + s[1:]
			else:
				s = 'a' + s[1:]
			if s[-1] == 'a':
				s = s[:-1] + 'b'
			else:
				s = s[:-1] + 'a'
			a = 0
			for i in range(len(s) - 1):
				if s[i] == 'a' and s[i + 1] == 'b':
					a += 1
				if s[i] == 'b' and s[i + 1] == 'a':
					a -= 1
			if a == 0:
				print(s)
			else:
				if s[0] == 'a':
					s = 'b' + s[1:]
				else:
					s = 'a' + s[1:]
				print(s)


t = int(input())
while t:
	t -= 1
	s = input()
	if s[0] == s[-1]:
		print(s)
	else:
		print(s[:len(s) - 1] + s[0])


t = int(input())
for k in range(t):
	s = input()
	count_ab = s.count('ab')
	count_ba = s.count('ba')
	if count_ab == count_ba:
		print(s)
	elif s[0] == 'a':
		print('b' + s[1:])
	else:
		print('a' + s[1:])


import math as m
for _ in range(int(input())):
	s = input()
	x = s.count('ab')
	y = s.count('ba')
	if x == y:
		print(s)
	if x > y:
		print('b' + s[1:])
	if y > x:
		print('a' + s[1:])


import sys
input = sys.stdin.readline
INF = int(1000000000.0) + 7

def solve():
	s = input().rstrip()
	if s.count('ab') == s.count('ba'):
		print(s)
	elif s[0] == 'a':
		print('b' + s[1:])
	else:
		print('a' + s[1:])
t = int(input())
while t:
	t -= 1
	solve()


t = eval(input())
while t:
	s = input()
	a = s.count('ab')
	b = s.count('ba')
	if a == b:
		print(s)
	elif a > b:
		print('b' + s[1:])
	else:
		print('a' + s[1:])
	t -= 1


for i in range(int(input())):
	s = input()
	s = s[len(s) - 1] + s[1:]
	print(s)


import sys
import math

def inpu():
	return sys.stdin.readline()
T = int(input())
for t in range(T):
	a = input()
	done = False
	print(a[:-1] + a[0])


T = int(input())
t = 0
while t < T:
	s = input()
	ab = s.count('ab')
	ba = s.count('ba')
	if ab == ba:
		print(s)
	else:
		if s[len(s) - 1] == 'a':
			s = s[:len(s) - 1] + 'b'
		elif s[len(s) - 1] == 'b':
			s = s[:len(s) - 1] + 'a'
		print(s)
	t += 1


l = []
for i in range(int(input())):
	l += [input()]
for j in l:
	if j[0] == j[-1]:
		print(j)
	elif j[0] == 'a':
		print('b' + j[1:])
	else:
		print('a' + j[1:])


n = int(input())
for i in range(n):
	s = input()
	a = len(s) - 1
	if a == 0:
		print(s)
	elif s[0] == s[a]:
		print(s)
	elif s[a] == 'a':
		print(s[:a] + 'b')
	else:
		print(s[:a] + 'a')


import sys
input = lambda : sys.stdin.readline().rstrip()
TC = int(input())

def AB(s):
	ret = 0
	for i in range(1, len(s)):
		if s[i - 1] == 'a' and s[i] == 'b':
			ret += 1
	return ret

def BA(s):
	ret = 0
	for i in range(1, len(s)):
		if s[i - 1] == 'b' and s[i] == 'a':
			ret += 1
	return ret
while TC:
	TC -= 1
	s = list(input())
	ab = AB(s)
	ba = BA(s)
	if ab == ba:
		print(''.join(s))
	else:
		s[-1] = s[0]
		print(''.join(s))