#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;

const ll P = 29;
const ll MOD = 998244353;
const ll N = 1000000;

string s;
ll n;
ll pre[N];
ll suf[N];
ll POW[N];

// Returns (n^p) % MOD in O(log(p))
// n >= 0, p > 0 and MOD > 1
ll fpow(ll n, ll p, ll MOD)
{
	ll res = 1;
	while (p) {
		if (p & 1) {
			res = (res * n) % MOD;
		}
		p = p >> 1;
		n = (n * n) % MOD;
	}
	return res;
}

// Returns modulo inverse of n modulo MOD using Fermat's Little Theorem
// n >= 0, MOD must be prime
ll mod_inv(ll n, ll MOD)
{
	return fpow(n, MOD - 2, MOD);
}

ll fwd_hash(int l, int r)
{
	ll hash = (pre[r] - (l == 0 ? 0 : pre[l - 1]) + MOD) % MOD;
	hash = (hash * mod_inv(fpow(P, l, MOD), MOD)) % MOD;
	return hash;
}

ll bwd_hash(int l, int r)
{
	ll hash = (suf[l] - (r == n - 1 ? 0 : suf[r + 1]) + MOD) % MOD;
	hash = (hash * mod_inv(fpow(P, n - r - 1, MOD), MOD)) % MOD;
	return hash;
}

int main()
{
	#ifndef ONLINE_JUDGE
	#include <fileio.h>
	#endif

	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

	POW[0] = 1;
	for (int i = 1; i < N; ++i) {
		POW[i] = (POW[i - 1] * P) % MOD;
	}
	int t; cin >> t;
	while (t--) {
		cin >> s; n = s.size();
		pre[0] = s[0] - 'a';
		suf[n - 1] = s[n - 1] - 'a';
		for (int i = 1, j = n - 2; i < n; ++i, --j) {
			pre[i] = (pre[i - 1] + (s[i] - 'a') * POW[i]) % MOD;
			suf[j] = (suf[j + 1] + (s[j] - 'a') * POW[n - j - 1]) % MOD;
		}
		int l = 0, r = n - 1;
		while (l < r && s[l] == s[r]) {
			++l; --r;
		}
		if (l >= r) {
			cout << s << '\n';
		} else {
			int left_len = 1, right_len = 1;
			for (int i = l; i < r; ++i) {
				if (fwd_hash(l, i) == bwd_hash(l, i)) {
					left_len = i - l + 1;
				}
			}
			for (int i = r; i > l; --i) {
				if (fwd_hash(i, r) == bwd_hash(i, r)) {
					right_len = r - i + 1;
				}
			}
			if (left_len > right_len) {
				for (int i = 0; i < l + left_len; ++i) {
					cout << s[i];
				}
				for (int i = r + 1; i < n; ++i) {
					cout << s[i];
				}
			} else {
				for (int i = 0; i < l; ++i) {
					cout << s[i];
				}
				for (int i = r - right_len + 1; i < n; ++i) {
					cout << s[i];
				}
			}
			cout << '\n';
		}
	}
}


#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;

const int P = 875329;
const int MOD = 100003;
const int N = 1000000;

string s;
int n;
int pre[N];
int suf[N];
ll POW[N];

// Returns (n^p) % MOD in O(log(p))
// n >= 0, p > 0 and MOD > 1
int fpow(ll n, int p, int MOD)
{
	ll res = 1;
	while (p) {
		if (p & 1) {
			res = (res * n) % MOD;
		}
		p = p >> 1;
		n = (n * n) % MOD;
	}
	return res;
}

// Returns modulo inverse of n modulo MOD using Fermat's Little Theorem
// n >= 0, MOD must be prime
int mod_inv(int n, int MOD)
{
	return fpow(n, MOD - 2, MOD);
}

int fwd_hash(int l, int r)
{
	ll hash = (pre[r] - (l == 0 ? 0 : pre[l - 1]) + MOD) % MOD;
	hash = (hash * mod_inv(POW[l], MOD)) % MOD;
	return hash;
}

int bwd_hash(int l, int r)
{
	ll hash = (suf[l] - (r == n - 1 ? 0 : suf[r + 1]) + MOD) % MOD;
	hash = (hash * mod_inv(POW[n - r - 1], MOD)) % MOD;
	return hash;
}

int main()
{
	#ifndef ONLINE_JUDGE
	#include <fileio.h>
	#endif

	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

	POW[0] = 1;
	for (int i = 1; i < N; ++i) {
		POW[i] = (POW[i - 1] * P) % MOD;
	}
	int t; cin >> t;
	while (t--) {
		cin >> s; n = s.size();
		pre[0] = s[0] - 'a';
		suf[n - 1] = s[n - 1] - 'a';
		for (int i = 1, j = n - 2; i < n; ++i, --j) {
			pre[i] = (pre[i - 1] + (s[i] - 'a') * POW[i]) % MOD;
			suf[j] = (suf[j + 1] + (s[j] - 'a') * POW[n - j - 1]) % MOD;
		}
		int l = 0, r = n - 1;
		while (l < r && s[l] == s[r]) {
			++l; --r;
		}
		if (l >= r) {
			cout << s << '\n';
		} else {
			int left_len = 1, right_len = 1;
			for (int i = l; i < r; ++i) {
				if (fwd_hash(l, i) == bwd_hash(l, i)) {
					left_len = i - l + 1;
				}
			}
			for (int i = r; i > l; --i) {
				if (fwd_hash(i, r) == bwd_hash(i, r)) {
					right_len = r - i + 1;
				}
			}
			if (left_len > right_len) {
				for (int i = 0; i < l + left_len; ++i) {
					cout << s[i];
				}
				for (int i = r + 1; i < n; ++i) {
					cout << s[i];
				}
			} else {
				for (int i = 0; i < l; ++i) {
					cout << s[i];
				}
				for (int i = r - right_len + 1; i < n; ++i) {
					cout << s[i];
				}
			}
			cout << '\n';
		}
	}
}


#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;

const int P = 875329;
const int MOD = 100591;
const int N = 1000000;

string s;
int n;
int pre[N];
int suf[N];
ll POW[N];

// Returns (n^p) % MOD in O(log(p))
// n >= 0, p > 0 and MOD > 1
int fpow(ll n, int p, int MOD)
{
	ll res = 1;
	while (p) {
		if (p & 1) {
			res = (res * n) % MOD;
		}
		p = p >> 1;
		n = (n * n) % MOD;
	}
	return res;
}

// Returns modulo inverse of n modulo MOD using Fermat's Little Theorem
// n >= 0, MOD must be prime
int mod_inv(int n, int MOD)
{
	return fpow(n, MOD - 2, MOD);
}

int fwd_hash(int l, int r)
{
	ll hash = (pre[r] - (l == 0 ? 0 : pre[l - 1]) + MOD) % MOD;
	hash = (hash * mod_inv(POW[l], MOD)) % MOD;
	return hash;
}

int bwd_hash(int l, int r)
{
	ll hash = (suf[l] - (r == n - 1 ? 0 : suf[r + 1]) + MOD) % MOD;
	hash = (hash * mod_inv(POW[n - r - 1], MOD)) % MOD;
	return hash;
}

int main()
{
	#ifndef ONLINE_JUDGE
	#include <fileio.h>
	#endif

	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

	POW[0] = 1;
	for (int i = 1; i < N; ++i) {
		POW[i] = (POW[i - 1] * P) % MOD;
	}
	int t; cin >> t;
	while (t--) {
		cin >> s; n = s.size();
		pre[0] = s[0] - 'a';
		suf[n - 1] = s[n - 1] - 'a';
		for (int i = 1, j = n - 2; i < n; ++i, --j) {
			pre[i] = (pre[i - 1] + (s[i] - 'a') * POW[i]) % MOD;
			suf[j] = (suf[j + 1] + (s[j] - 'a') * POW[n - j - 1]) % MOD;
		}
		int l = 0, r = n - 1;
		while (l < r && s[l] == s[r]) {
			++l; --r;
		}
		if (l >= r) {
			cout << s << '\n';
		} else {
			int left_len = 1, right_len = 1;
			for (int i = l; i < r; ++i) {
				if (fwd_hash(l, i) == bwd_hash(l, i)) {
					left_len = i - l + 1;
				}
			}
			for (int i = r; i > l; --i) {
				if (fwd_hash(i, r) == bwd_hash(i, r)) {
					right_len = r - i + 1;
				}
			}
			if (left_len > right_len) {
				for (int i = 0; i < l + left_len; ++i) {
					cout << s[i];
				}
				for (int i = r + 1; i < n; ++i) {
					cout << s[i];
				}
			} else {
				for (int i = 0; i < l; ++i) {
					cout << s[i];
				}
				for (int i = r - right_len + 1; i < n; ++i) {
					cout << s[i];
				}
			}
			cout << '\n';
		}
	}
}


#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;

const int P = 29;
const ll MOD = 998244353;
const int N = 1000000;

int n;
ll pre[N + 2];
ll suf[N + 2];

// Returns (n^p) modulo MOD in O(log(p))
// n >= 0, p > 0 and MOD > 1
// ll pow(ll n, ll p, ll MOD)
// {
// 	if (n == 0) {
// 		return 0;
// 	}
// 	if (p == 0) {
// 		return 1;
// 	}
// 	ll half = pow(n, p / 2, MOD);
// 	ll full = (half * half) % MOD;
// 	if (p & 1) {
// 		full = (full * n) % MOD;
// 	}
// 	return full;
// }

ll pow(ll a, ll n, ll MOD) {
	ll ans = 1;
	while (n) {
		if (n&1) ans = (ans*a)%MOD;
		a = (a*a)%MOD; n = n >> 1;
	}
	return ans;
}

// Returns modulo inverse of n modulo MOD using Fermat's Little Theorem
// n >= 0, MOD must be prime
ll mod_inv(int n, int MOD)
{
	return pow(n, MOD - 2, MOD);
}

ll fwd_hash(string &s, int l, int r)
{
	ll hash = (pre[r] - pre[l - 1] + MOD) % MOD;
	hash = (hash * mod_inv(pow(P, l - 1, MOD), MOD)) % MOD;
	return hash;
}

ll bwd_hash(string &s, int l, int r)
{
	ll hash = (suf[l] - suf[r + 1] + MOD) % MOD;
	hash = (hash * mod_inv(pow(P, n - r, MOD), MOD)) % MOD;
	return hash;
}

bool isPalin(string &s, int l, int r)
{
	return fwd_hash(s, l, r) == bwd_hash(s, l, r);
}

int main()
{
	#ifndef ONLINE_JUDGE
	#include <fileio.h>
	#endif

	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

	int t; cin >> t;
	while (t--) {
		string str; cin >> str;
		n = str.size();
		string s = "-" + str;
		pre[1] = s[1] - 'a';
		suf[n] = s[n] - 'a'; suf[n + 1] = 0;
		for (int i = 2; i <= n; ++i) {
			pre[i] = (pre[i - 1] + (s[i] - 'a') * pow(P, i - 1, MOD)) % MOD;
		}
		for (int i = n - 1; i >= 1; --i) {
			suf[i] = (suf[i + 1] + (s[i] - 'a') * pow(P, n - i, MOD)) % MOD;
		}
		int l = 1, r = n;
		while (l < r && s[l] == s[r]) {
			++l; --r;
		}
		if (l >= r) {
			cout << str << '\n';
		} else {
			int left_len = 1, right_len = 1;
			for (int i = l; i < r; ++i) {
				if (isPalin(s, l, i)) {
					left_len = i - l + 1;
				}
			}
			for (int i = r; i > l; --i) {
				if (isPalin(s, i, r)) {
					right_len = r - i + 1;
				}
			}
			if (left_len > right_len) {
				cout << s.substr(1, l + left_len - 1) + s.substr(r + 1, l - 1) << '\n';
			} else {
				cout << s.substr(1, l - 1) + s.substr(r - right_len + 1, l - 1 + right_len) << '\n';
			}
		}
	}
}


#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;

const int P = 29;
const ll MOD = 998244353;
const int N = 1000000;

int n;
ll pre[N + 2];
ll suf[N + 2];

// Returns (n^p) modulo MOD in O(log(p))
// n >= 0, p > 0 and MOD > 1
// ll pow(ll n, ll p, ll MOD)
// {
// 	if (n == 0) {
// 		return 0;
// 	}
// 	if (p == 0) {
// 		return 1;
// 	}
// 	ll half = pow(n, p / 2, MOD);
// 	ll full = (half * half) % MOD;
// 	if (p & 1) {
// 		full = (full * n) % MOD;
// 	}
// 	return full;
// }

inline ll pow(ll a, ll n, ll MOD) {
	ll ans = 1;
	while (n) {
		if (n&1) ans = (ans*a)%MOD;
		a = (a*a)%MOD; n = n >> 1;
	}
	return ans;
}

// Returns modulo inverse of n modulo MOD using Fermat's Little Theorem
// n >= 0, MOD must be prime
inline ll mod_inv(int n, int MOD)
{
	return pow(n, MOD - 2, MOD);
}

inline ll fwd_hash(string &s, int l, int r)
{
	ll hash = (pre[r] - pre[l - 1] + MOD) % MOD;
	hash = (hash * mod_inv(pow(P, l - 1, MOD), MOD)) % MOD;
	return hash;
}

inline ll bwd_hash(string &s, int l, int r)
{
	ll hash = (suf[l] - suf[r + 1] + MOD) % MOD;
	hash = (hash * mod_inv(pow(P, n - r, MOD), MOD)) % MOD;
	return hash;
}

inline bool isPalin(string &s, int l, int r)
{
	return fwd_hash(s, l, r) == bwd_hash(s, l, r);
}

