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

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:])


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:])


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 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:])


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())
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))


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))


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))


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())):
	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:])


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


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)


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)


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()
	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)


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()


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:])


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 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:])


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)


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()


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))


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))


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


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())


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 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)


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:])


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)


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:])


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:])


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())
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)


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:])