This is the easy version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task.

You are given a string $s$, consisting of lowercase English letters. Find the longest string, $t$, which satisfies the following conditions:   The length of $t$ does not exceed the length of $s$.  $t$ is a palindrome.  There exists two strings $a$ and $b$ (possibly empty), such that $t = a + b$ ( "$+$" represents concatenation), and $a$ is prefix of $s$ while $b$ is suffix of $s$. 


-----Input-----

The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 1000$), the number of test cases. The next $t$ lines each describe a test case.

Each test case is a non-empty string $s$, consisting of lowercase English letters.

It is guaranteed that the sum of lengths of strings over all test cases does not exceed $5000$.


-----Output-----

For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them.


-----Example-----
Input
5
a
abcdfdcecba
abbaxyzyx
codeforces
acbba

Output
a
abcdfdcba
xyzyx
c
abba



-----Note-----

In the first test, the string $s = $"a" satisfies all conditions.

In the second test, the string "abcdfdcba" satisfies all conditions, because:  Its length is $9$, which does not exceed the length of the string $s$, which equals $11$.  It is a palindrome.  "abcdfdcba" $=$ "abcdfdc" $+$ "ba", and "abcdfdc" is a prefix of $s$ while "ba" is a suffix of $s$. 

It can be proven that there does not exist a longer string which satisfies the conditions.

In the fourth test, the string "c" is correct, because "c" $=$ "c" $+$ "" and $a$ or $b$ can be empty. The other possible solution for this test is "s".

Difficulty: MEDIUM
Tags: String algorithms

Starter code:

Solutions:
t = int(input())

def LongestPalindromicPrefix(Str):
	temp = Str + '?'
	Str = Str[::-1]
	temp = temp + Str
	n = len(temp)
	lps = [0] * n
	for i in range(1, n):
		Len = lps[i - 1]
		while Len > 0 and temp[Len] != temp[i]:
			Len = lps[Len - 1]
		if temp[i] == temp[Len]:
			Len += 1
		lps[i] = Len
	return temp[0:lps[n - 1]]
for i in range(t):
	s = input()
	A = [ch for ch in s]
	pre = []
	suf = []
	i = 0
	j = len(A) - 1
	while i < j and A[i] == A[j]:
		pre.append(A[i])
		suf.append(A[j])
		i += 1
		j -= 1
	mid = s[i:j + 1]
	ans = ''
	if len(LongestPalindromicPrefix(mid)) > len(LongestPalindromicPrefix(mid[::-1])):
		ans = LongestPalindromicPrefix(mid)
	else:
		ans = LongestPalindromicPrefix(mid[::-1])
	ans = ''.join(pre) + ans + ''.join(suf[::-1])
	print(ans)


temp = ''

def is_palindrome(a, b):
	while a <= b:
		if temp[a] != temp[b]:
			return 0
		a += 1
		b -= 1
	return i + 1
t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	ans = 0
	a = 0
	b = n - 1
	while a < b:
		if s[a] != s[b]:
			break
		a += 1
		b -= 1
	ans = max(ans, a + n - 1 - b)
	oc = ans
	p = s[a:b + 1]
	toprint = ''
	temp = ''
	for i in range(len(p)):
		temp += p[i]
		if oc + is_palindrome(0, i) > ans:
			ans = oc + is_palindrome(0, i)
			toprint = temp
	temp = ''
	p = p[::-1]
	for i in range(len(p)):
		temp += p[i]
		if oc + is_palindrome(0, i) > ans:
			ans = oc + is_palindrome(0, i)
			toprint = temp
	print(s[0:a] + toprint + s[0:a][::-1])


from sys import stdin
from collections import defaultdict, Counter
from math import gcd

def solver(s):
	n = len(s)
	i = 0
	for i in range(n // 2):
		if s[i] != s[-(i + 1)]:
			break
	if i == 0:
		sub = s
	else:
		sub = s[i:-i]
	l = long_palindrome_ps(sub)
	return s[:i] + l + s[::-1][:i][::-1]

def long_palindrome_ps(s):
	n = len(s)
	lps = []
	ss = s + '#' + s[::-1]
	lps = lps_(ss)
	m_p = lps[-1]
	m_pp = s[:m_p]
	ss = s[::-1] + '#' + s
	lps = lps_(ss)
	m_s = lps[-1]
	m_sp = s[::-1][:m_s]
	if m_p > m_s:
		return m_pp
	else:
		return m_sp

def lps_(s):
	n = len(s)
	tr = [0]
	j = 0
	for i in range(1, n):
		if s[i] == s[j]:
			j += 1
			tr.append(j)
		else:
			while s[j] != s[i] and j > 0:
				j = tr[j - 1]
			if s[j] == s[i]:
				j += 1
				tr.append(j)
			elif j == 0:
				tr.append(j)
	return tr
T = int(input())
for _ in range(T):
	s = stdin.readline().strip()
	print(solver(s))


for case in range(int(input())):
	s = input()
	le = len(s)
	head = 0
	tail = le - 1
	for i in range(0, le >> 1):
		if s[i + head] != s[tail - i]:
			head += i
			tail -= i
			break
	num = 0
	tag1 = 0
	tag2 = 0
	for i in range(head, tail + 1):
		if s[i] == s[tail]:
			if s[i:tail + 1] == s[i:tail + 1][::-1]:
				num = tail - i + 1
				tag1 = i
				tag2 = tail
				break
	for i in range(tail, head - 1, -1):
		if s[i] == s[head]:
			if s[head:i + 1] == s[head:i + 1][::-1]:
				if i - head + 1 > num:
					num = i - head + 1
					tag1 = head
					tag2 = i
					break
	print(s[:head], end='')
	if num:
		print(s[tag1:tag2 + 1], end='')
	if tail >= head:
		print(s[tail + 1:])


def isPal(s):
	if len(s) == 1:
		return True
	l = len(s)
	for i in range(int(l / 2)):
		if s[i] != s[l - 1 - i]:
			return False
	return True
for iii in range(int(input())):
	s = input()
	left = ''
	l = len(s)
	for i in range(l):
		if isPal(s[0:l - i]):
			left = s[0:l - i]
			break
	right = ''
	for i in range(l):
		if isPal(s[i:l]):
			right = s[i:l]
			break
	a = b = ''
	for i in range(int(l / 2)):
		if s[i] != s[l - 1 - i]:
			break
		a = a + s[i]
		b = s[i] + b
	ans = a + b
	if l - (len(a) + len(b)) < 2:
		ans = s
	else:
		ret = l - len(a) - len(b)
		for i in range(ret):
			if isPal(s[len(a):len(a) + ret - i]):
				ans = a + s[len(a):len(a) + ret - i] + b
				break
			if isPal(s[len(a) + i:ret + len(a)]):
				ans = a + s[len(a) + i:ret + len(a)] + b
				break
	if len(left) >= len(right):
		if len(left) >= len(ans):
			print(left)
		else:
			print(ans)
	elif len(right) >= len(ans):
		print(right)
	else:
		print(ans)


import math
from decimal import *
import random
import math
from decimal import *
import random
for _ in range(int(input())):
	s = str(input())
	s1 = ''
	n = len(s)
	p1 = 0
	pos = len(s) - 1
	(pre, suf) = ('', '')
	longest = ''
	cont = True
	for i in range(n):
		if s[i:] == s[i:][::-1]:
			if len(s[i:]) > len(longest):
				longest = s[i:]
		if s[:i + 1] == s[:i + 1][::-1]:
			if len(s[:i + 1]) > len(longest):
				longest = s[:i + 1]
	while p1 <= pos:
		if p1 == pos:
			pre += s[p1]
		else:
			pre += s[p1]
			if s[pos] == s[p1] and cont:
				suf = s[pos] + suf
				pos -= 1
			else:
				cont = False
		comp = pre + suf
		if comp == comp[::-1]:
			if len(comp) > len(longest):
				longest = comp
		p1 += 1
	(p1, p2) = (0, len(s) - 1)
	(pre, suf) = ('', '')
	cont = True
	while p2 >= p1:
		if p2 == p1:
			suf = s[p1] + suf
		else:
			suf = s[p2] + suf
			if s[p1] == s[p2] and cont:
				pre += s[p1]
				p1 += 1
			else:
				cont = False
		comp = pre + suf
		if comp == comp[::-1]:
			if len(comp) > len(longest):
				longest = comp
		p2 -= 1
	print(longest)


def checkpallendrome(s, l):
	for i in range(l // 2):
		if s[i] != s[l - i - 1]:
			return False
	return True
t = int(input())
while t > 0:
	s = input()
	n = len(s)
	start = ''
	end = ''
	i = 0
	j = n - 1
	if n == 1:
		print(s)
		t -= 1
		continue
	while i < j:
		if s[i] == s[j]:
			i += 1
			j -= 1
		else:
			break
	pointer1 = i
	pointer2 = j
	temp = ''
	maxpall = ''
	for k in range(pointer1, pointer2 + 1):
		temp = s[pointer1:k + 1]
		if checkpallendrome(temp, k - pointer1 + 1) and len(maxpall) < len(temp):
			maxpall = temp
	for k in range(pointer2, pointer1 - 1, -1):
		temp = s[k:pointer2 + 1]
		if checkpallendrome(temp, pointer2 - k + 1) and len(maxpall) < len(temp):
			maxpall = temp
	ans = s[0:i] + maxpall + s[j + 1:n]
	print(ans)
	t -= 1


def checkpalindrome(string):
	strlen = len(string)
	half = (strlen + 1) // 2
	palindrome = True
	for n in range(half):
		if string[n] != string[strlen - 1 - n]:
			palindrome = False
			break
	return palindrome
T = int(input())
for test in range(T):
	S = input()
	strlen = len(S)
	(pre, suf) = (0, 0)
	while pre + suf < strlen - 1 and S[pre] == S[strlen - 1 - suf]:
		pre += 1
		suf += 1
	baselen = pre + suf
	maxlen = pre + suf
	opti = (pre, suf)
	for i in range(1, strlen - maxlen + 1):
		palpre = S[pre:pre + i]
		palsuf = S[strlen - suf - i:strlen - suf]
		if checkpalindrome(palpre):
			maxlen = baselen + i
			opti = (baselen // 2 + i, baselen // 2)
		if checkpalindrome(palsuf):
			maxlen = baselen + i
			opti = (baselen // 2, baselen // 2 + i)
	maxpalindrome = S[:opti[0]] + S[strlen - opti[1]:strlen]
	print(maxpalindrome)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	s = [i for i in input() if i != '\n']
	s = ''.join(s)
	n = len(s)
	left = ''
	right = ''
	i = 0
	j = n - 1
	while i < j:
		if s[i] == s[j]:
			left += s[i]
			right += s[j]
		else:
			break
		i += 1
		j -= 1
	rem = s[i:j + 1]
	curr = ''
	for i in range(len(rem)):
		if rem[:i + 1] == rem[:i + 1][::-1] and len(rem[:i + 1]) > len(curr):
			curr = rem[:i + 1]
	rem = rem[::-1]
	for i in range(len(rem)):
		if rem[:i + 1] == rem[:i + 1][::-1] and len(rem[:i + 1]) > len(curr):
			curr = rem[:i + 1]
	sys.stdout.write(left + curr + right[::-1] + '\n')


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def get_str():
	return input().decode().strip()

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

def oint():
	return int(input())

def find_maxp(s):
	t = '#'.join('^{}$'.format(s))
	n = len(t)
	p = [0] * n
	c = 0
	r = 0
	for i in range(1, n - 1):
		p[i] = r > i and min(r - i, p[2 * c - i])
		while t[i + 1 + p[i]] == t[i - 1 - p[i]]:
			p[i] += 1
		if i + p[i] > r:
			(c, r) = (i, i + p[i])
	(maxl, ci) = max(((n, i) for (i, n) in enumerate(p) if (i - n) // 2 == 0 or (i + n) // 2 == len(s)))
	return s[(ci - maxl) // 2:(ci + maxl) // 2]
t = oint()
for _ in range(t):
	s = get_str()
	li = 0
	ri = len(s) - 1
	if ri == 0:
		print(s[0])
		continue
	ps_len = 0
	while li < ri:
		if s[li] == s[ri]:
			ps_len += 1
			li += 1
			ri -= 1
		else:
			break
	left_len = 0
	right_len = 0
	midp = find_maxp(s[li:ri + 1])
	print(s[:li] + midp + s[ri + 1:])


from math import ceil
from collections import deque
n = int(input())
for i in range(n):
	s = input()
	t = len(s)
	s1 = s[::-1]
	s11 = ''
	s12 = ''
	for j in range(len(s)):
		if s[j] == s[t - j - 1] and j < t - j - 1:
			s11 = s11 + s[j]
			s12 = s[t - j - 1] + s12
		else:
			break
	r = j
	t1 = s[r:t - j]
	t2 = s[r:t - j]
	t2 = s[r:t - j]
	while len(t1):
		if len(t1) % 2 == 0:
			k1 = t1[:int(len(t1) / 2)]
			k2 = t1[int(len(t1) / 2):]
		elif len(t1) % 2 == 1:
			k1 = t1[:int(len(t1) / 2) + 1]
			k2 = t1[int(len(t1) / 2):]
		if k1 == k2[::-1]:
			break
		else:
			t1 = t1[:len(t1) - 1]
	while len(t2):
		if len(t2) % 2 == 0:
			k1 = t2[:int(len(t2) / 2)]
			k2 = t2[int(len(t2) / 2):]
		elif len(t2) % 2 == 1:
			k1 = t2[:int(len(t2) / 2) + 1]
			k2 = t2[int(len(t2) / 2):]
		if k1 == k2[::-1]:
			break
		else:
			t2 = t2[1:]
	if len(t1) > len(t2):
		print(s11 + t1 + s12)
	else:
		print(s11 + str(t2) + s12)


def isPali(s, i, j):
	half = (j - i + 1) // 2
	for k in range(half):
		if s[i + k] != s[j - k]:
			return False
	return True
for t in range(int(input())):
	s = input()
	ls = len(s)
	(i, j) = (0, ls - 1)
	if isPali(s, i, j):
		print(s)
		continue
	while s[i] == s[j]:
		i += 1
		j -= 1
	pre = s[:i]
	post = s[j + 1:]
	r = s[i:j + 1]
	lr = len(r)

	def longest_palindrome_prefix(r):
		if not r:
			return 0
		r = r + '#' + r[::-1] + '$'
		i = 0
		j = -1
		nt = [0] * len(r)
		nt[0] = -1
		while i < len(r) - 1:
			if j == -1 or r[i] == r[j]:
				i += 1
				j += 1
				nt[i] = j
			else:
				j = nt[j]
		return nt[len(r) - 1]
	lc1 = longest_palindrome_prefix(r)
	lc2 = longest_palindrome_prefix(r[::-1])
	maxLc = max(lc1, lc2)
	if isPali(r, 0, maxLc - 1):
		conj = r[0:maxLc]
	else:
		conj = r[lr - maxLc:lr]
	ans = pre + conj + post
	print(ans)


def kmp(s):
	leng = len(s)
	lps = [0 for _ in range(leng)]
	for i in range(1, leng):
		prev_id = lps[i - 1]
		while prev_id > 0 and s[i] != s[prev_id]:
			prev_id = lps[prev_id - 1]
		if s[i] == s[prev_id]:
			lps[i] = prev_id + 1
		else:
			lps[i] = prev_id
	return lps[leng - 1]

def pre_pal(s):
	t = s + '#' + s[::-1]
	return kmp(t)
for _ in range(int(input())):
	string = input()
	length = len(string)
	if string[:] == string[::-1]:
		print(string)
	else:
		ans = ''
		a = ''
		b = ''
		i = 0
		while string[i] == string[-(i + 1)]:
			a += string[i]
			i += 1
		ans += a
		b = a[::-1]
		val = string[i:i + length - 2 * i]
		if len(val) > 0:
			len1 = pre_pal(val)
			len2 = pre_pal(val[::-1])
			if len1 > len2:
				ans += val[:len1]
			else:
				val = val[::-1]
				ans += val[:len2]
		ans += b
		print(ans)


import os
import sys
from io import BytesIO, IOBase
from math import log, floor, ceil

def longestPrePalindrome(s):
	s = chr(0) + chr(0).join(list(s)) + chr(0)
	(c, right) = (0, 0)
	pa_radius = [0 for i in s]
	res = ''
	for i in range(0, len(s)):
		if i > right:
			radius = 0
		else:
			i_ = c - (i - c)
			radius = min(pa_radius[i_], right - i)
		j = 1
		while 0 <= i - radius - j and i + radius + j < len(s) and (s[i - radius - j] == s[i + radius + j]):
			j += 1
		j -= 1
		if i + radius + j > right:
			c = i
			right = i + radius + j
			new_s = s[i - radius - j:i + radius + j + 1].replace(chr(0), '')
			if len(new_s) > len(res) and i - radius - j == 0:
				res = new_s
		pa_radius[i] = radius + j
	return res

def longestSufPalindrome(s):
	s = chr(0) + chr(0).join(list(s)) + chr(0)
	(c, right) = (0, 0)
	pa_radius = [0 for i in s]
	res = ''
	for i in range(0, len(s)):
		if i > right:
			radius = 0
		else:
			i_ = c - (i - c)
			radius = min(pa_radius[i_], right - i)
		j = 1
		while 0 <= i - radius - j and i + radius + j < len(s) and (s[i - radius - j] == s[i + radius + j]):
			j += 1
		j -= 1
		if i + radius + j > right:
			c = i
			right = i + radius + j
			new_s = s[i - radius - j:i + radius + j + 1].replace(chr(0), '')
			if len(new_s) > len(res) and i + radius + j + 1 == len(s):
				res = new_s
		pa_radius[i] = radius + j
	return res

def main():
	for _ in range(int(input())):
		s = input()
		n = len(s)
		l = 0
		r = n
		for i in range(n):
			if s[l] != s[r - 1] or l > r:
				break
			l += 1
			r -= 1
		if i >= len(s) // 2:
			print(s)
			continue
		temp = s[l:r]
		t1 = longestPrePalindrome(temp)
		t2 = longestSufPalindrome(temp)
		t = t1
		if len(t2) > len(t1):
			t = t2
		print(s[:l] + t + s[r:])
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()


def longestPrefixSuffix(s):
	n = len(s)
	lps = [0] * n
	l = 0
	i = 1
	while i < n:
		if s[i] == s[l]:
			l = l + 1
			lps[i] = l
			i = i + 1
		elif l != 0:
			l = lps[l - 1]
		else:
			lps[i] = 0
			i = i + 1
	return lps[n - 1]

def longestPalFromStart(s):
	s2 = s + '#' + s[::-1]
	lpsTotal = longestPrefixSuffix(s2)
	return lpsTotal

def main():
	q = int(input())
	allans = []
	for _ in range(q):
		s = input()
		i = 0
		j = len(s) - 1
		while i <= j:
			if s[i] == s[j]:
				i += 1
				j -= 1
			else:
				break
		if i > j:
			allans.append(s)
			continue
		middleS = s[i:j + 1]
		l1 = longestPalFromStart(middleS)
		l2 = longestPalFromStart(middleS[::-1])
		if l1 >= l2:
			innerPalindrome = middleS[:l1]
		else:
			innerPalindrome = middleS[-l2:]
		ans = s[:i] + innerPalindrome + s[j + 1:]
		allans.append(ans)
	multiLineArrayPrint(allans)
	return
import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def readIntArr():
	return [int(x) for x in input().split()]

def makeArr(defaultVal, dimensionArr):
	dv = defaultVal
	da = dimensionArr
	if len(da) == 1:
		return [dv for _ in range(da[0])]
	else:
		return [makeArr(dv, da[1:]) for _ in range(da[0])]

def queryInteractive(x, y):
	print('? {} {}'.format(x, y))
	sys.stdout.flush()
	return int(input())

def answerInteractive(ans):
	print('! {}'.format(ans))
	sys.stdout.flush()
inf = float('inf')
MOD = 10 ** 9 + 7
for _abc in range(1):
	main()


T = int(input())

def manachers(T):
	C = 0
	R = -1
	rad = 0
	P = [0] * len(T)
	for i in range(len(T)):
		if i <= R:
			rad = min(P[2 * C - i], R - i)
		else:
			rad = 0
		while i + rad < len(T) and i - rad >= 0 and (T[i - rad] == T[i + rad]):
			rad += 1
		P[i] = rad
		if i + rad - 1 > R:
			C = i
			R = i + rad - 1
	return P

def pal(s, start, end):
	T = '*'.join(s[start:end + 1])
	P = manachers(T)
	center = 0
	size = 0
	for (i, el) in enumerate(P):
		if el == i + 1:
			center = i
			size = el
	answer = [c for c in T[center - size + 1:center + size] if c != '*']
	return answer
for t in range(T):
	s = input()
	N = len(s)
	start = []
	i = 0
	while s[i] == s[N - 1 - i] and i < N - 1 - i:
		start.append(s[i])
		i += 1
	palin1 = pal(s, i, N - 1 - i)
	palin2 = pal(s[::-1], i, N - 1 - i)
	if len(palin1) >= len(palin2):
		palin = palin1
	else:
		palin = palin2
	answer = start + list(palin) + start[::-1]
	assert len(answer) <= N
	print(''.join(answer))


import sys
import math
from collections import defaultdict, Counter

def getZarr(string, z):
	n = len(string)
	(l, r, k) = (0, 0, 0)
	for i in range(1, n):
		if i > r:
			(l, r) = (i, i)
			while r < n and string[r - l] == string[r]:
				r += 1
			z[i] = r - l
			r -= 1
		else:
			k = i - l
			if i + z[k] <= r:
				z[i] = z[k]
			else:
				l = i
				while r < n and string[r - l] == string[r]:
					r += 1
				z[i] = r - l
				r -= 1
t = int(input())
for i in range(t):
	s = input()
	l = len(s)
	pre = ''
	suf = ''
	ind = -1
	for j in range(l // 2):
		if s[j] == s[l - j - 1]:
			pre += s[j]
			suf = s[j] + suf
		else:
			ind = j
			break
	if ind == -1:
		print(s)
		continue
	text = s[l - ind - 1:ind - 1:-1]
	pattern = text[::-1]
	concat = pattern + '$' + text
	if concat == '$':
		text = s[::-1]
		pattern = s
		concat = pattern + '$' + text
	l1 = len(concat)
	z = [0] * l1
	getZarr(concat, z)
	ans1 = 0
	for i in range(l1 // 2 + 1, l1):
		if z[i] == l1 - i:
			ans1 = z[i]
			break
	concat = text + '$' + pattern
	z = [0] * l1
	getZarr(concat, z)
	ans2 = 0
	for i in range(l1 // 2 + 1, l1):
		if z[i] == l1 - i:
			ans2 = z[i]
			break
	if ans1 >= ans2:
		print(pre + s[ind:ind + ans1] + suf)
	else:
		ind = l - ind - 1
		print(pre + s[ind:ind - ans2:-1] + suf)


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))
input = lambda : sys.stdin.readline().rstrip('\r\n')
testcases = int(input())
for j in range(testcases):
	string1 = input()
	stringr = string1[::-1]
	if len(string1) == 1:
		print(string1[0])
	elif string1[0] == stringr[0]:
		highest = 0
		for b in range(len(string1) // 2 + 1):
			if string1[b] == stringr[b]:
				highest = b
			else:
				break
		outputl = ''
		outputr = ''
		if highest == len(string1) - 1:
			print(''.join(string1))
		else:
			store = string1[:highest + 1][::-1]
			for k in range(len(string1) - highest - 1):
				if string1[:k + 1] + store == (string1[:k + 1] + store)[::-1]:
					outputl = string1[:k + 1] + store
			for k in range(len(stringr) - highest - 1):
				if stringr[:k + 1] + store == (stringr[:k + 1] + store)[::-1]:
					outputr = stringr[:k + 1] + store
			if len(outputl) >= len(outputr):
				print(outputl)
			else:
				print(outputr)
	else:
		outputl = []
		for k in range(len(string1)):
			if string1[:k + 1] == string1[:k + 1][::-1]:
				outputl = string1[:k + 1]
		outputr = []
		for k in range(len(stringr)):
			if stringr[:k + 1] == stringr[:k + 1][::-1]:
				outputr = stringr[:k + 1]
		if len(outputl) >= len(outputr):
			print(''.join(outputl))
		else:
			print(''.join(outputr))


def pre(s):
	p = s + '#' + s[::-1]
	pi = [0] * len(p)
	j = 0
	for i in range(1, len(p)):
		while j != 0 and p[j] != p[i]:
			j = pi[j - 1]
		if p[j] == p[i]:
			j += 1
		pi[i] = j
	return s[:j]
t = int(input())
for i in range(t):
	s = input()
	t1 = ''
	while len(s) > 1 and s[0] == s[-1]:
		t1 += s[0]
		s = s[1:-1]
	pp = pre(s)
	sp = pre(s[::-1])
	t2 = pp if len(pp) > len(sp) else sp
	print(t1 + t2 + t1[::-1])


t = int(input())
sol = []
while t:
	t -= 1
	a = input()
	i = 0
	n = len(a)
	j = n - 1
	str = ''
	if len(a) > 1:
		if a[i] == a[j]:
			str = a[i]
			i += 1
			j -= 1
			if i < n:
				while a[i] == a[j] and i < j:
					str += a[i]
					i += 1
					j -= 1
		if i == j:
			str = a
		elif i > j:
			str = str + str[::-1]
		else:
			pal = ''
			gi = i
			g = ''
			while gi <= j:
				g += a[gi]
				if g == g[::-1]:
					if len(g) > len(pal):
						pal = g
				gi += 1
			g = ''
			while i <= j:
				g += a[j]
				if g == g[::-1]:
					if len(g) > len(pal):
						pal = g
				j -= 1
			str = str + pal + str[::-1]
	else:
		str = a
	sol.append(str)
print(*sol, sep='\n')


def RollingHash(S):
	h = 10 ** 11 + 7
	b = 10 ** 7 + 7
	L = len(S)
	ret = -1
	hashS = 0
	hashT = 0
	c = 1
	for i in range(L):
		hashS = (hashS * b + ord(S[i])) % h
		hashT = (hashT + ord(S[i]) * c) % h
		c = c * b % h
		if hashS == hashT:
			ret = i
	return S[:ret + 1]

def solve(S):
	T1 = RollingHash(S)
	T2 = RollingHash(S[::-1])
	if len(T1) < len(T2):
		return T2[::-1]
	return T1
import sys
input = sys.stdin.readline
Q = int(input())
Query = [list(input().rstrip()) for _ in range(Q)]
for S in Query:
	N = len(S)
	ind = 0
	while True:
		if S[ind] != S[N - 1 - ind] or ind >= N - 1 - ind:
			break
		ind += 1
	R = solve(S[ind:N - ind])
	ans = S[:ind] + R + S[N - ind:]
	print(''.join(ans))


from bisect import bisect_left as bl, bisect_right as br, insort
import sys
import heapq
from collections import defaultdict as dd, deque

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

def mdata():
	return map(int, data().split())
mod = 998244353

def pallin(x):
	n = len(x)
	for i in range(len(x)):
		if x[i] != x[n - i - 1]:
			return False
	return True
for t in range(int(data())):
	s = data()
	l = s[0]
	n = len(s)
	for i in range(n):
		k = s[:n - i]
		if pallin(k):
			l = k
			break
	for i in range(1, n):
		k = s[i:]
		if pallin(k):
			if len(k) > len(l):
				l = k
			break
	for j in range(n // 2):
		if s[j] != s[n - j - 1]:
			if j > 0:
				j -= 1
				for i in range(j + 1, n - j - 1):
					if pallin(s[j + 1:i + 1]):
						k = s[0:j + 1] + s[j + 1:i + 1] + s[n - j - 1:]
						if len(l) < len(k):
							l = k
				for i in range(j + 1, n - j - 1):
					if pallin(s[i:n - j - 1]):
						k = s[0:j + 1] + s[i:n - j - 1] + s[n - j - 1:]
						if len(k) > len(l):
							l = k
						break
			break
	print(l)


import sys
input = sys.stdin.readline
from math import *
import copy
mod = 998244353
t = int(input())

def ispln(s):
	i = 0
	j = len(s) - 1
	while i <= j:
		if s[i] == s[j]:
			i += 1
			j -= 1
		else:
			return 0
	return 1
while t > 0:
	t -= 1
	s = list(input().rstrip())
	n = len(s)
	p = []
	j = n - 1
	i = 0
	while i < n and i < j:
		if s[i] != s[j]:
			break
		i += 1
		j -= 1
	r = []
	if i != j:
		rr = i
		p = []
		fp = []
		for ii in range(i, j + 1):
			p.append(s[ii])
			if ispln(p):
				fp = ''.join(p)
		p = list(fp)
		r = s[:i] + p + s[j + 1:n]
		p = []
		fp = []
		for ii in range(j, i - 1, -1):
			p.append(s[ii])
			if ispln(p):
				fp = ''.join(p)
		p = list(fp)
		tt = s[:rr] + p + s[j + 1:n]
		if len(tt) > len(r):
			r = tt
	p = []
	fp = []
	for i in range(n):
		p.append(s[i])
		if ispln(p):
			fp = ''.join(p)
	p = list(fp)
	if len(p) > len(r):
		r = p
	s = s[::-1]
	p = []
	fp = []
	for i in range(n):
		p.append(s[i])
		if ispln(p):
			fp = ''.join(p)
	p = list(fp)
	if len(p) > len(r):
		r = p
	print(''.join(r))


def reverse(s):
	return s[::-1]

def ip(s):
	rev = reverse(s)
	if s == rev:
		return 1
	return -1
t = int(input())
for o in range(t):
	x = input()
	h = len(x)
	d = []
	c = []
	num = []
	for j in range(26):
		num.append([])
	for j in range(len(x)):
		c.append(x[j])
		num[ord(x[j]) - 97].append(j)
	if c[0] == c[-1]:
		j = 1
		y = 0
		j = 1
		while j < h - 1:
			if c[j] != c[h - j - 1]:
				y = 1
				p = len(num[ord(x[j]) - 97]) - 1
				while p >= 0:
					if num[ord(x[j]) - 97][p] <= h - j - 1:
						s = x[j:num[ord(x[j]) - 97][p] + 1]
						if ip(s) == 1:
							break
					p += -1
				M = s
				p = 0
				while p < len(num[ord(c[h - j - 1]) - 97]):
					if num[ord(c[h - j - 1]) - 97][p] >= j:
						s = x[num[ord(c[h - j - 1]) - 97][p]:h - j]
						if ip(s) == 1:
							break
					p += 1
				K = s
				if len(M) > len(K):
					m = M
				else:
					m = K
			if y == 1:
				l = x[0:j] + m + x[h - j:h]
				d.append(l)
				break
			j += 1
	j = len(num[ord(x[0]) - 97]) - 1
	while j >= 0:
		s = x[0:num[ord(x[0]) - 97][j] + 1]
		if ip(s) == 1:
			break
		j += -1
	d.append(s)
	j = 0
	while j < len(num[ord(c[-1]) - 97]):
		s = x[num[ord(c[-1]) - 97][j]:h]
		if ip(s) == 1:
			break
		j += 1
	d.append(s)
	g = ''
	for b in d:
		if len(b) > len(g):
			g = b
	print(g)


t = int(input())
for i in range(t):
	s = input()
	n = len(s)
	beg = 0
	end = n - 1
	while beg < end and s[beg] == s[end]:
		beg += 1
		end -= 1
	pali = 0
	end2 = end
	prob = 'a'
	probpos = n
	probpassed = 1
	posgood = 1
	while pali == 0:
		while end2 > beg and s[end2] != s[beg] or probpassed == 0 or posgood == 0:
			end2 -= 1
			if s[end2] == prob:
				probpassed = 1
			if s[end2] == s[beg]:
				if probpos > end2:
					posgood = 1
				else:
					posgood = 0
					if s[probpos] == s[end2 - probpos + beg]:
						posgood = 1
		pali = 1
		end3 = end2
		beg2 = beg
		while beg2 < end3 and pali == 1:
			if s[end3] == s[beg2]:
				beg2 += 1
				end3 -= 1
			else:
				pali = 0
				prob = s[end3]
				probpassed = 0
				probpos = beg2
	length1 = end2 - beg + 1
	pali = 0
	beg2 = beg
	prob = 'a'
	probpos = 0
	probpassed = 1
	posgood = 1
	while pali == 0:
		while beg2 < end and s[beg2] != s[end] or probpassed == 0 or posgood == 0:
			beg2 += 1
			if s[beg2] == prob:
				probpassed = 1
			if s[beg2] == s[end]:
				if probpos < beg2:
					posgood = 1
				else:
					posgood = 0
					if s[probpos] == s[beg2 - probpos + end]:
						posgood = 1
		pali = 1
		beg3 = beg2
		end2 = end
		while beg3 < end2 and pali == 1:
			if s[end2] == s[beg3]:
				beg3 += 1
				end2 -= 1
			else:
				pali = 0
				prob = s[beg3]
				probpassed = 0
				probpos = end2
	length2 = end - beg2 + 1
	string = s[0:beg]
	if length1 > length2:
		string += s[beg:beg + length1]
	else:
		string += s[end - length2 + 1:end + 1]
	string += s[end + 1:]
	print(string)


t = int(input())
for counter in range(t):
	s = input()
	l = len(s)
	left = right = ''
	i = 0
	j = l - 1
	while i < j:
		if s[i] == s[j]:
			left += s[i]
			right += s[j]
			i += 1
			j -= 1
		else:
			break
	rem = s[i:j + 1]
	req = ''
	for i in range(len(rem)):
		if rem[:i + 1] == rem[:i + 1][::-1] and len(rem[:i + 1]) > len(req):
			req = rem[:i + 1]
	rem = rem[::-1]
	for i in range(len(rem)):
		if rem[:i + 1] == rem[:i + 1][::-1] and len(rem[:i + 1]) > len(req):
			req = rem[:i + 1]
	print(right + req + left[::-1])


t = int(input())
for _ in range(t):
	s = input()
	if s == s[::-1]:
		print(s)
	else:
		left = ''
		right = ''
		for i in range(len(s)):
			if s[i] == s[-i - 1]:
				left += s[i]
				right += s[i]
			else:
				break
		rem = s[i:len(s) - i]
		l = 0
		r = 0
		l_s = ''
		r_s = ''
		for i in range(len(rem)):
			if rem[:i] == rem[:i][::-1]:
				l = len(rem[:i])
				l_s = rem[:i]
			if rem[-i - 1:] == rem[-i - 1:][::-1]:
				r = len(rem[-i - 1:])
				r_s = rem[-i - 1:]
		if len(s) <= 1:
			ans = s
		elif l >= r:
			ans = left + l_s + right[::-1]
			if len(ans) > len(s):
				ans = s
		else:
			ans = left + r_s + right[::-1]
			if len(ans) > len(s):
				ans = s
		print(ans)


for _ in range(int(input())):
	s = input()
	l = 0
	r = len(s) - 1
	while l < r and s[l] == s[r]:
		l += 1
		r -= 1
	s1 = s[:l] + s[r + 1:]
	n = len(s1)
	ans = s1
	for i in range(l, r + 1):
		d = s[:l] + s[i:r + 1] + s[r + 1:]
		if d == d[::-1] and len(d) > n:
			ans = d
			n = len(d)
	for i in range(r, l - 1, -1):
		d = s[:l] + s[l:i] + s[r + 1:]
		if d == d[::-1] and len(d) > n:
			ans = d
			n = len(d)
	print(ans)


def isPalindrome(s):
	return s == ''.join(reversed(s))
t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	if n == 1:
		print(s)
		continue
	i = 0
	j = n - 1
	final = [] * n
	while i <= j:
		if s[i] == s[j]:
			final.append(s[i])
			i += 1
			j -= 1
		else:
			break
	if i >= j:
		print(s)
		continue
	curr = ''
	for p in range(i, j):
		if isPalindrome(s[i:p + 1]) and p - i + 1 > len(curr):
			curr = s[i:p + 1]
		if isPalindrome(s[p:j + 1]) and j - p + 1 > len(curr):
			curr = s[p:j + 1]
	ans = [''] * (2 * len(final) + len(curr))
	ind = 0
	for i in range(len(final)):
		ans[i] = final[i]
		ans[len(ans) - i - 1] = final[i]
	for i in range(len(curr)):
		ans[i + len(final)] = curr[i]
	print(''.join(ans))


import sys
input = sys.stdin.readline
tc = int(input())
for _ in range(tc):
	s = input().rstrip()
	n = len(s)
	(l, r) = (0, n - 1)
	while l < r and s[l] == s[r]:
		l += 1
		r -= 1
	a = s[:l]
	b = s[r + 1:]
	s = s[l:r + 1]
	t = ''
	for i in range(len(s)):
		(l, r) = (i - 1, i + 1)
		while 0 <= l and r < len(s) and (s[l] == s[r]):
			l -= 1
			r += 1
		if (l == -1 or r == len(s)) and len(t) < r - l - 1:
			t = s[l + 1:r]
		(l, r) = (i, i + 1)
		while 0 <= l and r < len(s) and (s[l] == s[r]):
			l -= 1
			r += 1
		if (l == -1 or r == len(s)) and len(t) < r - l - 1:
			t = s[l + 1:r]
	print(a + t + b)


import sys
sys.setrecursionlimit(10000)

def palindrome(s, si, ei):
	if si >= ei:
		return True
	if s[si] == s[ei]:
		return palindrome(s, si + 1, ei - 1)
	elif s[si] != s[ei]:
		return False

def solve():
	s = input()
	n = len(s)
	if n == 1:
		print(s)
		return
	(i, j, l) = (0, n - 1, 0)
	while i < j and s[i] == s[j]:
		i += 1
		j -= 1
		l += 1
	if i == j or i - j == 1:
		print(s)
		return
	(pmaxx, smaxx) = (0, 0)
	(pnote, snote) = (l, n - l)
	for i in range(l + 1, n - l - 1):
		stm = palindrome(s, l, i)
		if stm and i - l + 1 > pmaxx:
			pnote = i
			pmaxx = i - l + 1
	for j in range(n - l - 2, l - 1, -1):
		stm = palindrome(s, j, n - l - 1)
		if stm and n - l - j > smaxx:
			snote = j
			smaxx = n - l - j
	if pmaxx == 0 and smaxx == 0 and (l == 0):
		print(s[0])
		return
	if pmaxx >= smaxx:
		final = s[0:l] + s[l:pnote + 1] + s[n - l:]
	else:
		final = s[0:l] + s[snote:n - l] + s[n - l:]
	print(final)
	return
l = int(input())
for i in range(l):
	solve()


import sys
input = sys.stdin.readline

def print(val):
	sys.stdout.write(val)

def prog():
	for _ in range(int(input())):
		s = input().strip()
		i = -1
		for i in range(0, len(s) // 2):
			if s[i] != s[-i - 1]:
				i = i - 1
				break
		end = s[:i + 1]
		l = i + 1
		r = len(s) - i - 2
		r2 = r
		while True:
			worked = True
			for i in range(r2 - l + 1):
				if s[l + i] != s[r2 - i]:
					worked = False
			if worked:
				break
			else:
				r2 -= 1
		l2 = l
		while True:
			worked = True
			for i in range(r - l2 + 1):
				if s[l2 + i] != s[r - i]:
					worked = False
			if worked:
				break
			else:
				l2 += 1
		if r2 - l > r - l2:
			r = r2
		else:
			l = l2
		print(end)
		print(s[l:r + 1])
		print(end[::-1])
		sys.stdout.write('\n')
prog()


import math
from bisect import bisect_left, bisect_right
from sys import stdin, stdout, setrecursionlimit
from collections import Counter
input = lambda : stdin.readline().strip()
print = stdout.write
t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	ls = []
	for i in range(n):
		ls.append([])
		for j in range(n):
			ls[-1].append(0)
	for k in range(n):
		i = 0
		j = k
		while j < n:
			if i == j:
				ls[i][j] = 1
			elif i + 1 == j:
				ls[i][j] = int(s[i] == s[j])
			else:
				ls[i][j] = int(ls[i + 1][j - 1] and s[i] == s[j])
			i += 1
			j += 1
	i = 0
	while i < n - i - 1:
		if s[i] == s[n - i - 1]:
			i += 1
		else:
			break
	if i > n - i - 1:
		print(s + '\n')
	else:
		right_index = i
		for k in range(n - i - 1, i - 1, -1):
			if ls[i][k]:
				right_index = k
				break
		left_index = n - i - 1
		for k in range(i, n - i):
			if ls[k][n - i - 1]:
				left_index = k
				break
		if right_index > n - left_index - 1:
			print(s[:i] + s[i:right_index + 1] + s[n - i:] + '\n')
		else:
			print(s[:i] + s[left_index:n - i] + s[n - i:] + '\n')


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def YES(c):
	return IF(c, 'YES', 'NO')

def Yes(c):
	return IF(c, 'Yes', 'No')

class ModInt:

	def __init__(self, n):
		self.n = n

	def __add__(self, x):
		x = ModInt.xn(x)
		return ModInt((self.n + x) % mod)

	def __sub__(self, x):
		x = ModInt.xn(x)
		return ModInt((self.n - x) % mod)

	def __mul__(self, x):
		x = ModInt.xn(x)
		return ModInt(self.n * x % mod)

	def __truediv__(self, x):
		x = ModInt.xn(x)
		return ModInt(self.n * pow(x, mod - 2, mod) % mod)

	@classmethod
	def xn(cls, x):
		if isinstance(x, ModInt):
			return x.n
		return x

	def __str__(self):
		return str(self.n)

def M(n):
	return ModInt(n)

class RollingHash:

	def __init__(self, s):
		self.N = n = len(s)
		a = [ord(c) for c in s]
		self.A1 = a1 = [M(a[0])]
		self.A2 = a2 = [M(a[0])]
		self.A3 = a3 = [M(a[-1])]
		self.A4 = a4 = [M(a[-1])]
		for c in a[1:]:
			a1.append(a1[-1] * 997 + c)
			a2.append(a2[-1] * 991 + c)
		for c in a[::-1][1:]:
			a3.append(a3[-1] * 997 + c)
			a4.append(a4[-1] * 991 + c)

	def get(self, l, r):
		if l == 0:
			return (self.A1[r].n, self.A2[r].n)
		t1 = (self.A1[r] - self.A1[l - 1] * pow(997, r - l + 1, mod)).n
		t2 = (self.A2[r] - self.A2[l - 1] * pow(991, r - l + 1, mod)).n
		return (t1, t2)

	def rget(self, l, r):
		(l, r) = (self.N - r - 1, self.N - l - 1)
		if l == 0:
			return (self.A3[r].n, self.A3[r].n)
		t3 = (self.A3[r] - self.A3[l - 1] * pow(997, r - l + 1, mod)).n
		t4 = (self.A4[r] - self.A4[l - 1] * pow(991, r - l + 1, mod)).n
		return (t3, t4)

	def getr(self, l, r):
		(l, r) = (self.N - r - 1, self.N - l - 1)
		return self.get(l, r)

	def rgetr(self, l, r):
		(l, r) = (self.N - r - 1, self.N - l - 1)
		return self.rget(l, r)

def main():
	t = I()
	rr = []
	for _ in range(t):
		s = S()
		sl = len(s)
		l = 0
		r = sl - 1
		for i in range(sl // 2):
			if s[l] == s[r]:
				l += 1
				r -= 1
		if l >= r:
			rr.append(s)
			continue
		t = s[l:r + 1]
		tl = len(t)
		rh = RollingHash(t)
		m = (1, 'L')
		for i in range(tl // 2):
			k = rh.get(0, i)
			if (i + 1) * 2 < tl and rh.rget(i + 2, (i + 1) * 2) == k:
				j = ((i + 1) * 2 + 1, 'L')
				m = max(m, j)
			elif rh.rget(i + 1, (i + 1) * 2 - 1) == k:
				j = ((i + 1) * 2, 'L')
				m = max(m, j)
		for i in range(tl // 2):
			k = rh.getr(0, i)
			if (i + 1) * 2 < tl and rh.rgetr(i + 2, (i + 1) * 2) == k:
				j = ((i + 1) * 2 + 1, 'R')
				m = max(m, j)
			elif rh.rgetr(i + 1, (i + 1) * 2 - 1) == k:
				j = ((i + 1) * 2, 'R')
				m = max(m, j)
		if m[1] == 'L':
			rr.append(s[:l] + t[:m[0]] + s[r + 1:])
		else:
			rr.append(s[:l] + t[-m[0]:] + s[r + 1:])
	return JA(rr, '\n')
print(main())


def manacher(s):
	n = 2 * len(s)
	rad = [0] * n
	i = j = 0
	while i < n:
		while i >= j and i + j + 1 < n and (s[(i - j) // 2] == s[(i + j + 1) // 2]):
			j += 1
		rad[i] = j
		k = 1
		while i >= k and rad[i] >= k and (rad[i - k] != rad[i] - k):
			rad[i + k] = min(rad[i - k], rad[i] - k)
			k += 1
		i += k
		j = max(j - k, 0)
	return rad
test = int(input())
while test > 0:
	test -= 1
	s = input()
	rad = manacher(s)
	(i, j) = (0, len(s) - 1)
	sol = ''
	while s[i] == s[j] and i < j:
		sol += s[i]
		i += 1
		j -= 1
	print(sol, end='')
	(best, index) = (0, -1)
	(b, e) = (2 * (i - 1), 2 * (j + 1))
	for it in range(b + 1, e):
		l = it - b - 1
		r = e - it - 1
		x = y = -1
		if rad[it] >= l:
			x = l
		if rad[it] >= r:
			y = r
		if x == -1 and y == -1:
			continue
		x = y if x == -1 else x
		y = x if y == -1 else y
		if best < min(x, y):
			index = it
			best = min(x, y)
	for i in range(index - best + 1, index + best, 2):
		print(s[i // 2], end='')
	for item in reversed(sol):
		print(item, end='')
	print()


def palindrome(s: str) -> bool:
	length = len(s)
	pre = length // 2
	for i in range(pre):
		if s[i] != s[-i - 1]:
			return False
	return True

def longestPalindrome(line: str) -> str:
	(b, e) = (0, len(line) - 1)
	while b < e and line[b] == line[e]:
		b += 1
		e -= 1
	if b >= e:
		return line
	i = e
	while i > b:
		if palindrome(line[b:i]) is True:
			break
		i -= 1
	p = line[:i] + line[e + 1:]
	i = b + 1
	while i < e:
		if palindrome(line[i:e + 1]) is True:
			break
		i += 1
	p2 = line[:b] + line[i:]
	return p2 if len(p2) > len(p) else p
n = int(input())
for i in range(n):
	line = input().strip(' \r\n')
	print(longestPalindrome(line))


for _ in range(int(input())):
	s = input()
	l = len(s)
	a = ''
	p = 0
	if s == s[::-1]:
		print(s)
		continue
	for i in range(l // 2):
		if s[i] == s[l - 1 - i]:
			a = a + s[i]
		else:
			p = i
			break
	mid1 = ''
	mid2 = ''
	for i in range(p, l - p):
		if s[p:i] == s[p:i][::-1]:
			mid1 = s[p:i]
	x = s[::-1]
	for i in range(p, l - p):
		if x[p:i] == x[p:i][::-1]:
			mid2 = x[p:i]
	ans1 = a + mid1 + a[::-1]
	ans2 = a + mid2 + a[::-1]
	if len(ans1) >= len(ans2):
		print(ans1)
	else:
		print(ans2)


t = int(input())
for x in range(t):
	s = input()
	i = 0
	j = len(s) - 1
	while i < j:
		if s[i] == s[j]:
			i = i + 1
			j = j - 1
		else:
			break
	if i < j:
		new = s[i:j + 1]
		new1 = s[i:j + 1][::-1]
		mid = ''
		mid2 = ''
		for k in range(1, len(new) + 1):
			if new[:k] == new[:k][::-1]:
				mid = new[:k]
		for k in range(1, len(new) + 1):
			if new1[:k] == new1[:k][::-1]:
				mid2 = new1[:k]
		if len(mid) > len(mid2):
			final = s[:i] + mid + s[:i][::-1]
		else:
			final = s[:i] + mid2 + s[:i][::-1]
	else:
		final = s[:i] + s[i:]
	print(final)


def poopoo(a):
	ret = ''
	if len(a) < 2:
		return a
	while len(a) >= 2 and a[0] == a[-1]:
		ret += a[0]
		a = a[1:-1]
	if len(a) < 2:
		return ret + a + ret[::-1]
	b = a
	g = True
	maxx = ''
	while g:
		if ispal(a):
			maxx = a
			g = False
		if ispal(b):
			maxx = b
			g = False
		else:
			a = a[1:]
			b = b[:-1]
	return ret + maxx + ret[::-1]

def ispal(st):
	while len(st) > 1:
		if st[0] != st[-1]:
			return False
		st = st[1:-1]
	return True
temp = int(input())
xx = []
for i in range(temp):
	xx.append(poopoo(input()))
for i in xx:
	print(i)


for _ in range(int(input())):
	s = input()
	n = len(s)
	left = ''
	right = ''
	i = 0
	j = n - 1
	while i < j:
		if s[i] == s[j]:
			left += s[i]
			right += s[j]
		else:
			break
		i += 1
		j -= 1
	rem = s[i:j + 1]
	curr = ''
	for i in range(len(rem)):
		if rem[:i + 1] == rem[:i + 1][::-1] and len(rem[:i + 1]) > len(curr):
			curr = rem[:i + 1]
	rem = rem[::-1]
	for i in range(len(rem)):
		if rem[:i + 1] == rem[:i + 1][::-1] and len(rem[:i + 1]) > len(curr):
			curr = rem[:i + 1]
	print(left + curr + right[::-1])


import sys
import math
from collections import deque

def read_line():
	return sys.stdin.readline()[:-1]

def read_int():
	return int(sys.stdin.readline())

def read_int_line():
	return [int(v) for v in sys.stdin.readline().split()]

def isPalindrome(str):
	for i in range(0, len(str) // 2):
		if str[i] != str[len(str) - i - 1]:
			return False
	return True
t = read_int()
for i in range(t):
	s = read_line()
	if len(s) == 1:
		print(s)
	else:
		t = ''
		a = ''
		b = ''
		i = 0
		j = len(s) - 1
		while i < j:
			if s[i] == s[j]:
				a += s[i]
				b = s[j] + b
				i += 1
				j -= 1
			else:
				break
		t1 = ''
		t2 = ''
		for x in range(j, i - 1, -1):
			ans1 = isPalindrome(s[i:x + 1])
			if ans1:
				t1 = s[i:x + 1]
				break
		for x in range(i, j + 1):
			ans1 = isPalindrome(s[x:j + 1])
			if ans1:
				t2 = s[x:j + 1]
				break
		n1 = len(t1)
		n2 = len(t2)
		if n1 >= n2:
			t = a + t1 + b
		else:
			t = a + t2 + b
		print(t)


t = int(input())

def palindrome(l):
	(k, mod) = (37, 998244353)
	(h1, h2, p, ans) = (0, 0, 1, 0)
	for i in range(len(l)):
		h1 = (h1 + (ord(l[i]) - 97) * p) % mod
		h2 = (h2 * k % mod + ord(l[i]) - 97) % mod
		p = p * k % mod
		if h1 == h2:
			ans = i + 1
	return ans
for t_ in range(t):
	s = list(input())
	i = 0
	while s[i] == s[len(s) - i - 1] and i < len(s) - i - 1:
		i += 1
	ans = s[:i]
	print(''.join((str(i) for i in ans)), end='')
	ans1 = palindrome(s[i:len(s) - i])
	if i == 0:
		ans2 = palindrome(s[len(s) - i - 1::-1])
	else:
		ans2 = palindrome(s[len(s) - i - 1:i - 1:-1])
	if ans1 >= ans2:
		print(''.join((str(i) for i in s[i:i + ans1])), end='')
	else:
		print(''.join((str(i) for i in s[len(s) - i - ans2:len(s) - i])), end='')
	print(''.join((str(i) for i in reversed(ans))))


import sys
import math
from collections import defaultdict, deque
import heapq

def check(n, s):
	m = len(s)
	fault = defaultdict(int)
	cnt = 0
	(l, r) = (0, n - 1)
	nums = []
	for i in range(n):
		nums.append(s[i])
	while l < r:
		if s[l] == s[r]:
			pass
		else:
			fault[l] = r
			fault[r] = l
			cnt += 1
		l += 1
		r -= 1
	ind = m - 1
	if cnt == 0:
		return nums
	for i in range(n - 1, -1, -1):
		if i >= n // 2:
			curl = n - i - 1
			curr = i
		else:
			curl = i
			curr = n - i - 1
		if nums[curr] != nums[curl]:
			cnt -= 1
		nums[i] = s[ind]
		if nums[curl] != nums[curr]:
			cnt += 1
		ind -= 1
		if cnt == 0:
			return nums
	return False
t = int(sys.stdin.readline())
for _ in range(t):
	s = list(sys.stdin.readline()[:-1])
	low = 1
	high = len(s)
	ans = 1
	for i in range(1, high + 1):
		if check(i, s):
			ans = i
	res = check(ans, s)
	print(''.join((x for x in res)))


for _ in range(int(input())):
	s = input()
	n = len(s)
	i = 0
	j = n - 1
	mx = 0
	while i < j:
		if s[i] == s[j]:
			i += 1
			j -= 1
			mx += 2
		else:
			break
	pal = ''
	m = 0
	mid = ''
	for k in range(i, j + 1):
		pal += s[k]
		if pal == pal[::-1]:
			m = len(pal)
			mid = pal
	pal = ''
	for k in range(j, i - 1, -1):
		pal += s[k]
		if pal == pal[::-1]:
			if m < len(pal):
				mid = pal
				m = len(pal)
	ans = ''
	for k in range(0, i):
		ans += s[k]
	ans += mid
	for k in range(j + 1, n):
		ans += s[k]
	print(ans)


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

def modinv(n, p):
	return pow(n, p - 2, p)

def ncr(n, r, p):
	t = fact[n] * (ifact[r] * ifact[n - r] % p) % p
	return t

def ain():
	return list(map(int, sin().split()))

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

def GCD(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def read2DIntArray(row, col):
	arr = []
	for i in range(0, row):
		temp = list(map(int, sin().split()))
		arr.append(temp)
	return arr

def read2DCharArray(row, col):
	arr = []
	for i in range(0, row):
		temp = str(sin())
		arr.append(temp)
	return arr

def smallestNumber(n):
	lst = list(str(n))
	lst.sort()
	tmp = ''
	for (i, n) in enumerate(lst):
		if n != '0':
			tmp = lst.pop(i)
			break
	return str(tmp) + ''.join(lst)

def main():
	t = int(sin())
	lst = []
	for i in range(t):
		lst.append(sin())
	for word in lst:
		n = len(word)
		i = 0
		j = n - 1
		while i < j and word[i] == word[j]:
			i += 1
			j -= 1
		start = word[:i]
		end = word[j + 1:]
		s = word[i:j + 1]
		temp = ''
		str1 = ''
		for m in range(0, len(s)):
			if s[0] == s[m]:
				substr = s[0:m + 1]
				if substr == substr[::-1]:
					temp = substr
					if len(temp) > len(str1):
						str1 = temp
		temp = ''
		str2 = ''
		s = s[::-1]
		for m in range(0, len(s)):
			if s[0] == s[m]:
				substr = s[0:m + 1]
				if substr == substr[::-1]:
					temp = substr
					if len(temp) > len(str2):
						str2 = temp
		middle = ''
		if len(str1) > len(str2):
			middle = str1
		else:
			middle = str2
		print(start + middle + end)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			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')
main()


from sys import stdin

def inp():
	return stdin.buffer.readline().rstrip().decode('utf8')

def itg():
	return int(stdin.buffer.readline())

def mpint():
	return map(int, stdin.buffer.readline().split())

def is_palindrome(s):
	for i in range(len(s) >> 1):
		if s[i] != s[-i - 1]:
			return False
	return True

def longest_prefix(s):
	if not s:
		return ''
	index = [i for i in range(1, len(s)) if s[i] == s[0]]
	if index:
		index.reverse()
		for idx in index:
			if is_palindrome(s[:idx + 1]):
				return s[:idx + 1]
	return s[0]
for case in range(itg()):
	s = inp()
	n = len(s)
	p_len = 0
	for i in range(n >> 1):
		if s[i] == s[-i - 1]:
			p_len = i + 1
		else:
			break
	s_pre = s[:p_len]
	s_suf = s[:p_len][::-1]
	if p_len:
		s = s[p_len:-p_len]
	s_mid = longest_prefix(s)
	longest_suffix = longest_prefix(s[::-1])
	if len(longest_suffix) > len(s_mid):
		s_mid = longest_suffix
	print(s_pre + s_mid + s_suf)


import random
hashmod = random.randint(1000000000000, 2000000000000)

def longpp(s):
	best = 0
	(hord, hrev) = (0, 0)
	(m1, m2) = (1, 1)
	for i in range(len(s)):
		hord = (hord + m1 * s[i]) % hashmod
		hrev = (s[i] + 26 * hrev) % hashmod
		m1 *= 26
		m1 %= hashmod
		m2 *= 26
		m2 %= hashmod
		if hord == hrev:
			best = i + 1
	return s[:best]

def conv(s):
	return list(map(lambda x: ord(x) - ord('a'), s))

def iconv(s):
	res = ''
	for e in s:
		res += chr(e + ord('a'))
	return res
n = int(input())
for i in range(n):
	s = conv(input())
	(ptrl, ptrr) = (0, len(s) - 1)
	while ptrl + 1 <= ptrr - 1 and s[ptrl] == s[ptrr]:
		ptrl += 1
		ptrr -= 1
	rem = s[ptrl:ptrr + 1]
	ll = longpp(rem)
	lr = longpp(rem[::-1])
	if len(ll) > len(lr):
		print(iconv(s[:ptrl] + ll + s[ptrr + 1:]))
	else:
		print(iconv(s[:ptrl] + lr + s[ptrr + 1:]))


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def get_str():
	return input().decode().strip()

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

def oint():
	return int(input())
t = oint()
for _ in range(t):
	s = get_str()
	li = 0
	ri = len(s) - 1
	ps_len = 0
	while li < ri:
		if s[li] == s[ri]:
			ps_len += 1
			li += 1
			ri -= 1
		else:
			break
	left_len = 0
	for i in range(ri - 1, li - 1, -1):
		ss = li
		ee = i
		while ss <= ee:
			if s[ss] == s[ee]:
				ss += 1
				ee -= 1
			else:
				break
		if ss > ee:
			left_len = i - li + 1
			break
	right_len = 0
	for i in range(li, ri + 1):
		ss = i
		ee = ri
		while ss <= ee:
			if s[ss] == s[ee]:
				ss += 1
				ee -= 1
			else:
				break
		if ss > ee:
			right_len = ri - i + 1
			break
	if left_len > right_len:
		print(s[:li + left_len] + s[ri + 1:])
	else:
		print(s[:li] + s[ri + 1 - right_len:])


import sys, re, math
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from heapq import heappush, heappop, heapify, nlargest, nsmallest
import re

def STR():
	return list(input())

def INT():
	return int(input())

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

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

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def sortListWithIndex(listOfTuples, idx):
	return sorted(listOfTuples, key=lambda x: x[idx])

def sortDictWithVal(passedDic):
	temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
	toret = {}
	for tup in temp:
		toret[tup[0]] = tup[1]
	return toret

def sortDictWithKey(passedDic):
	return dict(OrderedDict(sorted(passedDic.items())))
INF = float('inf')
mod = 10 ** 9 + 7

def KMP(s):
	n = len(s)
	lps = [0] * n
	l = 0
	i = 1
	while i < n:
		if s[i] == s[l]:
			l = l + 1
			lps[i] = l
			i = i + 1
		elif l != 0:
			l = lps[l - 1]
		else:
			lps[i] = 0
			i = i + 1
	res = lps[n - 1]
	if res > n / 2:
		return n // 2
	else:
		return res

def find_largest_pal_pref(st):
	temp = st + str('?') + st[::-1]
	return KMP(temp)
t = INT()
while t != 0:
	s = input()
	n = len(s)
	i = 0
	j = n - 1
	ans = 0
	while i < j:
		if s[i] == s[j]:
			ans += 1
		else:
			break
		i += 1
		j -= 1
	final = s[:i]
	if j - i + 1 > 0:
		rem = s[i:j + 1]
		temp = find_largest_pal_pref(rem)
		temp1 = find_largest_pal_pref(rem[::-1])
		if temp > temp1:
			final += rem[:temp]
		else:
			final += rem[::-1][:temp1]
	final += s[j + 1:]
	print(final)
	t -= 1


def palindrome(str):
	temp = str + '?'
	str = str[::-1]
	temp += str
	nn = len(temp)
	lps = [0] * nn
	for i in range(1, nn, 1):
		length = lps[i - 1]
		while length > 0 and temp[length] != temp[i]:
			length = lps[length - 1]
		if temp[i] == temp[length]:
			length = length + 1
		lps[i] = length
	return temp[0:lps[nn - 1]]
t = int(input())
for j in range(0, t):
	s = input()
	ss = s[::-1]
	n = len(s)
	a = 0
	for i in range(0, n // 2):
		if s[i] == s[n - 1 - i] and i != n - 1 - i:
			a = a + 1
		else:
			break
	pre = palindrome(s[a:n - a])
	suff = palindrome(ss[a:n - a])
	if len(pre) >= len(suff):
		print(s[0:a] + pre + s[n - a:n])
	else:
		print(s[0:a] + suff + s[n - a:n])


def sol():
	s = input()
	x = ''
	y = ''
	a = 0
	b = len(s) - 1
	while a < b:
		if s[a] == s[b]:
			x += s[a]
			y += s[a]
			a += 1
			b -= 1
		else:
			break
	y = y[::-1]
	r1 = ''
	t1 = ''
	for n in range(a, b + 1):
		t1 += s[n]
		if t1 == t1[::-1]:
			r1 = t1
	r2 = ''
	t2 = ''
	for n in range(b, a - 1, -1):
		t2 += s[n]
		if t2 == t2[::-1]:
			r2 = t2
	if len(r1) >= len(r2):
		x += r1
	else:
		x += r2
	x += y
	print(x)
for n in range(int(input())):
	sol()


import os
import io
from collections import Counter
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce
from collections import deque
import threading

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

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

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())
mod = int(1000000000.0) + 7

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def findprefix(s):
	n = len(s)
	prefix = [0] * n
	j = 0
	for i in range(1, n):
		j = prefix[i - 1]
		while j > 0 and s[i] != s[j]:
			j = prefix[j - 1]
		if s[i] == s[j]:
			j += 1
		prefix[i] = j
	return j
for _ in range(iinput()):
	s = list(input())
	n = len(s)
	a = []
	b = []
	(i, j) = (0, n - 1)
	while i < j:
		if s[i] != s[j]:
			break
		a.append(s[i])
		b.append(s[j])
		i += 1
		j -= 1
	if i >= j:
		print(''.join(s))
	else:
		process = s[i:j + 1]
		processrev = process[::-1]
		got1 = findprefix(process + ['#'] + processrev)
		got2 = findprefix(processrev + ['#'] + process)
		toadd = ''
		if got1 > got2:
			toadd = process[:got1]
		else:
			toadd = processrev[:got2]
		print(''.join(a) + ''.join(toadd) + ''.join(b[::-1]))


def pal(s):
	a = s + '.' + s[::-1]
	n = len(a)
	(j, p) = (0, [0] * n)
	for i in range(1, n):
		while j and a[j] != a[i]:
			j = p[j - 1]
		if a[j] == a[i]:
			j += 1
		p[i] = j
	return s[:j]

def longest(s):
	(a, b) = (pal(s), pal(s[::-1]))
	return a if len(a) > len(b) else b

def f(s):
	(i, j) = (0, len(s) - 1)
	while i < j and s[i] == s[j]:
		(i, j) = (i + 1, j - 1)
	if i >= j:
		return s
	return s[:i] + longest(s[i:j + 1]) + s[j + 1:]
for _ in range(int(input())):
	print(f(input()))


from sys import stdin, stdout, setrecursionlimit
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log, log2
from random import seed, randint
from datetime import datetime
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
from collections import defaultdict as dd
mod = pow(10, 9) + 7
mod2 = 998244353

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

def iinp():
	return int(inp())

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def smp():
	return map(str, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def remadd(x, y):
	return 1 if x % y else 0

def ceil(a, b):
	return (a + b - 1) // b

def def_value():
	return 0

def def_inf():
	return inf

def atoi(s):
	return ord(s) - ord('a')

def inc(s, n=1):
	return chr((ord(s) - ord('a') + n) % 26 + ord('a'))

class XY:

	def __init__(self, x, y):
		self.x = x
		self.y = y

def rotate(a, b, c):
	ang = (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y)
	if ang == 0:
		return 0
	else:
		return ang / abs(ang)
for _ in range(iinp()):
	s = inp()
	(n, k) = (len(s), 0)
	for i in range(n // 2):
		if s[i] != s[n - 1 - i]:
			break
		k += 1
	ss = s[k:n - k]
	sss = ss[::-1]
	temp = ss + '?' + sss
	nn = len(temp)
	lps = l1d(nn, 0)
	for i in range(1, nn):
		ln = lps[i - 1]
		while ln > 0 and temp[ln] != temp[i]:
			ln = lps[ln - 1]
		if temp[i] == temp[ln]:
			ln += 1
		lps[i] = ln
	pre = temp[0:lps[nn - 1]]
	temp = sss + '?' + ss
	lps = l1d(nn, 0)
	for i in range(1, nn):
		ln = lps[i - 1]
		while ln > 0 and temp[ln] != temp[i]:
			ln = lps[ln - 1]
		if temp[i] == temp[ln]:
			ln += 1
		lps[i] = ln
	suf = temp[0:lps[nn - 1]]
	if len(suf) > len(pre):
		print(s[:k] + suf + s[n - k:])
	else:
		print(s[:k] + pre + s[n - k:])


t = int(input())

def chk(ogi, st, start, ending):
	if start == 0 or ending == len(s_o):
		if len(st) > len(ogi):
			return st
	return ogi
for ti in range(t):
	s_o = input()
	if len(s_o) == 1:
		print(s_o)
		continue
	revl = 0
	for (j, jitm) in enumerate(s_o):
		if jitm == s_o[-j - 1] and j < len(s_o) // 2:
			revl += 1
		else:
			break
	revs = s_o[:revl]
	ans = ''
	if revl:
		s_o = s_o[revl:-revl]
	s = '^#'
	for i in s_o:
		s += i + '#'
	s += '$'
	_max = -1
	_len = len(s)
	p = [0 for i in range(_len)]
	mx = 0
	_id = 0
	for (idx, i) in enumerate(s):
		if idx < mx:
			p[idx] = min(p[2 * _id - idx], mx - idx)
		else:
			p[idx] = 1
		while idx + p[idx] < _len and s[idx - p[idx]] == s[idx + p[idx]]:
			p[idx] += 1
		if mx < idx + p[idx]:
			_id = idx
			mx = idx + p[idx]
		_tmp = p[idx] - 1
		if p[idx] - 1 & 1:
			r1 = (idx >> 1) - (_tmp >> 1) - 1
			r2 = (idx >> 1) + (_tmp >> 1)
		else:
			r1 = (idx >> 1) - (_tmp >> 1)
			r2 = (idx >> 1) + (_tmp >> 1)
		if idx > 0 and i != '$':
			ans = chk(ans, s[idx - _tmp:1 + idx + _tmp].replace('#', ''), r1, r2)
	print(revs + ans + revs[::-1])


import sys
from collections import Counter

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

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

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

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 998244353
for _ in range(INT()):
	S = input()
	N = len(S)
	L = []
	i = 0
	j = N - 1
	while i < j:
		if S[i] == S[j]:
			L.append(S[i])
			i += 1
			j -= 1
		else:
			break
	else:
		print(S)
		continue
	R = L[::-1]
	k = j
	mx = 0
	L2 = []
	while i < k:
		tmp = []
		i2 = i
		k2 = k
		while i2 < k2:
			if S[i2] == S[k2]:
				tmp.append(S[i2])
				i2 += 1
				k2 -= 1
			else:
				break
		else:
			tmp2 = tmp[::-1]
			if i2 == k2:
				tmp.append(S[i2])
			tmp += tmp2
			if len(tmp) > mx:
				mx = len(tmp)
				L2 = tmp
		k -= 1
	if not L2:
		L2.append(S[i])
	k = i
	mx = 0
	R2 = []
	while k < j:
		tmp = []
		k2 = k
		j2 = j
		while k2 < j2:
			if S[k2] == S[j2]:
				tmp.append(S[k2])
				k2 += 1
				j2 -= 1
			else:
				break
		else:
			tmp2 = tmp[::-1]
			if k2 == j2:
				tmp.append(S[j2])
			tmp += tmp2
			if len(tmp) > mx:
				mx = len(tmp)
				R2 = tmp
		k += 1
	if not R2:
		R2.append(S[j])
	if len(L2) > len(R2):
		ans = L + L2 + R
	else:
		ans = L + R2 + R
	print(''.join(ans))


def kmp(sub_t):
	s = sub_t + '#' + sub_t[::-1]
	c = 0
	n = len(s)
	pref = [0] * n
	for i in range(1, n):
		while c != 0 and s[c] != s[i]:
			c = pref[c - 1]
		if s[c] == s[i]:
			c += 1
		pref[i] = c
	return sub_t[0:c]

def arg_max(a, b):
	if len(b) > len(a):
		return b
	return a

def solve(t):
	l = 0
	n = len(t)
	while l < n - l - 1:
		if t[l] != t[n - l - 1]:
			break
		l += 1
	a = ''
	if n > 2 * l:
		sub_t = t[l:n - l]
		a = kmp(sub_t)
		sub_t = sub_t[::-1]
		b = kmp(sub_t)
		a = arg_max(a, b)
	print(t[:l] + a + t[n - l:])
te = int(input())
for _ in range(te):
	t = str(input())
	solve(t)


import sys
from math import sqrt
inp = sys.stdin.readline

def a():
	ans = ''
	for _ in range(int(inp())):
		n = int(inp())
		s = ''
		if n == 1:
			s = '-1'
		else:
			s = '34'
			n -= 2
			if n > 0:
				s += '3' * n
		ans += s + '\n'
	print(ans)

def b():
	n = int(inp())
	arr = list(map(int, inp().split()))
	arr2 = [arr[0]]
	maxi = arr[0]
	for i in range(1, n):
		arr2.append(maxi + arr[i])
		maxi = max(maxi, arr2[i])
	print(*arr2)

def isPalindrome(s):
	for i in range(0, len(s) // 2):
		if s[i] != s[len(s) - i - 1]:
			return False
	return True

def max_pal(s):
	n = len(s)
	maxi = ''
	k = 0
	while k < n + 1:
		if isPalindrome(s[:k]) and k > len(maxi):
			maxi = s[:k]
		k += 1
	k = 0
	s = s[::-1]
	while k < n + 1:
		if isPalindrome(s[:k]) and k > len(maxi):
			maxi = s[:k]
		k += 1
	return maxi

def max_pal2(s, rev=0):
	n = len(s)
	if rev:
		s = s[::-1]
	res = ''
	i = 0
	j = n - 1
	while s[i] == s[j] and i < j:
		i += 1
		j -= 1
	(p1, p2) = (s[:i], s[j + 1:])
	if p1 and p2:
		res = p1 + max_pal(s[i:j + 1]) + p2
	else:
		res = max_pal(s[i:j + 1])
	return res

def c():
	ans = ''
	for _ in range(int(inp())):
		s = inp().strip()
		res = max_pal2(s)
		if res:
			ans += res + '\n'
		else:
			ans += s[0] + '\n'
	print(ans)
c()


t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	(l, r) = (0, n - 1)
	for i in range(n):
		if i < n - 1 - i and s[i] == s[n - 1 - i]:
			(l, r) = (i + 1, n - 1 - (i + 1))
			continue
		break
	(pref, s, post) = (s[:l], s[l:r + 1], s[r + 1:])
	if not s:
		print(pref + post)
	else:
		n = len(s)
		d1 = [0] * n
		(l, r) = (0, -1)
		m = 0
		best = (0, 0)
		for i in range(n):
			k = 1 if i > r else min(d1[l + r - i], r - i + 1)
			while i + k < n and i - k >= 0 and (s[i + k] == s[i - k]):
				k += 1
			d1[i] = k
			if i + k - 1 > r:
				(l, r) = (i - k + 1, i + k - 1)
			w = 1 + (k - 1) * 2
			if m < w:
				if k - 1 == i:
					m = w
					best = (0, i + k)
				elif n - k == i:
					m = w
					best = (i - (k - 1), n)
		d2 = [0] * n
		(l, r) = (0, -1)
		for i in range(n):
			k = 0 if i > r else min(d2[l + r - i + 1], r - i + 1)
			while i + k < n and i - k - 1 >= 0 and (s[i + k] == s[i - k - 1]):
				k += 1
			d2[i] = k
			if i + k - 1 > r:
				(l, r) = (i - k, i + k - 1)
			w = k * 2
			if m < w:
				if k == i:
					m = w
					best = (0, i + k)
				elif n - k == i:
					m = w
					best = (i - k, n)
		print(pref + s[best[0]:best[1]] + post)


from sys import stdin

def ispal(s):
	if s == s[::-1]:
		return 1
	return 0

def func(s):
	n = len(s)
	for i in range(n, 0, -1):
		if ispal(s[0:i]) == 1:
			return i
for _ in range(int(stdin.readline())):
	s = stdin.readline().strip()
	m = s[::-1]
	if s == m:
		print(s)
		continue
	n = len(s)
	(i, j) = (0, n - 1)
	while j - 1 > 1:
		if s[i] == s[j]:
			i += 1
			j -= 1
		else:
			break
	pre = s[0:i]
	suf = s[j + 1:]
	s = s[i:j + 1]
	m = s[::-1]
	x = func(s)
	y = func(m)
	if x >= y:
		pre += s[0:x]
	else:
		suf = m[0:y] + suf
	print(pre + suf)


for i in range(int(input())):
	s = str(input())
	n = len(s)

	def reverse(s):
		return s[::-1]

	def isPalindrome(s):
		rev = reverse(s)
		if s == rev:
			return True
		return False
	length = 0
	string = s
	front = s[:int(n / 2) + 1]
	back = s[int(n / 2):]
	if isPalindrome(s) == 1:
		print(s)
	else:
		for j in range(int(n / 2) + 1):
			if s[j] == s[n - j - 1]:
				length += 1
			else:
				front = s[:length]
				back = s[n - length:]
				break
		s1 = ''
		for j in range(n - len(front) - len(back)):
			if isPalindrome(s[j + len(front):n - len(back)]) == 1:
				s1 = s[j + len(front):n - len(back)]
				break
			elif isPalindrome(s[len(front):n - len(back) - j - 1]) == 1:
				s1 = s[len(front):n - len(back) - j - 1]
				break
		print(front + s1 + back)


import sys
for _ in range(int(input())):
	s = input()
	(p, a) = ('', '')
	(i, j) = (0, len(s) - 1)
	while i <= j and s[i] == s[j]:
		p += s[i]
		if i < j:
			a = s[j] + a
			j -= 1
		i += 1
	s = s[i:j + 1]

	def manacher(ms):
		ms = '$#' + '#'.join(ms) + '#%'
		l = len(ms)
		p = [0] * len(ms)
		ind = mx = 0
		for i in range(1, l - 1):
			if i < mx:
				p[i] = min(p[2 * ind - i], mx - i)
			while ms[i - p[i] - 1] == ms[i + p[i] + 1]:
				p[i] += 1
			if mx < i + p[i]:
				(ind, mx) = (i, i + p[i])
		return p
	t = manacher(s)
	ans = ''
	for i in range(1, len(t)):
		if i - t[i] == 1 and t[i] > len(ans):
			ans = s[:t[i]]
		if i + t[i] == len(t) - 2 and t[i] > len(ans):
			ans = s[-t[i]:]
	print(p + ans + a)


def solve_palindrome(pat):
	pat += '#' + pat[::-1]
	lps = [0] * len(pat)
	length = 0
	i = 1
	while i < len(pat):
		if pat[i] == pat[length]:
			length += 1
			lps[i] = length
			i += 1
		elif length != 0:
			length = lps[length - 1]
		else:
			lps[i] = 0
			i += 1
	return pat[0:lps[len(pat) - 1]]

def solve():
	a = input()
	b = a[::-1]
	j = 0
	while a[j] == b[j] and j * 2 < len(a) - 1:
		j += 1
	c = a[:j]
	b = b[j:len(b) - j]
	a = a[j:len(a) - j]
	aa = solve_palindrome(a)
	bb = solve_palindrome(b)
	if len(aa) < len(bb):
		aa = bb
	print(c + aa + c[::-1])
n = int(input())
for i in range(n):
	solve()


t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	if s == s[::-1]:
		print(s)
		continue
	I = 0
	for i in range(n):
		if s[i] != s[n - i - 1]:
			I = i
			break
	pre = ''
	suf = ''
	flag = 1
	for i in range(I, n - I):
		S = s[I:i]
		P = s[i:n - I]
		if S == S[::-1]:
			pre = S
		if P == P[::-1] and flag:
			suf = P
			flag = 0
	if len(suf) >= len(pre):
		ans = suf
	else:
		ans = pre
	res = s[:I] + ans + s[n - I:]
	print(res)


def isPalindrome(str):
	for i in range(0, len(str) // 2):
		if str[i] != str[len(str) - i - 1]:
			return False
	return True
t = int(input())
while t:
	s = input()
	i = 0
	j = len(s) - 1
	while i < j and s[i] == s[j]:
		i += 1
		j -= 1
	temp = s[i:j + 1]
	for k in range(len(temp)):
		if isPalindrome(temp):
			break
		temp = temp[0:len(temp) - 1]
	temp1 = s[i:j + 1][::-1]
	for k in range(len(temp1)):
		if isPalindrome(temp1):
			break
		temp1 = temp1[0:len(temp1) - 1]
	if len(temp) >= len(temp1):
		print(s[0:i] + temp + s[j + 1:])
	else:
		print(s[0:i] + temp1[::-1] + s[j + 1:])
	t -= 1


import sys
sys.setrecursionlimit(2000000)
T = int(input())

def is_palindrom(s, left, right):
	if left == right:
		return True
	if left > right:
		return True
	if s[left] != s[right]:
		return False
	return is_palindrom(s, left + 1, right - 1)
for t in range(T):
	s = input()
	left = 0
	right = len(s) - 1
	len_common = 0
	special = False
	while left <= right:
		if left == right:
			break
		if s[left] != s[right]:
			break
		else:
			left += 1
			right -= 1
			len_common += 1
	if left == right:
		print(s)
		continue
	a = s[left:right + 1]
	first = 0
	second = 0
	left = 0
	right = len(a) - 1
	while not is_palindrom(a, left, right):
		right -= 1
	first = a[left:right + 1]
	right = len(a) - 1
	left = 0
	while not is_palindrom(a, left, right):
		left += 1
	second = a[left:right + 1]
	if len(first) < len(second):
		a = second
	else:
		a = first
	res = s[0:len_common] + a + s[len(s) - len_common:]
	print(res)


from sys import exit

def i():
	return input()

def ii():
	return int(input())

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

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

def print_array(a):
	print(' '.join(map(str, a)))

def get_mx_pl(s):
	s += ' '
	for i in range(1, len(s)):
		s2 = s[:-i]
		if s2 == s2[::-1]:
			return s2
	return ''

def get_mx_pl_r(s):
	for i in range(len(s)):
		s2 = s[i:]
		if s2 == s2[::-1]:
			return s2
	return ''
t = ii()
for _ in range(t):
	s = input()
	(pre, suf) = ('', '')
	i = 0
	while i < len(s) // 2 and s[i] == s[len(s) - 1 - i]:
		pre += s[i]
		suf += s[len(s) - 1 - i]
		i += 1
	pre2 = get_mx_pl(s[i:len(s) - len(suf)])
	suf2 = get_mx_pl_r(s[len(pre):len(s) - len(suf)])
	if len(pre2) >= len(suf2):
		pre += pre2
	else:
		suf += suf2
	print(pre + suf[::-1])


from sys import stdin

def kmp_table(pat):
	(m, fail, k) = (len(pat), [0] * len(pat), 0)
	for i in range(1, m):
		while k and pat[k] != pat[i]:
			k = fail[k - 1]
		if pat[k] == pat[i]:
			fail[i] = k + 1
			k += 1
		else:
			fail[i] = k
	return fail
n = int(stdin.readline())
(arr, ans) = ([input() for i in range(n)], [])
for s in arr:
	(be, en) = (-1, len(s))
	if s == s[::-1]:
		ans.append(s)
	else:
		for j in range(len(s)):
			if s[j] == s[-1 - j]:
				be += 1
				en -= 1
			else:
				break
		s1 = s[be + 1:en] + '#' + s[be + 1:en][::-1]
		s2 = s[be + 1:en][::-1] + '#' + s[be + 1:en]
		(ma1, ma2) = (s[be + 1:be + 1 + kmp_table(s1)[-1]], s[en - kmp_table(s2)[-1]:en])
		if len(ma1) >= len(ma2):
			ans.append(s[:be + 1] + ma1 + s[en:])
		else:
			ans.append(s[:be + 1] + ma2 + s[en:])
print('\n'.join(ans))


import sys
LI = lambda : list(map(int, sys.stdin.readline().strip('\n').split()))
MI = lambda : map(int, sys.stdin.readline().strip('\n').split())
SI = lambda : sys.stdin.readline().strip('\n')
II = lambda : int(sys.stdin.readline().strip('\n'))
for _ in range(II()):
	_s = list(SI())
	(t, ans, l, r) = (-1, '', 0, len(_s) - 1)
	while l < r and _s[l] == _s[r]:
		t += 1
		l += 1
		r -= 1
	if r - l >= 0:
		n = (r - l + 1) * 2 + 1
		s = ['#'] * n
		for i in range(n):
			if i & 1:
				s[i] = _s[l]
				l += 1
		(lsp, c, r, lindv, rindv) = ([0] * n, 0, 0, 0, 0)
		for i in range(n):
			mirror = 2 * c - i
			if i < r:
				lsp[i] = min(r - i, lsp[mirror])
			(a, b) = (i + 1 + lsp[i], i - 1 - lsp[i])
			while a < n and b >= 0 and (s[a] == s[b]):
				lsp[i] += 1
				a += 1
				b -= 1
			if i + lsp[i] > r:
				(c, r) = (i, i + lsp[i])
			if i - lsp[i] <= 0 and lsp[i] > lindv:
				lind = i
				lindv = lsp[i]
			if i + lsp[i] >= n - 1 and lsp[i] > rindv:
				rind = i
				rindv = lsp[i]
		if lsp[lind] > lsp[rind]:
			ind = lind
		else:
			ind = rind
		ans = ''.join(list(filter(lambda x: x != '#', s[ind - lsp[ind]:ind + 1] + s[ind + 1:ind + lsp[ind] + 1])))
	at = ''.join(_s[:t + 1])
	print(at + ans + at[::-1])


t = int(input())
for i in range(t):
	s = input()
	j = 0
	n = len(s)
	if s == s[::-1]:
		print(s)
	else:
		while s[j] == s[n - j - 1]:
			j += 1
		simv = s[j]
		c = ''
		for k in range(n - j - 1, j - 1, -1):
			if simv == s[k]:
				gh = s[j:k + 1]
				hg = gh[::-1]
				if hg == gh:
					c = s[j:k + 1]
					break
		simv = s[n - j - 1]
		length = len(c)
		for k in range(j, n - j, 1):
			if n - j - k < length:
				break
			elif simv == s[k]:
				gh = s[k:n - j]
				hg = gh[::-1]
				if hg == gh:
					c = s[k:n - j]
					break
		print(s[:j] + c + s[n - j:n])


for _ in range(int(input())):
	s = input()
	res1 = ''
	res2 = ''
	i = 0
	j = len(s) - 1
	cur1 = ''
	cur2 = ''
	while s[i] == s[j] and i < j:
		res1 += s[i]
		res2 = s[j] + res2
		i += 1
		j -= 1
	if i == j:
		res1 += s[i]
	else:
		for x in range(i, j):
			temp = s[i:x + 1]
			if temp == temp[::-1]:
				cur1 = temp
		for x in range(j, i, -1):
			temp = s[x:j + 1]
			if temp == temp[::-1]:
				cur2 = temp
		if len(cur1) >= len(cur2):
			res1 += cur1
		else:
			res2 = cur2 + res2
	if res1 == '' and res2 == '':
		print(s[0])
	else:
		print(res1 + res2)


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))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = int(input())

def pre(s):
	ln = len(s)
	for j in range(ln - 1, -1, -1):
		if s[:j + 1] == s[j::-1]:
			return s[:j + 1]
	return s[0]

def suf(s):
	s = s[::-1]
	ln = len(s)
	for j in range(ln - 1, -1, -1):
		if s[:j + 1] == s[j::-1]:
			return s[:j + 1]
	return s[0]
for i in range(t):
	s = input()
	x = len(s)
	s1 = ''
	if s == s[::-1]:
		print(s)
	else:
		for j in range(x):
			if s[j] != s[x - 1 - j]:
				s1 = s[:j]
				break
		s2 = s[j:x - j]
		sa = pre(s2)
		sb = suf(s2)
		if len(sa) > len(sb):
			s3 = s1 + sa + s1[::-1]
		else:
			s3 = s1 + sb + s1[::-1]
		sc = suf(s)
		sd = pre(s)
		if len(sc) >= len(sd):
			if len(sc) > len(s3):
				print(sc)
			else:
				print(s3)
		elif len(sd) > len(s3):
			print(sd)
		else:
			print(s3)


def manacher(S):
	(i, j, n) = (0, 0, len(S))
	R = [0] * n
	while i < n:
		while i - j >= 0 and i + j < n and (S[i - j] == S[i + j]):
			j += 1
		R[i] = j
		k = 1
		while i - k >= 0 and i + k < n and (k + R[i - k] < j):
			R[i + k] = R[i - k]
			k += 1
		i += k
		j -= k
	return R
T = int(input())
for _ in range(T):
	s = input()
	N = len(s)
	idx_s = 0
	for idx_s in range(N // 2):
		if s[idx_s] != s[-1 - idx_s]:
			break
	else:
		print(s)
		continue
	pre = idx_s
	s2 = [s[idx_s]]
	for c in s[idx_s + 1:-idx_s] if idx_s != 0 else s[1:]:
		s2.append('$')
		s2.append(c)
	man = manacher(s2)
	ma_l = 1
	for (idx_man, m) in enumerate(man, 1):
		if m == idx_man:
			ma_l = m
	ma_r = 1
	for (idx_man, m) in enumerate(man[::-1], 1):
		if m == idx_man:
			ma_r = m
	ans = s[:pre] + (s[pre:pre + ma_l] if ma_l > ma_r else s[-pre - ma_r:N - pre]) + (s[-pre:] if pre > 0 else '')
	print(ans)


def lps(s):
	n = len(s)
	lps = [0] * n
	l = 0
	i = 1
	while i < n:
		if s[i] == s[l]:
			l = l + 1
			lps[i] = l
			i = i + 1
		elif l != 0:
			l = lps[l - 1]
		else:
			lps[i] = 0
			i = i + 1
	return lps[n - 1]

def start(st):
	n = lps(st + ['*'] + st[::-1])
	return st[:n]

def end(st):
	temp = start(st[::-1])
	return temp[::-1]

def rep(ar):
	l = len(ar)
	p = 0
	while p <= l - 1 and ar[p] == ar[0]:
		p += 1
	return p
t = int(input())
for j in range(t):
	st = input()
	ast = list(st)
	l = len(ast)
	i = 0
	while i < l // 2:
		if ast[i] != ast[l - i - 1]:
			break
		i += 1
	a1 = ast[:i] + start(ast[i:l - i]) + ast[l - i:]
	a2 = ast[:i] + end(ast[i:l - i]) + ast[l - i:]
	if len(a1) >= len(a2):
		print(''.join(a1))
	else:
		print(''.join(a2))


def ispalin(s):
	ans = True
	for i in range(len(s)):
		if s[i] != s[len(s) - 1 - i]:
			ans = False
			break
		if i > len(s) // 2:
			break
	return ans
for _ in range(int(input())):
	s = input()
	k = ''
	l = ''
	for j in range(len(s)):
		if s[j] == s[len(s) - 1 - j]:
			k += s[j]
			l = s[j] + l
		else:
			break
	if j != len(s) - 1:
		t = ''
		y = ''
		for r in range(j, len(s) - j):
			t += s[r]
			if ispalin(t):
				y = t
		q = ''
		v = ''
		for r in range(len(s) - j - 1, j - 1, -1):
			q = s[r] + q
			if ispalin(q):
				v = q
		if len(v) > len(y):
			print(k + v + l)
		else:
			print(k + y + l)
	else:
		print(s)


import sys
import random
from math import log2, log, ceil
input = sys.stdin.readline

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a > 0:
		return gcd(b % a, a)
	return b

def primefs(n):
	if n == 1:
		return 1
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	while x != link[x]:
		x = link[x]
	return x

def union(x, y, size, link):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]
spf_sieve()

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

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

def isPal(s):
	if s == s[::-1]:
		return True
	else:
		return False
for _ in range(int(input())):
	s = input().strip()
	ans = ''
	n = len(s)
	for i in range(n // 2):
		if s[i] != s[n - i - 1]:
			break
		ans += s[i]
	l = len(ans)
	r = n - 1 - len(ans)
	pal = ''
	si = ''
	for i in range(l, r + 1):
		si += s[i]
		if isPal(si):
			pal = si
	si = ''
	for i in range(r, l - 1, -1):
		si += s[i]
		if len(si) > len(pal):
			if isPal(si):
				pal = si[::-1]
	ans = ans + pal + ans[::-1]
	print(ans)


def palindrome(s, start, end):
	i = 0
	while start + i < end - i and s[start + i] == s[end - i]:
		i += 1
	if s[start + i] == s[end - i]:
		return True
	else:
		return False

def prefix(s, l):
	ans = ''
	i = 0
	while s[i] == s[l - i - 1] and i < l - i - 1:
		i += 1
	ans = s[:i]
	start = i
	end = l - i - 1
	x = end
	m = ''
	n = ''
	while x >= start:
		if palindrome(s, start, x):
			m = s[start:x + 1]
			break
		x -= 1
	x = start
	while x <= end:
		if palindrome(s, x, end):
			n = s[x:end + 1]
			break
		x += 1
	l1 = len(m)
	l2 = len(n)
	if l1 >= l2:
		ans += m
	else:
		ans += n
	ans += s[end + 1:]
	return ans
for _ in range(int(input())):
	s = input()
	l = len(s)
	print(prefix(s, l))


for _ in range(int(input())):
	s = input()
	n = len(s)
	i = 0
	a = ''
	b = ''
	while s[i] == s[n - 1 - i] and i < n - i - 1:
		a += s[i]
		b = s[i] + b
		i += 1
	ans1 = a + b
	ans2 = a + b
	c = a
	for k in range(i, n - i):
		a += s[k]
		string = a + b
		if string == string[::-1]:
			ans1 = string
	for k in range(n - i - 1, i - 1, -1):
		b = s[k] + b
		string = c + b
		if string == string[::-1]:
			ans2 = string
	if len(ans1) > len(ans2):
		print(ans1)
	else:
		print(ans2)


def chunk(s):
	n = len(s)
	if n == 1:
		return ('', s, '')
	for i in range(n // 2):
		if s[i] != s[-(i + 1)]:
			break
	return (s[:i], s[i:n - i], s[n - i:])

def argmax(p):
	m = max(p)
	for (i, v) in enumerate(p):
		if v == m:
			return i

def argmaxfront(p):
	m = max([v for (i, v) in enumerate(p) if v == i - 1])
	for (i, v) in enumerate(p):
		if v == m:
			return i

def mana(s):
	s = '^#' + '#'.join(list(s)) + '#$'
	p = manap(s)
	i = argmax(p)
	return s[i - p[i]:i + p[i] + 1].replace('#', '')

def manastart(s):
	s = '^#' + '#'.join(list(s)) + '#$'
	p = manap(s)
	i = argmaxfront(p)
	return s[i - p[i]:i + p[i] + 1].replace('#', '')

def manap(s):
	p = [0] * len(s)
	c = 0
	r = 0
	for i in range(1, len(s) - 1):
		if i < r:
			p[i] = min(p[2 * c - i], r - i)
		while s[i + 1 + p[i]] == s[i - 1 - p[i]]:
			p[i] += 1
		if i + p[i] > r:
			c = i
			r = i + p[i]
	return p

def longest(s):
	n = len(s)
	if n == 0:
		return s
	possible = [True] * (n + 1)
	if n > 50:
		c0 = s[0]
		nodd = 0
		odds = {}
		for (i, c) in enumerate(s):
			if c in odds:
				nodd -= 1
				del odds[c]
			else:
				nodd += 1
				odds[c] = 1
			ii = i + 1
			if c != c0:
				possible[ii] = False
			elif ii % 2 == 0:
				if nodd > 0:
					possible[ii] = False
			elif nodd > 1:
				possible[ii] = False
			else:
				for d in odds:
					if s[i // 2] != d:
						possible[ii] = False

	def ispal0(j):
		for i in range(j // 2):
			if s[i] != s[j - i - 1]:
				return False
		return True
	for j in range(n, 0, -1):
		if possible[j]:
			if ispal0(j):
				return s[:j]

def solve(s):
	(pre, mid, suf) = chunk(s)
	m1 = manastart(mid)
	m2 = manastart(mid[::-1])
	m = m1 if len(m1) >= len(m2) else m2
	return pre + m + suf
t = int(input().strip())
for _ in range(t):
	s = input().strip()
	print(solve(s))


def isPalindrome(str):
	for i in range(0, len(str) // 2):
		if str[i] != str[len(str) - i - 1]:
			return False
	return True
t = int(input())
for i in range(t):
	s = input()
	if len(s) == 1:
		print(s)
	else:
		t = ''
		a = ''
		b = ''
		i = 0
		j = len(s) - 1
		while i < j:
			if s[i] == s[j]:
				a += s[i]
				b = s[j] + b
				i += 1
				j -= 1
			else:
				break
		t1 = ''
		t2 = ''
		for x in range(j, i - 1, -1):
			ans1 = isPalindrome(s[i:x + 1])
			if ans1:
				t1 = s[i:x + 1]
				break
		for x in range(i, j + 1):
			ans1 = isPalindrome(s[x:j + 1])
			if ans1:
				t2 = s[x:j + 1]
				break
		n1 = len(t1)
		n2 = len(t2)
		if n1 >= n2:
			t = a + t1 + b
		else:
			t = a + t2 + b
		print(t)


def palin(s):
	n = len(s)
	for i in range(n - 1, 0, -1):
		if s[:i] == s[:i][::-1]:
			return s[:i]
		if s[-i:] == s[-i:][::-1]:
			return s[-i:]
	return s[0]
for _ in range(int(input())):
	old = input()
	n = len(old)
	if n == 1:
		print(old)
		continue
	k = 0
	for i in range(n // 2):
		if old[i] != old[n - 1 - i]:
			break
		k += 1
	if 2 * k == n:
		print(old)
	elif k == 0:
		print(palin(old))
	else:
		print(old[:k] + palin(old[k:-k]) + old[-k:])


import sys

def my_rev(s):
	s = s[::-1]
	return s

def ispalindrome(s, p, i, x):
	if p == 0:
		length = i + 1
		for g in range(length // 2):
			if s[g] != s[i - g]:
				return -2
		return length
	else:
		length = n - i
		for h in range(length // 2):
			if s[i + h] != s[x - 1 - h]:
				return -2
		return length

def sub_ispalindrome(s, p, i, x, ind):
	if p == 0:
		length = i - ind + 1
		for g in range(length // 2):
			if s[ind + g] != s[i - g]:
				return -2
		return length
	else:
		length = x - i
		for h in range(length // 2):
			if s[i + h] != s[x - 1 - h]:
				return -2
		return length
t = int(sys.stdin.readline())
ans_arr = []
for i in range(t):
	s = sys.stdin.readline().strip()
	n = len(s)
	a = []
	for i in range(n):
		a.append(s[i])
	max_len = 1
	ans = [0, 0]
	for j in range(n):
		val = ispalindrome(a, 0, j, n)
		if val > max_len:
			ans = [0, j]
			val = max_len
	for k in range(n):
		num = ispalindrome(a, 1, k, n)
		if num > max_len:
			ans = [k, n - 1]
			max_len = num
	repeated = ''
	rl = 0
	ind = -2
	for i in range(len(s) // 2):
		if s[i] != s[n - i - 1]:
			ind = i
			break
		else:
			repeated += s[i]
			rl += 2
	if ind == -2:
		ans_arr.append(s)
	else:
		ml = 1
		tmp = [ind, ind]
		for g in range(ind, n - ind):
			v1 = sub_ispalindrome(a, 0, g, n - ind, ind)
			if v1 > ml:
				tmp = [ind, g]
				ml = v1
		for h in range(ind, n - ind):
			v2 = sub_ispalindrome(a, 1, h, n - ind, ind)
			if v2 > ml:
				tmp = [h, n - ind - 1]
				ml = v2
		if max_len >= rl + ml:
			ans_arr.append(s[ans[0]:ans[1] + 1])
		else:
			ans_arr.append(repeated + s[tmp[0]:tmp[1] + 1] + my_rev(repeated))
print('\n'.join(ans_arr))


t = int(input())

def prefix(s):
	s = s + '#' + s[::-1]
	n = len(s)
	l = [0] * n
	i = 0
	j = 1
	while j < n:
		if i == 0 or s[i] == s[j]:
			l[j] = i + 1
			i += 1
			j += 1
		else:
			i = l[i - 1]
	return l[n - 1]
for _ in range(t):
	s = input()
	n = len(s)
	i = 0
	for i in range(n // 2 + 1):
		if s[i] != s[n - i - 1]:
			break
	ans = i
	a1 = prefix(s[ans:n - ans])
	a2 = prefix(s[ans:n - ans][::-1])
	if a1 >= a2:
		mid = s[ans:ans + a1]
	else:
		mid = s[n - ans - a2:n - ans]
	print(s[:ans] + mid + s[n - ans:])


t = input()
t = int(t)
for i in range(t):
	s = input()
	s = str(s)
	if s == s[::-1]:
		print(s)
		continue
	if len(s) == 1:
		print(s)
		continue
	else:
		ml = 0
		ans1 = ''
		ans2 = ''
		low = 0
		high = len(s) - 1
		while low < high and s[low] == s[high]:
			low += 1
			high -= 1
		for l in range(high, low, -1):
			t = s[low:l]
			t2 = t[::-1]
			if t == t2:
				ans1 = s[:low] + t + s[high + 1:]
				break
		for h in range(low, high, 1):
			t = s[h:high + 1]
			t2 = t[::-1]
			if t == t2:
				ans2 = s[:low] + t + s[high + 1:]
				break
		if len(ans1) > len(ans2):
			print(ans1)
		else:
			print(ans2)


from functools import lru_cache
from collections import *
t = int(input())
while t:
	t -= 1
	s = input()
	res = ''
	(i, eidx) = (0, len(s) - 1)
	while i < eidx:
		if s[i] == s[eidx]:
			i += 1
			eidx -= 1
		else:
			break
	r = ''
	for j in range(i, eidx + 1):
		s1 = s[i:j + 1]
		if s1 == s1[::-1] and len(s1) > len(r):
			r = s1
		s2 = s[eidx - (j - i):eidx + 1]
		if s2 == s2[::-1] and len(s2) > len(r):
			r = s2
	print(s[:i] + r + s[eidx + 1:])


def isPalindrome(string):
	return string == string[::-1]

def longestPrefixPalindrome(string):
	end = len(string) - 1
	while end >= 0:
		if string[0] == string[end] and isPalindrome(string[:end + 1]):
			return string[:end + 1]
		else:
			end -= 1
t = int(input())
for _ in range(t):
	string = input()
	output_prefix = ''
	start = 0
	end = len(string) - 1
	while string[start] == string[end] and start < end:
		output_prefix += string[start]
		start += 1
		end -= 1
	if start <= end:
		cut = len(output_prefix)
		newstring = string[cut:len(string) - cut]
		output_middle = max(longestPrefixPalindrome(newstring), longestPrefixPalindrome(newstring[::-1]), key=len)
		print(output_prefix + output_middle + output_prefix[::-1])
	else:
		print(output_prefix + output_prefix[::-1])


def longest_prefix(s):
	kmps = s + '*' + s[::-1]
	lps = [0]
	for i in range(1, len(kmps)):
		prev = lps[i - 1]
		while prev > 0 and kmps[prev] != kmps[i]:
			prev = lps[prev - 1]
		lps.append(prev + 1 if kmps[prev] == kmps[i] else 0)
	return lps[-1]
for _ in range(int(input())):
	ss = input()
	s = 0
	e = len(ss) - 1
	i = 0
	while s < e and ss[s] == ss[e]:
		s += 1
		e -= 1
	if s == e:
		s += 1
	pref = longest_prefix(ss[s:e + 1])
	suff = longest_prefix(ss[s:e + 1][::-1])
	if pref >= suff:
		s += pref
	else:
		e -= suff
	print(ss[:s] + ss[e + 1:])


for _ in range(int(input())):
	s = input()
	ans = [1, s[0]]
	(l, r) = (0, len(s) - 1)
	while l < r:
		if s[l] == s[r]:
			l += 1
			r -= 1
		else:
			hehe = r
			while hehe != l - 1:
				cay = s[:hehe] + s[r + 1:]
				if cay == cay[::-1]:
					ans = [len(cay), cay]
					break
				hehe -= 1
			hehe = l + 1
			while hehe != r + 1:
				cay = s[:l] + s[hehe:]
				if len(cay) > ans[0] and cay == cay[::-1]:
					ans = [len(cay), cay]
					break
				hehe += 1
			break
	if r <= l:
		print(s)
	else:
		print(ans[1])


import time

def solve():
	s = input()
	n = len(s)
	istart = -1
	for i in range(n // 2):
		if s[i] != s[n - i - 1]:
			istart = i
			break
	if istart == -1:
		print(s)
		return 0
	best = 0
	for i in range(istart, n - istart):
		if s[i:n - istart] == s[i:n - istart][::-1]:
			best = n - istart - i
			break
	for i in range(n - istart - 1, istart - 1, -1):
		if s[istart:i] == s[istart:i][::-1]:
			b2 = i - istart + 1
			if b2 > best:
				print(s[0:i] + s[n - istart:n])
			else:
				print(s[0:istart] + s[n - istart - best:n])
			break
for t in range(int(input())):
	solve()


import sys
import math
get_string = lambda : sys.stdin.readline().strip()
get_list = lambda : list(map(int, sys.stdin.readline().strip().split()))
get_int = lambda : int(sys.stdin.readline())

def isPalindrome(s):
	t = s[::-1]
	if s == t:
		return 1
	else:
		return 0
for _ in range(get_int()):
	s = get_string()
	n = len(s)
	l = 0
	for i in range(n // 2):
		if s[i] != s[n - i - 1]:
			l = i
			break
	ans = s[:l]
	t = s[l:n - l]
	for i in range(len(t))[::-1]:
		temp1 = t[:i]
		if isPalindrome(temp1):
			break
	for i in range(len(t)):
		temp2 = t[i:]
		if isPalindrome(temp2):
			break
	if len(temp1) > len(temp2):
		ans = ans + temp1 + s[n - l:]
	else:
		ans = ans + temp2 + s[n - l:]
	print(ans)


from sys import stdin
inp = stdin.readline

def isP(str):
	strr = str[::-1]
	return strr == str

def prefc(str):
	for i in range(len(str) - 1, 0, -1):
		d = str[0:i]
		if isP(d):
			return len(d)
	return 1

def suffc(str):
	ans = 1
	for i in range(len(str) - 1):
		d = str[i:len(str)]
		if isP(d):
			return len(d)
	return 1
for _ in range(int(input())):
	s = inp().strip()
	pre = 0
	for i in range(len(s) // 2):
		if s[i] == s[len(s) - 1 - i]:
			pre += 1
			continue
		break
	k = s[pre:len(s) - pre]
	w = s[0:pre]
	if suffc(k) >= prefc(k):
		print(s[0:pre] + k[len(k) - suffc(k):] + w[::-1])
	else:
		print(s[0:pre] + k[0:prefc(k)] + w[::-1])


def is_palindrome(s):
	return s == s[::-1]

def longest_palindrome_prefix(s):
	if s == '':
		return s
	curr = ''
	ans = s[0]
	for i in s:
		curr += i
		if is_palindrome(curr):
			ans = curr
	return ans

def solve1():
	n = int(input())
	for _ in range(n):
		s = input()
		b = [s[-i:] for i in range(1, len(s) + 1)]
		b += ['']
		curr = ''
		(ans, mx) = (s[0], 1)
		for i in range(len(s) - 1):
			for j in b:
				if len(curr) + len(j) <= mx or len(curr) + len(j) > len(s):
					continue
				curr_t = curr + j
				if is_palindrome(curr_t):
					(ans, mx) = (curr_t, len(curr_t))
			curr += s[i]
		for j in b:
			if len(curr) + len(j) <= mx or len(curr) + len(j) > len(s):
				continue
			curr_t = curr + j
			if is_palindrome(curr_t):
				(ans, mx) = (curr_t, len(curr_t))
		print(ans)

def solve2():
	t = int(input())
	for _ in range(t):
		s = input()
		(a, b) = ('', '')
		indx = 0
		for i in range(len(s) // 2):
			if s[i] == s[-i - 1]:
				a += s[i]
				b = s[i] + b
				indx = i + 1
			else:
				indx = i
				break
		if indx == 0:
			middl = s
		elif indx > len(s) // 2 - 1:
			middl = '' if len(s) % 2 == 0 else s[len(s) // 2]
		else:
			middl = s[i:-i]
		(s1, s2) = (longest_palindrome_prefix(middl), longest_palindrome_prefix(middl[::-1]))
		res = s1 if len(s1) > len(s2) else s2
		res = a + res + b
		print(res)
solve2()


from sys import stdin
from collections import defaultdict, Counter
from math import gcd

def solver(s):
	n = len(s)
	i = 0
	for i in range(n // 2):
		if s[i] != s[-(i + 1)]:
			break
	if i == 0:
		sub = s
	else:
		sub = s[i:-i]
	l = palindrome_substring(sub)
	return s[:i] + l + s[::-1][:i][::-1]

def palindrome_substring(s):
	rs = s[::-1]
	ans = ''
	for i in range(1, len(s) + 1):
		if palindrome_check(s[:i]):
			ans = s[:i]
		if palindrome_check(rs[:i]):
			ans = rs[:i]
	return ans

def palindrome_check(s):
	n = len(s)
	(s_1, s_2) = (s[:n // 2], s[n // 2 + (n % 2 == 1):])
	if s_1 == s_2[::-1]:
		return True
	else:
		return False
T = int(input())
for _ in range(T):
	s = stdin.readline().strip()
	print(solver(s))


def know_from_start(s):
	end = len(s) - 1
	while end >= 0:
		if s[:end + 1] == s[:end + 1][::-1]:
			return s[:end + 1]
		end -= 1
for t in range(int(input())):
	string = input()
	n = len(string)
	rev_string = string[::-1]
	i = 0
	while i < n and rev_string[i] == string[i]:
		i += 1
	index = i
	if index == n:
		print(string)
		continue
	s1 = string[index:n - index]
	s2 = rev_string[index:n - index]
	x = know_from_start(s1)
	y = know_from_start(s2)
	part = x if len(x) > len(y) else y
	print(string[:index] + part + rev_string[:index][::-1])


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))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def pal(s):
	s = s + '#' + s[::-1]
	n = len(s)
	for res in range(n // 2, 0, -1):
		prefix = s[0:res]
		suffix = s[n - res:n]
		if prefix == suffix:
			return res
	return 0
t = int(input())
for i in range(t):
	s = input()
	n = len(s)
	d = 0
	for j in range(n // 2 - 1):
		if s[j] == s[n - j - 1]:
			d += 1
		else:
			break
	p = s[d:n - d][:pal(s[d:n - d])]
	p1 = s[d:n - d][::-1][:pal(s[d:n - d][::-1])]
	if len(p) > len(p1):
		s = s[:d] + p + s[n - d:]
	else:
		s = s[:d] + p1 + s[n - d:]
	print(s)


t = int(input())
while t > 0:
	t -= 1
	s = input()
	sr = s[::-1]
	mid = len(s) // 2
	line = 0
	for i in range(0, mid):
		if s[i] != sr[i]:
			line = i
			break
	sn = s[line:len(s) - line]
	ans = ''
	for mode in range(2):
		for i in range(len(sn)):
			if sn[0] == sn[len(sn) - 1 - i] and sn[0:len(sn) - i] == sn[0:len(sn) - i][::-1]:
				if len(ans) < len(sn[0:len(sn) - i]):
					ans = sn[0:len(sn) - i]
				break
		sn = sn[::-1]
	print(s[:line] + ans + s[len(s) - line:])


from sys import stdin, gettrace
if not gettrace():

	def input():
		return next(stdin)[:-1]

def longestPalindromes(s):
	T = '#'.join('^{}$'.format(s))
	n = len(T)
	P = [0] * n
	C = R = 0
	for i in range(1, n - 1):
		P[i] = R > i and min(R - i, P[2 * C - i])
		while T[i + 1 + P[i]] == T[i - 1 - P[i]]:
			P[i] += 1
		if i + P[i] > R:
			(C, R) = (i, i + P[i])
	return P

def main():

	def solve():
		ss = input()
		n = len(ss)
		for i in range(n // 2):
			if ss[i] != ss[n - i - 1]:
				break
		else:
			i = n // 2
		if i * 2 == n:
			print(ss)
			return
		pals = longestPalindromes(ss[i:n - i])
		besta = 0
		bestb = 0
		for (j, p) in enumerate(pals):
			if p == j - 1:
				besta = p
		for (j, p) in enumerate(pals[::-1]):
			if p == j - 1:
				bestb = p
		if bestb > besta:
			print(ss[:i] + ss[n - i - bestb:])
		else:
			print(ss[:i + besta] + ss[n - i:])
	q = int(input())
	for _ in range(q):
		solve()
main()


for _ in range(int(input())):
	S = input()
	if S == S[::-1]:
		print(S)
	else:
		i = 0
		j = len(S) - 1
		pre = ''
		while i <= j:
			if S[i] == S[j]:
				pre += S[i]
			else:
				break
			i += 1
			j -= 1
		Extra = S[i:j + 1]
		Ans1 = ''
		for i in range(1, len(Extra) + 1):
			T = Extra[:i]
			if T == T[::-1]:
				if len(T) > len(Ans1):
					Ans1 = T
		Ans1 = pre + Ans1 + pre[::-1]
		Ans2 = ''
		Extra = Extra[::-1]
		for i in range(1, len(Extra) + 1):
			T = Extra[:i]
			if T == T[::-1]:
				if len(T) > len(Ans2):
					Ans2 = T
		Ans2 = pre + Ans2 + pre[::-1]
		Ans3 = ''
		if len(Ans1) >= len(Ans2):
			Ans3 = Ans1
		else:
			Ans3 = Ans2
		print(Ans3)


def check_pali(s1):
	le = len(s1)
	for l in range(le // 2):
		if s1[l] != s1[le - l - 1]:
			return 0
	return 1
t = int(input())
for _ in range(t):
	s = input()
	res = ''
	n = len(s)
	if n == 1:
		print(s)
		continue
	l = 0
	r = n - 1
	for i in range(n // 2):
		if s[i] == s[n - i - 1]:
			l += 1
			r -= 1
			res += s[i]
		else:
			break
	if l - r == 0 or l - r == 1:
		print(s)
		continue
	k1 = s[l]
	for i in range(r - 1, l, -1):
		if s[i] == s[l]:
			if check_pali(s[l:i + 1]):
				k1 = s[l:i + 1]
				break
	k2 = ''
	for i in range(l + 1, r):
		if s[i] == s[r]:
			if check_pali(s[i:r + 1]):
				k2 = s[i:r + 1]
				break
	if len(k1) > len(k2):
		res = res + k1 + res[::-1]
	else:
		res = res + k2 + res[::-1]
		if res == '':
			res = s[0]
	print(res)


import sys
input = sys.stdin.readline

def manacher(S):
	N = len(S)
	(r, p) = (0, 0)
	A = [0] * N
	for i in range(N):
		if i <= r:
			A[i] = min(A[2 * p - i], r - i)
		else:
			A[i] = 0
		while i - A[i] - 1 >= 0 and i + A[i] + 1 < N and (S[i - A[i] - 1] == S[i + A[i] + 1]):
			A[i] += 1
		if r < i + A[i]:
			r = i + A[i]
			p = i
	return A

def f(S):
	return ''.join([i for i in S if i != '#'])
for i in range(int(input())):
	b = input().strip()
	n = len(b)
	if n % 2 == 0:
		c = ''
		v = b[:n // 2]
	else:
		c = b[n // 2]
		v = b[:n // 2]
	for i in range(n // 2):
		if b[i] != b[n - 1 - i]:
			c = b[i:n - i]
			v = b[:i]
			break
	a = '#'.join(c)
	a = '#' + a + '#'
	A = manacher(a)
	ans1 = ''
	for i in range(len(c), 0, -1):
		if i == A[i]:
			ans1 = a[:i * 2]
			break
	ans1 = f(ans1)
	ans2 = ''
	for i in range(len(c), 0, -1):
		if i == A[-1 - i]:
			ans2 = a[-i * 2 - 1:]
			break
	ans2 = f(ans2)
	if len(ans1) > len(ans2):
		ans = ans1
	else:
		ans = ans2
	print(v + ans + v[::-1])


t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	if s == s[::-1]:
		print(s)
		continue
	temp = ''
	i = 0
	flag = 0
	for i in range(n // 2):
		if s[i] == s[n - i - 1]:
			temp = temp + s[i]
		else:
			flag = 1
			break
	s = s[i:n - i]
	if flag == 0:
		s = ''
	if s == '':
		s = temp[::-1]
		print(temp + s)
		continue
	nxt = ''
	bck = ''
	nt = ''
	for j in s:
		nt = nt + j
		if nt == nt[::-1]:
			nxt = nt
	s = s[::-1]
	nt = ''
	for j in s:
		nt = nt + j
		if nt == nt[::-1]:
			bck = nt
	if len(nxt) > len(bck):
		s = temp[::-1]
		print(temp + nxt + s)
	else:
		s = temp[::-1]
		print(temp + bck + s)


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))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def print_list(l):
	print(' '.join(map(str, l)))
for _ in range(N()):
	s = input()
	n = len(s)
	if s == s[::-1]:
		print(s)
	else:
		tail = 0
		while s[tail] == s[n - 1 - tail]:
			tail += 1
		if tail > 0:
			ans = s[:tail] + s[-tail:]
		for m in range(n - 2 >> 1, tail - 1, -1):
			if s[m] == s[m + 1] and s[tail:m][::-1] == s[m + 2:2 * m + 2 - tail]:
				ans = s[:2 * m + 2 - tail] + s[:tail][::-1]
				break
			if s[tail:m][::-1] == s[m + 1:2 * m + 1 - tail]:
				ans = s[:2 * m + 1 - tail] + s[:tail][::-1]
				break
		k = len(ans)
		for m in range(n + 1 >> 1, n - tail):
			if (n - m) * 2 <= k:
				break
			if s[m] == s[m - 1] and s[2 * m - n + tail:m - 1] == s[m + 1:n - tail][::-1]:
				ans = s[:tail] + s[2 * m - n + tail:]
				break
			if s[2 * m - n + tail + 1:m] == s[m + 1:n - tail][::-1]:
				ans = s[:tail] + s[2 * m - n + tail + 1:]
				break
		print(ans)


for _ in range(int(input())):
	s = input()
	n = len(s)
	if n == 1:
		print(s)
		continue
	t1 = ''
	t2 = ''
	i = 0
	j = n - 1
	while i < j and s[i] == s[j]:
		t1 += s[i]
		t2 = s[j] + t2
		i += 1
		j -= 1
	x1 = ''
	j1 = j + 1
	while i < j1 and s[i:j1] != s[i:j1][::-1]:
		j1 -= 1
	x1 = s[i:j1]
	j += 1
	while i < j and s[i:j] != s[i:j][::-1]:
		i += 1
	x2 = s[i:j]
	if len(x1) > len(x2):
		x = x1
	else:
		x = x2
	print(t1 + x + t2)


def reverse(s):
	return s[::-1]

def isPalindrome(s):
	rev = reverse(s)
	if s == rev:
		return True
	return False
T = int(input())
while T:
	inp = str(input())
	if isPalindrome(inp):
		print(inp)
	else:
		l = 0
		r = len(inp) - 1
		ans = ''
		ans_temp = ''
		temp = ''
		max_temp = ''
		while l <= r:
			if inp[l] != inp[r]:
				break
			else:
				ans += inp[l]
				l += 1
				r -= 1
		t = r
		ans_temp = ans
		while t >= l:
			if inp[t] == inp[l] and isPalindrome(inp[l:t + 1]) == 1:
				temp = inp[l:t + 1]
				if len(max_temp) < len(temp):
					max_temp = temp
			t -= 1
		t = l
		while t <= r:
			if inp[t] == inp[r] and isPalindrome(inp[t:r + 1]) == 1:
				temp = inp[t:r + 1]
				if len(max_temp) < len(temp):
					max_temp = temp
			t += 1
		ans += max_temp
		ans += reverse(ans_temp)
		print(ans)
	T -= 1


for _ in range(int(input())):
	string = input()
	length = len(string)
	if string[:] == string[::-1]:
		print(string)
	else:
		ans = ''
		a = ''
		b = ''
		i = 0
		while string[i] == string[-(i + 1)]:
			a += string[i]
			i += 1
		b = a[::-1]
		j = i
		k = -(i + 1)
		if j == 0:
			j = 1
		temp_fixb = ''
		temp_fixa = ''
		while j != length - abs(k):
			if i == 0:
				if string[j:] == string[k:j - 1:-1]:
					temp_fixb = string[j:]
					break
				else:
					j += 1
			elif string[j:k + 1] == string[k:j - 1:-1]:
				temp_fixb = string[j:k + 1]
				break
			else:
				j += 1
		k = -(i + 1)
		j = i
		if j == 0:
			k = -2
		while j != length - abs(k):
			if i == 0:
				if string[j:k + 1] == string[k::-1]:
					temp_fixa = string[j:k + 1]
					break
				else:
					k -= 1
			elif string[j:k + 1] == string[k:j - 1:-1]:
				temp_fixa = string[j:k + 1]
				break
			else:
				k -= 1
		if temp_fixb == '' and temp_fixa == '' and (a == '') and (b == ''):
			print(string[0])
		if temp_fixb == '' and temp_fixa == '' and (a != ''):
			print(a, end='')
			print(string[i], end='')
			print(b)
		else:
			print(a, end='')
			len1 = len(temp_fixb)
			len2 = len(temp_fixa)
			if len1 > len2:
				print(temp_fixb, end='')
			else:
				print(temp_fixa, end='')
			print(b)


import os
import sys
from io import BytesIO, IOBase

def pie(s):
	s = s + '#' + s[::-1]
	n = len(s)
	pi = [0] * n
	j = 0
	for i in range(1, n):
		while j != 0 and s[i] != s[j]:
			j = pi[j - 1]
		j += s[i] == s[j]
		pi[i] = j
	return j

def main():
	for _ in range(int(input())):
		s = input().rstrip()
		n = len(s)
		(i, j) = (0, n - 1)
		while i <= j and s[i] == s[j]:
			i += 1
			j -= 1
		if i > j:
			print(s)
		else:
			ss = s[i:j + 1]
			ma1 = pie(ss)
			ma2 = pie(ss[::-1])
			if ma1 > ma2:
				print(s[:i] + ss[:ma1] + s[j + 1:])
			else:
				print(s[:i] + ss[-ma2:] + s[j + 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()


import sys

def fastio():
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
INF = 10 ** 20
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
from math import gcd
from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br

def manacher(s):
	(l, r) = (0, -1)
	n = len(s)
	m = [0] * n
	for i in range(n):
		if i <= r:
			mid = (l + r) // 2
			m[i] = min(r - i, m[2 * mid - i])
		while i - m[i] >= 0 and i + m[i] < n and (s[i + m[i]] == s[i - m[i]]):
			m[i] += 1
		if i + m[i] > r:
			(r, l) = (i + m[i], i - m[i])
	return m
(t,) = I()
while t:
	t -= 1
	s = input()
	n = len(s)
	(i, j) = (0, n - 1)
	while i < j and s[i] == s[j]:
		i += 1
		j -= 1
	left = s[:i]
	right = s[j + 1:]
	if i > j:
		print(left + right)
		continue
	next = s[i]
	s = s[i:j + 1]
	s = '#' + ''.join([i + '#' for i in s])
	m = manacher(s)
	n = len(s)
	(ans, idx) = (0, -1)
	for i in range(n):
		if m[i] == i + 1 and m[i] > ans:
			ans = m[i]
			idx = i
		if m[i] == n - i and m[i] > ans:
			ans = m[i]
			idx = i
	cur = s[idx - ans + 1:idx + ans]
	print(left, end='')
	for i in cur:
		if i != '#':
			print(i, end='')
	print(right)


def check(s):
	p = s[::-1]
	if s == p:
		return True
	else:
		return False
t = int(input())
while t != 0:
	s = input()
	ind = -1
	if check(s):
		print(s)
	else:
		for i in range(len(s) // 2):
			if s[i] != s[len(s) - i - 1]:
				ind = i
				break
		prefix = s[:ind]
		suffix = s[len(s) - ind:]
		mid = s[ind:len(s) - ind]
		maxi = ''
		for i in range(len(mid)):
			temp = mid[:i]
			if check(temp):
				maxi = temp
		for i in range(len(mid) - 1, -1, -1):
			temp = mid[i:]
			if check(temp):
				if len(temp) > len(maxi):
					maxi = temp
		print(prefix + maxi + suffix)
	t -= 1


def reverse(s):
	return s[::-1]

def isPalindrome(s):
	rev = reverse(s)
	if s == rev:
		return 1
	return 0
t = int(input())
while t > 0:
	s = input()
	n = len(s)
	i = 0
	j = n - 1
	while i <= n // 2:
		if s[i] == s[j]:
			i += 1
			j -= 1
		else:
			break
	i = i - 1
	j += 1
	if isPalindrome(s):
		print(s)
	elif i == -1:
		l1 = -1
		index1 = 0
		for iter in range(n):
			if isPalindrome(s[:iter + 1]) == 1:
				l1 = iter + 1
				index1 = iter
		l2 = -1
		index2 = 0
		for iter in range(n - 1, 0, -1):
			if isPalindrome(s[iter:]) == 1:
				l2 = n - iter
				index2 = iter
		if l1 > l2:
			print(s[:index1 + 1])
		else:
			print(s[index2:])
	else:
		l1 = -1
		index1 = 0
		for iter1 in range(i + 1, j):
			if isPalindrome(s[i + 1:iter1 + 1]) == 1:
				l1 = iter1 - i
				index1 = iter1
		l2 = -1
		index2 = 0
		for iter1 in range(j - 1, i, -1):
			if isPalindrome(s[iter1:j]) == 1:
				l2 = j - iter1
				index2 = iter1
		if l1 > l2:
			print(s[:index1 + 1] + s[j:])
		else:
			print(s[:i + 1] + s[index2:])
	t -= 1


import sys
input = sys.stdin.readline

def find_palindrome_start(s):
	res = ''
	for i in range(len(s)):
		odd = helper(s, i, i)
		if len(odd) > len(res):
			res = odd
		even = helper(s, i, i + 1)
		if len(even) > len(res):
			res = even
	return res

def helper(s, l, r):
	n = len(s)
	while l >= 0 and r < n and (s[l] == s[r]):
		l -= 1
		r += 1
	if l == -1:
		return s[l + 1:r]
	return ''

def find_palindrome_both(s):
	start = 0
	end = len(s) - 1
	while start < end and s[start] == s[end]:
		start += 1
		end -= 1
	a = find_palindrome_start(s[start:end + 1])
	b = find_palindrome_start(s[end:start - 1:-1])
	if len(a) > len(b):
		return s[0:start] + a + s[end + 1:]
	else:
		return s[0:start] + b + s[end + 1:]
n = int(input())
for i in range(n):
	s = input().strip()
	a = find_palindrome_start(s)
	b = find_palindrome_start(s[::-1])
	c = find_palindrome_both(s)
	if len(a) > len(b):
		if len(c) > len(a):
			print(c)
		else:
			print(a)
	elif len(c) > len(b):
		print(c)
	else:
		print(b)


from math import ceil

def get_longest_pal_starting_from_first(s):
	n = len(s)
	while True:
		i = ceil(n / 2)
		if n % 2:
			pal = True
			for j in range(1, i + 1):
				if s[j - 1] != s[2 * i - j - 1]:
					pal = False
					break
			if pal:
				return s[:2 * i - 1]
		else:
			pal = True
			for j in range(1, i + 1):
				if s[j - 1] != s[2 * i + 1 - j - 1]:
					pal = False
					break
			if pal:
				return s[:2 * i]
		n -= 1
for _ in range(int(input())):
	s = list(input())
	ap = get_longest_pal_starting_from_first(s)
	bp = get_longest_pal_starting_from_first(list(reversed(s)))
	n = len(s)
	pl = None
	for i in range(n // 2):
		if s[i] != s[n - i - 1]:
			break
		else:
			pl = i + 1
	psf = []
	if pl is not None:
		sbs = s[pl:n - pl]
		app = get_longest_pal_starting_from_first(sbs)
		bpp = get_longest_pal_starting_from_first(list(reversed(sbs)))
		psf = s[:pl]
		if len(app) > len(bpp):
			psf += app
		else:
			psf += list(reversed(bpp))
		psf += list(reversed(s[:pl]))
	nap = len(ap)
	nbp = len(bp)
	npsf = len(psf)
	if npsf > nap and npsf > nbp:
		print(''.join(psf))
	elif nap > nbp:
		print(''.join(ap))
	else:
		print(''.join(bp))


def find(s):
	if len(s) <= 1:
		return s
	if s[0] == s[-1]:
		index = 1
		while index < len(s) // 2:
			if s[index] == s[len(s) - index - 1]:
				index += 1
			else:
				break
		return s[0:index] + find(s[index:len(s) - index]) + s[len(s) - index:]
	left = -1
	for i in range(len(s), 0, -1):
		if isPal(s[:i]):
			left = i
			break
	right = -1
	for i in range(0, len(s)):
		if isPal(s[i:]):
			right = i
			break
	if left >= len(s) - right:
		return s[:left]
	return s[right:]

def isPal(s):
	for i in range(len(s) // 2):
		if s[i] != s[len(s) - i - 1]:
			return False
	return True
n = int(input())
for i in range(n):
	print(find(input()))


from sys import stdin
from itertools import takewhile

def isp(s):
	return all(map(lambda x: s[x] == s[~x], range(len(s) // 2)))

def greedy(s):
	if isp(s):
		return s
	for x in range(1, len(s)):
		if isp(s[:-x]):
			return s[:-x]
		if isp(s[x:]):
			return s[x:]
	return ''

def solve(s):
	ps = list(takewhile(lambda p: p[0] == p[1], map(lambda x: (s[x], s[~x]), range(len(s) // 2))))
	k = len(ps)
	if k > 0:
		(prefix, _) = zip(*ps)
		return ''.join(prefix) + greedy(s[k:-k]) + ''.join(prefix[::-1])
	return greedy(s)
input()
for line in stdin:
	print(solve(line.strip()))


import math, sys
import heapq
from collections import Counter, defaultdict

def li():
	return list(map(int, sys.stdin.readline().split()))

def ls():
	return list(map(int, list(input())))

def la():
	return list(input())

def ii():
	return int(input())

def dic(x):
	return defaultdict(lambda : x)

def isPrime(n):
	i = 2
	if n == 1:
		return False
	while i <= int(math.sqrt(n)):
		if n % i == 0:
			return False
		i = i + 1
	return True

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def binarySearch(arr, x):
	(l, r) = (0, len(arr) - 1)
	while l <= r:
		mid = l + (r - l) // 2
		if arr[mid] == x:
			return True
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return False
t = ii()
for _ in range(t):
	s = la()
	if len(s) == 1:
		print(''.join(s))
	else:
		n = len(s)
		for i in range(n // 2):
			if s[i] != s[n - i - 1]:
				break
		a = s[0:i]
		n = n - 2 * i
		b = ''
		for j in range(n, -1, -1):
			s1 = s[i:i + j]
			s2 = s[i + n - j:i + n]
			if s1 == s1[::-1]:
				b = s1
				break
			elif s2 == s2[::-1]:
				b = s2
				break
		print(''.join(a + b + a[::-1]))


def ispelin(ss, i, j):
	while i < j:
		if ss[i] != ss[j]:
			return False
		i += 1
		j -= 1
	return True

def fun1(ss):
	i = 0
	j = len(ss) - 1
	while i <= j:
		if ss[i] == ss[j]:
			if ispelin(ss, i, j):
				return [i, j]
		j -= 1

def fun2(ss):
	i = 0
	j = len(ss) - 1
	while i <= j:
		if ss[i] == ss[j]:
			if ispelin(ss, i, j):
				return [i, j]
		i += 1
t = int(input())
while t:
	s = input()
	i = 0
	n = len(s)
	j = n - 1
	while i < j and s[i] == s[j]:
		i += 1
		j -= 1
	ss = s[i:j + 1]
	(ii1, ii2, jj1, jj2) = (0, 0, -1, -1)
	if i <= j:
		(ii1, jj1) = fun1(ss)
		(ii2, jj2) = fun2(ss)
	if jj2 - ii2 > jj1 - ii1:
		res = s[:i] + s[i + ii2:i + jj2] + s[j:]
	else:
		res = s[:i] + s[i + ii1:i + jj1 + 1] + s[j + 1:]
	print(res)
	t -= 1


for _ in range(int(input())):
	s = input()
	x = 0
	y = 0
	if len(s) == 1:
		print(s)
		continue
	if s == s[::-1]:
		print(s)
		continue
	if len(s) == 2:
		print(s[0])
		continue
	if len(s) == 3:
		if s[0] == s[1]:
			print(s[0] + s[1])
			continue
		elif s[1] == s[2]:
			print(s[1] + s[2])
			continue
		print(s[0])
		continue
	aux = ''
	while x <= len(s) // 2:
		if s.startswith(s[0:x]) and s.endswith(s[0:x][::-1]):
			aux = s[0:x]
			y += 1
		x += 1
	auxi = 0
	auxj = len(s) - 1
	if aux == '':
		auxii = auxi + 1
		auxjj = auxj - 1
	else:
		auxii = s.index(aux) + len(aux)
		auxjj = len(s) - len(aux) - 1
	i = auxi + 1
	j = auxj - 1
	ii = auxii + 1
	jj = auxjj - 1
	s1 = ''
	s2 = ''
	s3 = ''
	s4 = ''
	temPali1 = False
	temPali2 = False
	while i <= auxj and j >= auxi:
		if len(s[auxi:i]) > 0 and s[auxi:i] == s[auxi:i][::-1]:
			s1 = s[auxi:i]
			temPali1 = True
		if len(s[auxj + 1:j + 1]) >= 0 and s[j + 1:auxj + 1] == s[j + 1:auxj + 1][::-1]:
			s3 = s[j + 1:auxj + 1]
			temPali1 = True
		i += 1
		j -= 1
	while ii <= auxjj and jj >= auxii:
		if len(s[auxii:ii]) > 0 and s[auxii:ii] == s[auxii:ii][::-1]:
			s2 = s[auxii:ii]
			temPali2 = True
		if len(s[auxjj + 1:jj + 1]) >= 0 and s[jj + 1:auxjj + 1] == s[jj + 1:auxjj + 1][::-1]:
			s4 = s[jj + 1:auxjj + 1]
			temPali2 = True
		ii += 1
		jj -= 1
	if not temPali1 and (not temPali2):
		if aux == '':
			print(s[0])
			continue
		else:
			print(aux + aux)
			continue
	elif len(s1) == len(s2) == len(s3) == len(s4):
		if aux == '':
			print(aux + sorted([s1, s3], key=len)[-1] + aux[::-1])
		else:
			print(aux + sorted([s1, s2, s3, s4], key=len)[-1] + aux[::-1])
	elif aux == '':
		print(aux + sorted([s1, s3], key=len)[-1] + aux[::-1])
		continue
	else:
		print(aux + sorted([s2, s4], key=len)[-1] + aux[::-1])
		continue


t = int(input())
for i in range(0, t, 1):
	s = input()
	s1 = ''
	s2 = ''
	low = 0
	high = len(s) - 1
	while low < high:
		if s[low] == s[high]:
			s1 = s1 + s[low]
			s2 = s2 + s[high]
		if s[low] != s[high]:
			break
		low = low + 1
		high = high - 1
	s3 = s[low]
	if high - low >= 1 or high == low:
		for i in range(low, high, 1):
			if s[i:high + 1] == s[i:high + 1][::-1]:
				s3 = ''
				s3 = s3 + s[i:high + 1]
				break
		s4 = s[low]
		for i in range(high, low, -1):
			if s[low:i] == s[low:i][::-1]:
				s4 = ''
				s4 = s[low:i]
				break
		ans = ''
		if len(s3) >= len(s4):
			ans = ans + s1 + s3 + s2[::-1]
		else:
			ans = ans + s1 + s4 + s2[::-1]
		print(ans)
	else:
		ans = ''
		ans = ans + s1 + s2[::-1]
		print(ans)


for _ in range(int(input())):
	a = input()
	n = len(a)
	prefix = 0
	suffix = n - 1
	while a[prefix] == a[suffix] and prefix < suffix:
		prefix = prefix + 1
		suffix = suffix - 1
	if prefix < suffix:
		prefixinc = prefix
		for i in range(prefix + 1, suffix + 1):
			chunk = a[prefix:i]
			if chunk == chunk[::-1]:
				prefixinc = i
		suffixinc = suffix
		for i in range(prefix + 1, suffix):
			chunk = a[i:suffix + 1]
			if chunk == chunk[::-1]:
				suffixinc = i
				break
		if abs(prefixinc - prefix) > abs(suffixinc - suffix):
			ans = a[:prefixinc] + a[suffix + 1:]
		else:
			ans = a[:prefix] + a[suffixinc:]
		print(ans)
	else:
		print(a)


t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	x = ''
	i = 0
	while i < n // 2:
		if s[i] == s[n - i - 1]:
			x += s[i]
			i += 1
		else:
			break
	if len(x) == n // 2:
		if n % 2:
			print(x + s[n // 2] + x[::-1])
		else:
			print(x + x[::-1])
	else:
		w = s[i:n - i]
		n = len(w)
		c = ''
		d = 0
		for i in range(n):
			l = w[:i + 1]
			r = w[i:]
			if l == l[::-1]:
				if i + 1 > d:
					d = i + 1
					c = l
			if r == r[::-1]:
				if n - i > d:
					d = n - i
					c = r
		print(x + c + x[::-1])


t = int(input())
for _ in range(t):
	s = str(input())
	p1 = 0
	p2 = len(s) - 1
	res = ''
	if len(s) == 1:
		print(s)
	else:
		while p1 < p2:
			if s[p1] == s[p2]:
				p1 = p1 + 1
				p2 = p2 - 1
			else:
				break
		s1 = s[0:p1]
		s2 = s[p2 + 1:len(s)]
		part = s[p1:p2 + 1]
		for i in range(len(part)):
			if part[0:i + 1][::-1] == part[0:i + 1]:
				if len(part[0:i + 1]) > len(res):
					res = part[0:i + 1]
		part = part[::-1]
		for i in range(len(part)):
			if part[0:i + 1][::-1] == part[0:i + 1]:
				if len(part[0:i + 1]) > len(res):
					res = part[0:i + 1]
		print(s1 + res, end='')
		print(s2)


def main():
	import sys
	input = sys.stdin.readline

	def manacher(S):
		C = []
		for a in S:
			C.append(a)
			C.append(0)
		C.pop()
		L = len(C)
		R = [0] * L
		i = j = 0
		while i < L:
			while j <= i < L - j and C[i - j] == C[i + j]:
				j += 1
			R[i] = j
			k = 1
			while j - R[i - k] > k <= i < L - k:
				R[i + k] = R[i - k]
				k += 1
			i += k
			j -= k
		return R
	for _ in range(int(input())):
		S = input().rstrip('\n')
		N = len(S)
		if N == 1:
			print(S)
			continue
		ii = -1
		for i in range(N // 2):
			if S[i] != S[N - 1 - i]:
				break
			ii = i
		if ii >= 0:
			ss = S[:ii + 1]
			S = S[ii + 1:N - ii - 1]
		else:
			ss = ''
		N = len(S)
		if N == 0:
			print(''.join([ss, ss[::-1]]))
			continue
		R = manacher(S)
		M = 0
		iii = 0
		for i in range(N):
			if R[i * 2] > M:
				if R[i * 2] % 2 == 0:
					continue
				k = R[i * 2] // 2
				if i - k == 0 or i + k == N - 1:
					M = R[i * 2]
					iii = i
		for i in range(N - 1):
			if R[i * 2 + 1] > M:
				if R[i * 2 + 1] % 2 == 1:
					continue
				k = R[i * 2 + 1] // 2
				if i - k + 1 == 0 or i + k == N - 1:
					M = R[i * 2 + 1]
					iii = i
		if M & 1:
			ans = S[iii - M // 2:iii + M // 2 + 1]
		else:
			ans = S[iii - M // 2 + 1:iii + M // 2 + 1]
		print(''.join([ss, ans, ss[::-1]]))
main()


def f1(i, j):
	for i in range(i, j):
		if s[i:j + 1] == s[i:j + 1][::-1]:
			return s[i:j + 1]
	return ''

def f2(i, j):
	for j in range(j, i - 1, -1):
		if s[i:j + 1] == s[i:j + 1][::-1]:
			return s[i:j + 1]
	return ''
for _ in range(int(input())):
	s = input()
	if s == s[::-1]:
		print(s)
		continue
	n = len(s)
	tp = ''
	ts = ''
	i = 0
	for i in range(n // 2):
		if s[i] == s[n - i - 1]:
			tp += s[i]
			ts += s[i]
		else:
			break
	x1 = f1(i, n - i - 1)
	x2 = f2(i, n - i - 1)
	y1 = tp + ts[::-1]
	y2 = tp + ts[::-1]
	z1 = tp + x1 + ts[::-1]
	z2 = tp + x2 + ts[::-1]
	if len(z1) <= n:
		y1 = z1
	if len(z2) <= n:
		y2 = z2
	print(max(y1, y2, key=len))


import math
from bisect import bisect_left, bisect_right
from sys import stdin, stdout, setrecursionlimit
from collections import Counter
input = lambda : stdin.readline().strip()
print = stdout.write

def LPS(s):
	n = len(s)
	lps = [0] * n
	l = 0
	i = 1
	while i < n:
		if s[i] == s[l]:
			l += 1
			lps[i] = l
			i += 1
		elif l != 0:
			l = lps[l - 1]
		else:
			lps[i] = 0
			i += 1
	res = lps[n - 1]
	if res > n / 2:
		return n // 2
	else:
		return res
t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	i = 0
	while i < n - i - 1:
		if s[i] == s[n - i - 1]:
			i += 1
		else:
			break
	if i > n - i - 1:
		print(s + '\n')
	else:
		string = s[i:n - i]
		right_index = i + LPS(string + '$' + string[::-1]) - 1
		left_index = n - i - LPS(string[::-1] + '$' + string)
		if right_index > n - left_index - 1:
			print(s[:i] + s[i:right_index + 1] + s[n - i:] + '\n')
		else:
			print(s[:i] + s[left_index:n - i] + s[n - i:] + '\n')


import io, os
from collections import deque
RL = lambda : list(map(int, input().split()))

def isPalindrome(r):
	p = r
	p = ''.join(reversed(p))
	return r == p

def PrefixSuffixPalindrome(st):
	n = len(st)
	length = 0
	for i in range(n // 2):
		if st[i] != st[n - i - 1]:
			length = i
			break
	prefix = ''
	suffix = ''
	midPal = ''
	prefix = st[:length]
	suffix = st[n - length:]
	st = st[length:n - 2 * length + length]
	for i in range(1, len(st) + 1):
		y = st[0:i]
		if isPalindrome(y):
			if len(midPal) < len(y):
				midPal = y
	for i in range(1, len(st) + 1):
		y = st[len(st) - i:]
		if isPalindrome(y):
			if len(midPal) < len(y):
				midPal = y
	answer = prefix + midPal + suffix
	return answer
T = int(input())
for i in range(T):
	st = input()
	print(PrefixSuffixPalindrome(st))


def is_palindrome(s):
	return s == s[::-1]
for t in range(int(input())):
	s = input()
	(l, r) = (0, len(s) - 1)
	while l <= r:
		if s[l] == s[r]:
			l += 1
			r -= 1
		else:
			break
	l -= 1
	r += 1
	if l == r:
		print(s)
		continue
	(a, b) = (s[:l + 1], s[r:])
	sub_s = s[l + 1:r]
	ans = ''
	(l, r) = (0, len(sub_s) - 1)
	while l <= r:
		if is_palindrome(sub_s[:l]):
			ans = sub_s[:l]
		l += 1
	while r >= 0:
		if is_palindrome(sub_s[r:]) and len(ans) < len(sub_s[r:]):
			ans = sub_s[r:]
		r -= 1
	print(a + ans + b)


t = int(input())
while t:
	t += -1
	s = input()
	ln = len(s)
	i = 0
	j = ln - 1
	a = ''
	aa = ''
	while 1:
		if i >= j:
			break
		if s[i] == s[j]:
			a += s[i]
		else:
			break
		i -= -1
		j += -1
	if i <= j:
		temp = j - i + 1
		for k in range(1, temp + 1):
			s1 = s[i:i + k - 1]
			s2 = s1[::-1]
			if s1 == s2:
				aa = s1
		for k in range(temp):
			s1 = s[i + k:j + 1]
			s2 = s1[::-1]
			if s1 == s2:
				if len(s1) > len(aa):
					aa = s1
				break
	a1 = a[::-1]
	print(a, aa, a1, sep='')


t = int(input())
for i in range(t):
	r = input()
	b = len(r)
	a = 0
	ok = True
	while ok == True:
		if a >= b - a - 1:
			break
		if r[a] != r[b - a - 1]:
			break
		a += 1
	l = 0
	z = ''
	for i in range(b - 2 * a):
		if r[a:a + i // 2 + 1] == r[a + (i + 1) // 2:a + i + 1][::-1]:
			l = i + 1
			z = r[a:a + i + 1]
	s = r[::-1]
	k = 0
	y = ''
	for i in range(b - 2 * a):
		if s[a:a + i // 2 + 1] == s[a + (i + 1) // 2:a + i + 1][::-1]:
			k = i + 1
			y = s[a:a + i + 1]
	if l > k:
		print(r[:a] + z + r[:a][::-1])
	else:
		print(r[:a] + y + r[:a][::-1])


def isPalindrome(temp):
	if temp[::-1] == temp:
		return True
	else:
		return False
for _ in range(int(input())):
	s = input()
	l = len(s)
	a = ''
	b = ''
	idx = None
	for i in range(l // 2):
		if s[i] == s[l - i - 1]:
			a += s[i]
			b += s[l - i - 1]
		else:
			idx = i
			break
	else:
		ans = s
	if idx != None:
		ai = None
		bi = None
		for i in range(l - idx - 1, idx, -1):
			temp = s[idx:i]
			if isPalindrome(temp):
				ai = temp
				break
		for i in range(idx + 1, l - idx):
			temp = s[i:l - idx]
			if isPalindrome(temp):
				bi = temp
				break
		if len(ai) > len(bi):
			ans = a + ai + b[::-1]
		else:
			ans = a + bi + b[::-1]
	print(ans)


t = int(input())
for _ in range(0, t):
	s = input()
	(l, r) = ('', '')
	(ll, rr) = (0, len(s) - 1)
	while ll < rr:
		if s[ll] == s[rr]:
			l += s[ll]
			r += s[rr]
			ll += 1
			rr -= 1
		else:
			break
	left = s[ll:rr + 1]
	middle = ''
	for i in range(0, len(left)):
		if left[:i + 1] == left[:i + 1][::-1] and len(middle) < len(left[:i + 1]):
			middle = left[:i + 1]
	for i in range(len(left) - 1, -1, -1):
		if left[i:len(left)] == left[i:len(left)][::-1] and len(middle) < len(left[i:len(left)]):
			middle = left[i:len(left)]
	print(l + middle + r[::-1])


def main():
	import sys
	input = sys.stdin.readline
	t = int(input())

	def check(s, l):
		if l == 0:
			return ''
		for i in range(l, 0, -1):
			s_ = s[:i]
			if s_ == s_[::-1]:
				return s_
			s_ = s[l - i:]
			if s_ == s_[::-1]:
				return s_
	for _ in range(t):
		s = input().rstrip()
		l = len(s)
		j = -1
		for i in range(l // 2):
			if s[i] != s[l - i - 1]:
				break
			else:
				j += 1
		ans = s[:j + 1] + check(s[j + 1:l - j - 1], l - 2 * j - 2) + s[l - j - 1:]
		print(ans)
main()


from sys import stdin, stdout

def solve(s):
	s = s + '#' + s[::-1]
	pre = [0] * len(s)
	c = 0
	for i in range(1, len(s)):
		while c != 0 and s[c] != s[i]:
			c = pre[c - 1]
		if s[c] == s[i]:
			c += 1
		pre[i] = c
	return s[0:c]

def main():
	t = int(stdin.readline())
	for _ in range(t):
		s = stdin.readline().rstrip()
		ans = ''
		c = 0
		for i in range(int(len(s) / 2)):
			if s[i] == s[len(s) - 1 - i]:
				c += 1
			else:
				break
		if c < int(len(s) / 2):
			if c == 0:
				ss = s
			else:
				ss = s[c:-c]
			ss2 = ss[::-1]
			ss = solve(ss)
			ss2 = solve(ss2)
			if len(ss2) >= len(ss):
				if c == 0:
					ans = ss2
				else:
					ans = s[:c] + ss2 + s[-c:]
			elif c == 0:
				ans = ss
			else:
				ans = s[:c] + ss + s[-c:]
		elif len(s) % 2 == 1:
			ans = s
		else:
			ans = s[:c] + s[-c:]
		stdout.write(ans + '\n')
main()


from sys import stdin
input = stdin.readline

def calc(a):
	p = a + '#' + a[::-1]
	pi = [0] * len(p)
	j = 0
	for i in range(1, len(p)):
		while j != 0 and p[j] != p[i]:
			j = pi[j - 1]
		if p[i] == p[j]:
			j += 1
		pi[i] = j
	return a[:j]
t = int(input())
for _ in range(t):
	s = input().rstrip()
	n = len(s)
	k = 0
	while k < n // 2 and s[k] == s[n - 1 - k]:
		k += 1
	prefix = calc(s[k:n - k])
	suffix = calc(s[k:n - k][::-1])
	s1 = s[:k] + prefix + s[n - k:]
	s2 = s[:k] + suffix + s[n - k:]
	if len(s1) >= len(s2):
		print(s1)
	else:
		print(s2)


import sys
inp = sys.stdin.readline

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

def iin():
	return int(input())

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

def palindromCentre(s):
	S = []
	n = len(s)
	N = 2 * n + 1
	for i in s:
		S.append('#')
		S.append(i)
	S.append('#')
	p = [0] * N
	c = 0
	(L, R) = (0, 0)
	i = 0
	while i < N:
		if i > R:
			start = 1
			p[i] = 0
			(l1, r1) = (i - start, i + start)
			while l1 >= 0 and r1 < N and (S[l1] == S[r1]):
				p[i] += 1
				l1 -= 1
				r1 += 1
			c = i
			(L, R) = (c - p[i], c + p[i])
		else:
			r = i
			l = 2 * c - r
			if p[l] < R - r or (p[l] == R - r and R == N - 1):
				p[r] = p[l]
			else:
				start = R - r
				p[r] = start
				(l1, r1) = (i - start - 1, i + start + 1)
				while l1 >= 0 and r1 < N and (S[l1] == S[r1]):
					p[r] += 1
					l1 -= 1
					r1 += 1
				c = r
				(L, R) = (c - p[r], c + p[r])
		i += 1
	return (p, S)

def main():

	def red(ans):
		return ''.join([i for i in ans if i != '#'])
	T = iin()
	while T:
		T -= 1
		s = list(input())
		(s1, s2) = palindromCentre(s)
		n = len(s1)
		s3 = [0] * n
		for i in range(n // 2):
			if s2[i] == s2[-i - 1]:
				s3[i] += 1
				s3[-i - 1] += 1
			else:
				break
		ans = []
		for i in range(n // 2, n):
			(x, y) = (n - i - 1, i)
			if s3[x - s1[x]]:
				ch = x - s1[x]
				ans = s2[:ch] + s2[x - s1[x]:x + s1[x] + 1] + s2[-ch:] if ch else s2[x - s1[x]:x + s1[x] + 1]
				break
			if s3[n - (y + s1[y]) - 1]:
				ch = n - (y + s1[y]) - 1
				ans = s2[:ch] + s2[y - s1[y]:y + s1[y] + 1] + s2[-ch:] if ch else s2[y - s1[y]:y + s1[y] + 1]
				break
		print(red(ans))
main()


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))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for ti in range(int(input())):
	s_o = input()
	revl = 0
	for (j, jitm) in enumerate(s_o):
		if jitm == s_o[-j - 1] and j < len(s_o) >> 1:
			revl += 1
		else:
			break
	revs = s_o[:revl]
	if len(s_o) >> 1 <= revl:
		print(s_o)
		continue
	ans = ''
	if revl:
		s_o = s_o[revl:-revl]
	s = '^#' + '#'.join(list(s_o)) + '#$'
	p = []
	mx = 0
	_id = 0
	br1 = 0
	br2 = 0
	for (idx, i) in enumerate(s):
		if idx < mx:
			p.append(min(p[2 * _id - idx], mx - idx))
		else:
			p.append(1)
		while idx + p[idx] < len(s) and s[idx - p[idx]] == s[idx + p[idx]]:
			p[idx] += 1
		if mx < idx + p[idx]:
			_id = idx
			mx = idx + p[idx]
		_tmp = p[idx] - 1
		if p[idx] - 1 & 1:
			r1 = (idx >> 1) - (_tmp >> 1) - 1
			r2 = (idx >> 1) + (_tmp >> 1)
		else:
			r1 = (idx >> 1) - (_tmp >> 1)
			r2 = (idx >> 1) + (_tmp >> 1)
		if idx > 0 and i != '$':
			if (r1 == 0 or r2 == len(s_o)) and r2 - r1 > br2 - br1:
				br1 = r1
				br2 = r2
	print(revs + s_o[br1:br2] + revs[::-1])


def kmp(s):
	l = len(s)
	ans = [-1 for i in range(l)]
	j = 0
	i = 1
	while i < l:
		if s[i] == s[j]:
			ans[i] = j
			j += 1
			i += 1
		elif j > 0:
			j = ans[j - 1] + 1
		else:
			i += 1
	return ans[l - 1] + 1

def find_pattern(s):
	temp = s + '?' + s[::-1]
	ans = kmp(temp)
	return ans
for _ in range(int(input())):
	a = input()
	n = len(a)
	i = 0
	j = n - 1
	l = 0
	ans = ''
	while i < j:
		if a[i] is a[j]:
			i += 1
			j -= 1
			l += 1
		else:
			break
	ans += a[:l]
	rem = a[i:j + 1]
	if i <= j:
		pre = find_pattern(rem)
		suf = find_pattern(rem[::-1])
		if pre > suf:
			ans += rem[:pre]
		else:
			ans += rem[::-1][:suf]
	ans += a[n - l:]
	print(ans)


import sys
from array import array

def input():
	return sys.stdin.buffer.readline().decode('utf-8')

class RollingHash(object):
	__slots__ = ['hash1', 'hash2']
	from random import randint
	N = 10 ** 6 + 100
	BASE = randint(10 ** 8, 10 ** 9)
	(MOD1, MOD2) = (10 ** 9 + 7, 10 ** 9 + 9)
	(BASE1, BASE2) = (array('i', [1]) * N, array('i', [1]) * N)
	for i in range(1, N):
		BASE1[i] = BASE1[i - 1] * BASE % MOD1
		BASE2[i] = BASE2[i - 1] * BASE % MOD2

	def __init__(self, source: list):
		self.hash1 = hash1 = array('i', [0] + source)
		self.hash2 = hash2 = array('i', [0] + source)
		for i in range(1, len(source) + 1):
			hash1[i] = (hash1[i] + hash1[i - 1] * self.BASE) % self.MOD1
			hash2[i] = (hash2[i] + hash2[i - 1] * self.BASE) % self.MOD2

	def get(self, l: int, r: int):
		return ((self.hash1[r] - self.hash1[l] * self.BASE1[r - l]) % self.MOD1, (self.hash2[r] - self.hash2[l] * self.BASE2[r - l]) % self.MOD2)
t = int(input())
ans = [''] * t
for ti in range(t):
	s = input().rstrip()
	n = len(s)
	(l, r) = (0, n - 1)
	while s[l] == s[r] and l < r:
		l += 1
		r -= 1
	if l >= r:
		ans[ti] = s
		continue
	rh = RollingHash([ord(c) for c in s[l:r + 1]])
	rh_rev = RollingHash([ord(c) for c in reversed(s[l:r + 1])])
	m = r - l + 1
	(mid_l, mid_r) = (0, 1)
	for i in range(1, m + 1):
		if rh.get(0, i) == rh_rev.get(m - i, m):
			(mid_l, mid_r) = (0, i)
		elif rh_rev.get(0, i) == rh.get(m - i, m):
			(mid_l, mid_r) = (m - i, m)
	ans[ti] = s[:l] + s[l + mid_l:l + mid_r] + s[r + 1:]
sys.stdout.buffer.write(('\n'.join(ans) + '\n').encode('utf-8'))


from sys import stdin, gettrace
if not gettrace():

	def input():
		return next(stdin)[:-1]

def main():

	def gen_lps(pattern, m):
		pre_match = 0
		i = 1
		lps = [0] * m
		while i < m:
			if pattern[i] == pattern[pre_match]:
				pre_match += 1
				lps[i] = pre_match
				i += 1
			elif pre_match == 0:
				lps[i] = 0
				i += 1
			else:
				pre_match = lps[pre_match - 1]
		return lps[-1]

	def largest_pre_pal(rem):
		new = rem + '#' + rem[::-1]
		m = len(new)
		lll = gen_lps(new, m)
		return lll

	def solve():
		s = input()
		n = len(s)
		if n == 1:
			print(s)
			return
		(i, j, l) = (0, n - 1, 0)
		while i < j and s[i] == s[j]:
			i += 1
			j -= 1
			l += 1
		if i == j or i - j == 1:
			print(s)
			return
		pre_len = largest_pre_pal(s[l:n - l - 1])
		suff_len = largest_pre_pal(s[n - l - 1:l:-1])
		if pre_len >= suff_len:
			final = s[0:l] + s[l:l + pre_len] + s[n - l:]
		else:
			final = s[0:l] + s[n - l - suff_len:n - l] + s[n - l:]
		print(final)
	t = int(input())
	for i in range(t):
		solve()
main()


import io
import os
import sys
cachedPow = {}
PRIME = 31
MOD = 2 ** 61 - 1
MOD = 10 ** 9 + 7

class RabinKarpRollingHash:

	def __init__(self, s, PRIME=PRIME, MOD=MOD):
		n = len(s)
		prefix = [0] * (n + 1)
		for i in range(n):
			prefix[i + 1] = (prefix[i] * PRIME + ord(s[i]) - ord('a')) % MOD
		if n not in cachedPow:
			cachedPow[0] = 1
			for i in range(n):
				cachedPow[i + 1] = cachedPow[i] * PRIME % MOD
		self.prefix = prefix

	def getHash(self, i, j):
		return (self.prefix[j] - self.prefix[i] * cachedPow[j - i]) % MOD

def solve(s):
	n = len(s)
	minAB = 0
	for i in range(n):
		if i < n - 1 - i and s[i] == s[n - 1 - i]:
			minAB = i + 1
		else:
			break
	if minAB == len(s) // 2:
		return s
	forward = RabinKarpRollingHash(s)
	backward = RabinKarpRollingHash(s[::-1])

	def isPalindrome(i, j):
		return forward.getHash(i, j) == backward.getHash(n - j, n - i)
	best = (2 * minAB, minAB, minAB)
	for l in range(n - 2 * minAB, -1, -1):
		if isPalindrome(minAB, minAB + l):
			a = minAB + l
			b = minAB
			best = max(best, (a + b, a, b))
			break
		elif isPalindrome(n - minAB - l, n - minAB):
			a = minAB
			b = minAB + l
			best = max(best, (a + b, a, b))
			break
	(l, a, b) = best
	return s[:a] + s[n - b:]
input = sys.stdin.readline
answers = []
T = int(input())
for t in range(T):
	S = input().rstrip()
	answers.append(solve(S))
print('\n'.join(answers))


from sys import stdin, stdout
from math import gcd, sqrt
from collections import deque
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7

def func1(p):
	ans = ''
	s1 = ''
	s2 = ''
	for i in p:
		s1 = s1 + i
		s2 = i + s2
		if s1 == s2:
			ans = s1
	return ans

def func2(p):
	s = ''
	j = 0
	for i in range(len(p) // 2):
		if p[i] == p[-i - 1]:
			s += p[i]
			j = i + 1
		else:
			break
	return s + MAX([func1(p[j:][::-1][j:][::-1]), func1(p[j:][::-1][j:])]) + s[::-1]

def MAX(p):
	ans = p[0]
	m = len(ans)
	for i in p[1:]:
		v = len(i)
		if v > m:
			ans = i
			m = v
	return ans
for _ in range(I()):
	a = S()
	if a == a[::-1]:
		print(a)
		continue
	print(MAX([func1(a), func1(a[::-1]), func2(a)]))


def help(s):
	if s == '':
		return ''
	base = 26
	mod = 86028121
	mod1 = 179424673
	hash1 = 0
	hash2 = 0
	hash11 = 0
	hash22 = 0
	pos = -1
	pow = 1
	pow1 = 1
	for i in range(len(s)):
		hash1 = (hash1 * base + ord(s[i]) - ord('a')) % mod
		hash2 = (hash2 + (ord(s[i]) - ord('a')) * pow) % mod
		hash11 = (hash11 * base + ord(s[i]) - ord('a')) % mod1
		hash22 = (hash22 + (ord(s[i]) - ord('a')) * pow1) % mod1
		if hash1 == hash2 and hash11 == hash22:
			pos = i
		pow = pow * base % mod
		pow1 = pow1 * base % mod1
	return s[:pos + 1]
for _ in range(int(input())):
	s = input()
	n = len(s)
	count = 0
	i = 0
	j = n - 1
	while j > i:
		if s[j] != s[i]:
			break
		count += 1
		i += 1
		j -= 1
	posi1 = help(s[count:n - count])
	posi2 = help(s[count:n - count][::-1])
	print(s[:count] + (posi1 if len(posi1) > len(posi2) else posi2) + s[:count][::-1])


import sys
import math
input = sys.stdin.readline
n = int(input())

def check(s):
	s1 = s + '*' + s[::-1]
	lps = [0] * len(s1)
	for i in range(1, len(lps)):
		j = lps[i - 1]
		while j > 0 and s1[i] != s1[j]:
			j = lps[j - 1]
		k = 0
		if s1[i] == s1[j]:
			k = 1
		lps[i] = j + k
	return s1[:lps[-1]]

def fun(s):
	z1 = check(s)
	z2 = check(s[::-1])
	if len(z1) >= len(z2):
		return z1
	else:
		return z2
for t1 in range(n):
	t = input()
	t = t.strip()
	i = 0
	j = len(t) - 1
	if t[i] == t[j]:
		while i <= j and t[i] == t[j]:
			i += 1
			j -= 1
		i -= 1
		j += 1
		m = ''
		if i + 1 <= j - 1:
			s = t[i + 1:j]
			ans = fun(s)
			m = ans
			if i == j:
				print(t[:i + 1] + m + t[j + 1:])
			else:
				print(t[:i + 1] + m + t[j:])
		elif i == j:
			print(t[:i + 1] + t[j + 1:])
		else:
			print(t[:i + 1] + t[j:])
	else:
		m = fun(t)
		print(m)


def checkpal(s):
	return s == s[::-1]

def side(s):
	l = r = ''
	while len(s) > 1:
		if s[0] == s[-1]:
			l = l + s[0]
			r = s[-1] + r
			s = s[1:-1]
		else:
			break
	return [l, s, r]

def mid(s):
	l = len(s)
	if l <= 1:
		return s
	else:
		i = l + 1
	while i > 0:
		i = i - 1
		if checkpal(s[:i]):
			return s[:i]
		elif checkpal(s[l - i:]):
			return s[l - i:]
		else:
			continue
	return ''
t = int(input())
while t > 0:
	s = input()
	if len(s) == 1:
		print(s)
	else:
		a = side(s)
		print(a[0] + mid(a[1]) + a[2])
	t = t - 1


from bisect import bisect_left as bl, bisect_right as br, insort
import sys
import heapq
from collections import defaultdict as dd, deque

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

def mdata():
	return map(int, data().split())
mod = 998244353

def computeLPSArray(s):
	pat = s + '.' + s[::-1]
	M = len(pat)
	l = 0
	lps = [0] * M
	i = 1
	while i < M:
		if pat[i] == pat[l]:
			l += 1
			lps[i] = l
			i += 1
		elif l != 0:
			l = lps[l - 1]
		else:
			lps[i] = 0
			i += 1
	return s[:lps[-1]]

def longest(s):
	(a, b) = (computeLPSArray(s), computeLPSArray(s[::-1]))
	return a if len(a) > len(b) else b
for t in range(int(data())):
	s = data()
	n = len(s)
	j = 0
	for i in range(n // 2):
		j = i
		if s[i] != s[n - 1 - i]:
			break
	print(s[:j] + longest(s[j:n - j]) + s[n - j:])


def kmp(s):
	lps = [0] * len(s)
	for i in range(1, len(s)):
		p_i = lps[i - 1]
		while p_i > 0 and s[i] != s[p_i]:
			p_i = lps[p_i - 1]
		lps[i] = p_i
		if s[i] == s[p_i]:
			lps[i] += 1
	return lps[-1]

def rest(s):
	t = s + '#'
	s = s[::-1]
	t += s
	return kmp(t)

def next(s):
	length = 0
	n = len(s)
	i = 0
	j = n - 1
	while i < j:
		if s[i] != s[j]:
			break
		i += 1
		j -= 1
		length += 1
	ans = s[0:length]
	remaining = s[length:length + (n - 2 * length)]
	if len(remaining):
		longest_prefix = rest(remaining)
		remaining = remaining[::-1]
		l_s = rest(remaining)
		if longest_prefix > l_s:
			remaining = remaining[::-1]
			ans += remaining[0:longest_prefix]
		else:
			ans += remaining[0:l_s]
	ans += s[n - length:n]
	return ans
for _ in range(int(input())):
	st = input()
	print(next(st))


import sys
import math

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

def idata():
	return [int(x) for x in ii().split()]

def binary_search(array, x):
	(left, right) = (-1, len(array))
	while left + 1 != right:
		middle = (left + right) // 2
		if array[middle] >= x:
			right = middle
		elif array[middle] < x:
			left = middle
	return right

def solve_of_problem():
	s = ii()
	ans = ''
	while len(s) >= 2 and s[0] == s[-1]:
		ans += s[0]
		s = s[1:-1]
	s1 = s
	qw = ''
	while True:
		while len(s1) > 1 and s1[0] != s1[-1]:
			s1 = s1[:-1]
		if s1 == s1[::-1] or len(s1) == 1:
			qw = s1
			break
		else:
			s1 = s1[:-1]
	s1 = s
	qw1 = ''
	while True:
		while len(s1) > 1 and s1[0] != s1[-1]:
			s1 = s1[1:]
		if s1 == s1[::-1] or len(s1) == 1:
			qw1 = s1
			break
		else:
			s1 = s1[1:]
	if len(qw) > len(qw1):
		print(ans + qw + ans[::-1])
	else:
		print(ans + qw1 + ans[::-1])
	return
for ______ in range(int(ii())):
	solve_of_problem()


t = int(input())

def checkpal(s):
	i = 0
	j = len(s) - 1
	while i < j:
		if s[i] != s[j]:
			return False
		i += 1
		j -= 1
	return True
for rep in range(t):
	s = input()
	a = []
	b = []
	i = 0
	j = len(s) - 1
	while i < j and s[i] == s[j]:
		a.append(s[i])
		b.append(s[j])
		i += 1
		j -= 1
	temp1 = ''
	temp2 = ''
	maxi1 = ''
	maxi2 = ''
	for i in range(len(a), len(s) - len(a)):
		temp1 = temp1 + s[i]
		temp2 = s[len(s) - 1 - i] + temp2
		if checkpal(temp1):
			if len(temp1) > len(maxi1):
				maxi1 = temp1
		if checkpal(temp2):
			if len(temp2) > len(maxi2):
				maxi2 = temp2
	b.reverse()
	a = ''.join(a)
	b = ''.join(b)
	maxi = ''
	if len(maxi1) >= len(maxi2):
		maxi = maxi1
	else:
		maxi = maxi2
	print(a + maxi + b)


def dp(s, i, j, n, count):
	while s[i] == s[j] and i <= j:
		i += 1
		j -= 1
		if i != j:
			count += 2
		else:
			count += 1
	if j < i:
		return count
	return max(dp(s, i + 1, j, n, count), dp(s, i, j - 1, n, count))

def strt(st, en, s, n):
	i = st
	j = en
	while i <= j:
		i = st
		dct = {}
		while s[i] != s[j] and i <= j:
			j -= 1
		dct[s[i:j + 1]] = 1
		if s[i:j + 1][::-1] in dct:
			return [s[i:j + 1], j + 1 - i]
		j -= 1
	return [s[0], 1]

def end(st, en, s, n):
	i = st
	j = en
	while i <= j:
		j = en
		dct = {}
		while s[i] != s[j] and i <= j:
			i += 1
		dct[s[i:j + 1]] = 1
		if s[i:j + 1][::-1] in dct:
			return [s[i:j + 1], j + 1 - i]
		i += 1
	return [s[0], 1]
for _ in range(int(input())):
	s = input()
	n = len(s)
	if s[0] != s[n - 1]:
		f = strt(0, n - 1, s, n)
		e = end(0, n - 1, s, n)
		if f[1] > e[1]:
			print(f[0])
		else:
			print(e[0])
	else:
		i = 0
		j = n - 1
		count = 0
		ls = list(s)
		while i <= j and ls[i] == ls[j]:
			i += 1
			j -= 1
			if i != j:
				count += 2
			else:
				count += 1
		if i > j:
			i -= 1
			j += 1
		(x, y) = (i, j)
		p = strt(i, j, s, n)
		q = end(i, j, s, n)
		if p[1] > q[1]:
			print(s[:i] + p[0] + s[j + 1:])
		else:
			print(s[:i] + q[0] + s[j + 1:])


def isPlaindrome(str1):
	n = len(str1)
	for i in range(n // 2):
		if str1[i] != str1[-i - 1]:
			return False
	return True

def function(str1, a):
	temp = str1 + '_'
	str1 = str1[::-1]
	temp = temp + str1
	lis = [0] * (2 * a + 1)
	for i in range(1, 2 * a + 1):
		num = lis[i - 1]
		while num > 0 and temp[num] != temp[i]:
			num = lis[num - 1]
		if temp[i] == temp[num]:
			num += 1
		lis[i] = num
	return temp[0:lis[-1]]
t = int(input())
for _ in range(t):
	s1 = str(input())
	n = len(s1)
	ind = -1
	for i in range(n // 2):
		if s1[i] != s1[-i - 1]:
			ind = i
			break
	if ind == -1:
		print(s1)
	else:
		s = s1[ind:n - ind]
		a = n - ind - ind
		temp1 = function(s, a)
		s = s[::-1]
		temp2 = function(s, a)
		if len(temp1) > len(temp2):
			temp = temp1
		else:
			temp = temp2
		if ind > 0:
			ans = s1[:ind] + temp + s1[-ind:]
		else:
			ans = temp
		print(ans)


from sys import stdin

def inp():
	return stdin.buffer.readline().rstrip().decode('utf8')

def itg():
	return int(stdin.buffer.readline())

def mpint():
	return map(int, stdin.buffer.readline().split())

def longest_palindromic_prefix(s):
	n = len(s)
	p = [[0] * (n + 1), [0] * n]
	for (z, p_z) in enumerate(p):
		(left, right) = (0, 0)
		for i in range(n):
			t = right - i + 1 - z
			if i < right:
				p_z[i] = min(t, p_z[left + t])
			(L, R) = (i - p_z[i], i + p_z[i] - 1 + z)
			while L >= 1 and R + 1 < n and (s[L - 1] == s[R + 1]):
				p_z[i] += 1
				L -= 1
				R += 1
			if R > right:
				(left, right) = (L, R)
	i1 = x1 = i2 = x2 = 0
	for (i, x) in enumerate(p[0]):
		if i == x and x > x1:
			(i1, x1) = (i, x)
	for (i, x) in enumerate(p[1]):
		if i == x and x > x2:
			(i2, x2) = (i, x)
	return max(s[i1 - x1:i1 + x1], s[i2 - x2:i2 + x2 + 1], key=len)
for case in range(itg()):
	s = inp()
	n = len(s)
	p_len = 0
	for i in range(n >> 1):
		if s[i] == s[-i - 1]:
			p_len = i + 1
		else:
			break
	s_pre = s[:p_len]
	s_suf = s[:p_len][::-1]
	if p_len:
		s = s[p_len:-p_len]
	s_mid = longest_palindromic_prefix(s)
	longest_suffix = longest_palindromic_prefix(s[::-1])
	if len(longest_suffix) > len(s_mid):
		s_mid = longest_suffix
	print(s_pre + s_mid + s_suf)


from sys import stdin
from itertools import takewhile

def isp(s):
	return all(map(lambda x: s[x] == s[~x], range(len(s) // 2)))

def greedy(s):
	if isp(s):
		return s
	for x in range(1, len(s)):
		if isp(s[:-x]):
			return s[:-x]
		if isp(s[x:]):
			return s[x:]
	return ''

def solve(s):
	k = max([-1] + list(takewhile(lambda x: s[x] == s[~x], range(len(s) // 2)))) + 1
	if k > 0:
		prefix = s[:k]
		return prefix + greedy(s[k:-k]) + prefix[::-1]
	return greedy(s)
input()
for line in stdin:
	print(solve(line.strip()))


def ps(s):
	p = s + '#' + s[::-1]
	lps = [0 for i in range(len(p))]
	j = 0
	i = 1
	while i < len(p):
		if p[i] == p[j]:
			j += 1
			lps[i] = j
			i += 1
		elif j != 0 and p[i] != p[j]:
			j = lps[j - 1]
		else:
			lps[i] = j
			i += 1
	return s[:j]
for _ in range(int(input())):
	s = input()
	l = 0
	while l < len(s) // 2 and s[l] == s[-(l + 1)]:
		l += 1
	pre = ps(s[l:len(s) - l])
	suf = ps(s[l:len(s) - l][::-1])
	p = pre if len(pre) > len(suf) else suf
	print(s[0:l] + p + s[len(s) - l:])


t = int(input())
mod = 998244353
for i3 in range(t):
	s = str(input())
	n = len(s)
	(hash, hash2) = (0, 0)
	o = ord('a')
	ans = 0
	(a, b) = (0, n - 1)
	ma = 0
	while s[a] == s[b] and b > a + 2:
		a += 1
		b -= 1
	a -= 1
	b += 1
	goal = [a, b]
	for i in range(a + 1, b):
		hash *= 26
		hash += ord(s[i]) - o
		hash2 += (ord(s[i]) - o) * pow(26, i - a - 1, mod)
		hash %= mod
		hash2 %= mod
		if hash == hash2 and ma < i - a:
			(ma, goal) = (i - a, [i, b])
	(hashb, hashb2) = (0, 0)
	for i in range(b - 1, a, -1):
		hashb += (ord(s[i]) - o) * pow(26, b - 1 - i, mod)
		hashb2 *= 26
		hashb2 += ord(s[i]) - o
		hashb %= mod
		hashb2 %= mod
		if hashb == hashb2 and ma < b - i:
			(ma, goal) = (b - i, [a, i])
	for i in range(goal[0] + 1):
		print(s[i], end='')
	for i in range(goal[1], n):
		print(s[i], end='')
	print('')


import sys, math
input = sys.stdin.readline

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

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

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
t = 1
t = inp()
for tt in range(t):
	a = insr()
	n = len(a)
	for i in range(len(a) // 2 + 1):
		if i < n - i - 1:
			if a[i] != a[n - i - 1]:
				break
	qwqw = i
	qw = a[i:n - i]
	if len(qw) == 0:
		print(a[:i] + a[n - i:])
	else:
		q = qw + ' ' + ''.join(reversed(qw))
		w = ''.join(reversed(qw)) + ' ' + qw
		qq = [0] * len(q)
		ww = [0] * len(w)
		for i in range(1, len(q)):
			qp = qq[i - 1]
			while qp > 0 and q[i] != q[qp]:
				qp = qq[qp - 1]
			qq[i] = qp + (1 if q[i] == q[qp] else 0)
		for i in range(1, len(w)):
			wp = ww[i - 1]
			while wp > 0 and w[i] != w[wp]:
				wp = ww[wp - 1]
			ww[i] = wp + (1 if w[i] == w[wp] else 0)
		qq = qq.pop()
		ww = ww.pop()
		if qq > ww:
			print(a[:qwqw] + q[:qq] + a[n - qwqw:])
		else:
			print(a[:qwqw] + w[:ww] + a[n - qwqw:])


class Solution:

	def manacher(self, s0: str) -> list:
		T = '$#' + '#'.join(s0) + '#@'
		l = len(T)
		P = [0] * l
		(R, C) = (0, 0)
		for i in range(1, l - 1):
			if i < R:
				P[i] = min(P[2 * C - i], R - i)
			while T[i + (P[i] + 1)] == T[i - (P[i] + 1)]:
				P[i] += 1
			if P[i] + i > R:
				R = P[i] + i
				C = i
		for i in reversed(range(len(P))):
			if P[i] > 0:
				start = (i - P[i]) // 2
				if start == 0:
					return s0[0:start + P[i]]

	def shortestPalindrome(self, s: str) -> str:
		if s == '':
			return ''
		return self.manacher(s)
slv = Solution()
n = int(input())
while n != 0:
	n -= 1
	s = input()
	l = len(s)
	L = 0
	R = l - 1
	while L < R:
		if s[L] == s[R]:
			L += 1
			R -= 1
		else:
			break
	if L < R:
		ss = s[L:R + 1]
		ss1 = slv.shortestPalindrome(ss)
		ss2 = slv.shortestPalindrome(''.join(list(reversed(ss))))
		if len(ss2) > len(ss1):
			ss1 = ss2
		print(s[:L] + ss1 + s[R + 1:])
	else:
		print(s)


printn = lambda x: print(x, end='')
inn = lambda : int(input())
inl = lambda : list(map(int, input().split()))
inm = lambda : map(int, input().split())
ins = lambda : input().strip()
DBG = True
BIG = 10 ** 18
R = 10 ** 9 + 7

def ddprint(x):
	if DBG:
		print(x)
t = inn()
for tt in range(t):
	s = ins()
	n = len(s)
	ok = True
	for k in range(n // 2):
		if s[k] != s[n - 1 - k]:
			ok = False
			break
	if ok:
		print(s)
		continue
	ok = False
	okp1 = okp2 = -1
	p = n - 1 - k
	while not ok and p > k:
		while s[p] != s[k]:
			p -= 1
			if p == k:
				break
		q = p - 1
		r = k + 1
		ok = True
		while r < q:
			if s[r] != s[q]:
				ok = False
				p -= 1
				break
			r += 1
			q -= 1
	if ok:
		okp1 = p
	ok = False
	p = k + 1
	while not ok and p < n - 1 - k:
		while s[p] != s[n - 1 - k]:
			p += 1
			if p == n - 1 - k:
				break
		q = p + 1
		r = n - k - 2
		ok = True
		while q < r:
			if s[r] != s[q]:
				ok = False
				p += 1
				break
			r -= 1
			q += 1
	if ok:
		okp2 = p
	if okp1 < 0 and okp2 < 0:
		print(s[:k + 1] + (s[n - k:] if k > 0 else ''))
	elif okp2 < 0:
		print(s[:okp1 + 1] + (s[n - k:] if k > 0 else ''))
	elif okp1 < 0:
		print((s[:k] if k > 0 else '') + s[okp2:])
	else:
		len1 = okp1 - k + 1
		len2 = n - k - okp2
		if len1 > len2:
			print(s[:okp1 + 1] + (s[n - k:] if k > 0 else ''))
		else:
			print((s[:k] if k > 0 else '') + s[okp2:])


def z_values(s):
	z = [len(s)]
	(l, r) = (0, 0)
	for i in range(1, len(s)):
		if i > r:
			(l, r) = (i, i)
			while r < len(s) and s[r] == s[r - l]:
				r += 1
			z.append(r - l)
		elif i + z[i - l] < r:
			z.append(z[i - l])
		else:
			l = i
			while r < len(s) and s[r] == s[r - l]:
				r += 1
			z.append(r - l)
	return z

def longest_palindromic_prefix(s):
	l = len(s)
	t = ''.join([s, '\x00', s[::-1]])
	z = z_values(t)
	k = 0
	for i in range(1, l + 1):
		if z[-i] == i:
			k = i
	return k
n = int(input())
for __ in range(n):
	s = input()
	l = len(s)
	i = 0
	while s[i] == s[l - i - 1] and i < l // 2:
		i += 1
	a = longest_palindromic_prefix(s[i:l - i])
	b = longest_palindromic_prefix(s[i:l - i][::-1])
	if a >= b:
		print(''.join([s[:i + a], s[l - i:]]))
	else:
		print(''.join([s[:i], s[l - i - b:]]))


t = 1
t = int(input())

def isPalindrome(s, i, j):
	while i <= j:
		if s[i] != s[j]:
			return False
		i += 1
		j -= 1
	return True
for _ in range(t):
	s = input()
	n = len(s)
	prefix = ''
	suffix = ''
	i = 0
	j = n - 1
	while i < j and s[i] == s[j]:
		prefix += s[i]
		suffix = s[i] + suffix
		i += 1
		j -= 1
	ans = ''
	for k in range(i, j + 1):
		if isPalindrome(s, i, k) and k - i + 1 > len(ans):
			ans = s[i:k + 1]
		if isPalindrome(s, k, j) and j - k + 1 > len(ans):
			ans = s[k:j + 1]
	ans = prefix + ans + suffix
	print(ans)


T = int(input())
for case in range(T):
	s = input()
	common = ''
	nc = 0
	for i in range(len(s) // 2):
		if s[i] == s[-i - 1]:
			common += s[i]
			nc = i + 1
		else:
			break
	if nc > 0:
		s = s[nc:-nc]
	check = 1
	lefta = ''
	righta = ''
	l = ''
	r = ''
	m = ''
	for (n, e) in enumerate(s):
		if len(m) == 0:
			try:
				m += r[-1]
				r = e + r[:-1]
			except:
				m = e
		else:
			l = l + m
			r = e + r
			m = ''
		if l == r:
			lefta = l + m + r[::-1]
	s = s[::-1]
	l = ''
	r = ''
	m = ''
	for (n, e) in enumerate(s):
		if len(m) == 0:
			try:
				m += r[-1]
				r = e + r[:-1]
			except:
				m = e
		else:
			l = l + m
			r = e + r
			m = ''
		if l == r:
			righta = l + m + r[::-1]
	if len(lefta) > len(righta):
		print(common + lefta + common[::-1])
	else:
		print(common + righta + common[::-1])


t = int(input())
for i in range(t):
	s = input()
	n = len(s)
	only_l = [0, '']
	only_r = [0, '']
	komb = [0, '']
	prost = [0, '']
	st = 0
	for j in range(n, -1, -1):
		if s[:j] == s[:j][::-1]:
			only_l = [j, s[:j]]
			break
	for j in range(n):
		if s[j:] == s[j:][::-1]:
			only_r = [n - j, s[j:]]
			break
	for j in range(n // 2, -1, -1):
		if s[:j] == s[::-1][:j]:
			if j == n // 2 and n % 2 == 0:
				prosto = [2 * j, s[:j] + s[::-1][:j][::-1]]
				st = j
			else:
				prosto = [2 * j + 1, s[:j] + s[j] + s[::-1][:j][::-1]]
				st = j
			break
	for j in range(st, -1, -1):
		strk = s[j:n - j]
		f = False
		for o in range(n - 2 * j, 1, -1):
			if strk[:o] == strk[:o][::-1]:
				f = True
				komb = [2 * j + o, s[:j + o] + s[::-1][:j][::-1]]
				break
			if strk[::-1][:o] == strk[::-1][:o][::-1]:
				f = True
				komb = [2 * j + o, s[:j] + s[::-1][:j + o][::-1]]
				break
		if f:
			break
	M = max(only_l[0], only_r[0], prosto[0], komb[0])
	if M == only_l[0]:
		print(only_l[1])
	elif M == only_r[0]:
		print(only_r[1])
	elif M == prosto[0]:
		print(prosto[1])
	else:
		print(komb[1])


from itertools import combinations
import sys
input = sys.stdin.readline

def palindrome(s):
	m = len(s)
	flg = 1
	for i in range(m // 2):
		if s[i] != s[-i - 1]:
			flg = 0
	return flg
t = int(input())
for _ in range(t):
	s = input().rstrip()
	n = len(s)
	ans = ''
	lenans = 0
	if palindrome(s):
		print(s)
		continue
	pre = ''
	suf = ''
	for i in range(n):
		if s[i] != s[-i - 1]:
			for j in range(i, n - i):
				w = s[i:j]
				if palindrome(w):
					ans = ''.join((s[:j], s[n - i:]))
					lenans = len(ans)
			for k in range(n - i - 1, i, -1):
				w = s[k:n - i]
				if palindrome(w) and len(w) + i * 2 >= lenans:
					ans = ''.join((s[:i], w, s[n - i:]))
					lenans = len(ans)
			break
	print(ans)


def ispalin(ss, s, e):
	while s < e:
		if ss[s] != ss[e]:
			return False
		s += 1
		e -= 1
	return True
for _ in range(int(input())):
	ss = input()
	s = 0
	e = len(ss) - 1
	i = 0
	while s < e and ss[s] == ss[e]:
		s += 1
		e -= 1
	if s == e:
		s += 1
	for le in range(e - s + 1, 0, -1):
		if ispalin(ss, s, s + le - 1):
			s = s + le
			break
		elif ispalin(ss, e - le + 1, e):
			e = e - le
			break
	print(ss[:s] + ss[e + 1:])


def is_palind(s):
	start = 0
	end = len(s) - 1
	valid = True
	while valid and start <= len(s) / 2:
		valid = s[start] == s[end]
		start += 1
		end -= 1
	return valid

def left_palind(s):
	start = 0
	end = len(s)
	found = False
	while not found and start < end:
		found = is_palind(s[:end])
		end -= 1
	return s[:end + 1]

def right_palind(s):
	start = 0
	end = len(s)
	found = False
	while not found and start < end:
		found = is_palind(s[start:end])
		start += 1
	return s[start - 1:end]

def paland(s):
	if len(s) <= 1 or is_palind(s):
		return s
	a = ''
	b = ''
	start = 0
	end = len(s) - 1
	valid = True
	while valid and start <= len(s) / 2:
		valid = s[start] == s[end]
		if valid:
			a = a + s[start]
			b = s[start] + b
			start += 1
			end -= 1
	if len(right_palind(s[start:end + 1])) > len(left_palind(s[start:end + 1])):
		return a + right_palind(s[start:end + 1]) + b
	else:
		return a + left_palind(s[start:end + 1]) + b
n_tests = int(input())
strings = []
for i in range(n_tests):
	strings.append(input())
for s in strings:
	print(paland(s))


import math
import sys
from collections import defaultdict, Counter, deque, OrderedDict
import bisect
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

def list2d(a, b, c):
	return [[c] * b for i in range(a)]
from itertools import accumulate, groupby
import sys
mod = 1000000007

def lpx(pat, patt):
	pat = pat + '#' + patt
	lenpat = len(pat)
	lps = [0] * lenpat
	l = 0
	i = 1
	while i < lenpat:
		if pat[i] == pat[l]:
			l += 1
			lps[i] = l
			i += 1
		elif l != 0:
			l = lps[l - 1]
		else:
			lps[i] = 0
			i += 1
	return lps[-1]

def findmax(S):
	i = 0
	j = len(S) - 1
	s = ''
	while i <= j:
		if S[i] == S[j]:
			s += S[i]
		else:
			return s
		i += 1
		j -= 1
	return s
for _ in range(int(input())):
	S = input()
	N = len(S)
	if N == 1:
		print(S)
	else:
		x = findmax(S)
		y = len(x)
		if N % 2 == 0:
			if y * 2 == N:
				print(S)
				continue
		elif y * 2 == N + 1:
			print(S)
			continue
		T = S[y:N - y]
		TT = T[::-1]
		t = lpx(T, TT)
		u = lpx(TT, T)
		if t > u:
			print(x + T[:t] + S[N - y:])
		else:
			print(x + T[len(T) - u:] + S[N - y:])


def pali(s):
	return s == s[::-1]

def check(s):
	self = ''
	l = len(s)
	for i in range(l):
		if pali(s[:i]):
			if len(s[:i]) > len(self):
				self = s[:i]
		if pali(s[i:]):
			if len(s[i:]) > len(self):
				self = s[i:]
	return self
for _ in range(int(input())):
	s = input()
	p = ''
	while s[0] == s[-1] and len(s) > 2:
		p = p + s[0]
		s = s[1:-1]
	p = p + check(s) + p[::-1]
	print(p)


def lp(s):
	n = len(s)
	(l, r) = (0, n - 1)
	while True:
		(i, j) = (0, r)
		while i < j and s[i] == s[j]:
			(i, j) = (i + 1, j - 1)
		if i >= j:
			return s[:r + 1]
		r -= 1
		(i, j) = (l, n - 1)
		while i < j and s[i] == s[j]:
			(i, j) = (i + 1, j - 1)
		if i >= j:
			return s[l:]
		l += 1

def problem(s):
	(i, j) = (0, len(s) - 1)
	while i < j and s[i] == s[j]:
		(i, j) = (i + 1, j - 1)
	if i == j:
		return s
	return s[:i] + lp(s[i:j + 1]) + s[j + 1:]
for _ in range(int(input())):
	print(problem(input()))


def calculate_PI(p):
	l = len(p)
	pi = [0]
	k = 0
	for i in range(1, l):
		while k > 0 and p[k] != p[i]:
			k = pi[k - 1]
		if p[k] == p[i]:
			k = k + 1
		pi.append(k)
	return pi[-1]
for _ in range(int(input())):
	s1 = input()
	n = len(s1)
	if n == 1:
		print(s1[0])
	else:
		i = 0
		while i != len(s1) // 2:
			if s1[i] == s1[n - i - 1]:
				i = i + 1
			else:
				break
		s = s1[i:n - i]
		n1 = len(s)
		a = calculate_PI(s + '#' + s[::-1])
		b = calculate_PI(s[::-1] + '#' + s)
		c = ''
		if a == 1 and b == 1:
			c = s[0]
		elif a > b:
			c = s[0:a]
		else:
			c = s[n1 - 1:n1 - 1 - b:-1]
		print(s1[0:i] + c + s1[0:i][::-1])


def getLPSFromIEq0_len(text):
	N = len(text)
	if N == 0:
		return
	N = 2 * N + 1
	L = [0] * N
	L[0] = 0
	L[1] = 1
	C = 1
	R = 2
	i = 0
	iMirror = 0
	maxLPSLength = 0
	maxLPSCenterPosition = 0
	start = -1
	end = -1
	diff = -1
	lpsFromIEq0Len = 1
	for i in range(2, N):
		iMirror = 2 * C - i
		L[i] = 0
		diff = R - i
		if diff > 0:
			L[i] = min(L[iMirror], diff)
		try:
			while (i + L[i] < N and i - L[i] > 0) and ((i + L[i] + 1) % 2 == 0 or text[(i + L[i] + 1) // 2] == text[(i - L[i] - 1) // 2]):
				L[i] += 1
		except Exception as e:
			pass
		if L[i] > maxLPSLength:
			maxLPSLength = L[i]
			maxLPSCenterPosition = i
		if i == L[i]:
			lpsFromIEq0Len = max(lpsFromIEq0Len, L[i])
		if i + L[i] > R:
			C = i
			R = i + L[i]
	return lpsFromIEq0Len

def main():
	q = int(input())
	allans = []
	for _ in range(q):
		s = input()
		i = 0
		j = len(s) - 1
		while i <= j:
			if s[i] == s[j]:
				i += 1
				j -= 1
			else:
				break
		if i > j:
			allans.append(s)
			continue
		middleS = s[i:j + 1]
		l1 = getLPSFromIEq0_len(middleS)
		l2 = getLPSFromIEq0_len(middleS[::-1])
		if l1 >= l2:
			innerPalindrome = middleS[:l1]
		else:
			innerPalindrome = middleS[-l2:]
		ans = s[:i] + innerPalindrome + s[j + 1:]
		allans.append(ans)
	multiLineArrayPrint(allans)
	return
import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def readIntArr():
	return [int(x) for x in input().split()]

def makeArr(defaultVal, dimensionArr):
	dv = defaultVal
	da = dimensionArr
	if len(da) == 1:
		return [dv for _ in range(da[0])]
	else:
		return [makeArr(dv, da[1:]) for _ in range(da[0])]

def queryInteractive(x, y):
	print('? {} {}'.format(x, y))
	sys.stdout.flush()
	return int(input())

def answerInteractive(ans):
	print('! {}'.format(ans))
	sys.stdout.flush()
inf = float('inf')
MOD = 10 ** 9 + 7
for _abc in range(1):
	main()


import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
from functools import lru_cache
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
if os.path.exists('out.txt'):
	sys.stdout = open('out.txt', 'w')

def input():
	return sys.stdin.readline()

def mapi(arg=0):
	return map(int if arg == 0 else str, input().split())
for _ in range(int(input())):
	a = input().strip()
	n = len(a)
	i = 0
	j = n - 1
	while i < j:
		if a[i] == a[j]:
			i += 1
			j -= 1
		else:
			break
	pf = list(a[:i])
	sf = list(a[j + 1:])
	a = a[i:j + 1]
	prep = []
	prep.append('#')
	for i in range(len(a)):
		prep.append(a[i])
		prep.append('#')
	n = len(prep)
	left = [0] * n
	(r, c) = (0, 0)
	for i in range(n):
		mirror = 2 * c - i
		if r > i:
			left[i] = min(r - i, left[mirror])
		while i + left[i] + 1 < n and i - left[i] - 1 >= 0 and (prep[i + left[i] + 1] == prep[i - left[i] - 1]):
			left[i] += 1
		if i + left[i] > r:
			c = i
			r = i + left[i]
	mxlen = 0
	center = 0
	for i in range(n):
		if left[i] > mxlen and (i == left[i] or i + left[i] >= n - 1):
			mxlen = left[i]
			center = i
	res = []
	for i in prep[center - mxlen:center + mxlen + 1]:
		if i == '#':
			continue
		res.append(i)
	res = pf + res + sf
	print(*res, sep='')


def prefixPalindrome(s):
	n = len(s)
	lst = [0] * n
	i = 1
	l = 0
	while i < n:
		if s[i] == s[l]:
			l += 1
			lst[i] = l
			i += 1
		elif l != 0:
			l = lst[l - 1]
		else:
			lst[i] = 0
			i += 1
	ans = lst[n - 1]
	ans = min(n // 2, ans)
	return s[:ans]

def longestPorSPalindrome(s):
	s1 = prefixPalindrome(s + '#' + s[::-1])
	s2 = prefixPalindrome(s[::-1] + '#' + s)
	if len(s1) > len(s2):
		return s1
	else:
		return s2
for _ in range(int(input())):
	s = input()
	n = len(s)
	l = 0
	r = n - 1
	while s[l] == s[r] and l < r:
		l += 1
		r -= 1
	if s[l] == s[r]:
		print(s)
		continue
	temp = s[0:l]
	a = s[l:r + 1]
	ans = temp + longestPorSPalindrome(a) + temp[::-1]
	print(ans)


def Z_Array(l):
	left = 0
	right = 0
	n = len(l)
	Z = [0] * n
	for i in range(1, n):
		if i > right:
			left = i
			right = i
			while right < n and l[right] == l[right - left]:
				right += 1
			Z[i] = right - left
			right -= 1
		else:
			k = i - left
			if Z[k] + i - 1 < right:
				Z[i] = Z[k]
			else:
				left = i
				while right < n and l[right] == l[right - left]:
					right += 1
				Z[i] = right - left
				right -= 1
	return Z

def Z_Algorithm(pattern, text):
	l = [p for p in pattern]
	l.append('#')
	l.extend([t for t in text])
	Z = Z_Array(l)
	return Z

def maximum_Length_Palindrome(Z, n):
	for i in range(n):
		if Z[i] == n - i:
			return Z[i]
for _ in range(int(input())):
	s = input()
	n = len(s)
	N = 2 * n + 1
	if n == 1 or s == s[::-1]:
		print(s)
	elif s[0] != s[-1]:
		ans1 = Z_Algorithm(s, s[::-1])
		ans2 = Z_Algorithm(s[::-1], s)
		ans1 = maximum_Length_Palindrome(ans1[N // 2 + 1:], n)
		ans2 = maximum_Length_Palindrome(ans2[N // 2 + 1:], n)
		if ans1 >= ans2:
			print(s[:ans1])
		else:
			s = s[::-1]
			print(s[:ans2])
	else:
		i = 0
		j = n - 1
		s1 = s[0:n]
		while i < j:
			if s[i] != s[j]:
				break
			else:
				i += 1
				j -= 1
		s = s[i:j + 1]
		n = len(s)
		N = 2 * n + 1
		ans1 = Z_Algorithm(s, s[::-1])
		ans2 = Z_Algorithm(s[::-1], s)
		ans1 = maximum_Length_Palindrome(ans1[N // 2 + 1:], n)
		ans2 = maximum_Length_Palindrome(ans2[N // 2 + 1:], n)
		if ans1 >= ans2:
			s = s[:ans1]
			if s == s[::-1]:
				print(s1[0:i] + s[:ans1] + s1[0:i][::-1])
		else:
			s = s[::-1]
			s = s[:ans2]
			if s == s[::-1]:
				print(s1[0:i] + s[:ans2] + s1[0:i][::-1])


from sys import stdin, stderr

def rl():
	return [int(w) for w in stdin.readline().split()]

def maxp(k):
	global s, n, e
	x = [True] * (n - k + 1)
	l = e2 = j = 0
	while True:
		for i in range(n - k + 1):
			if x[i]:
				e1 = min(i, n - i - k)
				if e1 <= e and 2 * e1 + k > l:
					l = 2 * e1 + k
					e2 = e1
					j = i
		x = [x[i + 1] and s[i] == s[i + k + 1] for i in range(n - k - 1)]
		if not any(x):
			break
		k += 2
	return (l, j, e2)
(t,) = rl()
for _ in range(t):
	s = stdin.readline().rstrip()
	n = len(s)
	if n < 2:
		print(s)
	else:
		e = 0
		n2 = n // 2
		while e <= n2 and s[e] == s[n - 1 - e]:
			e += 1
		(l, i, e) = max(maxp(0), maxp(1))
		print(s[:e] + s[i:i + l - 2 * e] + s[n - e:])


from collections import defaultdict, deque, Counter
from sys import stdin, stdout
from heapq import heappush, heappop
import math
import io
import os
import math
import bisect

def isPrime(x):
	for i in range(2, x):
		if i * i > x:
			break
		if x % i == 0:
			return False
	return True

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

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

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def digits(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def ceil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def mapin():
	return map(int, input().split())
t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	i = 0
	j = n - 1
	while i < j:
		if s[i] == s[j]:
			i += 1
			j -= 1
		else:
			break
	ans = [0, 0]
	anss = 0
	for kk in range(j + 1 - i):
		if s[i:kk + 1 + i] == s[i:kk + 1 + i][::-1] and kk + 1 > anss:
			anss = kk + 1
			ans = [i, kk + 1 + i]
	for kk in range(j + 1 - i):
		if s[j - kk:j + 1] == s[j - kk:j + 1][::-1] and kk + 1 > anss:
			anss = kk + 1
			ans = [j - kk, j + 1]
	print(s[:i] + s[ans[0]:ans[1]] + s[j + 1:])


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))
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())

def longest_palindrome_prefix(s):
	if not s:
		return 0
	s = s + '#' + s[::-1] + '$'
	i = 0
	j = -1
	nt = [0] * len(s)
	nt[0] = -1
	while i < len(s) - 1:
		if j == -1 or s[i] == s[j]:
			i += 1
			j += 1
			nt[i] = j
		else:
			j = nt[j]
	return nt[len(s) - 1]
for _ in range(n):
	s = input()
	left = 0
	right = len(s) - 1
	while left <= right:
		if s[left] == s[right]:
			left += 1
			right -= 1
		else:
			break
	if left > right:
		print(s)
		continue
	(pref, suff) = (s[:left], s[right + 1:])
	cut = s[left:right + 1]
	longest_left = cut[:longest_palindrome_prefix(cut)]
	cut = cut[::-1]
	longest_right = cut[:longest_palindrome_prefix(cut)]
	longest_right = longest_right[::-1]
	if len(longest_left) > len(longest_right):
		print(pref + longest_left + suff)
	else:
		print(pref + longest_right + suff)


t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	l = 0
	r = n - 1
	ct = 0
	pf = ''
	sf = ''
	mx1 = 0
	mid1 = ''
	mx2 = 0
	mid2 = ''
	while l < r:
		if s[l] == s[r]:
			pf += s[l]
			sf += s[r]
		else:
			break
		l += 1
		r -= 1
	mid = ''
	for i in range(l, r + 1):
		mid += s[i]
		if mid == mid[::-1]:
			mx1 = len(mid)
			mid1 = mid
	mid = ''
	for i in range(r, l - 1, -1):
		mid += s[i]
		if mid == mid[::-1]:
			mx2 = len(mid)
			mid2 = mid
	print(pf + (mid1 if mx1 > mx2 else mid2) + sf[::-1])


def reverse(s):
	return s[::-1]

def is_pal(s):
	rev = reverse(s)
	if s == rev:
		return True
	return False
t = int(input(''))
for _ in range(t):
	s = input('')
	if len(s) == 1:
		print(s)
		continue
	se = ss = ''
	i = 0
	n = len(s)
	while s[i] == s[n - 1 - i] and i < n / 2 and (i != n - 1 - i):
		ss += s[i]
		se = s[i] + se
		i += 1
	if i == n - i - 1:
		ss += s[i]
		print(ss + se)
		continue
	maxxlen = 0
	maxpal = ''
	for x in range(0, n - 2 * i):
		if is_pal(s[i:i + x]):
			if x + 1 > maxxlen:
				maxxlen = x
				maxpal = s[i:i + x]
	maxxlen1 = 0
	maxpal1 = ''
	for x in range(0, n - 2 * i):
		if is_pal(s[n - i - x:n - i]):
			if x + 1 > maxxlen1:
				maxxlen1 = x
				maxpal1 = s[n - i - x:n - i]
	if is_pal(ss + maxpal + maxpal1 + se):
		print(ss + maxpal + maxpal1 + se)
	elif is_pal(ss + maxpal + se) or is_pal(ss + maxpal1 + se):
		if is_pal(ss + maxpal + se):
			if len(ss + maxpal + se) > len(ss + maxpal1 + se):
				print(ss + maxpal + se)
			else:
				print(ss + maxpal1 + se)
		else:
			print(ss + maxpal1 + se)
	else:
		print(ss + se)


def LPS(s):
	s = s + '#' + s[::-1]
	n = len(s)
	dp = [0] * n
	j = 0
	i = 1
	while i < n:
		if s[i] == s[j]:
			dp[i] = j + 1
			i += 1
			j += 1
		elif j != 0:
			j = dp[j - 1]
		else:
			dp[i] = 0
			i += 1
	return s[:dp[-1]]
for _ in range(int(input())):
	s = input()
	l = 0
	n = len(s)
	while l < n // 2 and s[l] == s[-(l + 1)]:
		l += 1
	pp = LPS(s[l:n - l])
	ss = LPS(s[l:n - l][::-1])
	if len(ss) > len(pp):
		print(s[:l] + ss + s[:l][::-1])
	else:
		print(s[:l] + pp + s[:l][::-1])


import sys
sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]

def main():
	q = II()
	for _ in range(q):
		s = SI()
		n = len(s)
		mid = ''
		for i in range(n):
			if i > n - 1 - i or s[i] != s[n - 1 - i]:
				break
		if i >= n - 1 - i:
			print(s)
			continue
		pre = s[:i]
		t = s[i:n - i]
		tn = n - i - i
		for w in range(tn, 1, -1):
			w2 = w // 2
			if t[:w2] == t[w - w2:w][::-1]:
				mid = t[:w]
				break
			if t[tn - w:tn - w + w2] == t[tn - w2:tn][::-1]:
				mid = t[tn - w:tn]
				break
		if mid == '':
			mid = t[0]
		print(pre + mid + pre[::-1])
main()


qn = int(input())
for loop in range(qn):
	s = list(input())
	n = len(s)
	lastif = None
	for i in range((n + 1) // 2):
		if s[i] == s[-1 - i]:
			continue
		else:
			lastif = i
			break
	if lastif == None:
		print(''.join(s))
		continue
	form = s[:lastif]
	back = s[n - lastif:]
	midsub = s[lastif:n - lastif]
	mid = ['']
	for i in midsub:
		mid.append(i)
		mid.append('')
	R = [None] * len(mid)
	i = 0
	j = 0
	while i < len(mid):
		while i - j >= 0 and i + j < len(mid) and (mid[i - j] == mid[i + j]):
			j += 1
		R[i] = j
		k = 1
		while i - k >= 0 and i + k < len(mid) and (k + R[i - k] < j):
			R[i + k] = R[i - k]
			k += 1
		i += k
		j -= k
	maxind = 0
	for i in range(len(mid)):
		if (i + 1 == R[i] or R[i] == len(mid) - i) and R[maxind] < R[i]:
			maxind = i
	for i in range(maxind - R[maxind] + 1, maxind + R[maxind]):
		form.append(mid[i])
	ans = form + back
	print(''.join(ans))


try:
	tc = int(input())
except:
	exit()

def reverse(s):
	return s[::-1]

def isPalindrome(s):
	rev = reverse(s)
	if s == rev:
		return True
	return False
for _ in range(tc):
	s = input()
	i = 0
	j = len(s) - 1
	pre = []
	suf = []
	while i <= j:
		if s[i] == s[j]:
			if i == j:
				pre.append(s[i])
			else:
				pre.append(s[i])
				suf.append(s[j])
			i += 1
			j -= 1
		else:
			break
	if i <= j:
		mystr = s[i:j + 1]
		temp = ''
		ans = 0
		res1 = ''
		for i in range(len(mystr)):
			temp += mystr[i]
			if isPalindrome(temp):
				res1 = temp
		temp = ''
		res2 = ''
		for i in range(len(mystr) - 1, -1, -1):
			temp += mystr[i]
			if isPalindrome(temp):
				res2 = temp
		if len(res1) < len(res2):
			res1 = res2
		for i in range(len(res1)):
			pre.append(res1[i])
	for i in range(len(pre)):
		print(pre[i], end='')
	for i in range(len(suf) - 1, -1, -1):
		print(suf[i], end='')
	print()


t = int(input())
for query in range(t):
	s = list(input())
	n = len(s)
	c = 0
	while s[c] == s[n - c - 1] and n / 2 > c + 1:
		c = c + 1
	e = 1
	f = 1
	for b in range(c, n - c):
		for d in range(c, b + 1):
			if s[d] != s[b - d + c]:
				e = 0
		if e:
			f = b - c + 1
		e = 1
	g = 1
	h = 1
	for b in range(n - c - 1, c - 1, -1):
		for d in range(n - c - 1, b - 1, -1):
			if s[d] != s[b - d + n - c - 1]:
				g = 0
		if g:
			h = n - c - b
		g = 1
	for i in range(c):
		print(s[i], end='')
	if f > h:
		for j in range(c, c + f):
			print(s[j], end='')
	else:
		for k in range(n - c - h, n - c):
			print(s[k], end='')
	for l in range(n - c, n):
		print(s[l], end='')
	print('')


def ispal(s):
	for i in range(len(s) // 2):
		if s[i] != s[-(i + 1)]:
			return False
	return True

def chunk(s):
	n = len(s)
	if n == 1:
		return ('', s, '')
	for i in range(n // 2):
		if s[i] != s[-(i + 1)]:
			break
	return (s[:i], s[i:n - i], s[n - i:])

def longest(s):
	n = len(s)
	for i in range(n):
		s0 = s[:n - i]
		if ispal(s0):
			return s0

def solve(s):
	(pre, mid, suf) = chunk(s)
	m1 = longest(mid)
	m2 = longest(mid[::-1])
	m = m1 if len(m1) >= len(m2) else m2
	return pre + m + suf
t = int(input().strip())
for _ in range(t):
	s = input().strip()
	print(solve(s))


def manacher(s):
	t = ['#']
	for i in range(len(s)):
		t.append(s[i])
		t.append('#')
	n = len(t)
	r = [0] * n
	i = 0
	j = 0
	while i < n:
		while i - j >= 0 and i + j < n and (t[i - j] == t[i + j]):
			j += 1
		r[i] = j
		k = 1
		while i - k >= 0 and i + k < n and (k + r[i - k] < j):
			r[i + k] = r[i - k]
			k += 1
		i += k
		j -= k
	return r
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	s = input()[:-1]
	n = len(s)
	max_ = 0
	for i in range(n + 1):
		if i >= n - i - 1:
			break
		if s[i] == s[n - i - 1]:
			max_ += 1
		else:
			break
	man = manacher(s)
	ans = 0
	tmp = (0, 0)
	for i in range(max_ * 2 + 1, 2 * n + 1 - max_ * 2):
		l = i - (man[i] - 1)
		r = i + (man[i] - 1)
		l = l // 2
		r = r // 2
		diff = max(0, r - (n - max_), max_ - l)
		if r - diff == n - max_ or l + diff == max_:
			pass
		else:
			continue
		if r - diff - (l + diff) > ans:
			ans = r - diff - (l + diff)
			tmp = (l + diff, r - diff)
	(l, r) = tmp
	print(s[0:max_] + s[l:r] + s[n - max_:n])


for i in range(int(input())):
	s = input()
	n = len(s)
	i = 0
	x = ''
	ans = ''
	ans1 = ''
	while i < n:
		x += s[i]
		if x == x[::-1]:
			ans1 = x
		i += 1
	i = n - 1
	x = ''
	ans2 = ''
	while i >= 0:
		x = s[i] + x
		if x == x[::-1]:
			ans2 = x
		i -= 1
	x = ''
	y = ''
	i = 0
	j = n - 1
	while i < j and s[i] == s[j]:
		x += s[i]
		y = s[j] + y
		i += 1
		j -= 1
	p = j
	ans3 = ''
	z = ''
	while p >= i:
		z = s[p] + z
		g = x + z + y
		if g == g[::-1]:
			ans3 = g
		p -= 1
	p = i
	ans4 = ''
	z = ''
	while p <= j:
		z += s[p]
		g = x + z + y
		if g == g[::-1]:
			ans4 = g
		p += 1
	if len(ans1) > len(ans):
		ans = ans1
	if len(ans2) > len(ans):
		ans = ans2
	if len(ans3) > len(ans):
		ans = ans3
	if len(ans4) > len(ans):
		ans = ans4
	print(ans)


t = int(input())

def isPlaindrome(str1):
	n = len(str1)
	for i in range(n // 2):
		if str1[i] != str1[-i - 1]:
			return False
	return True
for _ in range(t):
	s1 = str(input())
	n = len(s1)
	arr = []
	ind = -1
	for i in range(n // 2):
		if s1[i] == s1[-i - 1]:
			arr.append(s1[i])
		else:
			ind = i
			break
	if ind == -1:
		print(s1)
	else:
		s = s1[ind:n - ind]
		a = n - ind - ind
		ans = 0
		str1 = []
		realAns = ''
		for i in range(a):
			str1.append(s[i])
			temp = isPlaindrome(str1)
			if temp == True and i + 1 > ans:
				ans = i + 1
				realAns = [ind, ind + i]
		str1 = []
		for i in range(a):
			str1.append(s[-i - 1])
			temp = isPlaindrome(str1)
			if temp == True and i + 1 > ans:
				ans = i + 1
				realAns = [n - ind - 1 - i, n - ind - 1]
		if ind > 0:
			ans = s1[:ind] + s1[realAns[0]:realAns[1] + 1] + s1[-ind:]
		else:
			ans = s[realAns[0]:realAns[1] + 1]
		print(ans)


n = int(input())

def Ze(s):
	s = s + '#' + s[::-1]
	Z = [0 for i in range(len(s))]
	l = 0
	r = 0
	for i in range(1, len(s)):
		if i <= r:
			Z[i] = min(r - i + 1, Z[i - l])
		while i + Z[i] < len(s) and s[Z[i]] == s[i + Z[i]]:
			Z[i] += 1
		if i + Z[i] - 1 > r:
			l = i
			r = i + Z[i] - 1
	ans = 0
	for i in range((len(s) - 1) // 2 + 1, len(s)):
		if Z[i] > ans and Z[i] + i == len(s):
			ans = Z[i]
	return ans
for i in range(n):
	s = input()
	a = -1
	for j in range(len(s) // 2):
		if s[j] != s[-j - 1]:
			a = j
			break
	if a == -1:
		a = len(s) // 2
	mpp = Ze(s[a:len(s) - a])
	msp = Ze(s[::-1][a:len(s) - a])
	if mpp > msp:
		print(s[:a] + s[a:a + mpp] + s[len(s) - a:])
	else:
		print(s[:a] + s[len(s) - a - msp:len(s) - a] + s[len(s) - a:])


def help(a):
	s = a + '#' + a[::-1]
	c = 0
	pref = [0] * len(s)
	for i in range(1, len(s)):
		while c != 0 and s[c] != s[i]:
			c = pref[c - 1]
		if s[i] == s[c]:
			c += 1
		pref[i] = c
	return a[:c]
for _ in range(int(input())):
	s = input()
	n = len(s)
	count = 0
	i = 0
	j = n - 1
	while j > i:
		if s[j] != s[i]:
			break
		count += 1
		i += 1
		j -= 1
	posi1 = help(s[count:n - count])
	posi2 = help(s[count:n - count][::-1])
	print(s[:count] + (posi1 if len(posi1) > len(posi2) else posi2) + s[:count][::-1])


def isPalindrome(s):
	for i in range(len(s) // 2):
		if s[i] != s[-i - 1]:
			return 0
	return 1
for _ in range(int(input())):
	s = input()
	k1 = -1
	k2 = len(s) - 1
	for i in range(len(s) // 2):
		if s[i] != s[-i - 1]:
			k1 = i
			k2 = len(s) - i - 1
			break
	if k1 == -1:
		print(s)
	else:
		s1 = s[k1:k2 + 1]
		s2 = s1[::-1]
		maxl = 1
		maxl1 = 1
		for k in range(1, len(s1)):
			if isPalindrome(s1[:-k]):
				maxl = len(s1) - k
				break
		for k in range(1, len(s2)):
			if isPalindrome(s2[:-k]):
				maxl1 = len(s2) - k
				break
		if maxl > maxl1:
			print(s[:k1 + maxl] + s[k2 + 1:])
		else:
			print(s[:k1] + s2[:maxl1] + s[k2 + 1:])


import sys
input = sys.stdin.readline

def print(val):
	sys.stdout.write(val)

def prog():
	for _ in range(int(input())):
		s = input().strip()
		i = -1
		for i in range(0, len(s) // 2):
			if s[i] != s[-i - 1]:
				i = i - 1
				break
		end = s[:i + 1]
		l = i + 1
		r = len(s) - i - 2
		new_s = s[l:r + 1] + '%' + s[r:l:-1] + s[l]
		pf = [0]
		for i in range(1, len(new_s)):
			L = pf[i - 1]
			while True:
				if new_s[L] == new_s[i]:
					pf.append(L + 1)
					break
				elif L == 0:
					pf.append(int(new_s[L] == new_s[i]))
					break
				else:
					L = pf[L - 1]
		length = pf[-1]
		new_s = s[r:l:-1] + s[l] + '%' + s[l:r + 1]
		pf = [0]
		for i in range(1, len(new_s)):
			L = pf[i - 1]
			while True:
				if new_s[L] == new_s[i]:
					pf.append(L + 1)
					break
				elif L == 0:
					pf.append(int(new_s[L] == new_s[i]))
					break
				else:
					L = pf[L - 1]
		print(end)
		if length >= pf[-1]:
			print(s[l:l + length])
		else:
			print(s[r:r - pf[-1]:-1])
		print(end[::-1])
		sys.stdout.write('\n')
prog()


def getMaximumPreffixPalindrome(line):
	answer = ''
	d = {}
	begin = end = ''
	for i in range(len(line)):
		begin = begin + line[i]
		end = line[i] + end
		if begin in d:
			d[begin] += 1
		else:
			d[begin] = 1
		if end in d:
			d[end] += 1
		else:
			d[end] = 1
		if d[begin] > 1 and len(begin) > len(answer):
			answer = begin
	return answer

def getMaximumSuffixPalindrome(line):
	answer = ''
	d = {}
	begin = end = ''
	for i in range(len(line) - 1, -1, -1):
		begin = begin + line[i]
		end = line[i] + end
		if begin in d:
			d[begin] += 1
		else:
			d[begin] = 1
		if end in d:
			d[end] += 1
		else:
			d[end] = 1
		if d[begin] > 1 and len(begin) > len(answer):
			answer = begin
	return answer

def getAnswer(line):
	left = ''
	right = ''
	i = 0
	j = len(line) - 1
	while i <= j:
		if line[i] == line[j]:
			left += line[i]
			if i < j:
				right = line[i] + right
			i += 1
			j -= 1
		else:
			l = line[i:j + 1]
			preffix = getMaximumPreffixPalindrome(l)
			suffix = getMaximumSuffixPalindrome(l)
			if len(preffix) > len(suffix):
				left += preffix
			else:
				left += suffix
			break
	return left + right
kt = int(input())
for t in range(kt):
	line = input()
	print(getAnswer(line))


def solve():
	a = input()
	b = a[::-1]
	j = 0
	while a[j] == b[j] and j * 2 < len(a) - 1:
		j += 1
	c = a[:j]
	b = b[j:len(b) - j]
	a = a[j:len(a) - j]
	ansa = 0
	for i in range(len(a)):
		if a[0:i + 1] == b[len(b) - (i + 1):len(b)]:
			ansa = i + 1
	ansb = 0
	for i in range(len(b)):
		if b[0:i + 1] == a[len(b) - (i + 1):len(b)]:
			ansb = i + 1
	if ansa > ansb:
		print(c + a[0:ansa] + c[::-1])
	else:
		print(c + b[0:ansb] + c[::-1])
n = int(input())
for i in range(n):
	solve()


from math import factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush
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))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 1000000007
INF = float('inf')

def main():
	for _ in range(N()):
		s = input()
		(b, e) = (0, len(s) - 1)
		res = []
		while b < e:
			if s[b] == s[e]:
				res.append(s[b])
			else:
				break
			b += 1
			e -= 1
		if b >= e:
			print(s)
		else:
			res = ''
			i = b
			while i < e + 1:
				pa = s[b:i]
				pb = s[i:e + 1]
				if s[b] == s[i - 1] and pa == pa[::-1]:
					if len(pa) > len(res):
						res = pa
				if s[i] == s[e] and pb == pb[::-1]:
					if len(pb) > len(res):
						res = pb
					if i < (b + e) // 2:
						i = b + len(pb)
				i += 1
			print(s[:b] + res + s[e + 1:])
main()


import os
import sys
from io import BytesIO, IOBase

def main():

	def find_pal_from_edge(a):
		s = []
		n = len(a)
		if n == 1:
			return a
		elif n == 0:
			return 0
		for i in range(n):
			s.append('#')
			s.append(a[i])
		s.append('#')
		p = [0] * len(s)
		c = 0
		r = 0
		max_len = 0
		for i in range(1, len(s)):
			if i < r:
				mirror = 2 * c - i
				p[i] = min(p[mirror], r - i)
			a = i + p[i] + 1
			b = i - p[i] - 1
			while a < len(s) and b >= 0 and (s[a] == s[b]):
				a += 1
				b -= 1
				p[i] += 1
			if p[i] >= max_len:
				if i + p[i] == len(s) - 1 or i - p[i] == 0:
					max_len = p[i]
					c = i
					r = i + p[i]
		m = ''.join(s[c - max_len:c + max_len + 1])
		m = ''.join(m.split('#'))
		return m
	for t in range(int(input())):
		s = input()
		n = len(s)
		i = 0
		j = n - 1
		flag = True
		while i < j:
			if s[i] == s[j]:
				i += 1
				j -= 1
			else:
				flag = False
				break
		if flag:
			ans = s
		else:
			ans = s[:i] + find_pal_from_edge(s[i:j + 1]) + s[j + 1:]
		print(ans)
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, gettrace
if not gettrace():

	def input():
		return next(stdin)[:-1]

def main():

	def solve():
		ss = input()
		n = len(ss)
		for i in range(n // 2):
			if ss[i] != ss[n - i - 1]:
				break
		else:
			i = n // 2
		if i * 2 == n:
			print(ss[:i] + ss[n - i:])
			return
		besta = 0
		for j in range(n - 2 * i):
			for k in range(j // 2 + 1):
				if ss[i + k] != ss[i + j - k]:
					break
			else:
				besta = j
		bestb = 0
		for j in range(besta, n - 2 * i):
			for k in range(j // 2 + 1):
				if ss[n - i - k - 1] != ss[n - i - j + k - 1]:
					break
			else:
				bestb = j
		if bestb > besta:
			print(ss[:i] + ss[n - i - bestb - 1:])
		else:
			print(ss[:i + besta + 1] + ss[n - i:])
	q = int(input())
	for _ in range(q):
		solve()
main()


import sys
inp = sys.stdin.readline

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

def iin():
	return int(input())

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

def main():

	def red(ans):
		return [i for i in ans if i != '$']
	T = iin()
	while T:
		T -= 1
		s = list(input())
		s1 = ['$']
		for i in s:
			s1.extend([i, '$'])
		s = s1
		n = len(s)
		ans = [s[1]]
		l = 1
		for i in range(1, n):
			j = 0
			while j + i < n and i - j >= 0 and (s[i + j] == s[i - j]):
				j += 1
			j -= 1
			x = i - j
			x1 = x
			(l, r) = (0, n - 1)
			while x and l < r:
				if s[l] != s[r]:
					break
				if r <= i + j:
					break
				l += 1
				r -= 1
				x -= 1
			else:
				if x:
					continue
				x1 = red(s[:i + j + 1] + (s[-x1:] if x1 else []))
				if len(ans) < len(x1):
					ans = x1
		s = s[::-1]
		for i in range(1, n):
			j = 0
			while j + i < n and i - j >= 0 and (s[i + j] == s[i - j]):
				j += 1
			j -= 1
			x = i - j
			x1 = x
			(l, r) = (0, n - 1)
			while x and l < r:
				if s[l] != s[r]:
					break
				if r <= i + j:
					break
				l += 1
				r -= 1
				x -= 1
			else:
				if x:
					continue
				x1 = red(s[:i + j + 1] + (s[-x1:] if x1 else []))
				if len(ans) < len(x1):
					ans = x1
		ans = red(ans)
		print(''.join(ans))
main()


from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
	return int(input())

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

def menacher(S):
	S = '#' + ''.join([i + '#' for i in S])
	n = len(S)
	P = [0] * n
	c = 0
	r = 0
	for i in range(n):
		mirror = 2 * c - i
		if i < r:
			P[i] = min(r - i, P[mirror])
		a = i + 1 + P[i]
		b = i - 1 - P[i]
		while a < n and b >= 0 and (S[a] == S[b]):
			P[i] += 1
			a += 1
			b -= 1
		if i + P[i] > r:
			c = i
			r = i + P[i]
	return (P[1::2], P[2::2])

def solve():
	s = input()
	(lo, hi) = (0, len(s) - 1)
	pref = 0
	while s[lo] == s[hi]:
		pref += 1
		lo += 1
		hi -= 1
		if lo > hi:
			print(s)
			return
	(P1, P2) = menacher(s[lo:hi + 1])
	longest = 0
	longest_term = (0, 0)
	for (i, l) in enumerate(P1):
		if l <= longest:
			continue
		elif i == (l - 1) // 2 or len(P1) - i - 1 == (l - 1) // 2:
			longest = l
			begin = lo + i - (l - 1) // 2
			longest_term = (begin, begin + l)
	for (i, l) in enumerate(P2):
		if l <= longest:
			continue
		elif i == l // 2 - 1 or len(P2) - i - 1 == l // 2:
			longest = l
			begin = lo + i - l // 2 + 1
			longest_term = (begin, begin + l)
	(l, h) = longest_term
	if pref > 0:
		print(s[:pref] + s[l:h] + s[-pref:])
	else:
		print(s[l:h])
	return
mode = 'T'
if mode == 'T':
	t = ri()
	for i in range(t):
		solve()
else:
	solve()


def isPali(s, i, j):
	half = (j - i + 1) // 2
	for k in range(half):
		if s[i + k] != s[j - k]:
			return False
	return True
for t in range(int(input())):
	s = input()
	ls = len(s)
	(i, j) = (0, ls - 1)
	if isPali(s, i, j):
		print(s)
		continue
	while s[i] == s[j]:
		i += 1
		j -= 1
	pre = s[:i]
	post = s[j + 1:]
	r = s[i:j + 1]
	lr = len(r)
	conj = r[0]
	maxLc = 1
	for lc in range(2, lr):
		if isPali(r, 0, lc - 1) or isPali(r, lr - lc, lr - 1):
			maxLc = lc
	if isPali(r, 0, maxLc - 1):
		conj = r[0:maxLc]
	else:
		conj = r[lr - maxLc:lr]
	ans = pre + conj + post
	print(ans)


n = int(input())
s = []
for i in range(n):
	s.append(input())
for string in s:
	k = len(string)
	i = 0
	j = k - 1
	while i < j and string[i] == string[j]:
		i += 1
		j -= 1
	prefix = string[:i]
	suffix = string[j + 1:]
	mid = string[i:j + 1]
	if len(mid) == 0:
		print(prefix + suffix)
		continue
	p_start = ''
	p_end = ''
	temp = ''
	for c in mid:
		temp += c
		if temp == temp[::-1]:
			p_start = temp
	temp = ''
	for c in mid[::-1]:
		temp += c
		if temp == temp[::-1]:
			p_end = temp
	p_end = p_end[::-1]
	if len(p_start) > len(p_end):
		mid = p_start
	else:
		mid = p_end
	print(prefix + mid + suffix)


from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

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

def iinp():
	return int(inp())

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def smp():
	return map(str, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def remadd(x, y):
	return 1 if x % y else 0

def ceil(a, b):
	return (a + b - 1) // b
S1 = 'abcdefghijklmnopqrstuvwxyz'
S2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True

def pal(s):
	for i in range(len(s) // 2):
		if s[i] != s[len(s) - 1 - i]:
			return False
	return True
for _ in range(int(inp())):
	s = inp()
	n = len(s)
	ans = []
	i = 0
	j = n - 1
	while i < j and s[i] == s[j]:
		ans += [s[i]]
		i += 1
		j -= 1
	if i == j:
		print(s)
		continue
	mx1 = 0
	for k in range(i + 1, j + 1):
		if pal(s[i:k]):
			mx1 = max(mx1, k - i)
	mx2 = 0
	for k in range(j, i, -1):
		if pal(s[k:j + 1]):
			mx2 = max(mx2, j - k + 1)
	if mx1 > mx2:
		print(''.join(ans) + s[i:i + mx1] + ''.join(ans[::-1]))
	else:
		print(''.join(ans) + s[j - mx2 + 1:j + 1] + ''.join(ans[::-1]))


class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)
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))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi
for _ in range(int(input())):
	i = 0
	s = input()
	j = len(s) - 1
	while i < j:
		if s[i] == s[j]:
			i += 1
			j -= 1
		else:
			break
	ans = ['', '', '']
	ans[0] = s[:i]
	ans[2] = ans[0][::-1]
	s = s[i:j + 1]
	if not len(s):
		print(''.join(ans))
		continue
	st = s + '#' + s[::-1]
	mx = pre(st)[-1]
	ans[1] = s[:mx]
	s = s[::-1]
	st = s + '#' + s[::-1]
	mx = pre(st)[-1]
	if mx > len(ans[1]):
		ans[1] = s[:mx]
	print(''.join(ans))


from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline
M = mod = 998244353

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip('\n').split()]

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

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n')]

def li3():
	return [int(i) for i in input().rstrip('\n')]
for _ in range(val()):
	s = st()
	ans = ''
	n = len(s)
	if n == 1:
		print(s)
		continue
	(i, j) = (0, n - 1)
	while i < j and s[i] == s[j]:
		i += 1
		j -= 1
	sidestring = s[:i]
	mid = ''
	for k in range(i, j + 1):
		curr = s[i:k + 1]
		if curr == curr[::-1] and len(curr) > len(mid):
			mid = curr
	for k in range(j, i - 1, -1):
		curr = s[k:j + 1]
		if curr == curr[::-1] and len(curr) > len(mid):
			mid = curr
	ans2 = sidestring + mid + sidestring[::-1]
	if len(ans2) > len(ans):
		ans = ans2
	print(ans)


MOD = 1000000007
MOD2 = 998244353
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
let = '@abcdefghijklmnopqrstuvwxyz'

def LPsubstr(string):
	maxLength = 1
	start = 0
	length = len(string)
	low = 0
	high = 0
	for i in range(1, length):
		low = i - 1
		high = i
		while low >= 0 and high < length and (string[low] == string[high]):
			if high - low + 1 > maxLength and (high == length - 1 or low == 0):
				start = low
				maxLength = high - low + 1
			low -= 1
			high += 1
		low = i - 1
		high = i + 1
		while low >= 0 and high < length and (string[low] == string[high]):
			if high - low + 1 > maxLength and (high == length - 1 or low == 0):
				start = low
				maxLength = high - low + 1
			low -= 1
			high += 1
	return string[start:start + maxLength]
for _ in range(ii()):
	s = si()
	n = len(s)
	(i, j) = (0, n - 1)
	while i < j:
		if s[i] != s[j]:
			break
		i += 1
		j -= 1
	print(s[:i] + LPsubstr(s[i:j + 1]) + s[j + 1:])


import sys
input = sys.stdin.readline

def manacher(s):
	center = 0
	mx = 0
	r = [1]
	t = '^#'
	for c in s[0:]:
		t += c + '#'
	t += '$'
	maxlen = 1
	maxcenter = 1
	length_t = len(t)
	length_s = len(s)
	for i in range(1, length_t - 1):
		if i < mx:
			r.append(min(mx - i, r[2 * idx - i]))
		else:
			r.append(1)
		while t[i - r[i]] == t[i + r[i]]:
			r[i] += 1
		if mx < i + r[i]:
			(mx, idx) = (i + r[i], i)
		if r[i] > maxlen and ((i - r[i]) // 2 == 0 or (i - r[i]) // 2 + r[i] - 1 == length_s):
			maxlen = r[i]
			maxcenter = i
	return s[(maxcenter - maxlen) // 2:(maxcenter - maxlen) // 2 + maxlen - 1]
for test in range(int(input())):
	s = input().strip()
	l = 0
	r = len(s) - 1
	(L, R) = ('', '')
	while l < r and s[l] == s[r]:
		L += s[l]
		R += s[r]
		l += 1
		r -= 1
	MID = manacher(s[l:r + 1])
	sys.stdout.write(''.join(L) + ''.join(MID) + ''.join(R[::-1]) + '\n')


testCases = int(input())
for i1 in range(testCases):
	sequence = input()
	sequenceR = sequence[::-1]
	length = len(sequence)
	ends = 0
	if length % 2 == 0:
		while ends < length / 2 and sequence[ends] == sequenceR[ends]:
			ends += 1
	else:
		while ends < (length - 1) / 2 and sequence[ends] == sequenceR[ends]:
			ends += 1
	sequenceLeft = sequence[ends:length - ends]
	left = ''
	right = ''
	for i2 in range(len(sequenceLeft) + 1):
		temp = sequenceLeft[0:i2]
		if temp == temp[::-1]:
			left = temp
	for i2 in range(len(sequenceLeft) + 1):
		temp = sequenceLeft[len(sequenceLeft) - i2:len(sequenceLeft)]
		if temp == temp[::-1]:
			right = temp
	result = ''
	if len(left) >= len(right):
		result = sequence[0:ends] + left + sequence[length - ends:length]
	else:
		result = sequence[0:ends] + right + sequence[length - ends:length]
	print(result)


def solv():
	s = input()
	a = ''
	b = ''
	p = 0
	if len(s) == 1:
		print(s)
		return
	q = len(s) - 1
	while p < q and s[p] == s[q]:
		a += s[p]
		b += s[q]
		p += 1
		q -= 1
	nw = ''
	mx = 0
	tm = ''
	b = b[::-1]
	for n in range(p, q + 1):
		tm += s[n]
		if tm == tm[::-1]:
			nw = tm
	tm = ''
	vv = ''
	for n in range(q, p, -1):
		tm += s[n]
		if tm == tm[::-1]:
			vv = tm
	res = nw if len(nw) > len(vv) else vv
	print(a + res + b)
for n in range(int(input())):
	solv()


t = int(input())
for _ in range(t):
	ss = input().strip()
	n = len(ss)
	r = ('', 0)
	for s in [ss, ss[::-1]]:
		for i in range(n - 1, -1, -1):
			(j, k) = (0, n - 1)
			ff = 1
			c = 0
			rr = []
			tmp = ''
			while j <= i and j <= k:
				if s[j] != s[k]:
					if k >= i + 1:
						k = i
						continue
					ff = 0
					break
				if j != k:
					c += 2
					rr.append(s[j])
				else:
					c += 1
					tmp = s[j]
				j += 1
				k -= 1
			if ff and c > r[1]:
				tt = ''.join(rr)
				r = (tt + tmp + tt[::-1], c)
	print(r[0])


for __ in range(int(input())):
	s = input()
	n = len(s)
	k = 0
	j = n - 1
	left = ''
	right = ''
	while k < j:
		if s[k] == s[j]:
			left += s[k]
			right += s[j]
		else:
			break
		k += 1
		j -= 1
	rem = s[k:j + 1]
	mid = ''
	for a in range(len(rem)):
		if rem[:a + 1] == rem[:a + 1][::-1] and len(rem[:a + 1]) > len(mid):
			mid = rem[:a + 1]
	rem = rem[::-1]
	for a in range(len(rem)):
		if rem[:a + 1] == rem[:a + 1][::-1] and len(rem[:a + 1]) > len(mid):
			mid = rem[:a + 1]
	print(left + mid + right[::-1])


def either_pal(p):
	left = p[0]
	j = -2
	while j > -len(p) - 1:
		if p[j] == p[0]:
			if p[:j + 1] == p[j::-1]:
				left = p[:j + 1]
				break
		j = j - 1
	right = p[-1]
	i = 1
	while i < len(p):
		if p[i] == p[-1]:
			if p[i:] == p[-1:i - 1:-1]:
				right = p[i:]
				break
		i = i + 1
	if len(left) > len(right):
		return left
	else:
		return right
t = int(input())
for _ in range(t):
	s = str(input())
	i = 0
	j = -1
	while i < len(s) and -j < len(s) + 1 and (s[i] == s[j]) and (i - j < len(s)):
		i = i + 1
		j = j - 1
	if i - j >= len(s):
		ans = s
	elif i == 0 and j == -1:
		ans = either_pal(s)
	else:
		ans = s[:i] + either_pal(s[i:j + 1]) + s[j + 1:]
	print(ans)


import sys

def isPalindrome(x):
	return x == x[::-1]
t = int(input())
for _ in range(t):
	s = input()
	length = len(s)
	if length == 1:
		print(s)
		continue
	l = 0
	r = length - 1
	while r > l:
		if s[l] == s[r]:
			l += 1
			r -= 1
		else:
			break
	if l > r:
		print(s)
		continue
	rs = s[l:r + 1]
	lt = 0
	rt = r - l
	ort = r - l
	while rt > lt:
		if rs[lt] == rs[rt]:
			lt += 1
			rt -= 1
		else:
			lt = 0
			ort -= 1
			rt = ort
	lGreatest = rs[:ort + 1]
	lt = 0
	rt = r - l
	olt = 0
	while rt > lt:
		if rs[lt] == rs[rt]:
			lt += 1
			rt -= 1
		else:
			rt = r - l
			olt += 1
			lt = olt
	rGreatest = rs[olt:]
	if len(lGreatest) > len(rGreatest):
		print(s[:l] + lGreatest + s[r + 1:])
	else:
		print(s[:l] + rGreatest + s[r + 1:])


import sys
from pprint import pprint
if not 'DEBUG' in globals():
	readline = sys.stdin.readline

def read(map_func):
	return map_func(readline().rstrip())

def reads(map_func):
	return list(map(map_func, readline().rstrip().split()))

def readn(map_func, n):
	return [read(map_func) for _ in range(n)]

def readint():
	return read(int)

def readints():
	return reads(int)

def readcol(n):
	return readn(int, n)

def readmat(n):
	return [readints() for _ in range(n)]

def readmap(n):
	return readn(list, n)

def makemat(n, m, v):
	return [[v] * m for _ in range(n)]

def zeromat(n, m=None):
	m = m if m else n
	return makemat(n, m, 0)

def listmat(n, m=None):
	m = m if m else n
	return [[[] for _ in range(m)] for _ in range(n)]

def crosslist(y, x, n=None, m=None):
	return [(p, q) for (p, q) in [(y, x + 1), (y - 1, x), (y, x - 1), (y + 1, x)] if (n == None or 0 <= p < n) and (m == None or 0 <= q < m)]

def roundlist(y, x, n=None, m=None):
	return [(p, q) for (p, q) in [(y, x + 1), (y - 1, x + 1), (y - 1, x), (y - 1, x - 1), (y, x - 1), (y + 1, x - 1), (y + 1, x), (y + 1, x + 1)] if (n == None or 0 <= p < n) and (m == None or 0 <= q < m)]

def plog(obj):
	pprint(obj) if 'DEBUG' in globals() else None

def log(msg, label=None):
	print((label + ': ' if label else '') + str(msg)) if 'DEBUG' in globals() else None

def _logmat(mat, label=None):
	fmt = '{:' + str(max((max((len(str(s)) for s in m)) for m in mat))) + 'd}'
	f = lambda row: '[' + ' '.join((fmt.format(x) for x in row)) + ']'
	[log('[' + f(row), label) if i == 0 else log((' ' * (len(label) + 3) if label else ' ') + f(row) + ']', None) if i == len(mat) - 1 else log((' ' * (len(label) + 3) if label else ' ') + f(row), None) for (i, row) in enumerate(mat)]

def logmat(mat, label=None):
	_logmat(mat, label) if 'DEBUG' in globals() else None
import math

def LeftLongestPalindrom(s):
	L = zeromat(len(s))
	for i in range(0, len(s)):
		L[i][i] = 1
	for k in range(1, len(s)):
		i = 0
		j = i + k
		while j < len(s):
			if s[i] == s[j]:
				L[i][j] = L[i + 1][j - 1] + 2
			else:
				L[i][j] = max(L[i][j - 1], L[i + 1][j])
			i += 1
			j += 1
	l = 0
	for j in range(0, len(s)):
		if L[0][j] == j + 1:
			l = max(l, j)
	r = 0
	for i in range(0, len(s)):
		if L[-i - 1][-1] == i + 1:
			r = max(r, i)
	if l >= r:
		return s[:l + 1]
	return s[-r - 1:]
for _ in range(1, readint() + 1):
	s = readline()[:-1]
	(i, j) = (0, len(s) - 1)
	while i < j and s[i] == s[j]:
		i += 1
		j -= 1
	t = LeftLongestPalindrom(s[i:j + 1])
	print(s[:i] + t + s[j + 1:])


import sys
input = sys.stdin.readline
tc = int(input())
for _ in range(tc):
	s = input().rstrip()
	(l, r) = (0, len(s) - 1)
	while l < r and s[l] == s[r]:
		l += 1
		r -= 1
	a = s[:l]
	b = s[r + 1:]
	s = s[l:r + 1]
	n = 2 * len(s) + 1
	if s == '':
		print(a + b)
		continue
	L = [0] * n
	L[1] = 1
	center = 1
	center_right = 2
	max_len = 1
	max_pos = 1
	for i in range(2, n):
		i_mirror = 2 * center - i
		diff = center_right - i
		if diff > 0:
			L[i] = min(L[i_mirror], diff)
		try:
			while i + L[i] < n and i - L[i] > 0 and ((i + L[i] + 1) % 2 == 0 or s[(i + L[i] + 1) // 2] == s[(i - L[i] - 1) // 2]):
				L[i] += 1
		except:
			pass
		if L[i] > max_len and (i - L[i] == 0 or i + L[i] == n - 1):
			max_len = L[i]
			max_pos = i
		if i + L[i] > center_right:
			center = i
			center_right = i + L[i]
	t = ''
	i = max_pos // 2
	if max_pos % 2 == 0:
		t = s[i - max_len // 2:i + max_len // 2]
	else:
		t = s[i - max_len // 2:i + max_len // 2 + 1]
	print(a + t + b)


testcase = int(input())
while testcase:
	s = input()
	new = ''
	front = ''
	back = ''
	i = 0
	j = len(s) - 1
	while i <= j and s[i] == s[j]:
		front += s[i]
		if i != j:
			back += s[j]
		i += 1
		j -= 1
	back = back[::-1]
	if i >= j:
		print(front + back)
	else:
		mid = ''
		for l in range(1, j - i + 2):
			sub = s[i:i + l]
			if sub == sub[::-1]:
				mid = sub
		for l in range(1, j - i + 2):
			sub = s[j - l + 1:j + 1]
			x = sub[::-1]
			if x == x[::-1]:
				if len(x) > len(mid):
					mid = x
		print(front + mid + back)
	testcase -= 1


def solve(s):
	ans = ''
	i = 0
	if len(s) == 1:
		return s
	while s[i] == s[-1 - i]:
		ans += s[i]
		i += 1
		if i >= len(s) // 2:
			break
	if i == 0:
		return solve_one_side(s)
	ans = ans + solve_one_side(s[i:-i]) + ans[::-1]
	return ans

def solve_one_side(s):
	if not s:
		return ''
	ans = s[0]
	n = len(s)
	for i in range(1, n):
		si = s[:i]
		if si == si[::-1]:
			ans = si
	s = s[::-1]
	for i in range(len(ans), n):
		si = s[:i]
		if si == si[::-1]:
			ans = si
	return ans

def main():
	t = int(input())
	for _ in range(t):
		s = input()
		print(solve(s))
main()


import sys
import math

def longest_pal(s):
	kmp = s + '#' + s[::-1]
	lps = [0] * len(kmp)
	n1 = len(kmp)
	for i in range(1, n1):
		prev_idx = lps[i - 1]
		while prev_idx > 0 and kmp[i] != kmp[prev_idx]:
			prev_idx = lps[prev_idx - 1]
		if kmp[i] == kmp[prev_idx]:
			lps[i] = prev_idx + 1
		else:
			lps[i] = prev_idx
	return lps[n1 - 1]
t = int(input())
for _ in range(t):
	s = input()
	maxi1 = longest_pal(s)
	maxi2 = longest_pal(s[::-1])
	n = len(s)
	(i, j, maxi) = (0, n - 1, 0)
	while i < j:
		if s[i] == s[j]:
			i += 1
			j -= 1
			maxi += 1
		else:
			break
	maxi3 = longest_pal(s[i:j + 1])
	maxi4 = longest_pal(s[j:i - 1:-1])
	if max(maxi1, maxi2) > 2 * maxi + max(maxi3, maxi4):
		if maxi1 > maxi2:
			print(s[:maxi1])
		else:
			print(s[n - maxi2:])
	elif maxi3 > maxi4:
		print(s[:i] + s[i:i + maxi3] + s[j + 1:])
	else:
		print(s[:i] + s[j + 1 - maxi4:j + 1] + s[j + 1:])


def ispalindrome(s):
	a = s + '.' + s[::-1]
	n = len(a)
	(j, p) = (0, [0] * n)
	for i in range(1, n):
		while j and a[j] != a[i]:
			j = p[j - 1]
		if a[j] == a[i]:
			j += 1
		p[i] = j
	return s[:j]
for _ in range(int(input())):
	s = input()
	if len(s) == 1:
		print(s)
		continue
	l = 0
	while l < len(s) - l - 1:
		if s[l] != s[len(s) - l - 1]:
			break
		l += 1
	m1 = ispalindrome(s[l:len(s) - l])
	m2 = ispalindrome(s[l:len(s) - l][::-1])
	if len(m1) < len(m2):
		m1 = m2
	print(s[:l] + m1 + s[len(s) - l:])


import sys

def in1():
	return int(input())

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

def inx():
	return [int(_) for _ in input().split()]
t = in1()

def find_palindrome(cropped_string):
	smax = ''
	prog = []
	final = [0, 0]
	cur = 0
	while cur < len(cropped_string):
		prog2 = []
		for (i, x) in enumerate(prog):
			h = x[1]
			if h == -1:
				if final[0] < x[0] or (final[0] == x[0] and x[2] == 1):
					final = [x[0], x[2]]
			elif cropped_string[h] == cropped_string[cur]:
				x[1] -= 1
				if x[1] == -1:
					if final[0] < x[0] or (final[0] == x[0] and x[2] == 1):
						final = [x[0], x[2]]
				else:
					prog2.append(x)
		if cur > 0:
			if cropped_string[cur] == cropped_string[cur - 1]:
				prog2.append([cur, cur - 2, 0])
		if cur > 1:
			if cropped_string[cur] == cropped_string[cur - 2]:
				prog2.append([cur - 1, cur - 3, 1])
		prog = prog2
		cur += 1
	olen = final[0] * 2 + final[1]
	return cropped_string[:olen]
for _ in range(t):
	s = input()
	r = s[::-1]
	i = 0
	while i < len(s) // 2:
		if s[i] == r[i]:
			i += 1
		else:
			break
	if i * 2 == len(s):
		print(s)
		continue
	crop = s[i:len(s) - i]
	side = i
	left = find_palindrome(crop)
	right = find_palindrome(crop[::-1])
	if not left and (not right):
		mid = crop[0]
	elif len(left) > len(right):
		mid = left
	else:
		mid = right
	print(s[:i] + mid + s[:i][::-1])


def manacher(s):
	t = ['#']
	for i in range(len(s)):
		t.append(s[i])
		t.append('#')
	n = len(t)
	r = [0] * n
	i = 0
	j = 0
	while i < n:
		while i - j >= 0 and i + j < n and (t[i - j] == t[i + j]):
			j += 1
		r[i] = j
		k = 1
		while i - k >= 0 and i + k < n and (k + r[i - k] < j):
			r[i + k] = r[i - k]
			k += 1
		i += k
		j -= k
	return r
import sys
input = sys.stdin.readline

def judge(string):
	n = len(string)
	for i in range(n):
		if i > n - 1:
			return True
		if string[i] == string[n - i - 1]:
			return False
	return True
t = int(input())
for _ in range(t):
	s = input()[:-1]
	n = len(s)
	max_ = 0
	for i in range(n + 1):
		if i >= n - i - 1:
			break
		if s[i] == s[n - i - 1]:
			max_ += 1
		else:
			break
	man = manacher(s)
	ans = 0
	for ll in range(n + 1):
		for rr in range(ll, n + 1):
			l_c = ll
			r_c = n - rr
			l = ll
			r = rr
			if min(l_c, r_c) > max_:
				continue
			if min(l_c, r_c) != l_c:
				(l, r) = (min(l_c, r_c), l)
				if man[2 * l + (r - l)] == r - l + 1:
					if l_c + r_c > ans:
						ans = l_c + r_c
						tmp = (l_c, r_c)
			elif min(l_c, r_c) != r_c:
				(l, r) = (r, n - min(l_c, r_c))
				if man[2 * l + (r - l)] == r - l + 1:
					if l_c + r_c > ans:
						ans = l_c + r_c
						tmp = (l_c, r_c)
	(l_c, r_c) = tmp
	res = s[0:l_c] + s[n - r_c:n]
	print(res)


def longestPrefix(txt: str) -> str:
	txt = txt + '#' + txt[::-1]
	n = len(txt)
	(j, i) = (-1, 0)
	nextJ = [0] * n
	nextJ[0] = -1
	while i < n - 1:
		if j == -1 or txt[i] == txt[j]:
			i += 1
			j += 1
			if txt[i] == txt[j]:
				nextJ[i] = nextJ[j]
			else:
				nextJ[i] = j
		else:
			j = nextJ[j]
	(j, i) = (0, 1)
	dp = [0] * n
	while i < n:
		if j < 0 or txt[i] == txt[j]:
			dp[i] = j + 1
			i += 1
			j += 1
		elif j == 0:
			dp[i] = 0
			i += 1
		else:
			j = nextJ[j]
	return txt[:dp[-1]]

def longestPalindrome(line: str) -> str:
	(b, e) = (0, len(line) - 1)
	while b < e and line[b] == line[e]:
		b += 1
		e -= 1
	if b >= e:
		return line
	p1 = longestPrefix(line[b:e + 1])
	p2 = longestPrefix(line[b:e + 1][::-1])
	p = p1 if len(p1) >= len(p2) else p2
	return line[:b] + p + line[e + 1:]
n = int(input())
for i in range(n):
	line = input().strip(' \r\n')
	print(longestPalindrome(line))


for _ in range(int(input())):
	a = input()
	st = ''
	if a == a[::-1]:
		print(a)
	else:
		le = len(a)
		for i in range(int(abs(-le // 2))):
			if a[i] == a[le - i - 1]:
				st += a[i]
			else:
				break
		tem = ''
		for j in range(i, le - i):
			if a[i:j + 1] == a[i:j + 1][::-1]:
				tem = a[i:j + 1]
		c_1 = st + tem + st[::-1]
		a = a[::-1]
		tem = ''
		for j in range(i, le - i):
			if a[i:j + 1] == a[i:j + 1][::-1]:
				tem = a[i:j + 1]
		c_2 = st + tem + st[::-1]
		if len(c_1) >= len(c_2):
			print(c_1)
		else:
			print(c_2)


t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	if n == 1:
		print(s)
	else:
		(l, r) = (0, n - 1)
		while l + 1 < r - 1 and s[l] == s[r]:
			l += 1
			r -= 1
		added = 0
		center_piece = []
		for center in range(l, r):
			offset = 1
			while center - offset >= l and center + offset <= r and (s[center - offset] == s[center + offset]):
				offset += 1
			if center - offset < l or center + offset > r:
				if 2 * (offset - 1) + 1 >= added:
					added = 2 * (offset - 1) + 1
					center_piece = [center - offset + 1, center + offset - 1]
		for center1 in range(l, r):
			center2 = center1 + 1
			if center2 < n and s[center1] == s[center2]:
				offset = 1
				while center1 - offset >= l and center2 + offset <= r and (s[center1 - offset] == s[center2 + offset]):
					offset += 1
				if center1 - offset < l or center2 + offset > r:
					if 2 * (offset - 1) + 2 >= added:
						added = 2 * (offset - 1) + 2
						center_piece = [center1 - offset + 1, center2 + offset - 1]
		print(s[:l] + s[center_piece[0]:center_piece[1] + 1] + s[r + 1:])


import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	S = input().rstrip()
	N = len(S)
	ans = -1
	ans_s = ''
	dp = [[-1] * N for _ in range(N)]
	for i in range(N):
		dp[i][i] = 1
	for i in range(N - 1):
		if S[i] == S[i + 1]:
			dp[i][i + 1] = 1
		else:
			dp[i][i + 1] = 0
	for k in range(2, N + 1):
		for i in range(N - k):
			j = i + k
			if dp[i + 1][j - 1] > 0 and S[i] == S[j]:
				dp[i][j] = 1
			else:
				dp[i][j] = 0
	if dp[0][-1]:
		print(S)
		continue
	p = 0
	while S[p] == S[-p - 1]:
		p += 1
	L = p
	R = N - p - 1
	for i in range(L, R):
		if dp[L][i]:
			sub = S[:L] + S[L:i + 1] + S[R + 1:]
			if ans < len(sub):
				ans = len(sub)
				ans_s = sub
	for i in range(R, L, -1):
		if dp[i][R]:
			sub = S[:L] + S[i:R + 1] + S[R + 1:]
			if ans < len(sub):
				ans = len(sub)
				ans_s = sub
	print(ans_s)


def pre(s):
	p = s + '#' + s[::-1]
	pi = [0] * len(p)
	j = 0
	for i in range(1, len(p)):
		while j != 0 and p[j] != p[i]:
			j = pi[j - 1]
		if p[j] == p[i]:
			j += 1
		pi[i] = j
	return s[:j]
for _ in range(int(input())):
	s = input()
	l = 0
	while l < len(s) // 2 and s[l] == s[-(l + 1)]:
		l += 1
	pp = pre(s[l:len(s) - l])
	sp = pre(s[l:len(s) - l][::-1])
	pp = pp if len(pp) > len(sp) else sp
	print(s[:l] + pp + s[len(s) - l:])


from math import ceil

def kmp(t, p):
	n = len(t)
	m = len(p)
	i = 1
	j = 0
	fail = [0] * n
	while i < n:
		if t[i] == p[j]:
			fail[i] = j + 1
			i += 1
			j += 1
			if j == m:
				j = 0
		elif j != 0:
			j = fail[j - 1]
		else:
			i += 1
	return fail[-1]
for T in range(int(input())):
	s = input()
	res = ''
	i = 0
	n = len(s)
	while i < n and s[i] == s[n - 1 - i]:
		res += s[i]
		i += 1
	if i == n:
		print(s)
		continue
	s = s[i:n - i]
	rev = ''.join(reversed(s))
	s1 = s + '$' + rev
	s2 = rev + '$' + s
	a = kmp(s1, s1)
	b = kmp(s2, s2)
	if a > b:
		res = res + s1[:a] + ''.join(reversed(res))
	else:
		res = res + s2[:b] + ''.join(reversed(res))
	print(res)


t = int(input())

def findLongestPalindromicString(text, maxL):
	N = len(text)
	if N == 0:
		return 0
	N = 2 * N + 1
	L = [0] * N
	L[0] = 0
	L[1] = 1
	C = 1
	R = 2
	i = 0
	iMirror = 0
	maxLPSLength = 0
	maxLPSCenterPosition = 0
	start = -1
	end = -1
	diff = -1
	for i in range(2, N):
		iMirror = 2 * C - i
		L[i] = 0
		diff = R - i
		if diff > 0:
			L[i] = min(L[iMirror], diff)
		try:
			while (i + L[i] < N and i - L[i] > 0) and ((i + L[i] + 1) % 2 == 0 or text[(i + L[i] + 1) // 2] == text[(i - L[i] - 1) // 2]):
				L[i] += 1
		except Exception:
			pass
		if L[i] > maxLPSLength:
			maxLPSLength = L[i]
			maxLPSCenterPosition = i
		if i + L[i] > R:
			C = i
			R = i + L[i]
	start_Max = 0
	end_Max = 0
	i = 1
	while i <= maxL:
		if L[i] == i:
			start_Max = i
		if L[N - 1 - i] == i:
			end_Max = i
		i += 1
	if start_Max > end_Max:
		return start_Max
	else:
		return -end_Max
for _ in range(t):
	s = input()
	n = len(s)
	prefix = ''
	suffix = ''
	remaining = s
	ans = 0
	for i in range(n // 2):
		if s[i] == s[n - 1 - i]:
			ans = i + 1
		else:
			break
	if ans != 0:
		prefix = s[0:ans]
		suffix = s[n - ans:n]
		remaining = s[ans:n - ans]
	x = findLongestPalindromicString(remaining, n - 2 * ans)
	temp = ''
	if x == 0:
		pass
	elif x < 0:
		temp = remaining[x:]
	else:
		temp = remaining[:x]
	print(prefix + temp + suffix)


def solve():
	a = input()
	b = a[::-1]
	j = 0
	while a[j] == b[j] and j * 2 < len(a) - 1:
		j += 1
	c = a[:j]
	b = b[j:len(b) - j]
	a = a[j:len(a) - j]
	a += '#' + a[::-1]
	b += '#' + b[::-1]
	al = [0] * len(a)
	bl = [0] * len(b)
	for i in range(1, len(a)):
		j = al[i - 1]
		while j > 0 and a[i] != a[j]:
			j = al[j - 1]
		if a[i] == a[j]:
			j += 1
		al[i] = j
	aa = a[0:al[len(a) - 1]]
	for i in range(1, len(b)):
		j = bl[i - 1]
		while j > 0 and b[i] != b[j]:
			j = bl[j - 1]
		if b[i] == b[j]:
			j += 1
		bl[i] = j
	bb = b[0:bl[len(b) - 1]]
	if len(bb) > len(aa):
		aa = bb
	print(c + aa + c[::-1])
n = int(input())
for i in range(n):
	solve()


for _ in range(int(input())):
	s = input()
	n = len(s)
	le = 0
	ans = ''
	for i in range(n):
		if s[i] != s[n - 1 - i]:
			break
		if i >= n - 1 - i:
			break
		le = i + 1
	ans = s[:le] + s[n - le:]
	for i in range(le, n - le):
		k = s[:i + 1] + s[n - le:]
		if k == k[::-1] and len(k) > len(ans):
			ans = k
	for i in range(n - le - 1, le, -1):
		k = s[:le] + s[i:]
		if k == k[::-1] and len(k) > len(ans):
			ans = k
	print(ans)


import sys

def read():
	return list(map(int, sys.stdin.readline().rstrip().split(' ')))

def poli(s):
	for l in range(len(s), 0, -1):
		if s[:l] == s[:l][::-1]:
			return s[:l]
	return ''
(n,) = read()
for line in sys.stdin:
	s = line.rstrip()
	len_prefix = 0
	for i in range(len(s) // 2):
		if s[i] == s[-i - 1]:
			len_prefix = i + 1
		else:
			break
	prefix = ''
	if len_prefix:
		prefix = s[:len_prefix]
		s = s[len_prefix:-len_prefix]
	p = ''
	left = poli(s)
	right = poli(s[::-1])
	if len(left) >= len(right):
		p = prefix + left + prefix[::-1]
	else:
		p = prefix + right[::-1] + prefix[::-1]
	print(p)


def pali(a):
	(s, e) = ('', '')
	for i in range(1, len(a) - 1):
		if a[i] == a[0] and a[:i + 1] == a[:i + 1][::-1]:
			s = a[:i + 1]
		if e == '' and a[i] == a[-1] and (a[i:] == a[i:][::-1]):
			e = a[i:]
	if s == '' and e == '':
		return a[0]
	if len(s) > len(e):
		return s
	return e
for _ in range(int(input())):
	s = input()
	if s == s[::-1]:
		print(s)
		continue
	i = -1
	j = len(s)
	while i < j and s[i + 1] == s[j - 1]:
		i += 1
		j -= 1
	print(s[:i + 1] + pali(s[i + 1:j]) + s[j:])


test = int(input())
for _ in range(test):
	s = input()
	limit = len(s)
	if s == s[::-1]:
		print(s)
		continue
	res = ''
	mid1 = ''
	mid2 = ''
	l = len(s) - 1
	index = 0
	while index < l:
		if s[index] == s[l]:
			res += s[index]
		else:
			break
		index += 1
		l -= 1
	p1 = index
	p2 = l
	cur = len(res)
	while p1 <= l:
		j = res + s[index:p1 + 1] + res[::-1]
		if j == j[::-1]:
			if len(j) > limit:
				break
			mid1 = j
		p1 += 1
	while p2 >= index:
		j = res + s[p2:l + 1] + res[::-1]
		if j == j[::-1]:
			if len(j) > limit:
				break
			mid2 = j
		p2 -= 1
	if len(mid1) > len(mid2):
		print(mid1)
	else:
		print(mid2)


from sys import stdin
from bisect import bisect_left as bl
from bisect import bisect_right as br

def input():
	return stdin.readline()[:-1]

def intput():
	return int(input())

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

def intsput():
	return map(int, sinput())

class RangedList:

	def __init__(self, start, stop, val=0):
		self.shift = 0 - start
		self.start = start
		self.stop = stop
		self.list = [val] * (stop - start)

	def __setitem__(self, key, value):
		self.list[key + self.shift] = value

	def __getitem__(self, key):
		return self.list[key + self.shift]

	def __repr__(self):
		return str(self.list)

	def __iter__(self):
		return iter(self.list)

def dprint(*args, **kwargs):
	if debugging:
		print(*args, **kwargs)

def prefix_function(x):
	n = len(x)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j > 0 and x[i] != x[j]:
			j = pi[j - 1]
		if x[i] == x[j]:
			j += 1
		pi[i] = j
	return pi[-1]
debugging = 1
t = intput()
for _ in range(t):
	front = []
	back = []
	s = list(input())
	fhead = 0
	bhead = len(s) - 1
	while s[fhead] == s[bhead] and fhead < bhead:
		front.append(s[fhead])
		back.append(s[bhead])
		fhead += 1
		bhead -= 1
	newstring = s[fhead:bhead + 1]
	formatted = newstring + ['*'] + newstring[::-1]
	prefixlength = prefix_function(formatted)
	bestprefix = newstring[:prefixlength]
	newstring = newstring[::-1]
	formatted = newstring + ['*'] + newstring[::-1]
	suffixlength = prefix_function(formatted)
	bestsuffix = newstring[:suffixlength]
	if prefixlength > suffixlength:
		front += bestprefix
	else:
		back += bestsuffix
	print(''.join(front) + ''.join(back[::-1]))


def chk(s):
	n = len(s) // 2
	for i in range(n):
		if s[i] != s[-1 - i]:
			return False
	return True

def left(s):
	n = len(s)
	for i in range(n - 1, 0, -1):
		if chk(s[:i]):
			return s[:i]

def right(s):
	n = len(s)
	for i in range(1, n):
		if chk(s[i:]):
			return s[i:]

def chk2(s):
	l = left(s)
	r = right(s)
	if len(l) >= len(r):
		return l
	else:
		return r

def chk1(s):
	i = 0
	while s[i] == s[-1 - i]:
		i += 1
	res = s[:i] + chk2(s[i:-i]) + s[-i:]
	return res

def main(s):
	if chk(s):
		return s
	if s[0] == s[-1]:
		res = chk1(s)
	else:
		res = chk2(s)
	return res
t = int(input())
for i in range(t):
	s = list(input())
	print(''.join(main(s)))


def longest_palindrome(s1):
	left_part = ''
	right_part = ''
	s2 = s1
	left = 0
	right = -1
	flag = 0
	while left + abs(right) <= len(s):
		if flag == 0:
			if s2[left] == s2[right]:
				left += 1
				right -= 1
			else:
				left_part += s2[:left]
				if right != -1:
					right_part += s2[right + 1:]
				flag = 1
		elif s2[left] != s2[right]:
			right -= 1
		else:
			start_left = left
			start_right = right
			while left + abs(right) <= len(s):
				if s2[left] == s2[right]:
					left += 1
					right -= 1
				else:
					break
			if left + abs(right) > len(s):
				left_part += s2[start_left:start_right + 1] + right_part
				break
			else:
				left = start_left
				right = start_right - 1
	return left_part
for _ in range(int(input())):
	s = input()
	if s == s[::-1]:
		print(s)
	else:
		s2 = longest_palindrome(s)
		s3 = longest_palindrome(s[::-1])
		print(s2 if len(s2) > len(s3) else s3)


import sys
input = sys.stdin.readline
print = sys.stdout.write

def manachers(s, n):
	L = [-1] * n
	r = 0
	p = 0
	for i in range(n):
		if i <= r:
			L[i] = min(L[2 * p - i], r - i)
		else:
			L[i] = 0
		while i - L[i] - 1 >= 0 and i + L[i] + 1 < n and (s[i - L[i] - 1] == s[i + L[i] + 1]):
			L[i] += 1
	if r < i + L[i]:
		r = i + L[i]
		p = i
	return L

def find(s):
	if len(s) == 0:
		return []
	news = ['*'] * (len(s) * 2 - 1)
	for i in range(len(s)):
		news[i * 2] = s[i]
	L = manachers(news, len(news))
	return L

def f(s):
	i = 0
	while i < len(s) // 2:
		if s[i] != s[-i - 1]:
			break
		i += 1
	return i
for i in ' ' * int(input()):
	s = input().rstrip('\n')
	k = f(s)
	news = s[k:len(s) - k]
	L = find(news)
	frontmax = 0
	backmax = 0
	for i in range(len(L)):
		if L[i] == i:
			frontmax = i
		if L[len(L) - 1 - i] == i:
			backmax = i
	if frontmax > backmax:
		pal = news[:frontmax + 1]
	else:
		pal = news[len(news) - backmax - 1:]
	i = 0
	while i < len(s):
		if i < k or i >= len(s) - k:
			print(s[i])
			i += 1
		elif i == k:
			print(pal)
			i = len(s) - k
	print('\n')


def longest_prefix(s, sr):
	p = s + '#' + sr
	pi = [0] * len(p)
	j = 0
	for i in range(1, len(p)):
		while j > 0 and p[j] != p[i]:
			j = pi[j - 1]
		if p[j] == p[i]:
			j += 1
		pi[i] = j
	return s[:j]

def solve(s):
	(a, b) = ('', '')
	(i, j) = (0, len(s) - 1)
	while i < j:
		if s[i] != s[j]:
			break
		i += 1
		j -= 1
	a = s[:i]
	b = s[j + 1:]
	if i + j < len(s):
		sub = s[i:j + 1]
		subr = sub[::-1]
		p1 = longest_prefix(sub, subr)
		p2 = longest_prefix(subr, sub)
		a += p1 if len(p1) >= len(p2) else p2
	return a + b

def main():
	t = int(input())
	for _ in range(t):
		s = input()
		t = solve(s)
		print(t)
main()


if 1:

	def lps(r):
		if not r:
			return 0
		r = r + '#' + r[::-1] + '$'
		i = 0
		j = -1
		nt = [0] * len(r)
		nt[0] = -1
		while i < len(r) - 1:
			if j == -1 or r[i] == r[j]:
				i += 1
				j += 1
				nt[i] = j
			else:
				j = nt[j]
		return nt[len(r) - 1]
for tc in range(int(input())):
	s = input()
	if s == s[::-1]:
		print(s)
		continue
	i = 0
	j = len(s) - 1
	aa = ''
	bb = ''
	while s[i] == s[j]:
		i += 1
		j -= 1
	aa = s[:i]
	ii = i
	jj = j
	ss = s[i:j + 1]
	tem = ''
	temp = []
	a = ''
	b = ''
	n = len(ss)
	sss = ss[::-1]
	l1 = lps(ss)
	l2 = lps(sss)
	ma = max(l1, l2)
	h = ss[:ma]
	hh = sss[:ma]
	if h == h[::-1]:
		print(aa + h + aa[::-1])
	else:
		print(aa + hh + aa[::-1])


def check(s):
	l = len(s)
	i = 0
	k = 0
	while i < l / 2:
		if s[i] == s[l - 1 - i]:
			i += 1
			continue
		else:
			k = k + 1
			break
	if k == 0:
		return s
	return 0
t = int(input())
for _ in range(t):
	a = input()
	l = len(a)
	i = 0
	(b, c) = ('', '')
	while i < l // 2:
		if a[i] == a[l - 1 - i]:
			b += a[i]
			c += a[i]
			i += 1
		else:
			break
	d = a[i:l - i]
	l = len(d)
	c = c[::-1]
	e = ''
	f = ''
	h = ''
	for i in range(l):
		e += d[i]
		g = check(e)
		if g != 0:
			f = g
	d = d[::-1]
	e = ''
	for i in range(l):
		e += d[i]
		g = check(e)
		if g != 0:
			h = g
	if len(f) > len(h):
		print(b + f + c)
	else:
		print(b + h + c)


for _ in range(int(input())):
	s = input()
	n = len(s)
	ans = ''
	if s[0] == s[n - 1]:
		ans += s[0]
		i = 1
		j = n - 2
		while j > i and s[j] == s[i]:
			ans += s[i]
			i += 1
			j -= 1
		extra = ''
		extra1 = ''
		if j != i:
			for k in range(i, j + 1):
				if s[i:k + 1] == s[i:k + 1][::-1]:
					extra = s[i:k + 1]
			k = j
			while k >= i:
				if s[k:j + 1] == s[k:j + 1][::-1]:
					extra1 = s[k:j + 1]
				k -= 1
			if len(extra) < len(extra1):
				extra = extra1
		temp = len(ans)
		ans += extra
		if ans != s:
			ans += ans[:temp][::-1]
	for i in range(n):
		if s[:i + 1] == s[:i + 1][::-1] and len(ans) < len(s[:i + 1]):
			ans = s[:i + 1]
	for i in range(n):
		if s[i:] == s[i:][::-1] and len(ans) < len(s[i:]):
			ans = s[i:]
	print(ans)


def ispal(x):
	return x == x[::-1]
for _ in range(int(input())):
	s = input()
	if s == s[::-1]:
		print(s)
	else:
		n = len(s)
		a = -1
		b = n
		for t in range(n):
			if s[t] == s[-1 - t]:
				a += 1
				b -= 1
			else:
				break
		st = ''
		cv = ''
		for t in range(a + 1, b):
			cv += s[t]
			if ispal(cv):
				st = cv
		st1 = ''
		cv1 = ''
		for t in range(b - 1, a, -1):
			cv1 += s[t]
			if ispal(cv1):
				st1 = cv1
		if len(st) > len(st1):
			print(s[:a + 1] + st + s[b:])
		else:
			print(s[:a + 1] + st1 + s[b:])


def check(s):
	return s == s[::-1]

def solve():
	s = input()
	n = len(s)
	(i, j) = (0, n - 1)
	while i < j and s[i] == s[j]:
		i += 1
		j -= 1
	if i >= j:
		print(s)
		return
	l = 0
	mi = ''
	for k in range(i, j + 1):
		if check(s[i:k + 1]) and l < k - i + 1:
			l = k - i + 1
			mi = s[i:k + 1]
	for k in range(j, i - 1, -1):
		if check(s[k:j + 1]) and l < j - k + 1:
			l = j - k + 1
			mi = s[k:j + 1]
	print(s[0:i] + mi + s[j + 1:])
for _ in range(int(input())):
	solve()


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
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 ps(s):
	p = s + '#' + s[::-1]
	pi = [0] * len(p)
	j = 0
	for i in range(1, len(p)):
		while j != 0 and p[j] != p[i]:
			j = pi[j - 1]
		if p[j] == p[i]:
			j += 1
		pi[i] = j
	return s[:j]
for _ in range(int(input())):
	s = input()
	l = 0
	while l < len(s) // 2 and s[l] == s[-(l + 1)]:
		l += 1
	pre = ps(s[l:len(s) - l])
	suf = ps(s[l:len(s) - l][::-1])
	p = pre if len(pre) > len(suf) else suf
	print(s[0:l] + p + s[len(s) - l:])


import sys, heapq as h
input = sys.stdin.readline

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

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

def listStr():
	return list(input().strip())
import collections as col
import math

def longest_prefix_suffix_palindrome(S, N):
	i = -1
	while i + 1 <= N - i - 2 and S[i + 1] == S[N - i - 2]:
		i += 1
	return i

def longest_palindromic_prefix(S):
	tmp = S + '?'
	S = S[::-1]
	tmp = tmp + S
	N = len(tmp)
	lps = [0] * N
	for i in range(1, N):
		L = lps[i - 1]
		while L > 0 and tmp[L] != tmp[i]:
			L = lps[L - 1]
		if tmp[i] == tmp[L]:
			L += 1
		lps[i] = L
	return tmp[:lps[N - 1]]

def solve():
	S = getStr()
	N = len(S)
	i = longest_prefix_suffix_palindrome(S, N)
	if i >= N // 2 - 1:
		return S
	new_str = S[i + 1:N - i - 1]
	longest_pref = longest_palindromic_prefix(new_str)
	longest_suff = longest_palindromic_prefix(new_str[::-1])[::-1]
	add_on = longest_pref if len(longest_pref) >= len(longest_suff) else longest_suff
	return S[:i + 1] + add_on + S[N - i - 1:]
for _ in range(getInt()):
	print(solve())


t = int(input())
for i in range(t):
	s = list(input())
	prefix = []
	endIndex = 0
	if len(s) == 1:
		print(s[0])
	else:
		for j in range(len(s) // 2):
			if s[j] == s[-1 - endIndex]:
				prefix.append(s[j])
				s[j] = 1
				s[-1 - endIndex] = 1
				endIndex += 1
			else:
				break
		middle = [x for x in s if x != 1]
		middleReversed = middle[::-1]
		bestMid = ''
		for j in range(len(middle), -1, -1):
			if middle[:j] == list(reversed(middle[:j])) and len(middle[:j]) > len(bestMid):
				bestMid = middle[:j]
				break
		for j in range(len(middleReversed), -1, -1):
			if middleReversed[:j] == list(reversed(middleReversed[:j])) and len(middleReversed[:j]) > len(bestMid):
				bestMid = middleReversed[:j]
				break
		print(''.join(prefix) + ''.join(bestMid) + ''.join(prefix[::-1]))


def Z(s):
	n = len(s)
	z = [0] * n
	z[0] = n
	left = right = 0
	for i in range(1, n):
		if i <= right:
			z[i] = max(0, min(z[i - left], right - i + 1))
		while i + z[i] < n and s[z[i]] == s[i + z[i]]:
			z[i] += 1
		if i + z[i] > right:
			left = i
			right = i + z[i] - 1
	return z

def solve_end(s):
	n = len(s)
	s1 = s[::-1] + '#' + s
	Z1 = Z(s1)
	mx = 0
	for l in range(n + 1, 2 * n + 1):
		if l + Z1[l] >= 2 * n - Z1[l]:
			mx = l
			break
	return s1[mx:]
for _ in range(int(input())):
	s = input()
	n = len(s)
	same = 0
	for i in range(len(s) // 2):
		if s[i] != s[n - 1 - i]:
			same = i
			break
	side = s[:same]
	s = s[same:n - same]
	sol_1 = solve_end(s)
	s = s[::-1]
	sol_2 = solve_end(s)[::-1]
	solution = side + max(sol_1, sol_2, key=len) + side[::-1]
	print(solution)


def isPalindrom(s):
	i = 0
	j = len(s) - 1
	while i <= j:
		if s[i] != s[j]:
			return False
		else:
			i += 1
			j -= 1
	return True
t = int(input())
for z in range(0, t):
	s = input()
	i = 0
	j = len(s) - 1
	while i <= j:
		if s[i] != s[j]:
			break
		else:
			i += 1
			j -= 1
	if j < i:
		print(s)
		continue
	else:
		end = s[j + 1:len(s)]
		start = s[0:i]
		left = i
		right = j
		leftSub = ''
		rightSub = ''
		j -= 1
		while j >= i:
			if isPalindrom(s[i:j + 1]):
				leftSub = s[i:j + 1]
				break
			else:
				j -= 1
		j = right
		i += 1
		while i < j:
			if isPalindrom(s[i:j + 1]):
				rightSub = s[i:j + 1]
				break
			else:
				i += 1
		print(start + (rightSub if len(rightSub) > len(leftSub) else leftSub) + end)


t = int(input())
for _ in range(t):
	s = input()
	i = 0
	j = len(s) - 1
	pre = ''
	sef = ''
	flag = 0
	while i < j:
		if s[i] == s[j]:
			pre = pre + s[i]
			sef = s[j] + sef
		else:
			flag = 1
			break
		i += 1
		j -= 1
	if flag == 0:
		print(s)
	else:
		mid1 = ''
		mid2 = ''
		start = i
		end = j
		while start <= end:
			k = 0
			flag = 0
			while k <= (end - start) // 2:
				if s[start + k] != s[end - k]:
					flag = 1
					break
				k += 1
			if flag == 0:
				mid1 = mid1 + s[start:end + 1]
				break
			else:
				end -= 1
		start = i
		end = j
		while start <= end:
			k = 0
			flag = 0
			while k <= (end - start) // 2:
				if s[start + k] != s[end - k]:
					flag = 1
					break
				k += 1
			if flag == 0:
				mid2 = mid2 + s[start:end + 1]
				break
			else:
				start += 1
		if len(mid1) < len(mid2):
			print(pre + mid2 + sef)
		else:
			print(pre + mid1 + sef)


def ispalindrome(s):
	ans = ''
	ans2 = ''
	for i in range(0, len(s), 2):
		if s[:i + 1] == s[:i + 1][::-1]:
			ans = s[:i + 1]
	for i in range(1, len(s), 2):
		if s[:i + 1] == s[:i + 1][::-1]:
			ans2 = s[:i + 1]
	ans = ans2 if len(ans2) > len(ans) else ans
	return ans
for _ in range(int(input())):
	s = input()
	if len(s) == 1:
		print(s)
		continue
	l = 0
	while l < len(s) - l - 1:
		if s[l] != s[len(s) - l - 1]:
			break
		l += 1
	m1 = ispalindrome(s[l:len(s) - l])
	m2 = ispalindrome(s[l:len(s) - l][::-1])
	if len(m1) < len(m2):
		m1 = m2
	print(s[:l] + m1 + s[len(s) - l:])


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def gift():
	for _ in range(t):
		string = input()
		n = len(string)
		if n == 1:
			yield string[0]
		else:
			(s, e) = (0, n - 1)
			while s < e:
				if string[s] == string[e]:
					s += 1
					e -= 1
				else:
					break
			form = [string[:s]]
			back = [string[e + 1:]]
			srem = string[s:e + 1]
			mid = ['']
			for i in srem:
				mid.append(i)
				mid.append('')
			R = [None] * len(mid)
			i = 0
			j = 0
			while i < len(mid):
				while i - j >= 0 and i + j < len(mid) and (mid[i - j] == mid[i + j]):
					j += 1
				R[i] = j
				k = 1
				while i - k >= 0 and i + k < len(mid) and (k + R[i - k] < j):
					R[i + k] = R[i - k]
					k += 1
				i += k
				j -= k
			maxind = 0
			for i in range(len(mid)):
				if (i + 1 == R[i] or R[i] == len(mid) - i) and R[maxind] < R[i]:
					maxind = i
			for i in range(maxind - R[maxind] + 1, maxind + R[maxind]):
				form.append(mid[i])
			ans = form + back
			yield ''.join(ans)
t = int(input())
ans = gift()
print(*ans, sep='\n')


t = int(input())
for i in range(t):
	s = input()
	start = 0
	end = len(s) - 1
	sbase = ''
	ebase = ''
	while s[start] == s[end] and start < end:
		sbase += s[start]
		ebase += s[end]
		start += 1
		end = end - 1
	curr = ''
	for j in range(start, end + 1):
		if s[j:end + 1] == s[j:end + 1][::-1]:
			l = s[j:end + 1]
			if len(l) > len(curr):
				curr = l
	for j in range(end, start - 1, -1):
		if s[start:j + 1] == s[start:j + 1][::-1]:
			l = s[start:j + 1]
			if len(l) > len(curr):
				curr = l
	print(sbase + curr + ebase[::-1])


import sys
input = sys.stdin.readline
from collections import deque
t = int(input())
for _ in range(t):
	suf = ''
	s = deque(input().rstrip())
	while len(s) > 1 and s[0] == s[-1]:
		suf += s[0]
		s.pop()
		s.popleft()
	s = ''.join([item for item in s])
	left = ''
	right = ''
	if len(s) > 0:
		ok = False
		for length in range(len(s), 1, -1):
			if length % 2 == 1:
				ff = s[:length // 2]
				bb = s[length // 2 + 1:length][::-1]
				if ff == bb:
					left += s[:length]
					ok = True
					break
			else:
				ff = s[:length // 2]
				bb = s[length // 2:length][::-1]
				if ff == bb:
					left += s[:length]
					ok = True
					break
		if not ok:
			left += s[0]
		s = s[::-1]
		ok = False
		for length in range(len(s), 1, -1):
			if length % 2 == 1:
				ff = s[:length // 2]
				bb = s[length // 2 + 1:length][::-1]
				if ff == bb:
					right += s[:length]
					ok = True
					break
			else:
				ff = s[:length // 2]
				bb = s[length // 2:length][::-1]
				if ff == bb:
					right += s[:length]
					ok = True
					break
		if not ok:
			right += s[0]
	if len(right) > len(left):
		ans = suf + right + suf[::-1]
	else:
		ans = suf + left + suf[::-1]
	print(ans)


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))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def print_list(l):
	print(' '.join(map(str, l)))

def expand(s, left, right):
	while left >= 0 and right < len(s) and (s[left] == s[right]):
		left -= 1
		right += 1
	return (right - left - 2) // 2

def longestPalindrome(s):
	(end, start) = (-1, 0)
	s = '#' + '#'.join(list(s)) + '#'
	arm_len = []
	right = -1
	j = -1
	for i in range(len(s)):
		if right >= i:
			i_sym = 2 * j - i
			min_arm_len = min(arm_len[i_sym], right - i)
			cur_arm_len = expand(s, i - min_arm_len, i + min_arm_len)
		else:
			cur_arm_len = expand(s, i, i)
		arm_len.append(cur_arm_len)
		if i + cur_arm_len > right:
			j = i
			right = i + cur_arm_len
		if 2 * cur_arm_len + 1 > end - start:
			start = i - cur_arm_len
			end = i + cur_arm_len
	return arm_len
for _ in range(N()):
	s = input()
	n = len(s)
	dd = longestPalindrome(s)
	if s == s[::-1]:
		print(s)
	else:
		tail = 0
		while s[tail] == s[n - 1 - tail]:
			tail += 1
		if tail > 0:
			ans = s[:tail] + s[-tail:]
		for m in range(n - 2 >> 1, tail - 1, -1):
			if s[m] == s[m + 1] and (dd[2 * m + 2] >> 1) + tail > m:
				ans = s[:2 * m + 2 - tail] + s[:tail][::-1]
				break
			if (dd[2 * m + 1] >> 1) + tail >= m:
				ans = s[:2 * m + 1 - tail] + s[:tail][::-1]
				break
		k = len(ans)
		for m in range(n + 1 >> 1, n - tail):
			if (n - m) * 2 <= k:
				break
			if s[m] == s[m - 1] and (dd[2 * m] >> 1) + tail >= n - m:
				ans = s[:tail] + s[2 * m - n + tail:]
				break
			if (dd[2 * m + 1] >> 1) + tail + m >= n - 1:
				ans = s[:tail] + s[2 * m - n + tail + 1:]
				break
		print(ans)


t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	if n == 1:
		print(s)
	else:
		(l_my, r_my) = (0, n - 1)
		while l_my + 1 < r_my - 1 and s[l_my] == s[r_my]:
			l_my += 1
			r_my -= 1
		d1 = [0 for i in range(n)]
		(l, r) = (0, -1)
		for i in range(n):
			if i > r:
				k = 1
			else:
				k = min(d1[l + r - i], r - i + 1)
			while i + k < n and i - k >= 0 and (s[i + k] == s[i - k]):
				k += 1
			d1[i] = k
			if i + k - 1 > r:
				l = i - k + 1
				r = i + k - 1
		d2 = [0 for i in range(n)]
		(l, r) = (0, -1)
		for i in range(n):
			if i > r:
				k = 0
			else:
				k = min(d2[l + r - i + 1], r - i + 1)
			while i + k < n and i - k - 1 >= 0 and (s[i + k] == s[i - k - 1]):
				k += 1
			d2[i] = k
			if i + k - 1 > r:
				l = i - k
				r = i + k - 1
		added = 0
		center_piece = []
		for center in range(l_my, r_my):
			if center - d1[center] < l_my or center + d1[center] > r_my:
				offset = min(d1[center], center - l_my + 1, r_my - center + 1)
				if 2 * (offset - 1) + 1 >= added:
					added = 2 * (offset - 1) + 1
					center_piece = [center - offset + 1, center + offset - 1]
		for center2 in range(l_my + 1, r_my + 1):
			center1 = center2 - 1
			offset = min(d2[center2], center1 - l_my + 1, r_my - center2 + 1)
			if center1 - offset < l_my or center2 + offset > r_my:
				if 2 * (offset - 1) + 2 >= added:
					added = 2 * (offset - 1) + 2
					center_piece = [center1 - offset + 1, center2 + offset - 1]
		print(s[:l_my] + s[center_piece[0]:center_piece[1] + 1] + s[r_my + 1:])


def get_palindrome(s):
	n = len(s)
	res = ''
	for i in range(n // 2):
		if s[i] != s[n - i - 1]:
			return res
		else:
			res += s[i]
	return res

def is_palindrome(s):
	n = len(s)
	for i in range(n // 2):
		if s[i] != s[n - i - 1]:
			return False
	return True
for _ in range(int(input())):
	s = input().strip()
	n = len(s)
	beg = get_palindrome(s)
	x = len(beg)
	temp = ''
	a = ''
	b = ''
	for i in range(x, n - x):
		temp += s[i]
		if is_palindrome(temp):
			a = temp
	temp = ''
	for i in range(n - x - 1, x - 1, -1):
		temp = s[i] + temp
		if is_palindrome(temp):
			b = temp
	if len(beg) == len(a) == len(b) == 0:
		print(s[0])
	if len(a) >= len(b):
		temp = (beg + a + beg[::-1]).strip()
		print(temp)
	else:
		temp = (beg + b + beg[::-1]).strip()
		print(temp)


def zalgo(s: str):
	l = 0
	r = 0
	n = len(s)
	mx = 0
	z = [0] * n
	for i in range(1, n):
		z[i] = 0
		if i <= r:
			z[i] = min(z[i - l], r - i + 1)
		while i + z[i] < n and s[z[i]] == s[i + z[i]]:
			z[i] += 1
		if i + z[i] - 1 > r:
			l = i
			r = i + z[i] - 1
		if i + z[i] == n:
			mx = max(mx, z[i])
	return s[:mx]
tests = int(input())
li = []
for i in range(0, tests):
	li.append(input())
for item in li:
	fs = item[:len(item) - 1]
	ls = item[1:len(item)]
	a = zalgo(item + '#' + fs[::-1])
	b = zalgo(ls[::-1] + '#' + item)
	cnt = 0
	if len(a) > len(b):
		MAX = a
	else:
		MAX = b
	i = 0
	j = len(item) - 1
	while i < j and item[i] == item[j]:
		i += 1
		j -= 1
		cnt += 1
	if i >= j:
		print(item)
	else:
		fs = item[i:j]
		ls = item[i + 1:j + 1]
		a = zalgo(item[i:j + 1] + '#' + fs[::-1])
		b = zalgo(ls[::-1] + '#' + item[i:j + 1])
		if len(a) < len(b):
			a = b
		if 2 * cnt + len(a) > len(MAX):
			print(item[:cnt] + a + item[len(item) - cnt:len(item)])
		else:
			print(MAX)


import sys
import math
from collections import defaultdict, deque
import heapq

def kmp(s):
	n = len(s)
	lps = [0 for _ in range(n)]
	l = 0
	lps[0] = 0
	i = 1
	while i < n:
		if s[i] == s[l]:
			l += 1
			lps[i] = l
			i += 1
		elif l != 0:
			l = lps[l - 1]
		else:
			lps[i] = 0
			i += 1
	return lps[-1]
t = int(sys.stdin.readline())
for _ in range(t):
	s = sys.stdin.readline()[:-1]
	n = len(s)
	(i, j) = (0, n - 1)
	while i < j:
		if s[i] == s[j]:
			i += 1
			j -= 1
		else:
			break
	left = s[:i]
	right = s[j + 1:]
	mid = s[i:j + 1]
	new = mid + '#' + mid[::-1]
	x = kmp(new)
	nnew = mid[::-1] + '#' + mid[::-1][::-1]
	y = kmp(nnew)
	if x > y:
		ans = left + new[:x] + right
	else:
		ans = left + nnew[::-1][:y][::-1] + right
	print(ans)


from sys import stdin

def is_Palindrome(S):
	for i in range(int(len(S) / 2)):
		if S[i] != S[len(S) - i - 1]:
			return False
	return True

def find_longgest_Palindrome(S):
	for j in range(len(S), 0, -1):
		if is_Palindrome(S[:j]):
			return S[:j]
	return ''
t = int(stdin.readline())
for _ in range(t):
	S = stdin.readline()[:-1]
	index = -1
	if len(S) == 1:
		print(S)
		continue
	for i in range(int(len(S) / 2)):
		if S[i] != S[len(S) - i - 1]:
			break
		else:
			index = i
	a = S[:index + 1]
	b = a[::-1]
	length = len(S)
	S = S[index + 1:length - index - 1]
	if len(S) > 0:
		A_a = find_longgest_Palindrome(S)
		B_a = find_longgest_Palindrome(S[::-1])
		if len(A_a) > len(B_a):
			a += A_a
		else:
			b = B_a[::-1] + b
	print(a + b)


def reverse(s):
	return s[::-1]

def isPalindrome(s):
	rev = reverse(s)
	if s == rev:
		return True
	return False

def SubStr(S, s, e):
	res = ''
	for i in range(s, e):
		res += S[i]
	return res

def ProcessInputs():
	S = input()
	T = ''
	for i in range(0, len(S)):
		if i >= len(S) - i - 1:
			break
		if S[i] != S[len(S) - i - 1]:
			break
		else:
			T += S[i]
	newS = SubStr(S, i, len(S) - i)
	newS1 = newS[::-1]
	L1 = 1
	for i in range(1, len(newS)):
		string = newS[:i + 1]
		if isPalindrome(string):
			L1 = len(string)
	L2 = 1
	for i in range(1, len(newS1)):
		string = newS1[:i + 1]
		if isPalindrome(string):
			L2 = len(string)
	if L2 > L1:
		res = newS1[:L2]
	else:
		res = newS[:L1]
	print(T + res + T[::-1])
T = int(input())
for _ in range(T):
	ProcessInputs()


import random
import math
import heapq
import collections
from collections import defaultdict
from collections import Counter
from collections import OrderedDict
from typing import List
from collections import deque
from functools import lru_cache
from bisect import bisect_left
import bisect
from itertools import product

def trace(*args, **kwargs):
	print(*args, file=sys.stderr, **kwargs)
import sys, os.path
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')

def input():
	return sys.stdin.readline().rstrip()

def inn():
	return int(input())

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

def ins():
	return input()
ncase = inn()
for _ in range(ncase):
	s = ins()
	le = 0
	ri = len(s) - 1
	while le <= ri:
		if s[le] == s[ri]:
			le += 1
			ri -= 1
		else:
			break
	if le > ri:
		print(s)
		continue

	def max_palindrome_prefix(st):
		st = st + '#' + st[::-1]
		match = [0] * len(st)
		cnt = 0
		for i in range(1, len(st)):
			while cnt > 0 and st[cnt] != st[i]:
				cnt = match[cnt - 1]
			if st[i] == st[cnt]:
				cnt += 1
			match[i] = cnt
		ma = match[len(st) - 1]
		return st[0:max(1, ma)]
	st = s[le:ri + 1]
	s1 = max_palindrome_prefix(st)
	s2 = max_palindrome_prefix(st[::-1])
	mid = s1 if len(s1) > len(s2) else s2
	pre = s[:le] if le > 0 else ''
	print(pre + mid + pre[::-1])


def palind(p):
	l = len(p)
	i = 0
	j = l - 1
	while i <= j:
		if p[i] != p[j]:
			return False
		i += 1
		j -= 1
	return True
t = int(input())
for test in range(t):
	s = input()
	if palind(s):
		print(s)
		continue
	pre = ''
	suf = ''
	while s[0] == s[-1]:
		pre += s[0]
		suf = s[0] + suf
		s = s[1:-1]
	max_str = s[0]
	max_size = 1
	l = len(s)
	for i in range(2, l):
		temp = s[0:i]
		if palind(temp):
			if max_size < len(temp):
				max_str = temp
				max_size = len(temp)
	for i in range(0, l - 1):
		temp = s[i:]
		if palind(temp):
			if max_size < len(temp):
				max_str = temp
				max_size = len(temp)
	print(pre + max_str + suf)


def front(s):
	n = len(s)
	m = 0
	ret = ''
	for i in range(1, n):
		if s[:i] == s[:i][::-1]:
			if i > m:
				m = i
				ret = s[:i]
	return ret

def back(s):
	n = len(s)
	m = 0
	ret = ''
	for i in range(n):
		if s[i:n] == s[i:n][::-1]:
			if n - i > m:
				m = n - i
				ret = s[i:n]
	return ret
for _ in range(int(input())):
	x = input()
	l = len(x)
	start = []
	end = []
	count = 0
	for i in range(l // 2):
		if x[i] == x[l - i - 1]:
			count += 1
			start.append(x[i])
			end.insert(0, x[l - i - 1])
		else:
			break
	temp = x[count:l - count]
	one = front(temp)
	two = back(temp)
	if len(one) >= len(two):
		print(''.join(start) + one + ''.join(end))
	else:
		print(''.join(start) + two + ''.join(end))


import math

class Read:

	@staticmethod
	def string():
		return input()

	@staticmethod
	def int():
		return int(input())

	@staticmethod
	def list(sep=' '):
		return input().split(sep)

	@staticmethod
	def list_int(sep=' '):
		return list(map(int, input().split(sep)))

def d1(s):
	d1 = []
	l = 0
	r = -1
	for i in s:
		d1.append(0)
	for i in range(len(s)):
		k = 1 if i > r else min(d1[l + r - i], r - i + 1)
		while i + k < len(s) and i - k >= 0 and (s[i + k] == s[i - k]):
			k += 1
		d1[i] = k
		if i + k - 1 > r:
			l = i - k + 1
			r = i + k - 1
	return d1

def d2(s):
	d1 = []
	l = 0
	r = -1
	for i in s:
		d1.append(0)
	for i in range(len(s)):
		k = 0 if i > r else min(d1[l + r - i + 1], r - i + 1)
		while i + k < len(s) and i - k - 1 >= 0 and (s[i + k] == s[i - k - 1]):
			k += 1
		d1[i] = k
		if i + k - 1 > r:
			l = i - k
			r = i + k - 1
	return d1

def solve():
	s = input()
	start = 0
	while True:
		l = start
		r = len(s) - 1 - l
		if s[l] == s[r]:
			start += 1
			if l + 1 >= r:
				print(s)
				return
		else:
			break
	s_n = s[start:len(s) - start]
	dd1 = d1(s_n)
	dd2 = d2(s_n)
	start_s = s[0:start]
	end_s = s[len(s) - start:]
	m = 0
	m_s = ''
	m2 = 0
	m_s2 = ''
	len_s_n = len(s_n)
	for i in range(len_s_n):
		l = i - dd1[i] + 1
		r = i + dd1[i]
		if dd1[i] * 2 - 1 > m and (l == 0 or r == len_s_n):
			m_s = s_n[l:r]
			m = len(m_s)
		l = i - dd2[i]
		r = i + dd2[i]
		if dd2[i] * 2 > m2 and (l == 0 or r == len_s_n):
			m_s2 = s_n[l:r]
			m2 = len(m_s2)
	if m2 > m:
		print(start_s + m_s2 + end_s)
	else:
		print(start_s + m_s + end_s)
query_count = Read.int()
while query_count:
	query_count -= 1
	solve()


t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	left = 0
	right = n - 1
	while left < right:
		if s[left] == s[right]:
			left += 1
			right -= 1
		else:
			break
	s1 = s[left:right + 1]
	max_len = 0
	ans = ''
	for i in range(len(s1)):
		c = s1[i:]
		if c == c[::-1] and len(c) > max_len:
			ans = c
			max_len = len(c)
	for i in range(len(s1), -1, -1):
		c = s1[:i]
		if c == c[::-1] and len(c) > max_len:
			ans = c
			max_len = len(c)
	print(s[:left] + ans + s[right + 1:])


for _ in range(int(input())):
	x = input()
	i = 0
	j = len(x) - 1
	mx = 0
	(c1, c2) = (-1, len(x))
	while i < j:
		if x[i] == x[j]:
			mx = max(mx, i + 1 + len(x) - j)
			c1 = i
			c2 = j
			i += 1
			j -= 1
		else:
			break
	(i, j) = (c1, c2)
	i += 1
	j -= 1
	(o, t) = ('', '')
	while i <= j:
		o = x[i:j + 1]
		if o == o[::-1]:
			break
		j -= 1
	j = c2 - 1
	while i <= j:
		t = x[i:j + 1]
		if len(t) < len(o):
			break
		if t == t[::-1]:
			break
		i += 1
	if len(o) < len(t):
		print(x[:c1 + 1] + t + x[c2:])
	else:
		print(x[:c1 + 1] + o + x[c2:])


n = int(input())
for i in range(n):
	s = input()
	t = 1
	for i in range(len(s)):
		q = i
		if s[q] == s[-t]:
			t = t + 1
		else:
			break
	r = s[t - 1:len(s) - t + 1]
	pre = ''
	suf = ''
	for i in range(len(r)):
		if r[0:i] == r[0:i][::-1]:
			pre = r[0:i]
	re = r[::-1]
	for i in range(0, len(r)):
		if re[0:i] == re[0:i][::-1]:
			suf = re[0:i]
	if s == s[::-1]:
		print(s)
	elif len(pre) >= len(suf):
		print(s[:t - 1] + pre + s[:t - 1][::-1])
	else:
		print(s[:t - 1] + suf + s[:t - 1][::-1])


import sys

def kmp(s):
	s = s + '$' + s[::-1]
	temp = [0 for i in range(len(s))]
	j = 0
	for i in range(1, len(s)):
		while j != 0 and s[i] != s[j]:
			j = temp[j - 1]
		if s[i] == s[j]:
			j += 1
			temp[i] = j
	return s[:temp[-1]]
for _ in range(int(input())):
	s = input()
	if s == s[::-1]:
		print(s)
		continue
	n = len(s)
	(i, j) = (0, n - 1)
	while i <= j:
		if s[i] == s[j]:
			i += 1
			j -= 1
		else:
			break
	a = ''
	if i <= j:
		(a, b) = (kmp(s[i:j + 1]), kmp(s[i:j + 1][::-1]))
		if len(a) < len(b):
			a = b
		print(s[:i] + a + s[:i][::-1])
	elif j + 1 == i:
		print(s)
	else:
		print(s[:i])


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	s = sys.stdin.readline().strip()
	en = 0
	for i in range(len(s) // 2):
		if s[i] == s[-(i + 1)]:
			en += 2
		else:
			break
	if en != len(s):
		bn = 1
		bi = en // 2
		for i in range(en // 2, len(s) - 1 - en // 2):
			pn = 2 * min(i - en // 2, len(s) - 1 - en // 2 - i) + 1
			if pn <= bn:
				continue
			for j in range(pn // 2 + 1):
				if s[i - j] != s[i + j]:
					break
			else:
				bn = pn
				bi = i
		for i in range(en // 2, len(s) - 1 - en // 2):
			pn = 2 * min(i - en // 2 + 1, len(s) - 1 - en // 2 - i)
			if pn <= bn:
				continue
			for j in range(pn // 2):
				if s[i - j] != s[i + j + 1]:
					break
			else:
				bn = pn
				bi = i
	else:
		bn = 0
		bi = 1
	ss = s[:en // 2] + s[bi - (bn - 1) // 2:bi + bn // 2 + 1] + s[len(s) - en // 2:]
	print(ss)


from collections import defaultdict
import math
from functools import reduce

def constructDP(dp, string):
	l = len(string)
	isPalin = [[False for i in range(l + 1)] for j in range(l + 1)]
	for i in range(l - 1, -1, -1):
		(isPalin[i][i], dp[i][i]) = (True, 1)
		for j in range(i + 1, l):
			isPalin[i][j] = string[i] == string[j] and (i + 1 > j - 1 or isPalin[i + 1][j - 1])
			dp[i][j] = dp[i][j - 1] + dp[i + 1][j] - dp[i + 1][j - 1] + isPalin[i][j]
	return isPalin

def pre_calc_idxs(string):
	l = len(string)
	dp = [[0 for i in range(len(string) + 1)] for j in range(len(string) + 1)]
	isPalin = constructDP(dp, string)
	j_at = [1 for j in range(l)]
	i_at = [1 for i in range(l)]
	for i in range(l):
		for j in range(i, l):
			if isPalin[i][j]:
				if j < l - i - 1:
					j_at[i] = max(j_at[i], abs(j - i) + 1)
				if i > l - j - 1:
					i_at[j] = max(i_at[j], abs(j - i) + 1)
	if string == string[::-1]:
		return string
	max_len = 1
	ans = string[0]
	for j in range(l):
		if isPalin[0][j]:
			if max_len < j + 1:
				max_len = j + 1
				ans = string[:j + 1]
		if isPalin[j][l - 1]:
			if max_len < l - j:
				max_len = l - j
				ans = string[j:]
	i = 0
	j = l - 1
	while i < j and string[i] == string[j]:
		if 2 * (i + 1) + min(j_at[i + 1], i_at[j - 1]) > l:
			continue
		if 2 * (i + 1) + max(j_at[i + 1], i_at[j - 1]) > max_len:
			max_len = 2 * (i + 1) + max(j_at[i + 1], i_at[j - 1])
			ans = string[:i + 1]
			if j_at[i + 1] > i_at[j - 1]:
				ans += string[i + 1:i + j_at[i + 1] + 1]
			else:
				ans += string[j - i_at[j - 1]:j]
			ans += string[j:]
		i += 1
		j -= 1
	return ans
for _ in range(int(input())):
	print(pre_calc_idxs(input()))


import sys

def reverse(s):
	return s[::-1]

def isPalindrome(s):
	rev = reverse(s)
	if s == rev:
		return True
	return False
q = int(sys.stdin.readline())
for _ in range(q):
	s = input()
	ans1 = ''
	ans2 = ''
	i = 0
	j = len(s) - 1
	while i < j:
		if s[i] == s[j]:
			ans1 += s[i]
			ans2 += s[j]
			i += 1
			j -= 1
		else:
			break
	cc = s[i:j + 1]
	maxx1 = -1
	for i in range(0, len(cc)):
		if isPalindrome(cc[0:i + 1]):
			maxx1 = max(maxx1, i + 1)
	maxx2 = -1
	for i in range(len(cc) - 1, -1, -1):
		if isPalindrome(cc[i:]):
			maxx2 = max(maxx2, len(cc) - i)
	ans = ''
	if maxx1 > maxx2:
		ans3 = cc[:maxx1]
	else:
		ans3 = cc[::-1][:maxx2]
	print(ans1 + ans3 + ans2[::-1])


from sys import stdin
input = stdin.readline
t = int(input())

def checkPalin(s, x, y):
	for i in range((y - x + 1) // 2):
		if s[x + i] != s[y - i]:
			return False
	else:
		return True
for _ in range(t):
	out = []
	s = input().rstrip()
	n = len(s)
	idx = 0
	for i in range(n // 2):
		if s[i] == s[n - i - 1]:
			out.append(s[i])
		else:
			idx = i
			break
	else:
		print(s)
		continue
	s1 = []
	s2 = []
	for i in range(n - idx * 2):
		if checkPalin(s, idx, n - idx - i - 1):
			for j in range(n - idx * 2 - i):
				s1.append(s[idx + j])
			break
	for i in range(n - idx * 2):
		if checkPalin(s, idx + i, n - idx - 1):
			for j in range(n - idx * 2 - i):
				s2.append(s[idx + i + j])
			break
	outBack = out.copy()
	outBack = outBack[::-1]
	if len(s1) > len(s2):
		out.extend(s1)
	else:
		out.extend(s2)
	out.extend(outBack)
	print(''.join(out))


def find_pal_prefix(s):
	if not s:
		return s
	res = s[0]
	for i in range(1, len(s)):
		s1 = s[:i]
		s2 = s1[::-1]
		if s1 == s2:
			res = s1
	return res

def solve():
	s1 = input().strip()
	s2 = s1[::-1]
	a = ''
	b = ''
	p = 0
	for p in range(len(s1) // 2):
		next_a = s1[p]
		next_b = s2[p]
		if next_a == next_b:
			a += next_a
			b += next_b
		else:
			break
	else:
		if len(s1) % 2 == 1:
			a += s1[len(s1) // 2]
	s = s1
	if len(b) == 0:
		s = s[len(a):]
	else:
		s = s[len(a):-len(b)]
	a_plus = find_pal_prefix(s)
	b_plus = find_pal_prefix(s[::-1])
	if len(a_plus) > len(b_plus):
		a += a_plus
	else:
		b += b_plus
	two_sided_pal = a + b[::-1]
	return two_sided_pal

def main():
	t = int(input())
	for _ in range(t):
		print(solve())
main()


from math import *
from operator import *
import sys, random

def nextInt():
	return int(input())

def nextStrs():
	return input().split()

def nextInts():
	return list(map(int, nextStrs()))

def main():
	t = nextInt()
	for _ in range(t):
		S = input()
		suf = ''
		pref = ''
		lo = 0
		hi = len(S) - 1
		while lo <= hi:
			if lo == hi:
				suf += S[lo]
				lo += 1
				hi -= 1
				break
			if S[lo] == S[hi]:
				suf = suf + S[lo]
				pref = S[hi] + pref
				lo += 1
				hi -= 1
			else:
				break
		res = suf + pref
		res1 = ''
		res2 = ''
		for i in range(lo, hi + 1):
			tmp = S[:i] + pref
			if tmp == tmp[::-1]:
				res1 = tmp
		for i in range(hi, lo, -1):
			tmp = suf + S[i:]
			if tmp == tmp[::-1]:
				res2 = tmp
		if len(res) < len(res1):
			res = res1
		if len(res) < len(res2):
			res = res2
		print(res)
exit(main())


class Solution:

	def solve(self, s):
		is_permuty = lambda s: s == s[::-1]
		(i, j) = (0, len(s) - 1)
		while i <= j and is_permuty(s[:i + 1] + s[j:]):
			i += 1
			j -= 1
		if i > j:
			return s
		(front, back) = ('', '') if i == 0 else (s[:i], s[j + 1:])
		mid = ''
		for it in range(i, j + 1):
			t = s[i:it + 1]
			if is_permuty(t):
				mid = t
		return front + mid + back

def main():
	s = Solution()
	for _ in range(int(input())):
		t = input()
		(a, b) = (s.solve(t), s.solve(t[::-1]))
		if len(a) > len(b):
			print(a)
		else:
			print(b)
main()


from sys import stdin, stdout

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

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

def manacher_str(s):
	ans = '*'
	for c in s:
		ans += c
		ans += '*'
	return ans

def manacher(s):
	n = len(s)
	(C, R) = (0, 0)
	L = [0] * n
	for i in range(n):
		mirrI = C - (i - C)
		if i <= R:
			rad = min(L[mirrI], R - i)
		else:
			rad = 0
		while i - rad - 1 >= 0 and i + rad + 1 < n and (s[i - rad - 1] == s[i + rad + 1]):
			rad += 1
		L[i] = rad
		if i + rad > R:
			(C, R) = (i, i + rad)
	return L
import time
start_time = time.time()
for _ in range(int(input())):
	s = stdin.readline().strip()
	n = len(s)
	l = 0
	r = n - 1
	while l < r and s[l] == s[r]:
		l += 1
		r -= 1
	(prefStr, sufStr) = (s[:l], s[r + 1:n])
	new_s = manacher_str(s[l:r + 1])
	L = manacher(new_s)
	(mx, C) = (-1, -1)
	for i in range(len(L)):
		if i - L[i] == 0 or i + L[i] == len(L) - 1:
			if L[i] > mx:
				mx = L[i]
				C = i
	midStr = new_s[C - L[C]:C + L[C] + 1].replace('*', '')
	st = prefStr + midStr + sufStr
	stdout.write(st + '\n')


def isP(s):
	l = 0
	r = len(s) - 1
	while r > l and s[l] == s[r]:
		r -= 1
		l += 1
	return r <= l

def getLongestP(s):
	r = len(s)
	while not isP(s[:r]):
		r -= 1
	return s[:r]

def reverse(s):
	return s[::-1]
t = int(input())
for i in range(t):
	s = input()
	l = 0
	r = len(s) - 1
	while r > l and s[l] == s[r]:
		l += 1
		r -= 1
	if r <= l:
		print(s)
		continue
	middle = s[l:r + 1]
	right = getLongestP(middle)
	left = reverse(getLongestP(reverse(middle)))
	final = right if len(right) > len(left) else left
	print(s[:l] + final + s[r + 1:])


def get_hash(L, R, rev):
	if L == 0:
		if rev:
			return h2[R]
		return h1[R]
	if rev:
		return (q + h2[R] - h2[L - 1] * p[R - L + 1]) % q
	return (q + h1[R] - h1[L - 1] * p[R - L + 1]) % q
ans = []
for _ in range(int(input())):
	s = list(input())
	n = len(s)
	q = 10 ** 9 + 7
	p0 = 31
	p = [0] * (n + 1)
	p[0] = 1
	h1 = [0] * n
	h1[0] = ord(s[0])
	h2 = [0] * n
	h2[0] = ord(s[-1])
	for i in range(1, n + 1):
		p[i] = p[i - 1] * p0 % q
	for i in range(1, n):
		h1[i] = (h1[i - 1] * p0 + ord(s[i])) % q
		h2[i] = (h2[i - 1] * p0 + ord(s[-i - 1])) % q
	ln = 0
	for i in range(n // 2):
		if s[i] == s[n - i - 1]:
			ln += 1
		else:
			break
	if ln == n // 2:
		ans.append(''.join(s))
		continue
	indi = -1
	ansi = -1
	for i in range(ln, n - ln):
		dst = min(i - ln, n - ln - i - 1)
		if get_hash(i - dst, i, 0) != get_hash(n - i - dst - 1, n - i - 1, 1):
			continue
		if dst > ansi:
			ansi = dst
			indi = i
	ansj = -1
	indj = -1
	for i in range(ln, n - ln):
		dst = min(i - ln, n - ln - i - 2)
		if get_hash(i - dst, i, 0) != get_hash(n - i - dst - 2, n - i - 2, 1):
			continue
		if dst > ansj:
			ansj = dst
			indj = i
	if ansi * 2 - 1 > ansj * 2:
		ss = s[:ln] + s[indi - ansi:indi + ansi + 1] + s[n - ln:]
		ss = ''.join(ss)
	else:
		ss = s[:ln] + s[indj - ansj:indj + ansj + 2] + s[n - ln:]
		ss = ''.join(ss)
	ans.append(ss)
print('\n'.join(ans))


def dev_3(st_list):
	res_left = []
	res_m = ''
	fl = True
	max_iter = len(st_list) // 2
	ind_right = -1
	ind_left = 0
	counter = 1
	while fl:
		left = st_list[ind_left]
		right = st_list[ind_right]
		if left != right:
			fl = False
		else:
			res_left.append(left)
		ind_left += 1
		ind_right -= 1
		counter += 1
		if counter > max_iter:
			fl = False
	if 2 * len(res_left) != len(st_list):
		res_m = st_list[len(res_left):len(st_list) - len(res_left)]
	res_right = list(res_left)
	res_right.reverse()
	if len(st_list) == 1:
		return ['', st_list[0], '']
	else:
		return [''.join(res_left), res_m, ''.join(res_right)]

def is_p(st_list):
	fl = True
	resp = True
	max_iter = len(st_list) // 2
	counter = 1
	left_ind = 0
	right_ind = -1
	while fl:
		left = st_list[left_ind]
		right = st_list[right_ind]
		if left != right:
			fl = False
			resp = False
		left_ind += 1
		right_ind -= 1
		counter += 1
		if counter > max_iter:
			fl = False
	return resp

def find_p(st_list):
	left_ptr = 0
	right_ptr = len(st_list) - 1
	res0 = []
	fl = True
	left = st_list[left_ptr]
	while fl:
		right = st_list[right_ptr]
		if left != right:
			right_ptr -= 1
		elif is_p(st_list[:right_ptr + 1]):
			fl = False
			res0 = st_list[:right_ptr + 1]
		else:
			right_ptr -= 1
		if right_ptr == 0:
			fl = False
	left_ptr = 0
	right_ptr = len(st_list) - 1
	res1 = []
	fl = True
	right = st_list[right_ptr]
	while fl:
		left = st_list[left_ptr]
		if left != right:
			left_ptr += 1
		elif is_p(st_list[left_ptr:]):
			fl = False
			res1 = st_list[left_ptr:]
		else:
			left_ptr += 1
		if left_ptr == right_ptr:
			fl = False
	if len(res0) >= len(res1):
		return res0
	else:
		return res1
t = int(input())
for t0 in range(t):
	s = list(input())
	s3 = dev_3(s)
	if s3[1]:
		m = ''.join(find_p(s3[1]))
		if not m:
			m = s3[1][0]
	else:
		m = ''
	res = s3[0] + m + s3[2]
	if res:
		print(res)
	else:
		print(s[0])


import sys
input = sys.stdin.readline
t = int(input())
ret = ''
for _ in range(t):
	suf = []
	s = list(input().rstrip())
	l = 0
	r = len(s) - 1
	while r - l > 1 and s[l] == s[r]:
		suf.append(s[l])
		l += 1
		r -= 1
	s = '$'.join(s[l:r + 1])
	i = 0
	j = 0
	length = len(s)
	par = [0] * length
	while i < length:
		while i - j >= 0 and i + j < length and (s[i - j] == s[i + j]):
			j += 1
		par[i] = j
		k = 1
		while i - k >= 0 and i + k < length and (k + par[i - k] < j):
			par[i + k] = par[i - k]
			k += 1
		i += k
		j -= k
	radius = 1
	mid = 0
	is_front = True
	for (i, item) in enumerate(par):
		if item == i + 1 and item > radius:
			radius = item
			mid = i
			is_front = True
		if item == length - i and item > radius:
			radius = item
			mid = i
			is_front = False
	if is_front:
		left = s[:mid + radius]
	else:
		left = s[mid - radius:]
	core = []
	for ch in left:
		if ch == '$':
			continue
		core.append(ch)
	core_s = ''.join(core)
	suf_s = ''.join(suf)
	pre_s = ''.join(suf[::-1])
	ans = ''.join([suf_s, core_s, pre_s])
	print(ans)


T = int(input())

class Node:

	def __init__(self, parent, letter):
		self.parent = parent
		self.letter = letter
		self.child = None

def pal(s, start, end):
	if start > end:
		return ''
	first_node = Node(None, s[start])
	current_nodes = [first_node]
	answer = start
	for i in range(start + 1, end + 1):
		next_nodes = []
		for node in current_nodes:
			if node.letter == s[i]:
				if node.parent:
					next_nodes.append(node.parent)
				else:
					answer = i
			if not node.child and node.parent and (node.parent.letter == s[i]):
				if node.parent.parent:
					next_nodes.append(node.parent.parent)
				else:
					answer = i
			if node.child is None:
				node.child = Node(node, s[i])
				next_nodes.append(node.child)
		current_nodes = next_nodes
	return s[start:answer + 1]
for t in range(T):
	s = input()
	N = len(s)
	start = []
	i = 0
	while s[i] == s[N - 1 - i] and i < N - 1 - i:
		start.append(s[i])
		i += 1
	palin1 = pal(s, i, N - 1 - i)
	palin2 = pal(s[::-1], i, N - 1 - i)
	if len(palin1) >= len(palin2):
		palin = palin1
	else:
		palin = palin2
	answer = start + list(palin) + start[::-1]
	assert len(answer) <= N
	print(''.join(answer))


import sys
input = sys.stdin.readline
x = int(input())
for i in range(x):
	s = input()
	s = s[:-1]
	maxa = 0
	save = ''
	ans = []
	count = 0
	if s == s[::-1]:
		print(s)
		continue
	for i in range(len(s)):
		if s[i] == s[len(s) - i - 1]:
			count += 1
		else:
			break
	if count == 0:
		ans.append(s[0])
	else:
		mu = s[0:count]
		ans.append(mu + mu[:-1])
		for i in range(count, len(s) - count):
			r = s[i:]
			tam = s[0:i]
			ram = tam + mu[::-1]
			if ram == ram[::-1]:
				ans.append(ram)
			gam = mu + r
			if gam == gam[::-1]:
				ans.append(gam)
	for i in range(len(s)):
		r = s[i:]
		if r == r[::-1]:
			ans.append(r)
			break
	for i in range(len(s)):
		r = s[0:i]
		if r == r[::-1]:
			ans.append(r)
	for i in range(len(ans)):
		if len(ans[i]) > maxa:
			maxa = len(ans[i])
			save = ans[i]
	print(save)


def kmp(s):
	arr = [0 for i in range(len(s))]
	(i, j) = (1, 0)
	while i < len(s):
		if s[i] != s[j] and j == 0:
			arr[i] = j
			i += 1
		elif s[i] != s[j] and j > 0:
			j = arr[j - 1]
		else:
			arr[i] = j + 1
			i += 1
			j += 1
	return arr[-1]
test = int(input())
for testcase in range(test):
	s = input()
	i = 0
	j = len(s) - 1
	while i < j:
		if s[i] != s[j]:
			i -= 1
			j += 1
			break
		else:
			i += 1
			j -= 1
	if i == j or i + 1 == j or i > j:
		print(s)
		continue
	s_new = s[i + 1:j]
	s1 = s_new + '?' + s_new[::-1]
	s2 = s_new[::-1] + '?' + s_new
	l1 = kmp(s1)
	l2 = kmp(s2)
	if l1 > l2:
		ans = s[:i + 1] + s1[:l1] + s[j:]
	else:
		ans = s[:i + 1] + s2[:l2] + s[j:]
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	l = 0
	h = n - 1
	comb = ''
	flag = False
	while s[l] == s[h] and l <= h:
		if l == h:
			temp = s[l]
			flag = True
			break
		comb += s[l]
		l += 1
		h -= 1
	t1 = ''
	t2 = ''
	if l != h:
		for j in range(h, l - 1, -1):
			t = s[l:j]
			if t == t[::-1]:
				t1 = t
				break
		for j in range(l + 1, h):
			t = s[j:h + 1]
			if t == t[::-1]:
				t2 = t
				break
	if len(t1) > len(t2):
		temp1 = t1
	else:
		temp1 = t2
	if flag:
		comb = comb + temp + comb[::-1]
	else:
		comb = comb + temp1 + comb[::-1]
	pre = ''
	for j in range(n - 1, -1, -1):
		t = s[:j + 1]
		if t == t[::-1]:
			pre = t
			break
	suff = ''
	for i in range(n):
		t = s[i:n]
		if t == t[::-1]:
			suff = t
			break
	p = len(comb)
	q = len(pre)
	r = len(suff)
	if p == max(p, q, r):
		print(comb)
	elif q == max(p, q, r):
		print(pre)
	else:
		print(suff)


def chk(s):
	i = 0
	j = len(s) - 1
	while i <= j:
		if s[i] == s[j]:
			i += 1
			j -= 1
		else:
			return 0
	return 1
for _ in range(int(input())):
	s = input()
	i = 0
	j = len(s) - 1
	while i <= j:
		if s[i] == s[j]:
			if i == j:
				break
			i += 1
			j -= 1
		else:
			break
	ff = s[:i]
	ss = s[j + 1:]
	nex = s[i:j + 1]
	p = ''
	q = ''
	if len(nex) == 1:
		print(ff + nex + ss)
		continue
	for i in range(len(nex) - 1, 0, -1):
		s1 = nex[:i]
		check = chk(s1)
		if check == 1:
			p = s1
			break
	for i in range(1, len(nex)):
		s1 = nex[i:]
		check = chk(s1)
		if check == 1:
			q = s1
			break
	if len(p) > len(q):
		print(ff + p + ss)
	else:
		print(ff + q + ss)


import math, collections, sys
sys.setrecursionlimit(10 ** 5)
x = ''
y = ''

def checkpal(s):
	for i in range(len(s)):
		if s[i] != s[len(s) - 1 - i]:
			return False
	return True

def suffix(l, r, s):
	test = ''
	val = ''
	for i in range(l, r + 1):
		test += s[i]
		if checkpal(test):
			val = test
	return val

def prefix(l, r, s):
	test = ''
	val = ''
	for i in range(r, l - 1, -1):
		test += s[i]
		if checkpal(test):
			val = test
	return val

def getString(l, r, s):
	global x, y
	if l > r:
		return
	if l == r:
		x += s[l]
		return
	if s[l] == s[r]:
		x += s[l]
		y += s[r]
		getString(l + 1, r - 1, s)
	else:
		p = suffix(l, r, s)
		q = prefix(l, r, s)
		if len(p) > len(q):
			x += p
		else:
			y += q
		return

def solve():
	s = input()
	n = len(s)
	getString(0, n - 1, s)
	print(x + y[::-1])
for _ in range(int(input())):
	x = ''
	y = ''
	solve()


import sys
import heapq
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt
from collections import defaultdict as dd, deque, Counter as c
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

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

def out(var):
	sys.stdout.write(str(var))

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]

def post_process(string, ml, c):
	result = []
	for i in string[c - ml:c + ml + 1]:
		if i == '#':
			continue
		result.append(i)
	return ''.join(result)

def manacher(string):
	arr = ['#']
	for i in string:
		arr.append(i)
		arr.append('#')
	left = l1d(len(arr))
	(r, c) = (0, 0)
	for i in range(len(arr)):
		mirror = 2 * c - i
		if r > i:
			left[i] = min(r - i, left[mirror])
		try:
			while arr[i + 1 + left[i]] == arr[i - 1 - left[i]]:
				left[i] += 1
		except:
			pass
		if i + left[i] > r:
			c = i
			r = i + left[i]
	maxlength = 0
	center = 0
	for i in range(len(left)):
		if left[i] > maxlength and (i == left[i] or i + left[i] >= len(left) - 1):
			maxlength = left[i]
			center = i
	return post_process(arr, maxlength, center)
for _ in range(int(data())):
	s = data()
	if s[::-1] == s:
		out(s + '\n')
		continue
	(i, j) = (0, len(s) - 1)
	while i < j:
		if s[i] == s[j]:
			i += 1
			j -= 1
		else:
			break
	if i == 0:
		out(manacher(s) + '\n')
	else:
		out(s[:i] + manacher(s[i:j + 1]) + s[j + 1:] + '\n')


def pref(s):
	n = len(s)
	prev = [0] * (n + 1)
	m = [0] * (n + 1)
	for i in range(1, n + 1):
		(m, prev) = (prev, m)
		for j in range(1, n + 1):
			if s[n - i] == s[j - 1]:
				m[j] = max(1, prev[j - 1] + 1)
			else:
				m[j] = 0
	l = 0
	for i in range(n):
		if m[i] == i:
			l = i
	return l

def go():
	s = input()
	n = len(s)
	(i, j) = (0, n - 1)
	while i < j and s[i] == s[j]:
		i += 1
		j -= 1
	res = s[:i]
	left = s[i:j + 1]
	x = pref(left)
	y = pref(left[::-1])
	if x > y:
		center = left[:x]
	else:
		center = left[-y:]
	res = res + center + res[::-1]
	return res
t = int(input())
ans = []
for _ in range(t):
	ans.append(go())
print('\n'.join(ans))


qn = int(input())
for loop in range(qn):
	s = list(input())
	n = len(s)
	lastif = None
	for i in range((n + 1) // 2):
		if s[i] == s[-1 - i]:
			continue
		else:
			lastif = i
			break
	if lastif == None:
		print(''.join(s))
		continue
	form = s[:lastif]
	back = s[n - lastif:]
	ans = []
	for i in range(n - 2 * lastif):
		now = s[lastif:i + lastif]
		flag = True
		for j in range(len(now) // 2):
			if now[j] != now[-1 - j]:
				flag = False
				break
		if flag:
			if len(ans) < len(form) + len(now) + len(back):
				ans = form + now + back
	s.reverse()
	for i in range(n - 2 * lastif):
		now = s[lastif:i + lastif]
		flag = True
		for j in range(len(now) // 2):
			if now[j] != now[-1 - j]:
				flag = False
				break
		if flag:
			if len(ans) < len(form) + len(now) + len(back):
				now.reverse()
				ans = form + now + back
	print(''.join(ans))


def bitrim(s):
	i = 0
	j = len(s) - 1
	while i < j and s[i] == s[j]:
		i += 1
		j -= 1
	return (i, s[i:j + 1])

def rebuild(s):
	n = len(s)
	ss = ['#'] * (n * 2 + 3)
	ss[0] = '$'
	for i in range(1, n + 1):
		ss[i * 2] = s[i - 1]
	ss[-1] = 0
	return ss

def Manacher(s):
	n = len(s) - 1
	p = [1] * n
	mx = 0
	hp = 1
	tp = 1
	for i in range(n):
		p[i] = 1 if mx <= i else min(p[mx - i + ml], mx - i)
		while s[i - p[i]] == s[i + p[i]]:
			p[i] += 1
		if i + p[i] > mx:
			mx = i + p[i]
			ml = i - p[i]
		if ml == 0:
			hp = max(hp, p[i] - 1)
		if mx == n:
			tp = max(tp, p[i] - 1)
	return [hp, tp]

def f(s):
	(bi, ts) = bitrim(s)
	(hp, tp) = Manacher(rebuild(ts))
	ps = ts[:hp] if hp > tp else ts[-tp:]
	return s[:bi] + ps + (s[-bi:] if bi > 0 else '')
[print(f(input())) for _ in range(int(input()))]


import os
import sys
from io import BytesIO, IOBase
import threading
from math import floor
from collections import Counter
from math import gcd
mod = 998244353

def kmp(pat, M):
	len = 0
	lps = [0] * M
	i = 1
	while i < M:
		if pat[i] == pat[len]:
			len += 1
			lps[i] = len
			i += 1
		elif len != 0:
			len = lps[len - 1]
		else:
			lps[i] = 0
			i += 1
	return lps[-1]

def main():
	for _ in range(int(input())):
		s = input()
		n = len(s)
		start = 0
		end = n - 1
		while start < end and s[start] == s[end]:
			start += 1
			end -= 1
		ans = s[:start]
		t = s[start:end + 1]
		t1 = t + '*' + t[::-1]
		t2 = t[::-1] + '*' + t
		c1 = kmp(t1, len(t1))
		c2 = kmp(t2, len(t2))
		if c1 < c2:
			ans += t2[:c2]
		else:
			ans += t1[:c1]
		ans += s[end + 1:]
		print(ans)
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 = input()
	palindrome = False
	for i in range(len(s) // 2):
		if s[i] != s[-i - 1]:
			break
	else:
		print(s)
		palindrome = True
	if not palindrome:
		temp = ''
		maximo = i - 1
		start = maximo + 1
		for j in range(len(s) - maximo - 2, maximo - 1, -1):
			palindrome = True
			for k in range((j - start + 1) // 2):
				if s[start + k] != s[j - k]:
					palindrome = False
					break
			if palindrome:
				temp = s[:j + 1] + s[len(s) - maximo - 1:]
				break
		else:
			if maximo >= 0:
				temp = s[:maximo + 1] + s[len(s) - maximo - 1:]
			else:
				temp = s[0]
		s = s[::-1]
		maximo = i - 1
		start = maximo + 1
		temp2 = ''
		for j in range(len(s) - maximo - 2, maximo - 1, -1):
			palindrome = True
			for k in range((j - start + 1) // 2):
				if s[start + k] != s[j - k]:
					palindrome = False
					break
			if palindrome:
				temp2 = s[:j + 1] + s[len(s) - maximo - 1:]
				break
		else:
			if maximo >= 0:
				temp2 = s[:maximo + 1] + s[len(s) - maximo - 1:]
			else:
				temp2 = s[0]
		if len(temp2) > len(temp):
			print(temp2)
		else:
			print(temp)


for _ in range(int(input())):
	s = input()
	(l, r) = (0, len(s) - 1)
	while l < r:
		if s[l] == s[r]:
			l += 1
			r -= 1
		else:
			break
	(left, right) = (s[:l], s[r + 1:])
	t = s[l:r + 1]
	curr = ''
	for i in range(len(t)):
		if t[:i + 1][::-1] == t[:i + 1] and len(t[:i + 1]) > len(curr):
			curr = t[:i + 1]
	t = t[::-1]
	for i in range(len(t)):
		if t[:i + 1][::-1] == t[:i + 1] and len(t[:i + 1]) > len(curr):
			curr = t[:i + 1]
	print(left + curr + right)


def longest_prefix(s, ss):
	p = s + '#' + s[::-1]
	pi = [0] * len(p)
	j = 0
	for i in range(1, len(p)):
		while j != 0 and p[j] != p[i]:
			j = pi[j - 1]
		if p[j] == p[i]:
			j += 1
		pi[i] = j
	return s[:j]

def solve(s):
	(a, b) = ('', '')
	(i, j) = (0, len(s) - 1)
	while i < j:
		if s[i] != s[j]:
			break
		i += 1
		j -= 1
	a = s[:i]
	b = s[j + 1:]
	if i + j < len(s):
		subs = s[i:j + 1]
		subsr = subs[::-1]
		p1 = longest_prefix(subs, subsr)
		p2 = longest_prefix(subsr, subs)
		a += p1 if len(p1) >= len(p2) else p2
	return a + b

def main():
	t = int(input())
	for _ in range(t):
		s = input()
		t = solve(s)
		print(t)
main()


import os
import sys
from io import BytesIO, IOBase
from math import log, floor, ceil

def ispal(x):
	for i in range(len(x) // 2 + 1):
		if x[i] != x[len(x) - i - 1]:
			return False
	return True

def main():
	for _ in range(int(input())):
		s = input()
		if ispal(s):
			print(s)
			continue
		n = len(s)
		(l, r) = (0, n - 1)
		while l <= r and s[l] == s[r]:
			l += 1
			r -= 1
		(l1, l2) = ('', '')
		for i in range(l, r + 1):
			(x, y) = (s[l:i + 1], s[i:r + 1])
			if ispal(x) and len(x) > len(l1):
				l1 = x
			if ispal(y) and len(y) > len(l2):
				l2 = y
		print(s[:l], end='')
		if len(l1) > len(l2):
			print(l1, end='')
		else:
			print(l2, end='')
		print(s[r + 1:n], end='')
		print()
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()


for tc in range(int(input())):
	s = input()
	if len(s) == 1:
		print(s)
	elif s == s[::-1]:
		print(s)
	else:
		t1 = ''
		t2 = ''
		ss = s[::-1]
		for i in range(1, len(s)):
			v = s[:i]
			vv = ss[:i]
			if v == v[::-1]:
				t1 = v
			if vv == vv[::-1]:
				t2 = vv
		at = t1 if len(t1) >= len(t2) else t2
		a = ''
		b = ''
		i = 0
		j = len(s) - 1
		while i < j:
			if s[i] == s[j]:
				a += s[i]
				b += s[j]
				i += 1
				j -= 1
			else:
				break
		j += 1
		te = ''
		ii = i
		jj = j
		aa = ''
		bb = ''
		ss = s[i:j]
		sss = ss[::-1]
		ttt = ''
		tttt = ''
		for i in range(1, len(ss)):
			v = ss[:i]
			if v == v[::-1]:
				ttt = v
		for j in range(1, len(sss)):
			v = sss[:j]
			if v == v[::-1]:
				tttt = v
		ans = ttt if len(ttt) > len(tttt) else tttt
		an = a + ans + b[::-1]
		print(an) if len(an) >= len(at) else print(at)


import io
import os
from random import randint
DEBUG = False

def bisect_f(f, lo, hi):
	if DEBUG:
		assert f(lo) and (not f(hi))
	while hi - lo > 1:
		mid = (lo + hi) // 2
		if f(mid):
			lo = mid
		else:
			hi = mid
		if DEBUG:
			assert f(lo) and (not f(hi))
	if DEBUG:
		assert hi - lo == 1
		assert f(lo) and (not f(hi))
	return lo
cachedPow = {}
PRIME = 31
MOD = 2 ** 61 - 1

class RabinKarpRollingHash:

	def __init__(self, s, PRIME=31, MOD=1000000007):
		n = len(s)
		prefix = [0] * (n + 1)
		for i in range(n):
			prefix[i + 1] = (prefix[i] * PRIME + ord(s[i]) - ord('a')) % MOD
		if n not in cachedPow:
			cachedPow[0] = 1
			for i in range(n):
				cachedPow[i + 1] = cachedPow[i] * PRIME % MOD
		self.PRIME = PRIME
		self.MOD = MOD
		self.prefix = prefix

	def getHash(self, i, j):
		return (self.prefix[j] - self.prefix[i] * cachedPow[j - i]) % self.MOD

def solve(s):
	n = len(s)
	forward = RabinKarpRollingHash(s, PRIME, MOD)
	backward = RabinKarpRollingHash(s[::-1], PRIME, MOD)

	def revEqual(i1, j1, i2, j2):
		ret = forward.getHash(i1, j1) == backward.getHash(n - j2, n - i2)
		if DEBUG:
			assert 0 <= i1 <= j1 <= n
			assert 0 <= i2 <= j2 <= n
			check = s[i1:j1] == s[i2:j2][::-1]
			if ret != check:
				print(forward.getHash(i1, j1), backward.getHash(n - j2, n - i2))
				print(s[i1:j1], s[i2:j2][::-1])
			assert ret == check
		return ret
	if revEqual(0, n, 0, n):
		return s
	minAB = 0
	for i in range(n):
		if i < n - 1 - i and s[i] == s[n - 1 - i]:
			minAB = i
		else:
			break
	best = (0, 0, 0)
	for center in range(minAB, n - minAB):
		for parity in [0, 1]:

			def isPal(r):
				i = center - r
				j = center + r + parity
				if not 0 <= i <= j <= n:
					return False
				return revEqual(i, j, i, j)
			r = bisect_f(isPal, 0, n)
			if DEBUG:
				for rr in range(0, n):
					i = center - rr
					j = center + rr + parity
					if i < 0 or j > n or s[i:j] != s[i:j][::-1]:
						assert r == rr - 1
						break
			i = center - r
			j = center + r + parity
			if DEBUG:
				assert revEqual(i, j, i, j)
			if revEqual(0, i, n - i, n) and j <= n - i:
				a = j
				b = i
				best = max(best, (a + b, a, b))
				if DEBUG:
					ans = s[:a] + s[n - b:]
					assert a <= n - b
					assert ans == ans[::-1]
			if revEqual(0, n - j, j, n) and n - j <= i:
				a = n - j
				b = n - i
				best = max(best, (a + b, a, b))
				if DEBUG:
					ans = s[:a] + s[n - b:]
					assert a <= n - b
					assert ans == ans[::-1]
	if DEBUG:
		best2 = 0
		for a in range(n + 1):
			for b in range(n - a + 1):
				w = s[:a] + s[n - b:]
				if w == w[::-1]:
					best2 = max(best2, a + b)
		assert best[0] == best2
	(l, a, b) = best
	return s[:a] + s[n - b:]
if False:
	for i in range(100):
		N = 500
		P = randint(0, 250)
		mid = [chr(ord('a') + randint(0, 26)) for j in range(N - 2 * P)]
		prefix = [chr(ord('a') + randint(0, 26)) for j in range(P)]
		solve(prefix + mid + prefix[::-1])
	exit()
if False:
	solve([chr(ord('a') + randint(0, 26)) for j in range(10 ** 6)])
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
answers = []
T = int(input())
for t in range(T):
	S = input().decode().rstrip()
	answers.append(solve(S))
print('\n'.join(answers))


tc = int(input())
mod = 10 ** 9 + 7
cur_p = 1
p = [cur_p]
for i in range(10 ** 5):
	cur_p *= 26
	cur_p %= mod
	p.append(cur_p)

def get_hashes(s):
	hl = [0] * len(s)
	h = 0
	for (i, ch) in enumerate(s):
		h += ch * p[len(s) - i - 1]
		hl[i] = h
	return hl

def manacher(text):
	N = len(text)
	if N == 0:
		return
	N = 2 * N + 1
	L = [0] * N
	L[0] = 0
	L[1] = 1
	C = 1
	R = 2
	i = 0
	iMirror = 0
	maxLPSLength = 0
	diff = -1
	for i in range(2, N):
		iMirror = 2 * C - i
		L[i] = 0
		diff = R - i
		if diff > 0:
			L[i] = min(L[iMirror], diff)
		try:
			while (i + L[i] < N and i - L[i] > 0) and ((i + L[i] + 1) % 2 == 0 or text[(i + L[i] + 1) // 2] == text[(i - L[i] - 1) // 2]):
				L[i] += 1
		except Exception:
			pass
		if L[i] > maxLPSLength:
			maxLPSLength = L[i]
		if i + L[i] > R:
			C = i
			R = i + L[i]
	return L
for _ in range(tc):
	s = input()
	sl = [ord(ch) for ch in s]
	sr = sl[::-1]
	h = 0
	hl = get_hashes(sl)
	hr = get_hashes(sr)
	man = manacher(sl)
	i = -1
	best_res = (0, 0, 0, 0)
	best_l = 0
	while i == -1 or (i < len(sl) // 2 and hl[i] == hr[i]):
		j = len(sl) - 1 - i
		for r in range(len(man)):
			ii = (r - man[r]) // 2
			jj = ii + man[r] - 1
			if ii == i + 1 and jj < j or (jj == j - 1 and ii > i):
				l = (i + 1) * 2 + (jj - ii) + 1
				if l > best_l:
					best_res = (i, j, ii, jj)
					best_l = l
		i += 1
	(i, j, ii, jj) = best_res
	if best_l == 0:
		print(s[0])
	else:
		print(s[:i + 1] + s[ii:jj + 1] + s[j:])


for qw in range(int(input())):
	s = input()
	n = len(s)
	if n == 1:
		print(s)
		continue
	else:
		i = 0
		j = n - 1
		count = 0
		while j > i:
			if s[i] == s[j]:
				count += 1
				i += 1
				j -= 1
			else:
				break
		suffix = s[:i]
		prefix = s[j + 1:]
		middle = s[i:j + 1]
		if len(middle) > 0:
			end = len(middle)
			while True:
				if middle[:end] == middle[:end][::-1]:
					break
				end -= 1
			middle1 = middle[::-1]
			ende = len(middle1)
			while True:
				if middle1[:ende] == middle1[:ende][::-1]:
					break
				ende -= 1
			if ende > end:
				print(suffix + middle1[:ende] + prefix)
			else:
				print(suffix + middle[:end] + prefix)
		else:
			print(suffix + prefix)


import math
import sys
from collections import defaultdict, Counter, deque, OrderedDict
import bisect
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

def list2d(a, b, c):
	return [[c] * b for i in range(a)]
from itertools import accumulate, groupby
import sys
mod = 1000000007

def prefix(s, lsa):
	index = 0
	i = 1
	while i < n:
		if s[i] == s[index]:
			lsa[i] = index + 1
			i += 1
			index += 1
		elif index == 0:
			lsa[i] = 0
			i += 1
		else:
			index = lsa[index - 1]
	return lsa[-1]
for _ in range(int(input())):
	s = input()
	ans = ''
	n = len(s)
	for i in range(n // 2):
		if s[i] == s[n - i - 1]:
			ans += s[i]
		else:
			break
	s = s[len(ans):len(s) - len(ans)]
	ss = ans
	tmp = s + '#' + s[::-1]
	n = len(tmp)
	lsa = [0] * n
	pre = prefix(tmp, lsa)
	tmp = s[::-1] + '#' + s
	suf = prefix(tmp, lsa)
	if pre >= suf:
		ans += s[:pre]
	else:
		s = s[::-1]
		ans += s[:suf]
	print(ans + ss[::-1])


import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
	S = input().strip()
	ANSP = []
	f = 0
	l = len(S) - 1
	while f < l and S[f] == S[l]:
		ANSP.append(S[f])
		f += 1
		l -= 1
	if f == l or f == l + 1:
		print(S)
		continue
	S2 = S[f:l + 1]
	SS2 = []
	for s in S2:
		SS2.append(s)
		SS2.append('$')
	SS2.pop()
	LEN = len(SS2)
	i = 0
	j = 0
	R = [0] * LEN
	while i < LEN:
		while i - j >= 0 and i + j < LEN and (SS2[i - j] == SS2[i + j]):
			j += 1
		R[i] = j
		k = 1
		while i - k >= 0 and i + k < LEN and (k + R[i - k] < j):
			R[i + k] = R[i - k]
			k += 1
		i += k
		j -= k
	MAX = 0
	for i in range(LEN):
		if i - R[i] + 1 == 0 or i + R[i] == LEN:
			if MAX < R[i]:
				MAX = R[i]
				MAXind = i
	S3 = SS2[MAXind - MAX + 1:MAXind + MAX]
	SS3 = []
	for s in S3:
		if s != '$':
			SS3.append(s)
	sys.stdout.write(''.join(ANSP) + ''.join(SS3) + ''.join(ANSP[::-1]) + '\n')


for t in range(int(input())):
	s = input()
	(i, j) = (0, len(s) - 1)
	start = ''
	end = ''
	while i < j and s[i] == s[j]:
		start += s[i]
		i += 1
		end = s[j] + end
		j -= 1
	(prefix, suffix) = (start, end)
	a = ''
	b = ''
	if j >= 0 and i < len(s):
		temp2 = start + end
		(i1, j1) = (i, j)
		maxlen = 0
		index = 0
		for cur in range(i, j + 1):
			temp = s[i1:cur + 1]
			if temp == temp[::-1]:
				if len(temp) > maxlen and len(temp) + len(temp2) <= len(s):
					start = prefix + temp
					maxlen = len(temp)
					index = cur
					a = temp
		for cur2 in range(i1, j1 + 1):
			temp = s[cur2:j1 + 1]
			if temp == temp[::-1]:
				if len(temp) > maxlen and len(temp) + len(temp2) <= len(s):
					end = temp + suffix
					maxlen = len(temp)
					b = temp
					index2 = cur2
	val = a + b
	value = val == val[::-1]
	if value:
		print(prefix + val + suffix)
		continue
	elif not value:
		if len(a) >= len(b):
			print(prefix + a + suffix)
			continue
		else:
			print(prefix + b + suffix)
			continue


def read_int():
	return int(input())

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

def read_float():
	return float(input())

def read_float_n():
	return list(map(float, input().split()))

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

def read_str_n():
	return list(map(str, input().split()))

def manacher(S):
	s = [0]
	for c in S:
		s.append(c)
		s.append(0)
	N = len(s)
	R = [0] * N
	i = 0
	j = 0
	while i < N:
		while i - j >= 0 and i + j < N and (s[i - j] == s[i + j]):
			j += 1
		R[i] = j
		k = 1
		while i - k >= 0 and i + k < N and (k + R[i - k] < j):
			R[i + k] = R[i - k]
			k += 1
		i += k
		j -= k
	return R

def slv(S):
	if S == S[::-1]:
		return S
	N = len(S)
	i = 0
	while i < N // 2:
		if S[i] != S[N - i - 1]:
			break
		i += 1
	s = S[i:N - i]
	ls = S[:i]
	rs = S[N - i:]
	mp = (0, -1, -1)
	for (j, rad) in enumerate(manacher(s)):
		l = max(0, -(-(j - rad) // 2))
		r = min((j + rad) // 2, len(s))
		if l == 0 or r == len(s):
			mp = max(mp, (r - l, l, r))
	return ls + s[mp[1]:mp[2]] + rs

def main():
	T = read_int()
	for _ in range(T):
		S = read_str()
		print(slv(S))
main()


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))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def getZarr(string, z):
	n = len(string)
	(l, r, k) = (0, 0, 0)
	for i in range(1, n):
		if i > r:
			(l, r) = (i, i)
			while r < n and string[r - l] == string[r]:
				r += 1
			z[i] = r - l
			r -= 1
		else:
			k = i - l
			if i + z[k] <= r:
				z[i] = z[k]
			else:
				l = i
				while r < n and string[r - l] == string[r]:
					r += 1
				z[i] = r - l
				r -= 1
		if z[i] == n - i:
			return z[i]
t = int(input())
for i in range(t):
	s = input()
	l = len(s)
	ind = -1
	for j in range(l // 2):
		if s[j] == s[l - j - 1]:
			continue
		else:
			ind = j
			break
	if ind == -1:
		print(s)
		continue
	if ind == 0:
		text = s[::-1]
		pattern = s
	else:
		text = s[l - ind - 1:ind - 1:-1]
		pattern = text[::-1]
	concat = pattern + '$' + text
	l1 = len(concat)
	z = [0] * l1
	ans1 = getZarr(concat, z)
	concat = text + '$' + pattern
	z = [0] * l1
	ans2 = getZarr(concat, z)
	pre = s[:ind]
	if ans1 >= ans2:
		print(pre + s[ind:ind + ans1] + pre[::-1])
	else:
		ind = l - ind - 1
		print(pre + s[ind:ind - ans2:-1] + pre[::-1])


def palindrome_checker(s):
	start = 0
	end = len(s) - 1
	while s[start] == s[end]:
		if end == 0:
			return True
		start += 1
		end -= 1
for _ in range(int(input())):
	s = input()
	n = len(s)
	left = 0
	right = n - 1
	if n == 1:
		print(s)
		continue
	while s[left] == s[right]:
		left += 1
		right -= 1
		if right == 0:
			break
	if right == 0:
		print(s)
		continue
	start = left
	while True:
		if palindrome_checker(s[start:right + 1]) == True:
			break
		else:
			start += 1
	end = right
	while True:
		if palindrome_checker(s[left:end + 1]) == True:
			break
		else:
			end -= 1
	if end - left > right - start:
		print(s[:end + 1] + s[right + 1:])
	else:
		print(s[:left] + s[start:])


from math import *

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

def js(arr, x):
	l = 0
	r = len(arr) - 1
	ans = -1
	while l <= r:
		m = (l + r) // 2
		if arr[m] <= x:
			ans = m
			l = m + 1
		else:
			r = m - 1
	return ans

def jg(arr, x, i):
	l = i
	r = len(arr) - 1
	ans = -1
	while l <= r:
		m = (l + r) // 2
		if arr[m] >= x:
			ans = m
			r = m - 1
		else:
			l = m + 1
	return ans
for __ in range(int(input())):
	s = input()
	x = 0
	y = len(s) - 1
	suf = ''
	pre = ''
	while x <= y:
		if s[x] == s[y]:
			if x != y:
				suf = s[x] + suf
				x += 1
			pre += s[y]
			y -= 1
		else:
			break
	p1 = ''
	for i in range(x + 1, y + 1):
		if s[x:i] == s[x:i][::-1]:
			p1 = s[x:i]
	p2 = ''
	i = y
	while i > x:
		if s[i:y + 1] == s[i:y + 1][::-1]:
			p2 = s[i:y + 1]
		i -= 1
	if pre == '':
		if len(p1) > len(p2):
			ans = p1
		elif len(p1) <= len(p2) and p1 != '':
			ans = p2
		else:
			ans = s[0]
		print(ans)
	else:
		ans1 = pre + p1 + suf
		ans2 = pre + p2 + suf
		if len(ans1) > len(ans2):
			ans = ans1
		else:
			ans = ans2
		print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for testcase in range(t):
	s = list(input())
	n = len(s) - 1
	c = [0] * (n + 1)
	for i in range(n):
		for j in range(min(n - i, i + 1)):
			if s[i - j] != s[i + j]:
				break
			if c[i - j] < 1 + j * 2 and i < n // 2:
				c[i - j] = 1 + j * 2
			if c[i + j] < 1 + j * 2 and i >= n // 2:
				c[i + j] = 1 + j * 2
	for i in range(n - 1):
		for j in range(min(i + 1, n - i - 1)):
			if s[i - j] != s[i + j + 1]:
				break
			if c[i - j] < (j + 1) * 2 and i < n // 2:
				c[i - j] = (j + 1) * 2
			if c[i + j + 1] < (j + 1) * 2 and i >= n // 2:
				c[i + j + 1] = (j + 1) * 2
	res = c[0]
	ma_pos = -1
	for i in range(n // 2):
		if s[i] != s[n - 1 - i]:
			break
		if i == n // 2 - 1:
			print(''.join(s[:n]))
			ma_pos = -2
		elif res < (i + 1) * 2 + max(c[i + 1], c[n - 2 - i]):
			res = (i + 1) * 2 + max(c[i + 1], c[n - 2 - i])
			ma_pos = i
	if ma_pos == -2:
		ma_pos = -2
	elif res < c[n - 1]:
		print(''.join(s[n - c[n - 1]:]))
	elif ma_pos == -1:
		print(''.join(s[:c[0]]))
	else:
		ans1 = s[:ma_pos + 1]
		if c[ma_pos + 1] < c[n - 2 - ma_pos]:
			ans2 = s[n - 1 - ma_pos - c[n - 2 - ma_pos]:n - 1 - ma_pos]
		else:
			ans2 = s[ma_pos + 1:ma_pos + 1 + c[ma_pos + 1]]
		print(''.join(ans1) + ''.join(ans2) + ''.join(ans1[::-1]))


import os
from io import BytesIO, IOBase
import sys
from collections import defaultdict, deque, Counter
from bisect import *
from math import sqrt, pi
import math
from itertools import permutations
from copy import deepcopy

def main():
	for t in range(int(input())):
		s = input().rstrip()
		if s == s[::-1]:
			print(s)
			continue
		n = len(s)
		i = 0
		j = n - 1
		while i < j and s[i] == s[j]:
			i += 1
			j -= 1
		x = i
		y = j
		m = y - x + 1
		pl = 0
		for k in range(m, 0, -1):
			i = x
			j = x + k - 1
			while i < j and s[i] == s[j]:
				i += 1
				j -= 1
			if j + 1 == i and k % 2 == 0 or (i == j and k % 2):
				pl = k
				break
		sl = 0
		for k in range(x, y + 1):
			i = k
			j = y
			while i < j and s[i] == s[j]:
				i += 1
				j -= 1
			if j + 1 == i and (y - k + 1) % 2 == 0 or (i == j and (y - k + 1) % 2):
				sl = y - k + 1
				break
		if pl > sl:
			print(s[:x + pl] + s[y + 1:])
		else:
			print(s[:x] + s[y - sl + 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()


def ispoly(ss):
	return ss == ss[::-1]

def lrpoly(T):
	n = len(T)
	for i in range(n - 1, 0, -1):
		tail = T[i:]
		if ispoly(tail[::-1] + T):
			return T[:i]
t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	left = 0
	right = n - 1
	while left < right and s[left] == s[right]:
		left += 1
		right -= 1
	if left == right:
		print(s)
	elif left > right:
		print(s)
	else:
		midd = lrpoly(s[left:right + 1])
		midd2 = lrpoly(s[left:right + 1][::-1])
		if len(midd) >= len(midd2):
			res = midd
		else:
			res = midd2
		print(s[:left] + res + s[:left][::-1])


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	s = sys.stdin.readline()[:-1]
	n = len(s)
	ans = ''
	(i, j) = (0, n - 1)
	while i <= j:
		if s[i] == s[j]:
			i += 1
			j -= 1
		else:
			break
	(lim1, lim2) = (-1, -1)
	if i > j:
		print(s)
		continue
	(i1, j1) = (i, j)
	news = ''
	for i in range(i, j + 1):
		news = news + s[i]
	n = len(news)
	dp = [[False for _ in range(n)] for x in range(n)]
	for i in range(n):
		dp[i][i] = True
	for i in range(n - 1, -1, -1):
		for j in range(i + 1, n):
			if news[i] == news[j] and i != j:
				if j - i == 1:
					dp[i][j] = True
					continue
				if dp[i + 1][j - 1]:
					dp[i][j] = True
	for i in range(n - 1, -1, -1):
		if dp[0][i]:
			lim1 = i
			break
	for i in range(n):
		if dp[i][n - 1]:
			lim2 = i
			break
	if lim1 + 1 > n - lim2:
		ans = s[:i1] + news[:lim1 + 1] + s[j1 + 1:]
	else:
		ans = s[:i1] + news[lim2:] + s[j1 + 1:]
	print(ans)


from sys import stdin, stdout

def left_palindrome(string, start, end):
	(i, j) = (start, end)
	while i <= j:
		start_j = j
		while i < j and string[i] == string[j]:
			i += 1
			j -= 1
		if i >= j:
			return start_j - start + 1
		i = start
		j = start_j - 1
	return 1

def right_palindrome(string, start, end):
	(i, j) = (start, end)
	while i <= j:
		start_i = i
		while i < j and string[i] == string[j]:
			i += 1
			j -= 1
		if i >= j:
			return end - start_i + 1
		i = start_i + 1
		j = end
	return 1

def middle_palindrome(string, start, end):
	if start > end:
		return
	l_left = left_palindrome(string, start, end)
	l_right = right_palindrome(string, start, end)
	if l_left >= l_right:
		for k in range(start, start + l_left):
			stdout.write(string[k])
	else:
		for k in range(end - l_right + 1, end + 1):
			stdout.write(string[k])

def search_palindrome(string):
	(i, j) = (0, len(string) - 1)
	suffix = []
	while i < j and string[i] == string[j]:
		stdout.write(string[i])
		suffix.append(string[j])
		i += 1
		j -= 1
	middle_palindrome(string, i, j)
	for w in reversed(suffix):
		stdout.write(w)
t = int(stdin.readline())
for _ in range(t):
	word = stdin.readline().strip()
	search_palindrome(word)
	stdout.write('\n')


import math
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	s = list(input())
	if s[-1] == '\n':
		s.pop()
	i = 0
	j = len(s) - 1
	fin = []
	while i < j and i < len(s) and (j >= 0):
		if s[i] == s[j]:
			fin.append(s[i])
			i += 1
			j -= 1
		else:
			break
	now = len(fin)
	s1 = s[i:j + 1]
	i1 = 0
	j1 = len(s1) - 1
	cur1 = 0
	while i1 <= j1 and j1 >= 0:
		if s1[i1] == s1[j1]:
			p1 = i1
			p2 = j1
			flag = True
			while p1 < len(s1) and p2 >= 0 and (p1 < p2):
				if s1[p1] == s1[p2]:
					p1 += 1
					p2 -= 1
				else:
					flag = False
					break
			if flag:
				cur1 = j1 - i1 + 1
				break
			else:
				j1 -= 1
		else:
			j1 -= 1
	s2 = s[i:j + 1]
	s2 = s2[::-1]
	i2 = 0
	j2 = len(s2) - 1
	cur2 = 0
	while i2 <= j2 and j2 >= 0:
		if s2[i2] == s2[j2]:
			p1 = i2
			p2 = j2
			flag = True
			while p1 < len(s2) and p2 >= 0 and (p1 < p2):
				if s2[p1] == s2[p2]:
					p1 += 1
					p2 -= 1
				else:
					flag = False
					break
			if flag:
				cur2 = j2 - i2 + 1
				break
			else:
				j2 -= 1
		else:
			j2 -= 1
	if cur1 >= cur2:
		for x in range(cur1):
			fin.append(s1[x])
	else:
		for x in range(cur2):
			fin.append(s2[x])
	for x in range(now):
		fin.append(fin[now - x - 1])
	print(''.join(fin))


def prowerka(stroka):
	if len(stroka) == 0:
		return True
	i = 0
	k = len(stroka) // 2
	while stroka[i] == stroka[-1 - i]:
		if i >= k:
			return True
		i += 1
	return False

def pref(stroka):
	i = 0
	k = len(stroka) // 2
	while i < k and stroka[i] == stroka[-1 - i]:
		i += 1
	newstroka = stroka[i:len(stroka) - i]
	ans1 = answ(newstroka)
	ans2 = answ(newstroka[::-1])[::-1]
	if len(ans1) > len(ans2):
		print(stroka[:i] + ans1 + stroka[len(stroka) - i:])
	else:
		print(stroka[:i] + ans2 + stroka[len(stroka) - i:])

def answ(stroka):
	gran = len(stroka) - 1
	while not prowerka(stroka[0:gran]):
		gran -= 1
	if gran != 0:
		return stroka[0:gran]
	else:
		return stroka[0]
for i in range(int(input())):
	st = input()
	pref(st)


class manacher:

	def __init__(self, l):
		self.n = len(l)
		self.m = 2 * self.n + 1
		self.d = ['&'] * self.m
		self.r = [0] * self.m
		for i in range(self.n):
			self.d[2 * i + 1] = l[i]
		self.make_r()

	def make_r(self):
		(i, j) = (0, 0)
		while i < self.m:
			while j <= i < self.m - j and self.d[i - j] == self.d[i + j]:
				j += 1
			self.r[i] = j
			k = 1
			while k <= i < self.m - k and k + self.r[i - k] < j:
				self.r[i + k] = self.r[i - k]
				k += 1
			i += k
			j -= k

	def judge(self, start, end):
		center = start + end
		return 2 * end - 1 < center + self.r[center]
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	s = input().strip()
	n = len(s)
	ll = 0
	for i in range(n // 2):
		if s[i] != s[n - i - 1]:
			break
		ll += 1
	x = s
	if ll > 0:
		x = s[ll:-ll]
	sss = manacher(x)
	jj = 0
	for i in range(len(x), 0, -1):
		if sss.judge(0, i):
			jj = i
			break
	lll = 0
	for i in range(len(x), 0, -1):
		if sss.judge(len(x) - i, len(x)):
			lll = i
			break
	if lll < jj:
		print(s[:ll] + x[:jj] + s[:ll][::-1])
	else:
		print(s[:ll] + x[::-1][:lll] + s[:ll][::-1])


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

def _():
	return sys.stdin.readline()

def __():
	return sys.stdin.readline().split()
maxn = 100005

def manacher(s, n):
	f = [0 for i in range(n)]
	a = ['.' for i in range(n)]
	s += '.'
	for i in range(0, n, 2):
		a[i] = s[int(i / 2)]
	(l, r, center, res, j) = (0, -1, 0, 0, 0)
	for i in range(n):
		j = 0 if i > r else min(f[l + r - i], r - i) + 1
		while i - j >= 0 and i + j < n and (a[i - j] == a[i + j]):
			j += 1
		j -= 1
		f[i] = j
		if i + j > r:
			r = i + j
			l = i - j
		len = (f[i] + i % 2) / 2 * 2 + 1 - i % 2
		if len > res:
			res = len
			center = i
	(ans, pos, rr) = (0, 0, '')
	for i in range(n):
		if f[i] == i:
			ans = max(ans, i)
			pos = i
	for i in range(pos * 2 + 1):
		if a[i] != '.':
			rr += a[i]
	return rr
q = int(_())
for id in range(q):
	inp = __()
	s = inp[0]
	(n, res, p, q, ans, pos1, pos2) = (len(s), '', '', '', '', '', '')
	(l, r) = (0, n - 1)
	while l < r:
		if s[l] == s[r]:
			res += s[l]
			l += 1
			r -= 1
		else:
			break
	for i in range(l, r + 1):
		p += s[i]
	for i in range(r, l - 1, -1):
		q += s[i]
	if p != '':
		pos1 = manacher(p, len(p) * 2 + 1)
		pos2 = manacher(q, len(q) * 2 + 1)
	if len(pos1) > len(pos2):
		ans = pos1
	else:
		ans = pos2
	print(res + ans + res[::-1])


for _ in range(int(input())):
	s = input()
	n = len(s) - 1
	flag = 0
	for i in range(len(s) // 2):
		if s[i] == s[n]:
			n = n - 1
		else:
			flag = 1
			break
	if flag == 1:
		prefix = s[0:i]
		suffix = s[n + 1:len(s)]
		x = ''
		y = ''
		for j in range(n, i - 1, -1):
			temp = s[i:j + 1]
			if temp == temp[::-1]:
				x = temp
				break
			else:
				continue
		for k in range(len(prefix), n + 1):
			temp1 = s[k:n + 1]
			if temp1 == temp1[::-1]:
				y = temp1
				break
			else:
				continue
		if len(x) >= len(y):
			print(prefix + x + suffix)
		else:
			print(prefix + y + suffix)
	else:
		print(s)


import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	S = input().rstrip()
	N = len(S)
	L = 0
	R = N - 1
	while L < R and S[L] == S[R]:
		L += 1
		R -= 1
	if L == R:
		print(S)
		continue
	SUB = S[L:R + 1]
	M = len(SUB)
	fail1 = [0] * M
	j = 0
	for i in range(1, M):
		while j > 0 and SUB[i] != SUB[j]:
			j = fail1[j - 1]
		if SUB[i] == SUB[j]:
			j += 1
			fail1[i] = j
	SUB2 = SUB[::-1]
	fail2 = [0] * M
	j = 0
	for i in range(1, M):
		while j > 0 and SUB2[i] != SUB2[j]:
			j = fail2[j - 1]
		if SUB2[i] == SUB2[j]:
			j += 1
			fail2[i] = j
	j = 0
	for i in range(M):
		while j > 0 and SUB[i] != SUB2[j]:
			j = fail2[j - 1]
		if SUB[i] == SUB2[j]:
			j += 1
	ans1 = S[:L] + S[R - j + 1:R + 1] + S[R + 1:]
	j = 0
	for i in range(M):
		while j > 0 and SUB2[i] != SUB[j]:
			j = fail1[j - 1]
		if SUB2[i] == SUB[j]:
			j += 1
	ans2 = S[:L] + S[L:L + j] + S[R + 1:]
	if len(ans1) < len(ans2):
		print(ans2)
	else:
		print(ans1)


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def longestPalindromicPrefix(Str):
	temp = Str + '?'
	Str = Str[::-1]
	temp = temp + Str
	n = len(temp)
	lps = [0] * n
	for i in range(1, n):
		Len = lps[i - 1]
		while Len > 0 and temp[Len] != temp[i]:
			Len = lps[Len - 1]
		if temp[i] == temp[Len]:
			Len += 1
		lps[i] = Len
	return lps[n - 1]
allAns = []
t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	maxDepth = 0
	for i in range(n // 2):
		if s[i] == s[n - 1 - i]:
			maxDepth += 1
		else:
			break
	start = s[:maxDepth]
	end = start[::-1]
	if maxDepth > 0:
		s2 = s[maxDepth:-maxDepth]
	else:
		s2 = s
	x1 = longestPalindromicPrefix(s2)
	ans1 = start + s2[:x1] + end
	s3 = s2[::-1]
	x2 = longestPalindromicPrefix(s3)
	ans2 = start + s3[:x2] + end
	ans2 = ans2[::-1]
	if len(ans1) > len(ans2):
		allAns.append(ans1)
	else:
		allAns.append(ans2)
multiLineArrayPrint(allAns)


def ok(s):
	(l, r) = (0, len(s) - 1)
	while l < r:
		if s[l] == s[r]:
			l += 1
			r -= 1
		else:
			return False
	return True

def find(s):
	res = s[0]
	for i in range(1, len(s)):
		if ok(s[0:i]):
			res = s[0:i]
	s = s[::-1]
	res2 = s[0]
	for i in range(1, len(s)):
		if ok(s[0:i]):
			res2 = s[0:i]
	if len(res2) > len(res):
		return res2
	else:
		return res

def main():
	for _ in range(int(input())):
		s = input()
		eq = ''
		for i in range(len(s)):
			if s[i] == s[len(s) - i - 1]:
				eq += s[i]
			else:
				eq = eq + find(s[i:len(s) - i]) + eq[::-1]
				break
		print(eq)
main()


def gen_lps(pattern, m):
	pre_match = 0
	i = 1
	lps = [0] * m
	while i < m:
		if pattern[i] == pattern[pre_match]:
			pre_match += 1
			lps[i] = pre_match
			i += 1
		elif pre_match == 0:
			lps[i] = 0
			i += 1
		else:
			pre_match = lps[pre_match - 1]
	return lps[-1]

def largest_pre_pal(rem):
	new = rem + '#' + rem[::-1]
	m = len(new)
	lll = gen_lps(new, m)
	return lll

def solve():
	s = input()
	n = len(s)
	if n == 1:
		print(s)
		return
	(i, j, l) = (0, n - 1, 0)
	while i < j and s[i] == s[j]:
		i += 1
		j -= 1
		l += 1
	if i == j or i - j == 1:
		print(s)
		return
	pre_len = largest_pre_pal(s[l:n - l - 1])
	suff_len = largest_pre_pal(s[n - l - 1:l:-1])
	if pre_len >= suff_len:
		final = s[0:l] + s[l:l + pre_len] + s[n - l:]
	else:
		final = s[0:l] + s[n - l - suff_len:n - l] + s[n - l:]
	print(final)
t = int(input())
for i in range(t):
	solve()


import sys

def longest_pal(s):
	kmp = s + '#' + s[::-1]
	lps = [0] * len(kmp)
	n1 = len(kmp)
	for i in range(1, n1):
		prev_idx = lps[i - 1]
		while prev_idx > 0 and kmp[i] != kmp[prev_idx]:
			prev_idx = lps[prev_idx - 1]
		if kmp[i] == kmp[prev_idx]:
			lps[i] = prev_idx + 1
		else:
			lps[i] = prev_idx
	return lps[n1 - 1]
t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	(i, j, maxi) = (0, n - 1, 0)
	while i < j:
		if s[i] == s[j]:
			i += 1
			j -= 1
			maxi += 1
		else:
			break
	if maxi > 0:
		print(s[:i], end='')
	if 2 * maxi != n:
		maxi3 = longest_pal(s[i:j + 1])
		s1 = s[i:j + 1]
		maxi4 = longest_pal(s1[::-1])
		if maxi3 > maxi4:
			print(s[i:i + maxi3], end='')
		else:
			print(s[j + 1 - maxi4:j + 1], end='')
	if maxi > 0:
		print(s[j + 1:])
	else:
		print()


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
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 value():
	return tuple(map(int, input().split()))

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

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
for _ in range(Int()):
	s = input()
	n = len(s)
	i = 0
	j = len(s) - 1
	while i <= j:
		if s[i] == s[j]:
			i += 1
			j -= 1
		else:
			break
	if i > j:
		print(s)
		continue
	ans = s[:i]
	(ti, tj) = (i, j)
	while i <= j:
		if s[i] == s[j]:
			start = i
			end = j
			while i < j and s[i] == s[j]:
				i += 1
				j -= 1
			if i < j:
				i = start
				j = end - 1
			else:
				ans1 = ans + s[start:end + 1] + ans[::-1]
				break
		else:
			j -= 1
	j = n - 1 - ti
	i = n - 1 - tj
	s = s[::-1]
	while i <= j:
		if s[i] == s[j]:
			start = i
			end = j
			while i < j and s[i] == s[j]:
				i += 1
				j -= 1
			if i < j:
				i = start
				j = end - 1
			else:
				ans2 = ans + s[start:end + 1] + ans[::-1]
				break
		else:
			j -= 1
	if len(ans1) > len(ans2):
		print(ans1)
	else:
		print(ans2)


for _ in range(int(input())):
	s = input()
	(pre, suf) = ('', '')
	flag = 1
	(i, j) = (0, len(s) - 1)
	while flag and j > i:
		if s[i] == s[j]:
			pre += s[i]
			suf = s[j] + suf
			i += 1
			j -= 1
		else:
			flag = 0
	(l, l1) = (i, j)
	(pp, ss) = ('', '')
	temp = s[i:j + 1]
	temp1 = temp[::-1]
	n = len(temp)
	for i in range(0, n):
		x = temp[0:i + 1]
		if x == x[::-1]:
			pp = x
	for i in range(0, n):
		x = temp1[0:i + 1]
		if x == x[::-1]:
			ss = x
	if len(ss) > len(pp):
		temp = ss
	else:
		temp = pp
	print(pre + temp + suf)


import sys

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

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

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

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7

def Manacher(S):
	N = len(S)
	R = [0] * N
	i = j = 0
	while i < N:
		while i - j >= 0 and i + j < N and (S[i - j] == S[i + j]):
			j += 1
		R[i] = j
		k = 1
		while i - k >= 0 and i + k < N and (k + R[i - k] < j):
			R[i + k] = R[i - k]
			k += 1
		i += k
		j -= k
	return R

def Manacher_even(_S):
	_N = len(_S)
	S = []
	for i in range(_N - 1):
		S.append(_S[i])
		S.append('$')
	S.append(_S[-1])
	N = len(S)
	R = [0] * N
	i = j = 0
	while i < N:
		while i - j >= 0 and i + j < N and (S[i - j] == S[i + j]):
			j += 1
		R[i] = j
		k = 1
		while i - k >= 0 and i + k < N and (k + R[i - k] < j):
			R[i + k] = R[i - k]
			k += 1
		i += k
		j -= k
	res = [0] * (_N - 1)
	j = 0
	for i in range(1, N, 2):
		res[j] = R[i] // 2
		j += 1
	return res
for _ in range(INT()):
	S = input()
	N = len(S)
	i = 0
	j = N - 1
	T1 = []
	while i < j:
		if S[i] == S[j]:
			T1.append(S[i])
			i += 1
			j -= 1
		else:
			break
	else:
		print(S)
		continue
	T3 = T1[::-1]
	R = Manacher(S[i:j + 1])
	mx = a = b = 0
	for (k, r) in enumerate(R):
		k += i
		ln = r * 2 - 1
		if k - r + 1 == i:
			if ln > mx:
				mx = ln
				(a, b) = (i, i + ln)
		if k + r - 1 == j:
			if ln > mx:
				mx = ln
				(a, b) = (j + 1 - ln, j + 1)
	R = Manacher_even(S[i:j + 1])
	for (k, r) in enumerate(R):
		k += i + 1
		ln = r * 2
		if k - r == i:
			if ln > mx:
				mx = ln
				(a, b) = (i, i + ln)
		if k + r - 1 == j:
			if ln > mx:
				mx = ln
				(a, b) = (j + 1 - ln, j + 1)
	T2 = list(S[a:b])
	ans = T1 + T2 + T3
	print(''.join(ans))


from sys import stdin

def inp():
	return stdin.buffer.readline().rstrip().decode('utf8')

def itg():
	return int(stdin.buffer.readline())

def mpint():
	return map(int, stdin.buffer.readline().split())

def longest_prefix(a):
	a = a + '#' + a[::-1]
	pref = [0] * len(a)
	c = 0
	for i in range(1, len(a)):
		while c > 0 and a[c] != a[i]:
			c = pref[c - 1]
		if a[c] == a[i]:
			c += 1
		pref[i] = c
	return a[:c]
for case in range(itg()):
	s = inp()
	n = len(s)
	k = 0
	for i in range(n >> 1):
		if s[i] == s[-i - 1]:
			k = i + 1
		else:
			break
	prefix = s[:k]
	suffix = s[:k][::-1]
	if k:
		s = s[k:-k]
	mid = max(longest_prefix(s), longest_prefix(s[::-1]), key=len)
	print(prefix + mid + suffix)


import sys
input = lambda : sys.stdin.readline().strip()
for i in range(int(input())):
	s = list(input())
	n = len(s)
	ans = -1
	nat = n
	for i in range(n // 2):
		if s[i] != s[-i - 1]:
			ans = i
			break
	if ans == -1:
		print(''.join(map(str, s)))
		continue
	an = s[:ans]
	s = s[ans:n - ans]
	n -= 2 * ans
	d2 = [0] * n
	l = 0
	r = -1
	for i in range(n):
		if i > r:
			k = 0
		else:
			k = min(d2[l + r - i + 1], r - i + 1)
		while i + k < n and i - k - 1 >= 0 and (s[i + k] == s[i - k - 1]):
			k += 1
		d2[i] = k
		if i + k - 1 > r:
			l = i - k
			r = i + k - 1
	d1 = [0] * n
	l = 0
	r = -1
	for i in range(n):
		if i > r:
			k = 1
		else:
			k = min(d1[l + r - i], r - i + 1)
		while i + k < n and i - k >= 0 and (s[i + k] == s[i - k]):
			k += 1
		d1[i] = k
		if i + k - 1 > r:
			l = i - k + 1
			r = i + k - 1
	an1 = 0
	an2 = 0
	an3 = 0
	an4 = 0
	for i in range(n):
		if d1[i] == i + 1:
			an1 = i + 1
		if d2[i] == i:
			an2 = i
		if d1[i] == n - i:
			an3 = max(d1[i], an3)
		if d2[i] == n - i:
			an4 = max(d2[i], an4)
	a = max(an1 * 2 - 1, an2 * 2, an3 * 2 - 1, an4 * 2)
	if a == an1 * 2 - 1:
		ans = an + s[:an1 * 2 - 1] + an[::-1]
	elif a == 2 * an2:
		ans = an + s[:an2 * 2] + an[::-1]
	elif a == an3 * 2 - 1:
		ans = an + s[n - an3 * 2 + 1:] + an[::-1]
	else:
		ans = an + s[n - an4 * 2:] + an[::-1]
	print(''.join(map(str, ans)))


import sys
input = sys.stdin.readline
print = sys.stdout.write

def isPalindrome(pat):
	pat = pat + '#' + pat[::-1]
	lenpat = len(pat)
	lps = [0] * lenpat
	l = 0
	i = 1
	while i < lenpat:
		if pat[i] == pat[l]:
			l += 1
			lps[i] = l
			i += 1
		elif l != 0:
			l = lps[l - 1]
		else:
			lps[i] = 0
			i += 1
	return lps[-1]
for _ in range(int(input())):
	s = input()[:-1]
	l = len(s)
	a = ''
	b = ''
	(ai, bi) = (0, 0)
	idx = None
	for i in range(l // 2):
		if s[i] == s[l - i - 1]:
			ai += 1
			bi += 1
		else:
			idx = i
			break
	else:
		ans = s
	if idx != None:
		(a, b) = (s[:ai], s[l - bi:])
		ai = None
		bi = None
		temp = s[idx:l - idx]
		ai = isPalindrome(temp)
		bi = isPalindrome(temp[::-1])
		if ai > bi:
			ans = a + temp[:ai] + b
		else:
			ans = a + temp[l - 2 * idx - bi:] + b
	print(ans + '\n')


for _ in range(int(input())):
	s = input()
	n = len(s)
	br = 0
	for i in range(n):
		if s[i] != s[n - i - 1]:
			br = i
			break
	else:
		print(s)
		continue
	string1 = ''
	string2 = ''
	res1 = ''
	res2 = ''
	st = i
	end = n - i - 1
	for i in range(br, n - br):
		string1 += s[i]
		if string1 == string1[::-1]:
			res1 = string1
	for i in range(n - br - 1, br - 1, -1):
		string2 += s[i]
		if string2 == string2[::-1]:
			res2 = string2
	if len(res1) > len(res2):
		print(s[:br] + res1 + s[n - br:])
	else:
		print(s[:br] + res2 + s[n - br:])


def palincheck(s):
	hel = ''
	for c in s:
		hel = c + hel
	return s == hel
t = int(input())
for i in range(t):
	s = input()
	n = len(s)
	a1 = 0
	a2 = len(s) - 1
	while a1 < a2 and s[a1] == s[a2]:
		a1 += 1
		a2 -= 1
	if a1 >= a2:
		print(s)
		continue
	prefix = []
	suffix = ['' for k in range(a2 - a1 + 1)]
	prefix.append(s[a1])
	suffix[-1] = s[a2]
	for k in range(a1 + 1, a2 + 1):
		prefix.append(prefix[-1] + s[k])
	for k in reversed(range(a1, a2)):
		suffix[k - a1] = s[k] + suffix[k - a1 + 1]
	ans = 2 * a1
	res = ''
	for pref in prefix:
		if palincheck(pref):
			ans = max(2 * a1 + len(pref), ans)
			if ans == 2 * a1 + len(pref):
				res = s[:a1] + pref + s[a2 + 1:]
	for suff in suffix:
		if palincheck(suff):
			ans = max(ans, 2 * a1 + len(suff))
			if ans == 2 * a1 + len(suff):
				res = s[:a1] + suff + s[a2 + 1:]
	print(res)


n = int(input())
for _ in range(n):
	s = input()
	l = 0
	r = len(s) - 1
	best = ''
	ln = 0
	while s[l] == s[r]:
		ln += 1
		l += 1
		r -= 1
		if r <= l:
			break
	if ln * 2 >= len(s):
		print(s)
		continue
	temp_sol = (s[:ln], s[:ln][::-1])
	s = s[ln:len(s) - ln]
	for i in range(len(s) + 1):
		ss = s[i:]
		if ss == ss[::-1] and len(ss) > len(best):
			best = ss
		ss = s[:i]
		if ss == ss[::-1] and len(ss) > len(best):
			best = ss
	print(temp_sol[0] + best + temp_sol[1])


t = int(input())
while t:
	t -= 1
	s = input()
	i = 0
	(st, e) = ('', '')
	n = len(s)
	if n == 1:
		print(s)
		continue
	while i != n // 2:
		if s[i] == s[n - i - 1]:
			st += s[i]
			e = s[n - i - 1] + e
			i += 1
			continue
		break
	temp = s[i:n - i]
	m = ''
	for i1 in range(n - 2 * i):
		z = temp[:i1 + 1]
		if z == z[::-1]:
			if len(z) > len(m):
				m = z
	temp = temp[::-1]
	for i1 in range(n - 2 * i):
		z = temp[:i1 + 1]
		if z == z[::-1]:
			if len(z) > len(m):
				m = z
	r = st + m + e
	print(r)


def z_function(s, z):
	n = len(s)
	(l, r, k) = (0, 0, 0)
	for i in range(1, n):
		if i > r:
			(l, r) = (i, i)
			while r < n and s[r - l] == s[r]:
				r += 1
			z[i] = r - l
			r -= 1
		else:
			k = i - l
			if z[k] < r - i + 1:
				z[i] = z[k]
			else:
				l = i
				while r < n and s[r - l] == s[r]:
					r += 1
				z[i] = r - l
				r -= 1
import sys
input = sys.stdin.readline
t = int(input())

def solve(ss, res):
	mx = 0
	z = [0] * len(ss)
	z_function(ss, z)
	ls = len(z)
	for i in range(len(z) - 1, -1, -1):
		if z[i] == ls - i:
			mx = max(mx, z[i])
	for i in range(mx):
		res.append(ss[i])
	nn = len(res)
	for i in range(nn - mx - 1, -1, -1):
		res.append(res[i])
	return ''.join(res)
while t:
	res1 = []
	res2 = []
	s = input().split()[0]
	n = len(s)
	if n == 1:
		print(s)
	else:
		i = 0
		j = n - 1
		while i < j:
			if s[i] == s[j]:
				res1.append(s[i])
				res2.append(s[i])
			else:
				break
			i += 1
			j -= 1
		ss = s[i:j]
		ss = ss + '*' + ss[::-1]
		s1 = solve(ss, res1)
		ss = s[i:j + 1]
		ss = ss[::-1] + '*' + ss
		s2 = solve(ss, res2)
		if len(s1) >= len(s2):
			print(s1)
		else:
			print(s2)
	t -= 1


t = int(input())

class manacher:

	def __init__(self, l):
		self.n = len(l)
		self.m = 2 * self.n + 1
		self.d = ['&'] * self.m
		self.r = [0] * self.m
		for i in range(self.n):
			self.d[2 * i + 1] = l[i]
		self.make_r()

	def make_r(self):
		(i, j) = (0, 0)
		while i < self.m:
			while j <= i < self.m - j and self.d[i - j] == self.d[i + j]:
				j += 1
			self.r[i] = j
			k = 1
			while k <= i < self.m - k and k + self.r[i - k] < j:
				self.r[i + k] = self.r[i - k]
				k += 1
			i += k
			j -= k

	def judge(self, start, end):
		center = start + end
		return 2 * end - 1 < center + self.r[center]
for _ in range(t):
	s = input()
	n = len(s)
	ans = []
	k = 0
	for i in range(n // 2):
		if s[i] == s[-1 - i]:
			continue
		else:
			k = i
			break
	ans.append(s[:k])
	kk = -k
	if k == 0:
		kk = n
	ss = s[k:kk]
	r = manacher(ss)
	tmp = []
	for i in range(1, len(ss) + 1):
		if r.judge(0, i):
			tmp.append(ss[:i])
	for i in range(len(ss)):
		if r.judge(i, len(ss)):
			tmp.append(ss[i:])
	if tmp:
		tmp.sort(key=lambda x: len(x))
		ans.append(tmp[-1])
	if k > 0:
		ans.append(s[-k:])
	print(''.join(ans))


from sys import stdin
from bisect import bisect_left as bl
from bisect import bisect_right as br

def input():
	return stdin.readline()[:-1]

def intput():
	return int(input())

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

def intsput():
	return map(int, sinput())

class RangedList:

	def __init__(self, start, stop, val=0):
		self.shift = 0 - start
		self.start = start
		self.stop = stop
		self.list = [val] * (stop - start)

	def __setitem__(self, key, value):
		self.list[key + self.shift] = value

	def __getitem__(self, key):
		return self.list[key + self.shift]

	def __repr__(self):
		return str(self.list)

	def __iter__(self):
		return iter(self.list)

def dprint(*args, **kwargs):
	if debugging:
		print(*args, **kwargs)

def palindrome(x):
	return x == x[::-1]
debugging = 1
t = intput()
for _ in range(t):
	front = []
	back = []
	s = input()
	fhead = 0
	bhead = len(s) - 1
	while s[fhead] == s[bhead] and fhead < bhead:
		front.append(s[fhead])
		back.append(s[bhead])
		fhead += 1
		bhead -= 1
	bst = ''
	bstlength = 0
	is_front = True
	for i in range(fhead, bhead + 1):
		frnt = s[fhead:i]
		bck = s[i:bhead + 1]
		if palindrome(frnt):
			if len(frnt) > bstlength:
				bstlength = len(frnt)
				bst = frnt
				is_front = True
		if palindrome(bck):
			if len(bck) > bstlength:
				bstlength = len(bck)
				bst = bck
				is_front = False
	if is_front:
		front += bst
	else:
		back += bst
	print(''.join(front) + ''.join(back[::-1]))


t = int(input())
for _ in range(t):
	s = input()
	i = 0
	j = len(s) - 1
	l = ''
	r = ''
	while i < j:
		if s[i] == s[j]:
			l += s[i]
			r += s[j]
		else:
			break
		i += 1
		j -= 1
	rem = s[i:j + 1]
	c = ''
	for i in range(len(rem)):
		if rem[0:i + 1] == rem[:i + 1][::-1] and len(rem[:i + 1]) > len(c):
			c = rem[:i + 1]
	rem = rem[::-1]
	for i in range(len(rem)):
		if rem[0:i + 1] == rem[:i + 1][::-1] and len(rem[:i + 1]) > len(c):
			c = rem[:i + 1]
	print(l + c + r[::-1])


for _ in range(int(input())):
	s = input()
	n = len(s)
	k = 0
	for i in range(n // 2):
		if s[i] != s[n - 1 - i]:
			break
		else:
			k += 1
	p = s[:k] + s[n - k:]
	if k == n // 2:
		if n % 2 != 0:
			print(s[:k + 1] + s[n - k:])
		else:
			print(p)
	else:
		q = s
		s = s[k:n - k]
		l = n - 2 * k
		maxx = 0
		si = ''
		for i in range(l):
			if s[:i + 1] == s[:i + 1][::-1]:
				maxx = i + 1
				si = s[:i + 1]
		s = s[::-1]
		for i in range(l):
			if s[:i + 1] == s[:i + 1][::-1]:
				if maxx < i + 1:
					maxx = i + 1
					si = s[:i + 1]
		print(q[:k] + si + q[n - k:])


for _ in range(int(input())):
	s = input()
	n = len(s)
	ans = ''
	for i in range(n):
		if s[:i + 1] == s[:i + 1][::-1]:
			ans = s[:i + 1]
	length = len(ans)
	for i in range(n - 1, -1, -1):
		if s[i:] == s[i:][::-1]:
			if n - i > length:
				ans = s[i:]
				length = n - i
	i = 0
	j = n - 1
	while i < j and s[i] == s[j]:
		i += 1
		j -= 1
	first_part = s[:i]
	second_part = s[j + 1:]
	second_length = i + (n - j - 1)
	if second_length > length:
		ans = first_part + second_part
	for k in range(i, j + 1):
		if s[i:k + 1] == s[i:k + 1][::-1]:
			if second_length < k + 1 + (n - j - 1):
				second_length = k + 1 + (n - j - 1)
				ans = first_part + s[i:k + 1] + second_part
	for k in range(j, i - 1, -1):
		if s[k:j + 1] == s[k:j + 1][::-1]:
			if second_length < i + (n - k):
				second_length = i + (n - k)
				ans = first_part + s[k:j + 1] + second_part
	print(ans)


def kmp(s):
	l = len(s)
	ans = [-1 for i in range(l)]
	j = 0
	i = 1
	while i < l:
		if s[i] == s[j]:
			ans[i] = j
			j += 1
			i += 1
		elif j > 0:
			j = ans[j - 1] + 1
		else:
			i += 1
	return s[:ans[l - 1] + 1]
for _ in range(int(input())):
	x = input()
	l = len(x)
	count = 0
	for i in range(l // 2):
		if x[i] == x[l - i - 1]:
			count += 1
		else:
			break
	start = x[:count]
	end = x[l - count:]
	temp = x[count:l - count]
	if len(temp) >= 1:
		one = kmp(temp + '#' + temp[::-1])
		two = kmp(temp[::-1] + '#' + temp)
		if len(one) >= len(two):
			print(start + one + end)
		else:
			print(start + two + end)
	else:
		print(start + end)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def gift():
	for _ in range(t):
		string = input()
		n = len(string)
		if n == 1:
			yield string[0]
		else:
			ans = ''
			(s, e) = (0, n - 1)
			while s < e:
				if string[s] == string[e]:
					ans += string[s]
					s += 1
					e -= 1
				else:
					break
			(sp, se) = (s, e)
			prev = ''
			while sp <= se:
				temprev = string[sp:se + 1]
				if temprev == temprev[::-1]:
					prev = temprev
					break
				else:
					se -= 1
			(ep, ee) = (s, e)
			end = ''
			while ep <= ee:
				temprev = string[ep:ee + 1]
				if temprev == temprev[::-1]:
					end = temprev
					break
				else:
					ep += 1
			(len1, len2) = (len(prev), len(end))
			if len1 >= len2:
				yield (ans + prev + ans[::-1])
			else:
				yield (ans + end + ans[::-1])
t = int(input())
ans = gift()
print(*ans, sep='\n')


for i in range(int(input())):
	string = input()
	i = 0
	j = len(string) - 1
	output = ''
	prefix = ''
	suffix = ''
	while i < j:
		if string[i] != string[j]:
			break
		prefix += string[i]
		suffix = string[j] + suffix
		i += 1
		j -= 1
	if i > j:
		print(prefix + suffix)
		continue

	def longest(string):
		max1 = ''
		for j in range(len(string)):
			if string[:j + 1] == ''.join(reversed(string[:j + 1])):
				max1 = string[:j + 1]
		return max1
	max1 = longest(string[i:j + 1])
	max2 = longest(''.join(reversed(string[i:j + 1])))
	if len(max1) > len(max2):
		print(prefix + max1 + suffix)
	else:
		print(prefix + max2 + suffix)


def longpal(s):
	l = len(s)
	ml = 1
	for i in range(1, l):
		s1 = s[:i]
		f = 0
		for j in range(i // 2):
			if s1[j] != s1[i - j - 1]:
				f = 1
				break
		if f == 0:
			ml = max(ml, i)
	return (ml, s[:ml])
cases = int(input())
for t in range(cases):
	s = input()
	l = len(s)
	if l == 1:
		print(s)
	else:
		i = 0
		j = l - 1
		prefix = ''
		while i < j:
			if s[i] == s[j]:
				prefix += s[i]
				i += 1
				j -= 1
			else:
				break
		if i <= j:
			(l1, s1) = longpal(s[i:j + 1][::-1])
			(l2, s2) = longpal(s[i:j + 1])
			if l1 >= l2:
				print(prefix + s1 + prefix[::-1])
			else:
				print(prefix + s2 + prefix[::-1])
		else:
			print(prefix + prefix[::-1])


def is_polindrom(s):
	return s == reverse(s)

def reverse(s):
	return s[::-1]
result = []
test_am = int(input())
stroks = []
for alolo in range(test_am):
	stroks.append(input())
for test in range(test_am):
	string = stroks[test]
	if is_polindrom(string):
		print(string)
		continue
	i = 1
	while string[:i] == reverse(string[-i:]):
		i += 1
	res1 = string[:i - 1]
	if len(res1) == 0:
		temp_string = string
	else:
		temp_string = string[i - 1:-i + 1]
	left = temp_string[0]
	res2 = ''
	for j in range(len(temp_string), 0, -1):
		if is_polindrom(temp_string[:j]):
			res2 = temp_string[:j]
			break
	res3 = ''
	for j in range(0, len(temp_string), 1):
		if is_polindrom(temp_string[j:]):
			res3 = temp_string[j:]
			break
	res4 = res2 if len(res2) >= len(res3) else res3
	result = res1 + res4 + reverse(res1)
	print(result)


def callps(s):
	n = len(s)
	lps = []
	for i in range(n):
		lps.append(0)
	i = 1
	l = 0
	while i < n:
		if s[i] == s[l]:
			l += 1
			lps[i] = l
			i += 1
		elif l != 0:
			l = lps[l - 1]
		else:
			lps[i] = 0
			i += 1
	return lps[n - 1]

def fun(s):
	n = len(s)
	if n == 1:
		return s
	for i in range(n // 2):
		if s[i] != s[n - i - 1]:
			break
	res = s[:i]
	s = s[i:n - i]
	premid = s[:callps(s + '#' + s[::-1])]
	postmid = s[::-1][:callps(s[::-1] + '#' + s)]
	if len(postmid) > len(premid):
		mid = postmid
	else:
		mid = premid
	res = res + mid + res[::-1]
	return res
t = int(input())
while t:
	t -= 1
	s = input()
	print(fun(s))


import sys

def lpre(w):
	kk = len(w)
	ww = w[:]
	w.reverse()
	ww += ['1'] * kk + w
	dp = [0] * (3 * kk + 1)
	dp[0] = -1
	for i in range(3 * kk):
		k = dp[i]
		while k >= 0 and ww[k] != ww[i]:
			k = dp[k]
		dp[i + 1] = k + 1
	return min(kk, dp[-1])
for q in range(int(sys.stdin.readline())):
	w = list(sys.stdin.readline().strip())
	n = len(w)
	st = 0
	for i in range(n // 2):
		if w[i] == w[n - 1 - i]:
			st += 1
		else:
			break
	if st == n // 2:
		sys.stdout.write(''.join(w) + '\n')
		continue
	a = w[st:n - st]
	l1 = lpre(a)
	l2 = lpre(a)
	if l1 > l2:
		ans = w[:st] + a[:l1] + w[n - st:]
	else:
		ans = w[:st] + a[len(a) - l2:] + w[n - st:]
	sys.stdout.write(''.join(ans) + '\n')


def checkpal(s, i):
	k1 = 0
	k2 = i
	while k1 <= k2:
		if s[k1] != s[k2]:
			return False
		else:
			k1 += 1
			k2 -= 1
	return True

def lps(s):
	l = len(s)
	ans = ''
	for i in range(0, l):
		if checkpal(s, i):
			ans = s[0:i + 1]
	return ans
for _ in range(int(input())):
	s = input()
	l = len(s)
	if l == 1:
		print(s)
	else:
		i = 0
		j = l - 1
		begin = ''
		while i < j:
			if s[i] != s[j]:
				break
			else:
				begin += s[i]
				i += 1
				j -= 1
		rs = s[::-1]
		mids = s[i:j + 1]
		k = lps(mids)
		rmids = lps(mids[::-1])
		mids = k
		print(begin, end='')
		if len(mids) > len(rmids):
			print(mids, end='')
		else:
			print(rmids, end='')
		print(begin[::-1])


a = int(input())
c = []
for i in range(0, a, 1):
	d = input()
	c.append(d)
k = 0
while k < a:
	l = len(c[k])
	o = c[k]
	b = list(o)
	A = ''
	B = ''
	i = 0
	j = l - 1
	e = 0
	f = j
	p = 0
	if l % 2 != 0:
		l += 1
	l = l // 2
	while i < l:
		if b[i] == b[j]:
			if i != j:
				A += b[i]
				B = b[j] + B
			else:
				A += b[i]
		else:
			p = 1
			e = i
			f = j
			break
		i += 1
		j -= 1
	C = ''
	g = e
	h = f
	F = ''
	l_1 = 0
	while g < h - 1:
		m = g - e
		if m + g < h:
			n = e
			D = ''
			E = ''
			G = ''
			while n < g:
				D += b[n]
				n += 1
			n = g + m
			while n > g:
				E += b[n]
				G = b[n] + G
				n -= 1
			if D == E:
				l_2 = len(D) + len(E) + 1
				if l_2 > l_1:
					F = D + b[g] + G
					l_1 = l_2
		if m + g + 1 < h:
			n = e
			D = ''
			E = ''
			G = ''
			while n <= g:
				D += b[n]
				n += 1
			n = g + m + 1
			while n > g:
				E += b[n]
				G = b[n] + G
				n -= 1
			if D == E:
				l_2 = len(D) + len(E)
				if l_2 > l_1:
					F = D + G
					l_1 = l_2
		g += 1
	g = e
	h = f
	while h >= g + 1:
		m = f - h
		if h > m + g:
			n = f
			D = ''
			E = ''
			G = ''
			while n > h:
				D += b[n]
				G = b[n] + G
				n -= 1
			n = h - m
			while n < h:
				E += b[n]
				n += 1
			if D == E:
				l_2 = len(D) + len(E) + 1
				if l_2 > l_1:
					F = E + b[h] + G
					l_1 = l_2
		if h > m + g + 1:
			n = f
			D = ''
			E = ''
			G = ''
			while n >= h:
				D += b[n]
				G = b[n] + G
				n -= 1
			n = h - m - 1
			while n < h:
				E += b[n]
				n += 1
			if D == E:
				l_2 = len(D) + len(E)
				if l_2 > l_1:
					F = E + G
					l_1 = l_2
		h -= 1
	r = len(b)
	if e == 0 and f == r - 1:
		if p == 0:
			H = A + B
			print(H)
		else:
			H = A + F + B
			print(H)
	else:
		H = A + F + B
		print(H)
	k += 1


for i in range(int(input())):
	s = input()
	(be, en) = (-1, len(s))
	if s == s[::-1]:
		print(s)
	else:
		for j in range(len(s)):
			if s[j] == s[-1 - j]:
				be += 1
				en -= 1
			else:
				break
		(tem, ma) = ('', '')
		for j in range(be + 1, en):
			tem += s[j]
			if tem == tem[::-1]:
				ma = tem
		(tem2, ma2) = ('', '')
		for j in range(en - 1, be, -1):
			tem2 += s[j]
			if tem2 == tem2[::-1]:
				ma2 = tem2
		if len(ma) >= len(ma2):
			print(s[:be + 1] + ma + s[en:])
		else:
			print(s[:be + 1] + ma2 + s[en:])