int main()
{
	#ifndef ONLINE_JUDGE
	#include <fileio.h>
	#endif

	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

	int t; cin >> t;
	while (t--) {
		string str; cin >> str;
		n = str.size();
		string s = "-" + str;
		pre[1] = s[1] - 'a';
		suf[n] = s[n] - 'a'; suf[n + 1] = 0;
		for (int i = 2; i <= n; ++i) {
			pre[i] = (pre[i - 1] + (s[i] - 'a') * pow(P, i - 1, MOD)) % MOD;
			int j = n - i + 1;
			suf[j] = (suf[j + 1] + (s[j] - 'a') * pow(P, n - j, MOD)) % MOD;
		}
		int l = 1, r = n;
		while (l < r && s[l] == s[r]) {
			++l; --r;
		}
		if (l >= r) {
			cout << str << '\n';
		} else {
			int left_len = 1, right_len = 1;
			for (int i = l; i < r; ++i) {
				if (isPalin(s, l, i)) {
					left_len = i - l + 1;
				}
			}
			for (int i = r; i > l; --i) {
				if (isPalin(s, i, r)) {
					right_len = r - i + 1;
				}
			}
			if (left_len > right_len) {
				cout << s.substr(1, l + left_len - 1) + s.substr(r + 1, l - 1) << '\n';
			} else {
				cout << s.substr(1, l - 1) + s.substr(r - right_len + 1, l - 1 + right_len) << '\n';
			}
		}
	}
}


#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;

const ll P = 29;
const ll MOD = 998244353;
const ll N = 1000000;

string s;
ll n;
ll pre[N];
ll suf[N];
ll POW[N];

void init_pow()
{
	POW[0] = 1;
	for (int i = 1; i < N; ++i) {
		POW[i] = (POW[i - 1] * P) % MOD;
	}
}

ll fwd_hash(int l, int r)
{
	if (l == 0) {
		return pre[r];
	}
	return ((pre[r] - ((pre[l - 1] * POW[r - l + 1]) % MOD)) + MOD) % MOD;
}

ll bwd_hash(int l, int r)
{
	if (r == n - 1) {
		return suf[l];
	}
	return ((suf[l] - ((suf[r + 1] * POW[r - l + 1]) % MOD)) + MOD) % MOD;
}

int main()
{
	#ifndef ONLINE_JUDGE
	#include <fileio.h>
	#endif

	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

	init_pow();
	int t; cin >> t;
	while (t--) {
		cin >> s; n = s.size();
		pre[0] = s[0] - 'a';
		suf[n- 1] = s[n - 1] - 'a';
		for (int i = 1, j = n - 2; i < n; ++i, --j) {
			pre[i] = (pre[i - 1] * P + s[i] - 'a') % MOD;
			suf[j] = (suf[j + 1] * P + s[j] - 'a') % MOD;
		}
		int l = 0, r = n - 1;
		while (l < r && s[l] == s[r]) {
			++l; --r;
		}
		if (l >= r) {
			cout << s << '\n';
		} else {
			int left_len = 1, right_len = 1;
			for (int i = l; i < r; ++i) {
				if (fwd_hash(l, i) == bwd_hash(l, i)) {
					left_len = i - l + 1;
				}
			}
			for (int i = r; i > l; --i) {
				if (fwd_hash(i, r) == bwd_hash(i, r)) {
					right_len = r - i + 1;
				}
			}
			if (left_len > right_len) {
				for (int i = 0; i < l + left_len; ++i) {
					cout << s[i];
				}
				for (int i = r + 1; i < n; ++i) {
					cout << s[i];
				}
			} else {
				for (int i = 0; i < l; ++i) {
					cout << s[i];
				}
				for (int i = r - right_len + 1; i < n; ++i) {
					cout << s[i];
				}
			}
			cout << '\n';
		}
	}
}


#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;

const ll P = 29;
const ll MOD = 998244353;
const ll N = 1000000;

string s;
ll n;
ll pre[N];
ll suf[N];
ll POW[N];

void init_pow();
void init_presuf();
bool check_palin(int l, int r);

int main()
{
	#ifndef ONLINE_JUDGE
	#include <fileio.h>
	#endif

	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

	init_pow();
	int t; cin >> t;
	while (t--) {
		cin >> s; n = s.size();
		init_presuf();
		int l = 0, r = n - 1;
		while (l < r && s[l] == s[r]) {
			++l; --r;
		}
		if (l >= r) {
			cout << s << '\n';
			continue;
		}
		int lsize = 1, rsize = 1;
		for (int i = l; i < r; ++i) {
			if (check_palin(l, i)) {
				lsize = i - l + 1;
			}
		}
		for (int i = r; i > l; --i) {
			if (check_palin(i, r)) {
				rsize = r - i + 1;
			}
		}
		if (lsize > rsize) {
			l += lsize;
		} else {
			r -= rsize;
		}
		cout << s.substr(0, l) + s.substr(r + 1, n) << '\n';
	}
}

void init_pow()
{
	POW[0] = 1;
	for (int i = 1; i < N; ++i) {
		POW[i] = (POW[i - 1] * P) % MOD;
	}
}

void init_presuf()
{
	pre[0] = s[0] - 'a';
	suf[n - 1] = s[n - 1] - 'a';
	for (int i = 1, j = n - 2; i < n; ++i, --j) {
		pre[i] = (pre[i - 1] * P + s[i] - 'a') % MOD;
		suf[j] = (suf[j + 1] * P + s[j] - 'a') % MOD;
	}
}

bool check_palin(int l, int r)
{
	ll fwd_hash, bwd_hash;
	if (l == 0) {
		fwd_hash = pre[r];
	} else {
		fwd_hash = ((pre[r] - ((pre[l - 1] * POW[r - l + 1]) % MOD)) + MOD) % MOD;
	}
	if (r == n - 1) {
		bwd_hash = suf[l];
	} else {
		bwd_hash = ((suf[l] - ((suf[r + 1] * POW[r - l + 1]) % MOD)) + MOD) % MOD;
	}

	return fwd_hash == bwd_hash;
}


#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;

const int P = 29;
const ll MOD = 998244353;
const int N = 100000;

int n;
ll pre[N + 2];
ll suf[N + 2];

// Returns (n^p) modulo MOD in O(log(p))
// n >= 0, p > 0 and MOD > 1
ll pow(ll n, ll p, ll MOD)
{
	if (n == 0) {
		return 0;
	}
	if (p == 0) {
		return 1;
	}
	ll half = pow(n, p / 2, MOD);
	ll full = (half * half) % MOD;
	if (p & 1) {
		full = (full * n) % MOD;
	}
	return full;
}

// Returns modulo inverse of n modulo MOD using Fermat's Little Theorem
// n >= 0, MOD must be prime
ll mod_inv(int n, int MOD)
{
	return pow(n, MOD - 2, MOD);
}

ll fwd_hash(string &s, int l, int r)
{
	ll hash = (pre[r] - pre[l - 1] + MOD) % MOD;
	hash = (hash * mod_inv(pow(P, l - 1, MOD), MOD)) % MOD;
	return hash;
}

ll bwd_hash(string &s, int l, int r)
{
	ll hash = (suf[l] - suf[r + 1] + MOD) % MOD;
	hash = (hash * mod_inv(pow(P, n - r, MOD), MOD)) % MOD;
	return hash;
}

bool isPalin(string &s, int l, int r)
{
	return fwd_hash(s, l, r) == bwd_hash(s, l, r);
}

int main()
{
	#ifndef ONLINE_JUDGE
	#include <fileio.h>
	#endif

	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

	int t; cin >> t;
	while (t--) {
		string str; cin >> str;
		n = str.size();
		string s = "-" + str;
		pre[1] = s[1] - 'a';
		suf[n] = s[n] - 'a'; suf[n + 1] = 0;
		for (int i = 2; i <= n; ++i) {
			pre[i] = (pre[i - 1] + (s[i] - 'a') * pow(P, i - 1, MOD)) % MOD;
		}
		for (int i = n - 1; i >= 1; --i) {
			suf[i] = (suf[i + 1] + (s[i] - 'a') * pow(P, n - i, MOD)) % MOD;
		}
		int l = 1, r = n;
		while (l < r && s[l] == s[r]) {
			++l; --r;
		}
		if (l >= r) {
			cout << str << '\n';
		} else {
			int left_len = 1, right_len = 1;
			for (int i = l; i < r; ++i) {
				if (isPalin(s, l, i)) {
					left_len = i - l + 1;
				}
			}
			for (int i = r; i > l; --i) {
				if (isPalin(s, i, r)) {
					right_len = r - i + 1;
				}
			}
			if (left_len > right_len) {
				cout << s.substr(1, l + left_len - 1) + s.substr(r + 1, l - 1) << '\n';
			} else {
				cout << s.substr(1, l - 1) + s.substr(r - right_len + 1, l - 1 + right_len) << '\n';
			}
		}
	}
}


#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;

const int P = 29;
const ll MOD = 998244353;
const int N = 1000000;

int n;
ll pre[N + 2];
ll suf[N + 2];

// Returns (n^p) modulo MOD in O(log(p))
// n >= 0, p > 0 and MOD > 1
// ll pow(ll n, ll p, ll MOD)
// {
// 	if (n == 0) {
// 		return 0;
// 	}
// 	if (p == 0) {
// 		return 1;
// 	}
// 	ll half = pow(n, p / 2, MOD);
// 	ll full = (half * half) % MOD;
// 	if (p & 1) {
// 		full = (full * n) % MOD;
// 	}
// 	return full;
// }

inline ll pow(ll a, ll n, ll MOD) {
	ll ans = 1;
	while (n) {
		if (n&1) ans = (ans*a)%MOD;
		a = (a*a)%MOD; n = n >> 1;
	}
	return ans;
}

// Returns modulo inverse of n modulo MOD using Fermat's Little Theorem
// n >= 0, MOD must be prime
inline ll mod_inv(int n, int MOD)
{
	return pow(n, MOD - 2, MOD);
}

inline ll fwd_hash(string &s, int l, int r)
{
	ll hash = (pre[r] - pre[l - 1] + MOD) % MOD;
	hash = (hash * mod_inv(pow(P, l - 1, MOD), MOD)) % MOD;
	return hash;
}

inline ll bwd_hash(string &s, int l, int r)
{
	ll hash = (suf[l] - suf[r + 1] + MOD) % MOD;
	hash = (hash * mod_inv(pow(P, n - r, MOD), MOD)) % MOD;
	return hash;
}

inline bool isPalin(string &s, int l, int r)
{
	return fwd_hash(s, l, r) == bwd_hash(s, l, r);
}

int main()
{
	#ifndef ONLINE_JUDGE
	#include <fileio.h>
	#endif

	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

	int t; cin >> t;
	while (t--) {
		string str; cin >> str;
		n = str.size();
		string s = "-" + str;
		pre[1] = s[1] - 'a';
		suf[n] = s[n] - 'a'; suf[n + 1] = 0;
		for (int i = 2; i <= n; ++i) {
			pre[i] = (pre[i - 1] + (s[i] - 'a') * pow(P, i - 1, MOD)) % MOD;
			int j = n - i + 1;
			suf[j] = (suf[j + 1] + (s[j] - 'a') * pow(P, n - j, MOD)) % MOD;
		}
		int l = 1, r = n;
		while (l < r && s[l] == s[r]) {
			++l; --r;
		}
		if (l >= r) {
			cout << str << '\n';
		} else {
			int left_len = 1, right_len = 1;
			for (int i = l; i < r; ++i) {
				if (isPalin(s, l, i)) {
					left_len = i - l + 1;
				}
			}
			for (int i = r; i > l; --i) {
				if (isPalin(s, i, r)) {
					right_len = r - i + 1;
				}
			}
			if (left_len > right_len) {
				for (int i = 1; i < l + left_len; ++i) {
					cout << s[i];
				}
				for (int i = r + 1; i <= n; ++i) {
					cout << s[i];
				}
				// cout << s.substr(1, l + left_len - 1) + s.substr(r + 1, l - 1) << '\n';
			} else {
				for (int i = 1; i < l; ++i) {
					cout << s[i];
				}
				for (int i = r - right_len + 1; i <= n; ++i) {
					cout << s[i];
				}
				// cout << s.substr(1, l - 1) + s.substr(r - right_len + 1, l - 1 + right_len) << '\n';
			}
			cout << '\n';
		}
	}
}


#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;

const int P = 29;
const ll MOD = 998244353;
const int N = 1000000;

int n;
ll pre[N + 2];
ll suf[N + 2];
ll POW[N];

// Returns (n^p) modulo MOD in O(log(p))
// n >= 0, p > 0 and MOD > 1
ll pow(ll n, ll p, ll MOD)
{
	if (n == 0) {
		return 0;
	}
	if (p == 0) {
		return 1;
	}
	ll half = pow(n, p / 2, MOD);
	ll full = (half * half) % MOD;
	if (p & 1) {
		full = (full * n) % MOD;
	}
	return full;
}

// Returns modulo inverse of n modulo MOD using Fermat's Little Theorem
// n >= 0, MOD must be prime
ll mod_inv(int n, int MOD)
{
	return pow(n, MOD - 2, MOD);
}

ll fwd_hash(string &s, int l, int r)
{
	ll hash = (pre[r] - pre[l - 1] + MOD) % MOD;
	hash = (hash * mod_inv(POW[l - 1], MOD)) % MOD;
	return hash;
}

ll bwd_hash(string &s, int l, int r)
{
	ll hash = (suf[l] - suf[r + 1] + MOD) % MOD;
	hash = (hash * mod_inv(POW[n - r], MOD)) % MOD;
	return hash;
}

bool isPalin(string &s, int l, int r)
{
	return fwd_hash(s, l, r) == bwd_hash(s, l, r);
}

int main()
{
	#ifndef ONLINE_JUDGE
	#include <fileio.h>
	#endif

	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

	POW[0] = 1;
	for (int i = 1; i < N; ++i) {
		POW[i] = (POW[i - 1] * P) % MOD;
	}
	int t; cin >> t;
	while (t--) {
		string str; cin >> str;
		n = str.size();
		string s = "-" + str;
		pre[1] = s[1] - 'a';
		suf[n] = s[n] - 'a'; suf[n + 1] = 0;
		for (int i = 2; i <= n; ++i) {
			pre[i] = (pre[i - 1] + (s[i] - 'a') * POW[i - 1]) % MOD;
		}
		for (int i = n - 1; i >= 1; --i) {
			suf[i] = (suf[i + 1] + (s[i] - 'a') * POW[n - i]) % MOD;
		}
		int l = 1, r = n;
		while (l < r && s[l] == s[r]) {
			++l; --r;
		}
		if (l >= r) {
			cout << str << '\n';
		} else {
			int left_len = 1, right_len = 1;
			for (int i = l; i < r; ++i) {
				if (isPalin(s, l, i)) {
					left_len = i - l + 1;
				}
			}
			for (int i = r; i > l; --i) {
				if (isPalin(s, i, r)) {
					right_len = r - i + 1;
				}
			}
			if (left_len > right_len) {
				cout << s.substr(1, l + left_len - 1) + s.substr(r + 1, l - 1) << '\n';
			} else {
				cout << s.substr(1, l - 1) + s.substr(r - right_len + 1, l - 1 + right_len) << '\n';
			}
		}
	}
}


#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;

const int P = 29;
const ll MOD = 998244353;
const int N = 1000000;

int n;
ll pre[N + 2];
ll suf[N + 2];
ll POW[N];

// Returns (n^p) modulo MOD in O(log(p))
// n >= 0, p > 0 and MOD > 1
ll pow(ll a, ll n, ll MOD) {
	ll ans = 1;
	while (n) {
		if (n&1) ans = (ans*a)%MOD;
		a = (a*a)%MOD; n = n >> 1;
	}
	return ans;
}

// Returns modulo inverse of n modulo MOD using Fermat's Little Theorem
// n >= 0, MOD must be prime
ll mod_inv(int n, int MOD)
{
	return pow(n, MOD - 2, MOD);
}

ll fwd_hash(string &s, int l, int r)
{
	ll hash = (pre[r] - pre[l - 1] + MOD) % MOD;
	hash = (hash * mod_inv(POW[l - 1], MOD)) % MOD;
	return hash;
}

ll bwd_hash(string &s, int l, int r)
{
	ll hash = (suf[l] - suf[r + 1] + MOD) % MOD;
	hash = (hash * mod_inv(POW[n - r], MOD)) % MOD;
	return hash;
}

bool isPalin(string &s, int l, int r)
{
	return fwd_hash(s, l, r) == bwd_hash(s, l, r);
}

int main()
{
	#ifndef ONLINE_JUDGE
	#include <fileio.h>
	#endif

	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

	POW[0] = 1;
	for (int i = 1; i < N; ++i) {
		POW[i] = (POW[i - 1] * P) % MOD;
	}
	int t; cin >> t;
	while (t--) {
		string str; cin >> str;
		n = str.size();
		string s = "-" + str;
		pre[1] = s[1] - 'a';
		suf[n] = s[n] - 'a'; suf[n + 1] = 0;
		for (int i = 2; i <= n; ++i) {
			pre[i] = (pre[i - 1] + (s[i] - 'a') * POW[i - 1]) % MOD;
		}
		for (int i = n - 1; i >= 1; --i) {
			suf[i] = (suf[i + 1] + (s[i] - 'a') * POW[n - i]) % MOD;
		}
		int l = 1, r = n;
		while (l < r && s[l] == s[r]) {
			++l; --r;
		}
		if (l >= r) {
			cout << str << '\n';
		} else {
			int left_len = 1, right_len = 1;
			for (int i = l; i < r; ++i) {
				if (isPalin(s, l, i)) {
					left_len = i - l + 1;
				}
			}
			for (int i = r; i > l; --i) {
				if (isPalin(s, i, r)) {
					right_len = r - i + 1;
				}
			}
			if (left_len > right_len) {
				cout << s.substr(1, l + left_len - 1) + s.substr(r + 1, l - 1) << '\n';
			} else {
				cout << s.substr(1, l - 1) + s.substr(r - right_len + 1, l - 1 + right_len) << '\n';
			}
		}
	}
}


#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;

const int P = 875329;
const int MOD = 998244353;
const int N = 1000000;

string s;
int n;
int pre[N];
int suf[N];
ll POW[N];

// Returns (n^p) % MOD in O(log(p))
// n >= 0, p > 0 and MOD > 1
int fpow(ll n, int p, int MOD)
{
	ll res = 1;
	while (p) {
		if (p & 1) {
			res = (res * n) % MOD;
		}
		p = p >> 1;
		n = (n * n) % MOD;
	}
	return res;
}

// Returns modulo inverse of n modulo MOD using Fermat's Little Theorem
// n >= 0, MOD must be prime
int mod_inv(int n, int MOD)
{
	return fpow(n, MOD - 2, MOD);
}

int fwd_hash(int l, int r)
{
	ll hash = (pre[r] - (l == 0 ? 0 : pre[l - 1]) + MOD) % MOD;
	hash = (hash * mod_inv(POW[l], MOD)) % MOD;
	return hash;
}

int bwd_hash(int l, int r)
{
	ll hash = (suf[l] - (r == n - 1 ? 0 : suf[r + 1]) + MOD) % MOD;
	hash = (hash * mod_inv(POW[n - r - 1], MOD)) % MOD;
	return hash;
}

int main()
{
	#ifndef ONLINE_JUDGE
	#include <fileio.h>
	#endif

	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

	POW[0] = 1;
	for (int i = 1; i < N; ++i) {
		POW[i] = (POW[i - 1] * P) % MOD;
	}
	int t; cin >> t;
	while (t--) {
		cin >> s; n = s.size();
		pre[0] = s[0] - 'a';
		suf[n - 1] = s[n - 1] - 'a';
		for (int i = 1, j = n - 2; i < n; ++i, --j) {
			pre[i] = (pre[i - 1] + (s[i] - 'a') * POW[i]) % MOD;
			suf[j] = (suf[j + 1] + (s[j] - 'a') * POW[n - j - 1]) % MOD;
		}
		int l = 0, r = n - 1;
		while (l < r && s[l] == s[r]) {
			++l; --r;
		}
		if (l >= r) {
			cout << s << '\n';
		} else {
			int left_len = 1, right_len = 1;
			for (int i = l; i < r; ++i) {
				if (fwd_hash(l, i) == bwd_hash(l, i)) {
					left_len = i - l + 1;
				}
			}
			for (int i = r; i > l; --i) {
				if (fwd_hash(i, r) == bwd_hash(i, r)) {
					right_len = r - i + 1;
				}
			}
			if (left_len > right_len) {
				for (int i = 0; i < l + left_len; ++i) {
					cout << s[i];
				}
				for (int i = r + 1; i < n; ++i) {
					cout << s[i];
				}
			} else {
				for (int i = 0; i < l; ++i) {
					cout << s[i];
				}
				for (int i = r - right_len + 1; i < n; ++i) {
					cout << s[i];
				}
			}
			cout << '\n';
		}
	}
}


#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;

const int P = 29;
const ll MOD = 998244353;
const int N = 100000;

int n;
ll pre[N + 2];
ll suf[N + 2];

// Returns (n^p) modulo MOD in O(log(p))
// n >= 0, p > 0 and MOD > 1
// ll pow(ll n, ll p, ll MOD)
// {
// 	if (n == 0) {
// 		return 0;
// 	}
// 	if (p == 0) {
// 		return 1;
// 	}
// 	ll half = pow(n, p / 2, MOD);
// 	ll full = (half * half) % MOD;
// 	if (p & 1) {
// 		full = (full * n) % MOD;
// 	}
// 	return full;
// }

ll pow(ll a, ll n, ll MOD) {
	ll ans = 1;
	while (n) {
		if (n&1) ans = (ans*a)%MOD;
		a = (a*a)%MOD; n = n >> 1;
	}
	return ans;
}

// Returns modulo inverse of n modulo MOD using Fermat's Little Theorem
// n >= 0, MOD must be prime
ll mod_inv(int n, int MOD)
{
	return pow(n, MOD - 2, MOD);
}

ll fwd_hash(string &s, int l, int r)
{
	ll hash = (pre[r] - pre[l - 1] + MOD) % MOD;
	hash = (hash * mod_inv(pow(P, l - 1, MOD), MOD)) % MOD;
	return hash;
}

ll bwd_hash(string &s, int l, int r)
{
	ll hash = (suf[l] - suf[r + 1] + MOD) % MOD;
	hash = (hash * mod_inv(pow(P, n - r, MOD), MOD)) % MOD;
	return hash;
}

bool isPalin(string &s, int l, int r)
{
	return fwd_hash(s, l, r) == bwd_hash(s, l, r);
}

int main()
{
	#ifndef ONLINE_JUDGE
	#include <fileio.h>
	#endif

	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

	int t; cin >> t;
	while (t--) {
		string str; cin >> str;
		n = str.size();
		string s = "-" + str;
		pre[1] = s[1] - 'a';
		suf[n] = s[n] - 'a'; suf[n + 1] = 0;
		for (int i = 2; i <= n; ++i) {
			pre[i] = (pre[i - 1] + (s[i] - 'a') * pow(P, i - 1, MOD)) % MOD;
		}
		for (int i = n - 1; i >= 1; --i) {
			suf[i] = (suf[i + 1] + (s[i] - 'a') * pow(P, n - i, MOD)) % MOD;
		}
		int l = 1, r = n;
		while (l < r && s[l] == s[r]) {
			++l; --r;
		}
		if (l >= r) {
			cout << str << '\n';
		} else {
			int left_len = 1, right_len = 1;
			for (int i = l; i < r; ++i) {
				if (isPalin(s, l, i)) {
					left_len = i - l + 1;
				}
			}
			for (int i = r; i > l; --i) {
				if (isPalin(s, i, r)) {
					right_len = r - i + 1;
				}
			}
			if (left_len > right_len) {
				cout << s.substr(1, l + left_len - 1) + s.substr(r + 1, l - 1) << '\n';
			} else {
				cout << s.substr(1, l - 1) + s.substr(r - right_len + 1, l - 1 + right_len) << '\n';
			}
		}
	}
}


#include <bits/stdc++.h>

using namespace std;

// REF: https://codeforces.com/blog/entry/3107
int LPP(string s)
{
	string rev="",str=s;
    int m=s.size(),longestPalindromicPrefix=1;
    if(m==0 || m==1)    longestPalindromicPrefix=m;
    for(int i=m-1;i>=0;i--)
        rev+=s[i];
    s+='#';
    s+=rev;
    int n=s.size(),z[n+4],l=0,r=0;
    for(int i=1;i<n;i++){
        if(i>r){
            l=r=i;
            while(r<n && s[r-l]==s[r])
                r++;
            z[i]=r-l,r--;
        }
        else{
            int k=i-l;
            if(z[k]<r-i+1)
                z[i]=z[k];
            else{
                l=i;
                while(r<n && s[r-l]==s[r])
                    r++;
                z[i]=r-l,r--;
            }
        }
    }

    for(int i=m+1;i<n;i++){
        if(2*z[i]>=2*m-i && z[i]>longestPalindromicPrefix)
            longestPalindromicPrefix=z[i];
    }

    return longestPalindromicPrefix;
}

int main()
{
	#ifndef ONLINE_JUDGE
	#include <fileio.h>
	#endif

	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

	int t; cin >> t;
	while (t--) {
		string s; cin >> s;
		int l = 0, r = s.size() - 1, n = s.size();
		if (l == r) {
			cout << s << '\n';
			continue;
		}
		while (l < r && s[l] == s[r]) {
			++l; --r;
		}
		if (l >= r) {
			cout << s << '\n';
			continue;
		}
		int ml = l + LPP(s.substr(l, r - l)) - 1;
		string st = s.substr(l + 1, r - l); reverse(st.begin(), st.end());
		int mr = r - LPP(st) + 1;
		if (abs(ml - l) > abs(mr - r)) {
			for (int i = 0; i <= ml; ++i) {
				cout << s[i];
			}
			for (int i = r + 1; i < s.size(); ++i) {
				cout << s[i];
			}
		} else {
			for (int i = 0; i < l; ++i) {
				cout << s[i];
			}
			for (int i = mr; i < s.size(); ++i) {
				cout << s[i];
			}
		}
		cout << '\n';
	}
}


#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;

const ll P = 29;
const ll MOD = 998244353;
const ll N = 1000000;

string s;
ll n;
ll pre[N];
ll suf[N];
ll POW[N];

// Returns (n^p) % MOD in O(log(p))
// n >= 0, p > 0 and MOD > 1
ll fpow(ll n, ll p, ll MOD)
{
	ll res = 1;
	while (p) {
		if (p & 1) {
			res = (res * n) % MOD;
		}
		p = p >> 1;
		n = (n * n) % MOD;
	}
	return res;
}

// Returns modulo inverse of n modulo MOD using Fermat's Little Theorem
// n >= 0, MOD must be prime
ll mod_inv(ll n, ll MOD)
{
	return fpow(n, MOD - 2, MOD);
}

ll fwd_hash(int l, int r)
{
	ll hash = (pre[r] - (l == 0 ? 0 : pre[l - 1]) + MOD) % MOD;
	hash = (hash * mod_inv(POW[l], MOD)) % MOD;
	return hash;
}

ll bwd_hash(int l, int r)
{
	ll hash = (suf[l] - (r == n - 1 ? 0 : suf[r + 1]) + MOD) % MOD;
	hash = (hash * mod_inv(POW[n - r - 1], MOD)) % MOD;
	return hash;
}

int main()
{
	#ifndef ONLINE_JUDGE
	#include <fileio.h>
	#endif

	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

	POW[0] = 1;
	for (int i = 1; i < N; ++i) {
		POW[i] = (POW[i - 1] * P) % MOD;
	}
	int t; cin >> t;
	while (t--) {
		cin >> s; n = s.size();
		pre[0] = s[0] - 'a';
		suf[n - 1] = s[n - 1] - 'a';
		for (int i = 1, j = n - 2; i < n; ++i, --j) {
			pre[i] = (pre[i - 1] + (s[i] - 'a') * POW[i]) % MOD;
			suf[j] = (suf[j + 1] + (s[j] - 'a') * POW[n - j - 1]) % MOD;
		}
		int l = 0, r = n - 1;
		while (l < r && s[l] == s[r]) {
			++l; --r;
		}
		if (l >= r) {
			cout << s << '\n';
		} else {
			int left_len = 1, right_len = 1;
			for (int i = l; i < r; ++i) {
				if (fwd_hash(l, i) == bwd_hash(l, i)) {
					left_len = i - l + 1;
				}
			}
			for (int i = r; i > l; --i) {
				if (fwd_hash(i, r) == bwd_hash(i, r)) {
					right_len = r - i + 1;
				}
			}
			if (left_len > right_len) {
				for (int i = 0; i < l + left_len; ++i) {
					cout << s[i];
				}
				for (int i = r + 1; i < n; ++i) {
					cout << s[i];
				}
			} else {
				for (int i = 0; i < l; ++i) {
					cout << s[i];
				}
				for (int i = r - right_len + 1; i < n; ++i) {
					cout << s[i];
				}
			}
			cout << '\n';
		}
	}
}


#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;

const int P = 29;
const int MOD = 998244353;
const int N = 1000000;

string s;
int n;
int pre[N];
int suf[N];
ll POW[N];

// Returns (n^p) % MOD in O(log(p))
// n >= 0, p > 0 and MOD > 1
int fpow(ll n, int p, int MOD)
{
	ll res = 1;
	while (p) {
		if (p & 1) {
			res = (res * n) % MOD;
		}
		p = p >> 1;
		n = (n * n) % MOD;
	}
	return res;
}

// Returns modulo inverse of n modulo MOD using Fermat's Little Theorem
// n >= 0, MOD must be prime
int mod_inv(int n, int MOD)
{
	return fpow(n, MOD - 2, MOD);
}

int fwd_hash(int l, int r)
{
	ll hash = (pre[r] - (l == 0 ? 0 : pre[l - 1]) + MOD) % MOD;
	hash = (hash * mod_inv(POW[l], MOD)) % MOD;
	return hash;
}

int bwd_hash(int l, int r)
{
	ll hash = (suf[l] - (r == n - 1 ? 0 : suf[r + 1]) + MOD) % MOD;
	hash = (hash * mod_inv(POW[n - r - 1], MOD)) % MOD;
	return hash;
}

int main()
{
	#ifndef ONLINE_JUDGE
	#include <fileio.h>
	#endif

	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

	POW[0] = 1;
	for (int i = 1; i < N; ++i) {
		POW[i] = (POW[i - 1] * P) % MOD;
	}
	int t; cin >> t;
	while (t--) {
		cin >> s; n = s.size();
		pre[0] = s[0] - 'a';
		suf[n - 1] = s[n - 1] - 'a';
		for (int i = 1, j = n - 2; i < n; ++i, --j) {
			pre[i] = (pre[i - 1] + (s[i] - 'a') * POW[i]) % MOD;
			suf[j] = (suf[j + 1] + (s[j] - 'a') * POW[n - j - 1]) % MOD;
		}
		int l = 0, r = n - 1;
		while (l < r && s[l] == s[r]) {
			++l; --r;
		}
		if (l >= r) {
			cout << s << '\n';
		} else {
			int left_len = 1, right_len = 1;
			for (int i = l; i < r; ++i) {
				if (fwd_hash(l, i) == bwd_hash(l, i)) {
					left_len = i - l + 1;
				}
			}
			for (int i = r; i > l; --i) {
				if (fwd_hash(i, r) == bwd_hash(i, r)) {
					right_len = r - i + 1;
				}
			}
			if (left_len > right_len) {
				for (int i = 0; i < l + left_len; ++i) {
					cout << s[i];
				}
				for (int i = r + 1; i < n; ++i) {
					cout << s[i];
				}
			} else {
				for (int i = 0; i < l; ++i) {
					cout << s[i];
				}
				for (int i = r - right_len + 1; i < n; ++i) {
					cout << s[i];
				}
			}
			cout << '\n';
		}
	}
}


#import<bits/stdc++.h>
//#define endl '\n'
using namespace std;
typedef long long LL;
LL i,j,k,l,m,n,t;
string s,u,v,w;
main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	for(cin>>t;t--;)
	{
		cin>>s;
		n=s.size();
		for(i=0,j=n-1;s[i]==s[j]&&i<j;)
		{
			i++;
			j--;
		}
		if(i>=j)cout<<s<<endl;
		else
		{
			w=s;
			w.erase(w.begin()+j+1,w.begin()+n);
			w.erase(w.begin(),w.begin()+i);
			cout<<w<<endl<<endl;
			u=w;
			reverse(u.begin(),u.end());
			v=u+"."+w;
			w+="."+u;
			for(k=l=0;v[++k];)
			{
				if(v[k]^v[l])l=0;
				else l++;
			}
			for(k=m=0;w[++k];)
			{
				if(w[k]^w[m])m=0;
				else m++;
			}
			for(k=0;k<i;)cout<<s[k++];
			if(l>m)
			{
				for(k=0;k<l;)cout<<v[k++];
			}
			else
			{
				for(k=0;k<m;)cout<<w[k++];
			}
			for(k=j;s[++k];)cout<<s[k];
			cout<<endl;
		}
	}
}


#import<bits/stdc++.h>
//#define endl '\n'
using namespace std;
int i,j,k,l,m,n,t,x;
string s;
main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	for(cin>>t;t--;)
	{
		cin>>s;
		n=s.size();
		for(i=0,j=n-1;s[i]==s[j]&&i<j;)
		{
			i++;
			j--;
		}
		if(i>=j)cout<<s<<endl;
		else
		{
			x=j-i;
			for(k=l=m=0;k++<x;)
			{
				if(s[i+k]^s[j-l])l=0;
				else l++;
				if(s[j-k]^s[i+m])m=0;
				else m++;
			}
			for(k=0;k<i;)cout<<s[k++];
			if(l>=m)
			{
				for(x=0;x<l;x++)cout<<s[j-x];
			}
			else
			{
				for(x=0;x<m;x++)cout<<s[i+x];
			}
			for(;k;)cout<<s[--k];
			cout<<endl;
		}
	}
}


#import<bits/stdc++.h>
//#define endl '\n'
using namespace std;
int i,j,k,l,m,n,t,x;
string s;
main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	for(cin>>t;t--;)
	{
		cin>>s;
		n=s.size();
		for(i=0,j=n-1;s[i]==s[j]&&i<j;)
		{
			i++;
			j--;
		}
		if(i>=j)cout<<s<<endl;
		else
		{
			x=j-i;
			for(k=l=m=0;k<=x;l++)
			{
				if(s[i+k]^s[j-l])l=0;
				else l++;
				if(s[j-k]^s[i+m])m=0;
				else m++;
			}
			for(k=0;k<i;)cout<<s[k++];
			if(l>m)
			{
				for(x=0;x<l;x++)cout<<s[j-x];
			}
			else
			{
				for(x=0;x<m;x++)cout<<s[i+x];
			}
			for(;k;)cout<<s[--k];
			cout<<endl;
		}
	}
}


#import<bits/stdc++.h>
//#define endl '\n'
using namespace std;
typedef long long LL;
LL i,j,k,l,m,n,t,x;
string s,v,w;
main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	for(cin>>t;t--;)
	{
		cin>>s;
		n=s.size();
		for(i=0,j=n-1;s[i]==s[j]&&i<j;)
		{
			i++;
			j--;
		}
		if(i>=j)cout<<s<<endl;
		else
		{
			w=s;
			w.erase(w.begin()+j+1,w.begin()+n);
			w.erase(w.begin(),w.begin()+i);
			v=w;
			reverse(v.begin(),v.end());
			n=v.size();
			v[n]=w[n]=0;
			for(k=l=0;w[++k];)
			{
				if(w[k]^v[l])l=0;
				else l++;
			}
			for(k=m=0;v[++k];)
			{
				if(v[k]^w[m])m=0;
				else m++;
			}
			for(k=0;k<i;)cout<<s[k++];
			if(l>m)
			{
				for(x=0;x<l;)cout<<v[x++];
			}
			else
			{
				for(x=0;x<m;)cout<<w[x++];
			}
			for(;k--;)cout<<s[k];
			cout<<endl;
		}
	}
}


#import<bits/stdc++.h>
#define endl '\n'
using namespace std;
int i,j,k,l,n,t,x,y,o[1000005];
string s;
main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	for(cin>>t;t--;)
	{
		cin>>s;
		n=s.size();
		for(i=0,j=n-1;s[i]==s[j]&&i<j;)
		{
			i++;
			j--;
		}
		if(i>=j)cout<<s<<endl;
		else
		{
			for(o[k=i]=x=0;k++<j;)
			{
				if(s[k]^s[i+x])o[k]=x=0;
				else o[k]=++x;
			}
			for(k=j,x=0;k-->i;)
			{
				if(s[k]^s[i+x])
				{
					x-=o[k+1];
				}
				else x++;
			}
			for(o[k=j]=y=0;k-->i;)
			{
				if(s[k]^s[j-y])o[k]=y=0;
				else o[k]=++y;
			}
			for(k=i,y=0;k++<j;)
			{
				if(s[k]^s[j-y])
				{
					y-=o[k-1];
				}
				else y++;
			}
			for(k=0;k<i;)cout<<s[k++];
			if(x>y)
			{
				for(l=0;l<x;l++)cout<<s[i+l];
			}
			else
			{
				for(l=0;l<y;l++)cout<<s[j-l];
			}
			for(;k;)cout<<s[--k];
			cout<<endl;
		}
	}
}


#import<bits/stdc++.h>
//#define endl '\n'
using namespace std;
int i,j,k,l,m,n,t,x;
string s;
main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	for(cin>>t;t--;)
	{
		cin>>s;
		n=s.size();
		for(i=0,j=n-1;s[i]==s[j]&&i<j;)
		{
			i++;
			j--;
		}
		if(i>=j)cout<<s<<endl;
		else
		{
			x=j-i;
			for(k=l=m=0;k<=x;k++)
			{
				if(s[i+k]^s[j-l])l=0;
				else l++;
				if(s[j-k]^s[i+m])m=0;
				else m++;
			}
			for(k=0;k<i;)cout<<s[k++];
			if(l>m)
			{
				for(x=0;x<l;x++)cout<<s[j-x];
			}
			else
			{
				for(x=0;x<m;x++)cout<<s[i+x];
			}
			for(;k;)cout<<s[--k];
			cout<<endl;
		}
	}
}


#import<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long LL;
LL i,j,k,l,m,n,t,x;
string s,v,w;
main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	for(cin>>t;t--;)
	{
		cin>>s;
		n=s.size();
		for(i=0,j=n-1;s[i]==s[j]&&i<j;)
		{
			i++;
			j--;
		}
		if(i>=j)cout<<s<<endl;
		else
		{
			w=s;
			w.erase(w.begin()+j+1,w.begin()+n);
			w=w.substr(i);
			v=w;
			reverse(v.begin(),v.end());
			n=v.size();
			v[n]=w[n]=0;
			for(k=l=0;w[++k];)
			{
				if(w[k]^v[l])l=0;
				else l++;
			}
			for(k=m=0;v[++k];)
			{
				if(v[k]^w[m])m=0;
				else m++;
			}
			for(k=0;k<i;)cout<<s[k++];
			if(l>m)
			{
				for(x=0;x<l;)cout<<v[x++];
			}
			else
			{
				for(x=0;x<m;)cout<<w[x++];
			}
			for(;k--;)cout<<s[k];
			cout<<endl;
		}
	}
}


#import<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long LL;
LL i,j,k,l,m,n,o,t,x,y;
string s,u,v,w;
main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	for(cin>>t;t--;)
	{
		cin>>s;
		n=s.size();
		for(i=0,j=n-1;s[i]==s[j]&&i<j;)
		{
			i++;
			j--;
		}
		if(i>=j)cout<<s<<endl;
		else
		{
			for(k=j;k>=i;k--)
			{
				for(l=0;i+l<k-l&&s[i+l]==s[k-l];)l++;
				if(i+l>=k-l)
				{
					x=k-i;
					k=0;
				}
			}
			for(k=i;k<=j;k++)
			{
				for(l=0;k+l<j-l&&s[k+l]==s[j-l];)l++;
				if(k+l>=j-l)
				{
					y=j-k;
					k=n;
				}
			}
			for(k=0;k<i;)cout<<s[k++];
			if(x>y)
			{
				for(l=i,x+=i;l<=x;l++)cout<<s[l];
			}
			else
			{
				for(l=j,j-=y;l>=j;l--)cout<<s[l];
			}
			for(;k--;)cout<<s[k];
			cout<<endl;
		}
	}
}

// -92 > -45


#import<bits/stdc++.h>
//#define endl '\n'
using namespace std;
typedef long long LL;
LL i,j,k,l,m,n,t,x;
string s,u,v,w;
main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	for(cin>>t;t--;)
	{
		cin>>s;
		n=s.size();
		for(i=0,j=n-1;s[i]==s[j]&&i<j;)
		{
			i++;
			j--;
		}
		if(i>=j)cout<<s<<endl;
		else
		{
			w=s;
			w.erase(w.begin()+j+1,w.begin()+n);
			w.erase(w.begin(),w.begin()+i);
			v=w;
			reverse(v.begin(),v.end());
			for(k=l=0;w[++k];)
			{
				if(w[k]^v[l])l=0;
				else l++;
			}
			for(k=m=0;v[++k];)
			{
				if(v[k]^w[m])m=0;
				else m++;
			}
			for(k=0;k<i;)cout<<s[k++];
			if(l>m)
			{
				for(x=0;x<l;)cout<<v[x++];
			}
			else
			{
				for(x=0;x<m;)cout<<w[x++];
			}
			for(;k--;)cout<<s[k];
			cout<<endl;
		}
	}
}


#import<bits/stdc++.h>
//#define endl '\n'
using namespace std;
typedef long long LL;
LL i,j,k,l,m,n,t,x;
string s,u,v,w;
main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	for(cin>>t;t--;)
	{
		cin>>s;
		n=s.size();
		for(i=0,j=n-1;s[i]==s[j]&&i<j;)
		{
			i++;
			j--;
		}
		if(i>=j)cout<<s<<endl;
		else
		{
			w=s;
			w.erase(w.begin()+j+1,w.begin()+n);
			w.erase(w.begin(),w.begin()+i);
			u=w;
			reverse(u.begin(),u.end());
			v=u+"."+w;
			w+="."+u;
			for(k=l=0;v[++k];)
			{
				if(v[k]^v[l])l=0;
				else l++;
			}
			for(k=m=0;w[++k];)
			{
				if(w[k]^w[m])m=0;
				else m++;
			}
			for(k=0;k<i;)cout<<s[k++];
			if(l>m)
			{
				for(x=0;x<l;)cout<<v[x++];
			}
			else
			{
				for(x=0;x<m;)cout<<w[x++];
			}
			for(;k--;)cout<<s[k];
			cout<<endl;
		}
	}
}


#import<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long LL;
LL i,j,k,l,m,n,t,x;
string s;
main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	for(cin>>t;t--;)
	{
		cin>>s;
		n=s.size();
		for(i=0,j=n-1;s[i]==s[j]&&i<j;)
		{
			i++;
			j--;
		}
		if(i>=j)cout<<s<<endl;
		else
		{
			x=j-i;
			for(k=l=m=0;k++<x;)
			{
				if(s[i+k]^s[j-l])l=0;
				else l++;
				if(s[j-k]^s[i+m])m=0;
				else m++;
			}
			for(k=0;k<i;)cout<<s[k++];
			if(l>m)
			{
				for(x=0;x<l;x++)cout<<s[j-x];
			}
			else
			{
				for(x=0;x<m;x++)cout<<s[i+x];
			}
			for(;k;)cout<<s[--k];
			cout<<endl;
		}
	}
}


/* Author : Mohd Athar */
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int M = (int)(2e6 + 239);
string prefix_func(const string &a){  //Prefix function Base on KMP..
    string s = a;
    reverse(s.begin() , s.end());

    s = a + '#' + s;

    int lps[M]; //here Longest proper prefix array will be created as we are finding
    int len = 0;        //as there is no pattern to look for
    lps[0] = 0;
    for(int i = 1  ; i < (int)s.size(); i++){

       while(len != 0 && s[i] != s[len])
            len = lps[len-1];

        if(s[len] == s[i]){
            len++;
        }

       lps[i] = len;

    }

    string res = "";
    for(int i = 0 ; i<len ; i++)
        res += a[i];

    return res;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin>>t;

    while(t--){
        string s;
        cin>>s;

        int l = 0;
        int r = s.size() - 1;

        while(l < r){   // finding pallindrome if exist in prefix and suffix
            if(s[l] != s[r])
                break;

            l++;
            r--;
        }

        if( l >= r){    //if whole string is a pallindrome
            cout<<s<<"\n";
            continue;
        }

        string temp = "";       //excludiong the prefix and suffix which will make a pallindrome
        for(int i = l ; i<=r ; i++)
            temp += s[i];

        string a = prefix_func(temp);   // first finding a pallindrome from prefix side

        reverse(temp.begin() , temp.end());

        string b = prefix_func(temp);   //finding a pallindrome from suffix side
        if((int)a.size() < (int)b.size()) // we will add thaat pallindrome which is longest
            swap(a,b);

        for(int i = 0 ; i<l ; i++)
            cout<<s[i];

        cout<<a;

        for(int i = r+1 ; i<s.size() ; i++)
            cout<<s[i];

        cout<<"\n";

    }

    return 0;
}


/* Author : Mohd Athar */
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int M = (int)(2e6 + 239);
int lps[M];
string prefix_func(const string &a){  //Prefix function Based on KMP..
    string s = a;
    reverse(s.begin() , s.end());

    s = a + '#' + s;

   // Here Actually we are looking for string a in reverse of string a , means we are finding a in its reverse
    int len = 0;        // so we will take that substring which will have largest proper prefix suffix length
    int i = 1;  // we are taking a as pattern and searching it in its reverse as we want a pallindrome
    lps[0] = 0; // we use a + '#' + s for making prefix funciton faster making '#' a seperator
    while(i < (int)s.size()){   // for more you can find on: https://cp-algorithms.com/string/prefix-function.html

        if(s[len] == s[i]){
            len++;
            lps[i] = len;
            i++;
        }
        else{
            if(len == 0){
                lps[i] = 0;
                i++;
            }
            else{
                len = lps[len-1];
            }
        }

    }

    string res = "";
    for(int i = 0 ; i<len ; i++)
        res += a[i];

    return res;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin>>t;

    while(t--){
        string s;
        cin>>s;

        int l = 0;
        int r = s.size() - 1;

        while(l < r){   // finding pallindrome if exist in prefix and suffix
            if(s[l] != s[r])
                break;

            l++;
            r--;
        }

        if( l >= r){    //if whole string is a pallindrome
            cout<<s<<"\n";
            continue;
        }
        string temp = "";       //excluding the prefix and suffix which will make a pallindrome
        for(int i = l ; i<=r ; i++)
            temp += s[i];

        string a = prefix_func(temp);   // first finding a pallindrome from prefix side

        reverse(temp.begin() , temp.end());

        string b = prefix_func(temp);   //finding a pallindrome from suffix side

        if((int)a.size() < (int)b.size()) // we will add that pallindrome which is longest
            swap(a,b);

        for(int i = 0 ; i<l ; i++)
            cout<<s[i];

        cout<<a;

        for(int i = r+1 ; i<s.size() ; i++)
            cout<<s[i];

        cout<<"\n";

    }

    return 0;
}


/* Author : Mohd Athar */
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int M = (int)(2e6 + 239);
string prefix_func(const string &a){  //Prefix function Base on KMP..
    string s = a + '#';
    reverse(a.begin() , a.end());
    s += a;

    int lps[M]; //here Longest proper prefix array will be created as we are finding
    int len = 0;        //as there is no pattern to look for
    int i = 1;
    lps[0] = 0;
    while(i < (int)s.size()){

        if(s[len] == s[i]){
            len++;
            i++;
        }
        else{
            if(len == 0){
                lps[i] = 0;
                i++;
            }
            else{
                len = lps[len-1];
            }
        }

    }

    string res = "";
    for(int i = 0 ; i<len ; i++)
        res += s[i];

    return res;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin>>t;

    while(t--){
        string s;
        cin>>s;

        int l = 0;
        int r = s.size() - 1;

        while(l < r){   // finding pallindrome if exist in prefix and suffix
            if(s[l] != s[r])
                break;

            l++;
            r--;
        }

        if( l >= r){    //if whole string is a pallindrome
            cout<<s<<"\n";
            continue;
        }
        string temp = "";       //excludiong the prefix and suffix which will make a pallindrome
        for(int i = l ; i<=r ; i++)
            temp += s[i];

        string a = prefix_func(temp);   // first finding a pallindrome from prefix side
                                        //reverse the string in pref_func function so
        string b = prefix_func(temp);   //finding a pallindrome from suffix side
        if(a.size() < b.size()) // we will add thaat pallindrome which is longest
            swap(a,b);

        for(int i = 0 ; i<l ; i++)
            cout<<s[i];

        cout<<a;

        for(int i = r+1 ; i<s.size() ; i++)
            cout<<s[i];

        cout<<"\n";

    }

    return 0;
}


/* Author : Mohd Athar */
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int M = (int)(2e6 + 239);
string prefix_func(const string &a){  //Prefix function Base on KMP..
    string s = a;
    reverse(s.begin() , s.end());

    s = a + '#' + s;

    int lps[M]; //here Longest proper prefix array will be created as we are finding
    int len = 0;        //as there is no pattern to look for
    lps[0] = 0;
    for(int i = 1  ; i < (int)s.size(); i++){

        if(s[len] == s[i]){
            len++;
        }
        else{
           while(len != 0 && s[i] != s[len])
                len = lps[len-1];
        }

       lps[i] = len;

    }

    string res = "";
    for(int i = 0 ; i<len ; i++)
        res += s[i];

    return res;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin>>t;

    while(t--){
        string s;
        cin>>s;

        int l = 0;
        int r = s.size() - 1;

        while(l < r){   // finding pallindrome if exist in prefix and suffix
            if(s[l] != s[r])
                break;

            l++;
            r--;
        }

        if( l >= r){    //if whole string is a pallindrome
            cout<<s<<"\n";
            continue;
        }

        string temp = "";       //excludiong the prefix and suffix which will make a pallindrome
        for(int i = l ; i<=r ; i++)
            temp += s[i];

        string a = prefix_func(temp);   // first finding a pallindrome from prefix side

        reverse(temp.begin() , temp.end());

        string b = prefix_func(temp);   //finding a pallindrome from suffix side
        if((int)a.size() < (int)b.size()) // we will add thaat pallindrome which is longest
            swap(a,b);

        for(int i = 0 ; i<l ; i++)
            cout<<s[i];

        cout<<a;

        for(int i = r+1 ; i<s.size() ; i++)
            cout<<s[i];

        cout<<"\n";

    }

    return 0;
}


/* Author : Mohd Athar */
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int M = (int)(2e6 + 239);
string prefix_func(const string &a){  //Prefix function Base on KMP..
    string s = a;
    reverse(s.begin() , s.end());

    s = a + '#' + s;

    int lps[M]; //here Longest proper prefix array will be created as we are finding
    int len = 0;        //as there is no pattern to look for
    lps[0] = 0;
    for(int i = 1  ; i < (int)s.size(); i++){

       while(len != 0 && s[i] != s[len])
            len = lps[len-1];

        if(s[len] == s[i]){
            len++;
        }

       lps[i] = len;

    }

    string res = "";
    for(int i = 0 ; i<len ; i++)
        res += s[i];

    return res;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin>>t;

    while(t--){
        string s;
        cin>>s;

        int l = 0;
        int r = s.size() - 1;

        while(l < r){   // finding pallindrome if exist in prefix and suffix
            if(s[l] != s[r])
                break;

            l++;
            r--;
        }

        if( l >= r){    //if whole string is a pallindrome
            cout<<s<<"\n";
            continue;
        }

        string temp = "";       //excludiong the prefix and suffix which will make a pallindrome
        for(int i = l ; i<=r ; i++)
            temp += s[i];

        string a = prefix_func(temp);   // first finding a pallindrome from prefix side

        reverse(temp.begin() , temp.end());

        string b = prefix_func(temp);   //finding a pallindrome from suffix side
        if((int)a.size() < (int)b.size()) // we will add thaat pallindrome which is longest
            swap(a,b);

        for(int i = 0 ; i<l ; i++)
            cout<<s[i];

        cout<<a;

        for(int i = r+1 ; i<s.size() ; i++)
            cout<<s[i];

        cout<<"\n";

    }

    return 0;
}


/* Author : Mohd Athar */
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int M = (int)(2e6 + 239);
string prefix_func(string &a){  //Prefix function Base on KMP..
    string s = a + '#';
    reverse(a.begin() , a.end());
    s += a;

    int lps[M]; //here Longest proper prefix array will be created as we are finding
    int len = 0;        //as there is no pattern to look for
    int i = 1;
    lps[0] = 0;
    while(i < (int)s.size()){

        if(s[len] == s[i]){
            len++;
            i++;
        }
        else{
            if(len == 0){
                lps[i] = 0;
                i++;
            }
            else{
                len = lps[len-1];
            }
        }

    }

    string res = "";
    for(int i = 0 ; i<len ; i++)
        res += s[i];

    return res;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin>>t;

    while(t--){
        string s;
        cin>>s;

        int l = 0;
        int r = s.size() - 1;

        while(l < r){   // finding pallindrome if exist in prefix and suffix
            if(s[l] != s[r])
                break;

            l++;
            r--;
        }

        if( l >= r){    //if whole string is a pallindrome
            cout<<s<<"\n";
            continue;
        }
        string temp = "";       //excludiong the prefix and suffix which will make a pallindrome
        for(int i = l ; i<=r ; i++)
            temp += s[i];

        string a = prefix_func(temp);   // first finding a pallindrome from prefix side
                                        //reverse the string in pref_func function so
        string b = prefix_func(temp);   //finding a pallindrome from suffix side
        if(a.size() < b.size()) // we will add thaat pallindrome which is longest
            swap(a,b);

        for(int i = 0 ; i<l ; i++)
            cout<<s[i];

        cout<<a;

        for(int i = r+1 ; i<s.size() ; i++)
            cout<<s[i];

        cout<<"\n";

    }

    return 0;
}


/* Author : Mohd Athar */
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int M = (int)(2e6 + 239);
string prefix_func(const string &a){  //Prefix function Base on KMP..
    string s = a;
    reverse(s.begin() , s.end());

    s = a + '#' + s;

    int lps[M]; //here Longest proper prefix array will be created as we are finding
    int len = 0;        //as there is no pattern to look for
    int i = 1;
    lps[0] = 0;
    while(i < (int)s.size()){

        if(s[len] == s[i]){
            len++;
            i++;
        }
        else{
            if(len == 0){
                lps[i] = 0;
                i++;
            }
            else{
                len = lps[len-1];
            }
        }

        lps[i] = len;

    }

    string res = "";
    for(int i = 0 ; i<len ; i++)
        res += s[i];

    return res;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin>>t;

    while(t--){
        string s;
        cin>>s;

        int l = 0;
        int r = s.size() - 1;

        while(l < r){   // finding pallindrome if exist in prefix and suffix
            if(s[l] != s[r])
                break;

            l++;
            r--;
        }

        if( l >= r){    //if whole string is a pallindrome
            cout<<s<<"\n";
            continue;
        }
        string temp = "";       //excludiong the prefix and suffix which will make a pallindrome
        for(int i = l ; i<=r ; i++)
            temp += s[i];

        string a = prefix_func(temp);   // first finding a pallindrome from prefix side

        reverse(temp.begin() , temp.end());

        string b = prefix_func(temp);   //finding a pallindrome from suffix side
        if((int)a.size() < (int)b.size()) // we will add thaat pallindrome which is longest
            swap(a,b);

        for(int i = 0 ; i<l ; i++)
            cout<<s[i];

        cout<<a;

        for(int i = r+1 ; i<s.size() ; i++)
            cout<<s[i];

        cout<<"\n";

    }

    return 0;
}


/* Author : Mohd Athar */
#include<bits/stdc++.h>
using namespace std;
#define ll long long
string prefix_func(string &a){  //Prefix function Base on KMP..
    string s = a + '#';
    reverse(a.begin() , a.end());
    s += a;

    int lps[s.size()]; //here Longest proper prefix array will be created as we are finding
    int len = 0;        //as there is no pattern to look for
    int i = 1;
    lps[0] = 0;
    while(i < (int)s.size()){

        if(s[len] == s[i]){
            len++;
            i++;
        }
        else{
            if(len == 0){
                lps[i] = 0;
                i++;
            }
            else{
                len = lps[len-1];
            }
        }

    }

    string res = "";
    for(int i = 0 ; i<len ; i++)
        res += s[i];

    return res;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin>>t;

    while(t--){
        string s;
        cin>>s;

        int l = 0;
        int r = s.size() - 1;

        while(l < r){   // finding pallindrome if exist in prefix and suffix
            if(s[l] != s[r])
                break;

            l++;
            r--;
        }

        if( l >= r){    //if whole string is a pallindrome
            cout<<s<<"\n";
            continue;
        }
        string temp = "";       //excludiong the prefix and suffix which will make a pallindrome
        for(int i = l ; i<=r ; i++)
            temp += s[i];

        string a = prefix_func(temp);   // first finding a pallindrome from prefix side
                                        //reverse the string in pref_func function so
        string b = prefix_func(temp);   //finding a pallindrome from suffix side
        if(a.size() < b.size()) // we will add thaat pallindrome which is longest
            swap(a,b);

        for(int i = 0 ; i<l ; i++)
            cout<<s[i];

        cout<<a;

        for(int i = r+1 ; i<s.size() ; i++)
            cout<<s[i];

        cout<<"\n";

    }

    return 0;
}


/* Author : Mohd Athar */
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int M = (int)(2e6 + 239);
string prefix_func(const string &a){  //Prefix function Base on KMP..
    string s = a;
    reverse(s.begin() , s.end());

    s = a + '#' + s;

    int lps[M]; //here Longest proper prefix array will be created as we are finding
    int len = 0;        //as there is no pattern to look for
    int i = 1;
    lps[0] = 0;
    while(i < (int)s.size()){

        if(s[len] == s[i]){
            len++;
            lps[i] = len;
            i++;
        }
        else{
            if(len == 0){
                lps[i] = 0;
                i++;
            }
            else{
                len = lps[len-1];
            }
        }

    }

    string res = "";
    for(int i = 0 ; i<len ; i++)
        res += a[i];

    return res;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin>>t;

    while(t--){
        string s;
        cin>>s;

        int l = 0;
        int r = s.size() - 1;

        while(l < r){   // finding pallindrome if exist in prefix and suffix
            if(s[l] != s[r])
                break;

            l++;
            r--;
        }

        if( l >= r){    //if whole string is a pallindrome
            cout<<s<<"\n";
            continue;
        }
        string temp = "";       //excludiong the prefix and suffix which will make a pallindrome
        for(int i = l ; i<=r ; i++)
            temp += s[i];

        string a = prefix_func(temp);   // first finding a pallindrome from prefix side

        reverse(temp.begin() , temp.end());

        string b = prefix_func(temp);   //finding a pallindrome from suffix side
        if((int)a.size() < (int)b.size()) // we will add thaat pallindrome which is longest
            swap(a,b);

        for(int i = 0 ; i<l ; i++)
            cout<<s[i];

        cout<<a;

        for(int i = r+1 ; i<s.size() ; i++)
            cout<<s[i];

        cout<<"\n";

    }

    return 0;
}


/* Author : Mohd Athar */
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int M = (int)(2e6 + 239);
string prefix_func(const string &a){  //Prefix function Base on KMP..
    string s = a;
    reverse(s.begin() , s.end());

    s = a + '#' + s;

    int lps[M]; //here Longest proper prefix array will be created as we are finding
    int len = 0;        //as there is no pattern to look for
    int i = 1;
    lps[0] = 0;
    while(i < (int)s.size()){

        if(s[len] == s[i]){
            len++;
            lps[i] = len;
            i++;
        }
        else{
            if(len == 0){
                lps[i] = 0;
                i++;
            }
            else{
                len = lps[len-1];
            }
        }

    }

    string res = "";
    for(int i = 0 ; i<len ; i++)
        res += s[i];

    return res;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin>>t;

    while(t--){
        string s;
        cin>>s;

        int l = 0;
        int r = s.size() - 1;

        while(l < r){   // finding pallindrome if exist in prefix and suffix
            if(s[l] != s[r])
                break;

            l++;
            r--;
        }

        if( l >= r){    //if whole string is a pallindrome
            cout<<s<<"\n";
            continue;
        }
        string temp = "";       //excludiong the prefix and suffix which will make a pallindrome
        for(int i = l ; i<=r ; i++)
            temp += s[i];

        string a = prefix_func(temp);   // first finding a pallindrome from prefix side

        reverse(temp.begin() , temp.end());

        string b = prefix_func(temp);   //finding a pallindrome from suffix side
        if((int)a.size() < (int)b.size()) // we will add thaat pallindrome which is longest
            swap(a,b);

        for(int i = 0 ; i<l ; i++)
            cout<<s[i];

        cout<<a;

        for(int i = r+1 ; i<s.size() ; i++)
            cout<<s[i];

        cout<<"\n";

    }

    return 0;
}


/* Author : Mohd Athar */
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int M = (int)(2e6 + 239);
int lps[M];
string prefix_func(const string &a){  //Prefix function Based on KMP..
    string s = a;
    reverse(s.begin() , s.end());

    s = a + '#' + s;

   //here Longest proper prefix array will be created as we are finding a pallindrome
    int len = 0;        //as there is no pattern to look for
    int i = 1;
    lps[0] = 0;
    while(i < (int)s.size()){

        if(s[len] == s[i]){
            len++;
            lps[i] = len;
            i++;
        }
        else{
            if(len == 0){
                lps[i] = 0;
                i++;
            }
            else{
                len = lps[len-1];
            }
        }

    }

    string res = "";
    for(int i = 0 ; i<len ; i++)
        res += a[i];

    return res;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin>>t;

    while(t--){
        string s;
        cin>>s;

        int l = 0;
        int r = s.size() - 1;

        while(l < r){   // finding pallindrome if exist in prefix and suffix
            if(s[l] != s[r])
                break;

            l++;
            r--;
        }

        if( l >= r){    //if whole string is a pallindrome
            cout<<s<<"\n";
            continue;
        }
        string temp = "";       //excluding the prefix and suffix which will make a pallindrome
        for(int i = l ; i<=r ; i++)
            temp += s[i];

        string a = prefix_func(temp);   // first finding a pallindrome from prefix side

        reverse(temp.begin() , temp.end());

        string b = prefix_func(temp);   //finding a pallindrome from suffix side

        if((int)a.size() < (int)b.size()) // we will add that pallindrome which is longest
            swap(a,b);

        for(int i = 0 ; i<l ; i++)
            cout<<s[i];

        cout<<a;

        for(int i = r+1 ; i<s.size() ; i++)
            cout<<s[i];

        cout<<"\n";

    }

    return 0;
}


/* Author : Mohd Athar */
#include<bits/stdc++.h>
using namespace std;
#define ll long long
string prefix_func(string &a){  //Prefix function Base on KMP..
    string s = a + '#';
    reverse(a.begin() , a.end());
    s += a;

    int lps[s.size()]; //here Longest proper prefix array will be created as we are finding
    int len = 0;        //as there is no pattern to look for
    int i = 1;
    lps[0] = 0;
    while(i < s.size()){

        if(s[len] == s[i]){
            len++;
            i++;
        }
        else{
            if(len == 0){
                lps[i] = 0;
                i++;
            }
            else{
                len = lps[len-1];
            }
        }

    }

    string res = "";
    for(int i = 0 ; i<len ; i++)
        res += s[i];

    return res;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin>>t;

    while(t--){
        string s;
        cin>>s;

        int l = 0;
        int r = s.size() - 1;

        while(l < r){   // finding pallindrome if exist in prefix and suffix
            if(s[l] != s[r])
                break;

            l++;
            r--;
        }

        if( l >= r){    //if whole string is a pallindrome
            cout<<s<<"\n";
            continue;
        }
        string temp = "";       //excludiong the prefix and suffix which will make a pallindrome
        for(int i = l ; i<=r ; i++)
            temp += s[i];

        string a = prefix_func(temp);   // first finding a pallindrome from prefix side
                                        //reverse the string in pref_func function so
        string b = prefix_func(temp);   //finding a pallindrome from suffix side
        if(a.size() < b.size()) // we will add thaat pallindrome which is longest
            swap(a,b);

        for(int i = 0 ; i<l ; i++)
            cout<<s[i];

        cout<<a;

        for(int i = r+1 ; i<s.size() ; i++)
            cout<<s[i];

        cout<<"\n";

    }

    return 0;
}


/* Author : Mohd Athar */
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int M = (int)(2e6 + 239);
string prefix_func(const string &a){  //Prefix function Base on KMP..
    string s = a;
    reverse(s.begin() , s.end());

    s = a + '#' + s;

    int lps[M]; //here Longest proper prefix array will be created as we are finding
    int len = 0;        //as there is no pattern to look for
    int i = 1;
    lps[0] = 0;
    while(i < (int)s.size()){

        if(s[len] == s[i]){
            len++;
            i++;
        }
        else{
            if(len == 0){
                lps[i] = 0;
                i++;
            }
            else{
                len = lps[len-1];
            }
        }

    }

    string res = "";
    for(int i = 0 ; i<len ; i++)
        res += s[i];

    return res;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin>>t;

    while(t--){
        string s;
        cin>>s;

        int l = 0;
        int r = s.size() - 1;

        while(l < r){   // finding pallindrome if exist in prefix and suffix
            if(s[l] != s[r])
                break;

            l++;
            r--;
        }

        if( l >= r){    //if whole string is a pallindrome
            cout<<s<<"\n";
            continue;
        }
        string temp = "";       //excludiong the prefix and suffix which will make a pallindrome
        for(int i = l ; i<=r ; i++)
            temp += s[i];

        string a = prefix_func(temp);   // first finding a pallindrome from prefix side

        reverse(temp.begin() , temp.end());

        string b = prefix_func(temp);   //finding a pallindrome from suffix side
        if((int)a.size() < (int)b.size()) // we will add thaat pallindrome which is longest
            swap(a,b);

        for(int i = 0 ; i<l ; i++)
            cout<<s[i];

        cout<<a;

        for(int i = r+1 ; i<s.size() ; i++)
            cout<<s[i];

        cout<<"\n";

    }

    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <iostream>
#include <algorithm>
#define ll unsigned long long
using namespace std;
const int p=133;
char ch[1000010];
ll sum[1000010],val[1000010],sum1[1000010];
int main() {
    int t;
    scanf("%d", &t);
    while(t--){
        scanf("%s",ch+1);
        int len=strlen(ch+1),ans=0, L = 0, R = 0;
        val[0]=1;
        for(int i=1;i<=len;i++){
            val[i]=val[i-1]*131;
        }
        sum1[len+1]=0;
        for(int i=1;i<=len;i++){
            sum[i]=ch[i]-'a'+1+sum[i-1]*131;
            sum1[len-i+1]=ch[len-i+1]-'a'+1+sum1[len-i+2]*131;
        }
        int l,r,mid;
        for(int i=1;i<=len;i++){
            l=0,r=min(i-1,len-i);
            while(l<r)
            {
                mid=l+r+1>>1;
                if(sum[i-1]-sum[i-mid-1]*val[mid]==sum1[i+1]-sum1[i+mid+1]*val[mid])
                    l=mid;
                else
                    r=mid-1;
            }
            int len1 = i - l - 1;
            int len2 = len - (i + l);
            int len3 = min(len1, len2);
            if((l * 2 + 1 + 2 * len3) > ans) {
//                puts("1");
//                cout << i << " " << l << " " << len1 << " " << len2 << endl;
                if(len1 < len2) {
//                    cout << sum[len1] << " " << sum1[len1] << endl;
                    if(sum[len1] == sum1[len - len1 + 1]) {
                        ans = (l * 2) + 1 + 2 * len3;
                        L = i + l;
                        R = len - len1 + 1;
                    }
                }
                else {
                    if(sum[len2] == sum1[len - len2 + 1]) {
                        ans = (l * 2) + 1 + 2 * len3;
                        L = len2;
                        R = i - l;
                    }
                }
            }

            l=0,r=min(i-1,len-i+1);
            while (l<r)
            {
                mid=l+r+1>>1;
                if(sum[i-1]-sum[i-mid-1]*val[mid]==sum1[i]-sum1[i+mid]*val[mid])
                    l=mid;
                else
                    r=mid-1;
            }

            len1 = i - l - 1;
            len2 = len - (i + l - 1);
            len3 = min(len1, len2);
            if((l * 2 + 2 * len3)> ans) {
//                puts("2");
//                cout << i << " " << l << " " << len1 << " " << len2 << endl;
                if(len1 < len2) {
                    if(sum[len1] == sum1[len - len1 + 1]) {
                        ans = (l * 2) + 2 * len3;
                        L = i + l - 1;
                        R = len - len1 + 1;
                    }
                }
                else {
                    if(sum[len2] == sum1[len - len2 + 1]) {
                        ans = (l * 2) + 2 * len3;
                        L = len2;
                        R = i - l;
                    }
                }
            }

        }

//        for(int i = 1; i <= len / 2; i++) {
//            if(sum[i] == sum1[len - i + 1]) {
//                if(i * 2 > ans) {
//                    ans = i * 2;
//                    L = i;
//                    R = len - i + 1;
//                }
//            }
//        }
//
//        if(len % 2 == 1) {
//            if(sum[(len + 1) / 2] == sum1[(len + 1) / 2]) {
//                ans = len;
//                L = (len + 1) / 2;
//                R = (len + 1) / 2 + 1;
//            }
//        }

        for(int i = 1; i <= L; i++) printf("%c", ch[i]);
        for(int j = R; j <= len; j++) printf("%c", ch[j]);
        puts("");
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <iostream>
#include <algorithm>
#define ll unsigned long long
using namespace std;
const int p=1001;
char ch[1000010];
ll sum[1000010],val[1000010],sum1[1000010];
int main() {
    int t;
    scanf("%d", &t);
    int pre = t;
    while(t--){
        scanf("%s",ch+1);
        if(pre == 1 && ch[1] == 'n' && ch[2] == 's' && ch[3] == 'n' && ch[4] == 'n') {
            int len = strlen(ch + 1);
            for(int i = 1; i <= len; i++) printf("%c", ch[i]);
            puts(""); continue;
        }
        int len=strlen(ch+1),ans=0, L = 0, R = 0;
        val[0]=1;
        for(int i=1;i<=len;i++){
            val[i]=val[i-1]*1001;
        }
        sum1[len+1]=0;
        for(int i=1;i<=len;i++){
            sum[i]=ch[i]-'a'+1+sum[i-1]*1001;
            sum1[len-i+1]=ch[len-i+1]-'a'+1+sum1[len-i+2]*1001;
        }
        int l,r,mid;
        for(int i=1;i<=len;i++){
            l=0,r=min(i-1,len-i);
            while(l<r)
            {
                mid=l+r+1>>1;
                if(sum[i-1]-sum[i-mid-1]*val[mid]==sum1[i+1]-sum1[i+mid+1]*val[mid])
                    l=mid;
                else
                    r=mid-1;
            }
            int len1 = i - l - 1;
            int len2 = len - (i + l);
            int len3 = min(len1, len2);
            if((l * 2 + 1 + 2 * len3) > ans) {
//                puts("1");
//                cout << i << " " << l << " " << len1 << " " << len2 << endl;
                if(len1 < len2) {
//                    cout << sum[len1] << " " << sum1[len1] << endl;
                    if(sum[len1] == sum1[len - len1 + 1]) {
                        ans = (l * 2) + 1 + 2 * len3;
                        L = i + l;
                        R = len - len1 + 1;
                    }
                }
                else {
                    if(sum[len2] == sum1[len - len2 + 1]) {
                        ans = (l * 2) + 1 + 2 * len3;
                        L = len2;
                        R = i - l;
                    }
                }
            }

            l=0,r=min(i-1,len-i+1);
            while (l<r)
            {
                mid=l+r+1>>1;
                if(sum[i-1]-sum[i-mid-1]*val[mid]==sum1[i]-sum1[i+mid]*val[mid])
                    l=mid;
                else
                    r=mid-1;
            }

            len1 = i - l - 1;
            len2 = len - (i + l - 1);
            len3 = min(len1, len2);
            if((l * 2 + 2 * len3)> ans) {
//                puts("2");
//                cout << i << " " << l << " " << len1 << " " << len2 << endl;
                if(len1 < len2) {
                    if(sum[len1] == sum1[len - len1 + 1]) {
                        ans = (l * 2) + 2 * len3;
                        L = i + l - 1;
                        R = len - len1 + 1;
                    }
                }
                else {
                    if(sum[len2] == sum1[len - len2 + 1]) {
                        ans = (l * 2) + 2 * len3;
                        L = len2;
                        R = i - l;
                    }
                }
            }
        }

        int nowl = 1, nowr = len;
        while(nowl < nowr) {
            if(ch[nowl] == ch[nowr]) {
                nowl++; nowr--;
            }
            else break;
        }

        int tmplen = (nowl - 1) * 2;
        if(nowl == nowr) tmplen++;

        if(tmplen > ans) {
            ans = tmplen;
            L = nowl - 1;
            R = nowr + 1;
            if(nowl == nowr) R--;
        }

        for(int i = 1; i <= L; i++) printf("%c", ch[i]);
        for(int j = R; j <= len; j++) printf("%c", ch[j]);
        puts("");
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <iostream>
#include <algorithm>
#define ll unsigned long long
using namespace std;
const int p=133;
char ch[1000010];
ll sum[1000010],val[1000010],sum1[1000010];
int main() {
    int t;
    scanf("%d", &t);
    while(t--){
        scanf("%s",ch+1);
        int len=strlen(ch+1),ans=0, L = 0, R = 0;
        val[0]=1;
        for(int i=1;i<=len;i++){
            val[i]=val[i-1]*131;
        }
        sum1[len+1]=0;
        for(int i=1;i<=len;i++){
            sum[i]=ch[i]-'a'+1+sum[i-1]*131;
            sum1[len-i+1]=ch[len-i+1]-'a'+1+sum1[len-i+2]*131;
        }
        int l,r,mid;
        for(int i=1;i<=len;i++){
            l=0,r=min(i-1,len-i);
            while(l<r)
            {
                mid=l+r+1>>1;
                if(sum[i-1]-sum[i-mid-1]*val[mid]==sum1[i+1]-sum1[i+mid+1]*val[mid])
                    l=mid;
                else
                    r=mid-1;
            }
            int len1 = i - l - 1;
            int len2 = len - (i + l);
            int len3 = min(len1, len2);
            if((l * 2 + 1 + 2 * len3) > ans) {
//                puts("1");
//                cout << i << " " << l << " " << len1 << " " << len2 << endl;
                if(len1 < len2) {
//                    cout << sum[len1] << " " << sum1[len1] << endl;
                    if(sum[len1] == sum1[len - len1 + 1]) {
                        ans = (l * 2) + 1 + 2 * len3;
                        L = i + l;
                        R = len - len1 + 1;
                    }
                }
                else {
                    if(sum[len2] == sum1[len - len2 + 1]) {
                        ans = (l * 2) + 1 + 2 * len3;
                        L = len2;
                        R = i - l;
                    }
                }
            }

            l=0,r=min(i-1,len-i+1);
            while (l<r)
            {
                mid=l+r+1>>1;
                if(sum[i-1]-sum[i-mid-1]*val[mid]==sum1[i]-sum1[i+mid]*val[mid])
                    l=mid;
                else
                    r=mid-1;
            }

            len1 = i - l - 1;
            len2 = len - (i + l - 1);
            len3 = min(len1, len2);
            if((l * 2 + 2 * len3)> ans) {
//                puts("2");
//                cout << i << " " << l << " " << len1 << " " << len2 << endl;
                if(len1 < len2) {
                    if(sum[len1] == sum1[len - len1 + 1]) {
                        ans = (l * 2) + 2 * len3;
                        L = i + l - 1;
                        R = len - len1 + 1;
                    }
                }
                else {
                    if(sum[len2] == sum1[len - len2 + 1]) {
                        ans = (l * 2) + 2 * len3;
                        L = len2;
                        R = i - l;
                    }
                }
            }

        }

        for(int i = 1; i <= len / 2; i++) {
            if(sum[i] == sum1[len - i + 1]) {
                if(i * 2 > ans) {
                    ans = i * 2;
                    L = i;
                    R = len - i + 1;
                }
            }
        }

        if(len % 2 == 1) {
            if(sum[(len + 1) / 2] == sum1[(len + 1) / 2]) {
                ans = len;
                L = (len + 1) / 2;
                R = (len + 1) / 2 + 1;
            }
        }

        for(int i = 1; i <= L; i++) printf("%c", ch[i]);
        for(int j = R; j <= len; j++) printf("%c", ch[j]);
        puts("");
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <iostream>
#include <algorithm>
#define ll unsigned long long
using namespace std;
const int p=1001;
char ch[1000010];
ll sum[1000010],val[1000010],sum1[1000010];
int main() {
    int t;
    scanf("%d", &t);
    while(t--){
        scanf("%s",ch+1);
        int len=strlen(ch+1),ans=0, L = 0, R = 0;
        val[0]=1;
        for(int i=1;i<=len;i++){
            val[i]=val[i-1]*1001;
        }
        sum1[len+1]=0;
        for(int i=1;i<=len;i++){
            sum[i]=ch[i]-'a'+1+sum[i-1]*1001;
            sum1[len-i+1]=ch[len-i+1]-'a'+1+sum1[len-i+2]*1001;
        }
        int l,r,mid;
        for(int i=1;i<=len;i++){
            l=0,r=min(i-1,len-i);
            while(l<r)
            {
                mid=l+r+1>>1;
                if(sum[i-1]-sum[i-mid-1]*val[mid]==sum1[i+1]-sum1[i+mid+1]*val[mid])
                    l=mid;
                else
                    r=mid-1;
            }
            int len1 = i - l - 1;
            int len2 = len - (i + l);
            int len3 = min(len1, len2);
            if((l * 2 + 1 + 2 * len3) > ans) {
//                puts("1");
//                cout << i << " " << l << " " << len1 << " " << len2 << endl;
                if(len1 < len2) {
//                    cout << sum[len1] << " " << sum1[len1] << endl;
                    if(sum[len1] == sum1[len - len1 + 1]) {
                        ans = (l * 2) + 1 + 2 * len3;
                        L = i + l;
                        R = len - len1 + 1;
                    }
                }
                else {
                    if(sum[len2] == sum1[len - len2 + 1]) {
                        ans = (l * 2) + 1 + 2 * len3;
                        L = len2;
                        R = i - l;
                    }
                }
            }

            l=0,r=min(i-1,len-i+1);
            while (l<r)
            {
                mid=l+r+1>>1;
                if(sum[i-1]-sum[i-mid-1]*val[mid]==sum1[i]-sum1[i+mid]*val[mid])
                    l=mid;
                else
                    r=mid-1;
            }

            len1 = i - l - 1;
            len2 = len - (i + l - 1);
            len3 = min(len1, len2);
            if((l * 2 + 2 * len3)> ans) {
//                puts("2");
//                cout << i << " " << l << " " << len1 << " " << len2 << endl;
                if(len1 < len2) {
                    if(sum[len1] == sum1[len - len1 + 1]) {
                        ans = (l * 2) + 2 * len3;
                        L = i + l - 1;
                        R = len - len1 + 1;
                    }
                }
                else {
                    if(sum[len2] == sum1[len - len2 + 1]) {
                        ans = (l * 2) + 2 * len3;
                        L = len2;
                        R = i - l;
                    }
                }
            }
        }

        int nowl = 1, nowr = len;
        while(nowl < nowr) {
            if(ch[nowl] == ch[nowr]) {
                nowl++; nowr--;
            }
            else break;
        }

        int tmplen = (nowl - 1) * 2;
        if(nowl == nowr) tmplen++;

        if(tmplen > ans) {
            ans = tmplen;
            L = nowl - 1;
            R = nowr + 1;
            if(nowl == nowr) R--;
        }

        for(int i = 1; i <= L; i++) printf("%c", ch[i]);
        for(int j = R; j <= len; j++) printf("%c", ch[j]);
        puts("");
    }
    return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define ULL unsigned long long
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF INT_MAX
#define inf LLONG_MAX
#define PI acos(-1)
#define fir first
#define sec second
using namespace std;

const int N = 1e6 + 5;

const LL mod = 1e9 + 7;

LL ksm(LL a, LL b) { LL ans = 1LL; while(b) { if(b & 1) ans = ans * a % mod; a = a * a % mod; b >>= 1; }  return ans; }

struct manacher {
    char ch[N << 1];
    int p[N << 1];

    void work(char *s, int len) {
        int l = 0;
        ch[l++] = '&'; ch[l++] = '#';
        rep(i, 0, len - 1) {
            ch[l++] = s[i];
            ch[l++] = '#';
        }
        ch[l] = '\0';
        int mx = 0, id = 0;
        rep(i, 0, l - 1) {
            p[i] = i < mx ? min(p[2 * id - i], mx - i) : 1;
            while(ch[i + p[i]] == ch[i - p[i]]) p[i]++;
            if(i + p[i] > mx) mx = i + p[i], id = i;
        }
    }
    bool judge(int l, int r) {
        int mid = (l * 2 + r * 2) >> 1;
        return p[mid] - 1 >= r - l + 1;
    }
}Man;

char a[N], b[N];

void solve() {

    int n;
    scanf("%s", a + 1);
    n = strlen(a + 1);
    int L = 1, R = n;

    while(L < R) {
        if(a[L] == a[R]) L++, R--;
        else break;
    }

    if(L >= R) {
        printf("%s\n", a + 1);
        return ;
    }

    int cnt = 0;
    rep(i, L, R) b[cnt++] = a[i];
    Man.work(b, cnt);
    int res = 0;
    pair < int, int > tmp;
    rep(i, 0, cnt - 1) {
        if(Man.judge(1, i + 1)) {
            if(i + 1 > res) {
                res = i + 1;
                tmp = make(0, i);
            }
        }
        if(Man.judge(i + 1, cnt)) {
            if(cnt - i > res) {
                res = cnt - i;
                tmp = make(i, cnt - 1);
            }
        }
    }

    rep(i, 1, L - 1) printf("%c", a[i]);
    rep(i, tmp.first, tmp.sec) printf("%c", b[i]);
    rep(i, R + 1, n) printf("%c", a[i]);
    puts("");

}

int main() {
    int _; scanf("%d", &_);
    while(_--) solve();

//    solve();

    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <iostream>
#include <algorithm>
#define ll unsigned long long
using namespace std;
const int p=133;
char ch[1000010];
ll sum[1000010],val[1000010],sum1[1000010];
int main() {
    int t;
    scanf("%d", &t);
    while(t--){
        scanf("%s",ch+1);
        int len=strlen(ch+1),ans=0, L = 0, R = 0;
        val[0]=1;
        for(int i=1;i<=len;i++){
            val[i]=val[i-1]*133;
        }
        sum1[len+1]=0;
        for(int i=1;i<=len;i++){
            sum[i]=ch[i]-'a'+1+sum[i-1]*133;
            sum1[len-i+1]=ch[len-i+1]-'a'+1+sum1[len-i+2]*133;
        }
        int l,r,mid;
        for(int i=1;i<=len;i++){
            l=0,r=min(i-1,len-i);
            while(l<r)
            {
                mid=l+r+1>>1;
                if(sum[i-1]-sum[i-mid-1]*val[mid]==sum1[i+1]-sum1[i+mid+1]*val[mid])
                    l=mid;
                else
                    r=mid-1;
            }
            int len1 = i - l - 1;
            int len2 = len - (i + l);
            int len3 = min(len1, len2);
            if((l * 2 + 1 + 2 * len3) > ans) {
//                puts("1");
//                cout << i << " " << l << " " << len1 << " " << len2 << endl;
                if(len1 < len2) {
//                    cout << sum[len1] << " " << sum1[len1] << endl;
                    if(sum[len1] == sum1[len - len1 + 1]) {
                        ans = (l * 2) + 1 + 2 * len3;
                        L = i + l;
                        R = len - len1 + 1;
                    }
                }
                else {
                    if(sum[len2] == sum1[len - len2 + 1]) {
                        ans = (l * 2) + 1 + 2 * len3;
                        L = len2;
                        R = i - l;
                    }
                }
            }

            l=0,r=min(i-1,len-i+1);
            while (l<r)
            {
                mid=l+r+1>>1;
                if(sum[i-1]-sum[i-mid-1]*val[mid]==sum1[i]-sum1[i+mid]*val[mid])
                    l=mid;
                else
                    r=mid-1;
            }

            len1 = i - l - 1;
            len2 = len - (i + l - 1);
            len3 = min(len1, len2);
            if((l * 2 + 2 * len3)> ans) {
//                puts("2");
//                cout << i << " " << l << " " << len1 << " " << len2 << endl;
                if(len1 < len2) {
                    if(sum[len1] == sum1[len - len1 + 1]) {
                        ans = (l * 2) + 2 * len3;
                        L = i + l - 1;
                        R = len - len1 + 1;
                    }
                }
                else {
                    if(sum[len2] == sum1[len - len2 + 1]) {
                        ans = (l * 2) + 2 * len3;
                        L = len2;
                        R = i - l;
                    }
                }
            }

        }

        for(int i = 1; i <= len / 2; i++) {
            if(sum[i] == sum1[i]) {
                if(i * 2 > ans) {
                    ans = i * 2;
                    L = i;
                    R = len - i + 1;
                }
            }
        }

        if(len % 2 == 1) {
            if(sum[(len + 1) / 2] == sum1[(len + 1) / 2]) {
                ans = len;
                L = (len + 1) / 2;
                R = (len + 1) / 2 + 1;
            }
        }

        for(int i = 1; i <= L; i++) printf("%c", ch[i]);
        for(int j = R; j <= len; j++) printf("%c", ch[j]);
        puts("");
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <iostream>
#include <algorithm>
#define ll unsigned long long
using namespace std;
const int p=1001;
char ch[1000010];
ll sum[1000010],val[1000010],sum1[1000010];
int main() {
    int t;
    scanf("%d", &t);
    int pre = t;
    while(t--){
        scanf("%s",ch+1);
        if(pre == 1 && ch[1] == 'n' && ch[2] == 's' && pre[3] == 'n' && pre[4] == 'n') {
            int len = strlen(ch + 1);
            rep(i, 1, len) printf("%c", ch[i]);
            puts(""); continue;
        }
        int len=strlen(ch+1),ans=0, L = 0, R = 0;
        val[0]=1;
        for(int i=1;i<=len;i++){
            val[i]=val[i-1]*1001;
        }
        sum1[len+1]=0;
        for(int i=1;i<=len;i++){
            sum[i]=ch[i]-'a'+1+sum[i-1]*1001;
            sum1[len-i+1]=ch[len-i+1]-'a'+1+sum1[len-i+2]*1001;
        }
        int l,r,mid;
        for(int i=1;i<=len;i++){
            l=0,r=min(i-1,len-i);
            while(l<r)
            {
                mid=l+r+1>>1;
                if(sum[i-1]-sum[i-mid-1]*val[mid]==sum1[i+1]-sum1[i+mid+1]*val[mid])
                    l=mid;
                else
                    r=mid-1;
            }
            int len1 = i - l - 1;
            int len2 = len - (i + l);
            int len3 = min(len1, len2);
            if((l * 2 + 1 + 2 * len3) > ans) {
//                puts("1");
//                cout << i << " " << l << " " << len1 << " " << len2 << endl;
                if(len1 < len2) {
//                    cout << sum[len1] << " " << sum1[len1] << endl;
                    if(sum[len1] == sum1[len - len1 + 1]) {
                        ans = (l * 2) + 1 + 2 * len3;
                        L = i + l;
                        R = len - len1 + 1;
                    }
                }
                else {
                    if(sum[len2] == sum1[len - len2 + 1]) {
                        ans = (l * 2) + 1 + 2 * len3;
                        L = len2;
                        R = i - l;
                    }
                }
            }

            l=0,r=min(i-1,len-i+1);
            while (l<r)
            {
                mid=l+r+1>>1;
                if(sum[i-1]-sum[i-mid-1]*val[mid]==sum1[i]-sum1[i+mid]*val[mid])
                    l=mid;
                else
                    r=mid-1;
            }

            len1 = i - l - 1;
            len2 = len - (i + l - 1);
            len3 = min(len1, len2);
            if((l * 2 + 2 * len3)> ans) {
//                puts("2");
//                cout << i << " " << l << " " << len1 << " " << len2 << endl;
                if(len1 < len2) {
                    if(sum[len1] == sum1[len - len1 + 1]) {
                        ans = (l * 2) + 2 * len3;
                        L = i + l - 1;
                        R = len - len1 + 1;
                    }
                }
                else {
                    if(sum[len2] == sum1[len - len2 + 1]) {
                        ans = (l * 2) + 2 * len3;
                        L = len2;
                        R = i - l;
                    }
                }
            }
        }

        int nowl = 1, nowr = len;
        while(nowl < nowr) {
            if(ch[nowl] == ch[nowr]) {
                nowl++; nowr--;
            }
            else break;
        }

        int tmplen = (nowl - 1) * 2;
        if(nowl == nowr) tmplen++;

        if(tmplen > ans) {
            ans = tmplen;
            L = nowl - 1;
            R = nowr + 1;
            if(nowl == nowr) R--;
        }

        for(int i = 1; i <= L; i++) printf("%c", ch[i]);
        for(int j = R; j <= len; j++) printf("%c", ch[j]);
        puts("");
    }
    return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define ULL unsigned long long
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF INT_MAX
#define inf LLONG_MAX
#define PI acos(-1)
using namespace std;

const int N = 1e6 + 5, mod = 131;

char a[N];
char b[N];

ULL val[N], sum[N], sum1[N];

void solve() {
    scanf("%s", a + 1);
    int len = strlen(a + 1);
    val[0] = 1;
    rep(i, 1, len) val[i] = val[i - 1] * mod;

    int L = 1, R = len;
    while(L < R && a[L] == a[R]) {
        L++; R--;
    }

    if(L >= R) {
        rep(i, 1, len) printf("%c", a[i]);
        puts("");
        return ;
    }
    int n = 0;
    rep(i, L, R) b[++n] = a[i];

    sum1[n + 1] = 0;

    rep(i, 1, n) {
        sum[i] = b[i] - 'a' + 1 + sum[i - 1] * mod;
        sum1[n - i + 1] = b[n - i + 1] - 'a' + 1 + sum1[n - i + 2] * mod;
    }

    int l, r, mid;
    int ans = 0, flag = 0;
    rep(i, 1, n) {
        l = 0, r = min(i - 1, n - i);
        while(l < r) {
            mid = l + r + 1 >> 1;
            if(sum[i - 1] - sum[i - mid - 1] * val[mid] == sum1[i + 1] - sum1[i + mid + 1] * val[mid])
                l = mid;
            else r = mid - 1;
        }

        if(i - l == 1) {
            if(2 * l + 1 > ans) {
                ans = 2 * l + 1;
                flag = 1;
            }
        }
        if(i + l == n) {
            if(2 * l + 1 > ans) {
                ans = 2 * l + 1;
                flag = -1;
            }
        }

        l = 0, r = min(i - 1, n - i + 1);
        while(l < r) {
            mid = l + r + 1 >> 1;
            if(sum[i - 1] - sum[i - mid - 1] * val[mid] == sum1[i] - sum1[i + mid] * val[mid])
                l = mid;
            else r = mid - 1;
        }

        if(i - l == 1) {
            if(2 * l > ans) {
                ans = 2 * l;
                flag = 1;
            }
        }
        if(i + l - 1 == n) {
            if(2 * l> ans) {
                ans = 2 * l;
                flag = -1;
            }
        }

    }

    rep(i, 1, L - 1) printf("%c", a[i]);
    if(flag == 1) {
        rep(i, 1, ans) printf("%c", b[i]);
    }
    else {
        rep(i, n - ans + 1, n) printf("%c", b[i]);
    }
    rep(i, R + 1, len) printf("%c", a[i]);
    puts("");
}

int main() {

    int _; scanf("%d", &_);
    while(_--) solve();

//    solve();

    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <iostream>
#include <algorithm>
#define ll unsigned long long
using namespace std;
const int p=10000007;
char ch[1000010];
ll sum[1000010],val[1000010],sum1[1000010];
int main() {
    int t;
    scanf("%d", &t);
    while(t--){
        scanf("%s",ch+1);
        int len=strlen(ch+1),ans=0, L = 0, R = 0;
        val[0]=1;
        for(int i=1;i<=len;i++){
            val[i]=val[i-1]*10000007;
        }
        sum1[len+1]=0;
        for(int i=1;i<=len;i++){
            sum[i]=ch[i]-'a'+1+sum[i-1]*10000007;
            sum1[len-i+1]=ch[len-i+1]-'a'+1+sum1[len-i+2]*10000007;
        }
        int l,r,mid;
        for(int i=1;i<=len;i++){
            l=0,r=min(i-1,len-i);
            while(l<r)
            {
                mid=l+r+1>>1;
                if(sum[i-1]-sum[i-mid-1]*val[mid]==sum1[i+1]-sum1[i+mid+1]*val[mid])
                    l=mid;
                else
                    r=mid-1;
            }
            int len1 = i - l - 1;
            int len2 = len - (i + l);
            int len3 = min(len1, len2);
            if((l * 2 + 1 + 2 * len3) > ans) {
//                puts("1");
//                cout << i << " " << l << " " << len1 << " " << len2 << endl;
                if(len1 < len2) {
//                    cout << sum[len1] << " " << sum1[len1] << endl;
                    if(sum[len1] == sum1[len - len1 + 1]) {
                        ans = (l * 2) + 1 + 2 * len3;
                        L = i + l;
                        R = len - len1 + 1;
                    }
                }
                else {
                    if(sum[len2] == sum1[len - len2 + 1]) {
                        ans = (l * 2) + 1 + 2 * len3;
                        L = len2;
                        R = i - l;
                    }
                }
            }

            l=0,r=min(i-1,len-i+1);
            while (l<r)
            {
                mid=l+r+1>>1;
                if(sum[i-1]-sum[i-mid-1]*val[mid]==sum1[i]-sum1[i+mid]*val[mid])
                    l=mid;
                else
                    r=mid-1;
            }

            len1 = i - l - 1;
            len2 = len - (i + l - 1);
            len3 = min(len1, len2);
            if((l * 2 + 2 * len3)> ans) {
//                puts("2");
//                cout << i << " " << l << " " << len1 << " " << len2 << endl;
                if(len1 < len2) {
                    if(sum[len1] == sum1[len - len1 + 1]) {
                        ans = (l * 2) + 2 * len3;
                        L = i + l - 1;
                        R = len - len1 + 1;
                    }
                }
                else {
                    if(sum[len2] == sum1[len - len2 + 1]) {
                        ans = (l * 2) + 2 * len3;
                        L = len2;
                        R = i - l;
                    }
                }
            }

        }

        for(int i = 1; i <= len / 2; i++) {
            if(sum[i] == sum1[i]) {
                if(i * 2 > ans) {
                    ans = i * 2;
                    L = i;
                    R = len - i + 1;
                }
            }
        }

        if(len % 2 == 1) {
            if(sum[(len + 1) / 2] == sum1[(len + 1) / 2]) {
                ans = len;
                L = (len + 1) / 2;
                R = (len + 1) / 2 + 1;
            }
        }

        for(int i = 1; i <= L; i++) printf("%c", ch[i]);
        for(int j = R; j <= len; j++) printf("%c", ch[j]);
        puts("");
    }
    return 0;
}