// Problem : D1. Prefix-Suffix Palindrome (Easy version)
// Contest : Codeforces Global Round 7
// URL : https://codeforces.com/contest/1326/problem/D1
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>

using namespace std;

long long hshf[2][1000005], hshr[2][1000005];
long long sd[2] = {1331, 29};
vector<long long> wesleysasshole = {1319, 4073,7841,9181,8017};
long long pows[2][1000005];
const long long MOD = 1e9+7;

mt19937 g1(chrono::steady_clock::now().time_since_epoch().count());

int randint(int a, int b){return uniform_int_distribution<int>(a, b)(g1);}

pair<long long, long long> gethsh(int l, int r, bool rev){
	if(rev){
		long long h1 = ((hshr[0][l] - hshr[0][r+1] * pows[0][r-l+1])%MOD+MOD)%MOD;
		long long h2 = ((hshr[1][l] - hshr[1][r+1] * pows[1][r-l+1])%MOD+MOD)%MOD;
		return {h1, h2};
	}
	else{
		long long h1 = ((hshf[0][r]-hshf[0][l-1]*pows[0][r-l+1])%MOD+MOD)%MOD;
		long long h2 = ((hshf[1][r]-hshf[1][l-1]*pows[1][r-l+1])%MOD+MOD)%MOD;
		return {h1, h2};
	}
}

int main(){
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int T;
	cin >> T;
	while(T--){
		/*
		for(int i = 0; i<rand()%97; i++){
			sd[0] ^= rand();
		}
		sd[0] %= wesleysasshole.size();
		sd[0] = wesleysasshole[sd[0]];
		int smalldickwesley = rand();
		if(smalldickwesley >= 29){
			wesleysasshole.push_back(smalldickwesley);
		}
		*/
		int n = randint(30, 300000);
		bool isprime = 1;
		for(int p = 2; p<=sqrt(n); p++){
			if(n%p == 0){
				isprime = 0;
			}
		}
		if(isprime){
			wesleysasshole.push_back(n);
		}
		sd[0] = wesleysasshole[randint(0, 300000)%wesleysasshole.size()];
		sd[1] = wesleysasshole[randint(0, 300000)%wesleysasshole.size()];
		string s;
		cin >> s;
		bool p = 1;
		int idx = -1;
		for(int i = 0; i<s.size(); i++){
			if(s[i] != s[s.size()-i-1]){
				p = 0;
			}
			if(p){
				idx = i;
				//cout << i << " " << idx << "\n";
			}
		}
		int N = s.size();
		if(!p){
			pows[0][0] = pows[1][0] = 1;
			for(int i = 1; i<=N; i++){
				hshf[0][i] = hshf[0][i-1] * sd[0] + s[i-1] - 'a' + 1;
				hshf[1][i] = hshf[1][i-1] * sd[1] + s[i-1] - 'a' + 1;
				hshf[0][i] %= MOD;
				hshf[1][i] %= MOD;
				pows[0][i] = pows[0][i-1] * sd[0] %MOD;
				pows[1][i] = pows[1][i-1] * sd[1] %MOD;
			}
			hshr[0][N+1] = hshr[1][N+1] = 0;
			for(int i = N; i; i--){
				hshr[0][i] = hshr[0][i+1] * sd[0] + s[i-1] - 'a' + 1;
				hshr[1][i] = hshr[1][i+1] * sd[1] + s[i-1] - 'a' + 1;
				hshr[0][i] %= MOD;
				hshr[1][i] %= MOD;
			}
			idx++;
			//cout << idx << "\n";
			int l = 0, r = -1;
			for(int i = idx+1; i<=N-idx; i++){
				if(gethsh(idx+1, i, 0) == gethsh(idx+1, i, 1)){
					l = idx+1, r = i;
				}
			}
			for(int i = idx+1; i<=N-idx; i++){
				//cout << i << " " << N-idx << " " << gethsh(i, N-idx, 0).first << " " << gethsh(i, N-idx, 1).first << "\n";
				if(gethsh(i, N-idx, 0) == gethsh(i, N-idx, 1) && N-idx-i > r-l){
					l = i, r = N-idx;
				}
			}
			//cout << l << " " << r << "\n";
			for(int i = 0; i<=idx-1; i++){
				cout << s[i];
			}
			for(int i = l-1; i<r; i++){
				cout << s[i];
			}
			for(int i = idx-1; i>=0; i--){
				cout << s[i];
			}
			cout << "\n";
		}
		else{
			cout << s << "\n";
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 50;
int p[maxn << 1];
void Manachar(string &s) {
    memset(p, 0, sizeof(p));
    string t = "$#";
    int len = s.size();
    for (int i = 0; i < len; ++i) {
        t += s[i];
        t += '#';
    }
    len = t.size();
    int mx = 0, id = 0, rcen = 0, rlen = 0, ans = 0;
    for (int i = 0; i < len; ++i) {
        p[i] = mx > i ? min(p[2 * id - i], mx - i) : 1;
        while (t[i - p[i]] == t[i + p[i]]) p[i]++;
        if (p[i] + i > mx) {
            mx = p[i] + i;
            id = i;
        }
        if (rlen < p[i]) {
            int start = (i - p[i]) / 2, end = start + p[i] - 1;
            if (start == 0 || end == s.size()) {
                rlen = p[i];
                rcen = i;
            }
        }
    }
    int start = (rcen - rlen) / 2, end = start + rlen - 1;
    for (int i = start; i < end; i++) {
        cout << s[i];
    }
}
void solve() {
    string s;
    cin >> s;
    int len = s.size(), f = 0;
    for (int i = 0; i < len; ++i) {
        if (s[i] != s[len - 1 - i]) {
            f = i;
            break;
        }
        if (i == len / 2) {
            cout << s << "\n";
            return;
        }
    }
    for (int i = 0; i < f; i++) {
        cout << s[i];
    }
    string temp = s.substr(f, len - 2 * f);
    Manachar(temp);
    for (int i = f + len - 2 * f; i < len; i++) {
        cout << s[i];
    }
    cout << "\n";
    return ;
}
int main() {
    std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 1e6 + 5;
const int INF = 0x3f3f3f3f;
char str[MAXN << 1],s[MAXN];
int p[MAXN << 1];
void manacher(int left, int right) {
    int ind = 0;
    str[ind++] = '@';
    str[ind++] = '#';
    for (int i = left; i < right; i++) {
        str[ind++] = s[i];
        str[ind++] = '#';
    }
    str[ind++] = '\0';
    int mid = 0,mx = 0,c = 0,r = 0;
    p[0] = 0;
    for (int i = 1; i < ind; i++) {
        p[i] = mx > i ? min(p[(mid << 1) - i],mx - i) : 1;
        while (str[i + p[i]] == str[i - p[i]]) {
            p[i]++;
        }
        if (p[i] + i > mx) {
            mx = p[i] + i;
            mid = i;
        }
        if (p[i] > r) {
            int start = (i - p[i]) / 2, end = start + p[i] - 1;
            if (start == 0 || end == right - left) {
                r = p[i];
                c = i;
            }
        }
    }
    int start = (c - r) / 2 + left, end = start + r - 1;
    for (int i = start; i < end; i++) {
        putchar(s[i]);
    }
}
void solve() {
    scanf("%s",&s);
    int len = strlen(s) - 1;
    if (len == 0) {
        printf("%c\n",s[0]);
        return ;
    }
    for (int i = 0,j = len; i <= j; i++,j--) {
        if (s[i] != s[j]) {
            for (int k = 0; k < i; k++) {
                putchar(s[k]);
            }
            manacher(i,j + 1);
            for (int k = j + 1; k <= len; k++) {
                putchar(s[k]);
            }
            putchar('\n');
            return ;
        }
    }
    for (int i = 0; i <= len; i++) {
        putchar(s[i]);
    }
    putchar('\n');
}

int main() {
    int T;
    scanf("%d",&T);
    while (T--) {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 50;
char s[maxn],str[maxn << 1];
int p[maxn << 1];
void Manachar(int left, int right) {
    int ind = 0;
    str[ind++] = '@';
    str[ind++] = '#';
    for (int i = left; i < right; i++) {
        str[ind++] = s[i];
        str[ind++] = '#';
    }
    str[ind] = '\0';
    int mx = 0, id = 0, rcen = 0, rlen = 0, ans = 0;
    for (int i = 0; i < ind; ++i) {
        p[i] = mx > i ? min(p[2 * id - i], mx - i) : 1;
        while (str[i - p[i]] == str[i + p[i]]) p[i]++;
        if (p[i] + i > mx) {
            mx = p[i] + i;
            id = i;
        }
        if (rlen < p[i]) {
            int start = (i - p[i]) / 2, end = start + p[i] - 1;
            if (start == 0 || end == right - left) {
                rlen = p[i];
                rcen = i;
            }
        }
    }
    int start = (rcen - rlen) / 2 + left, end = start + rlen - 1;
    for (int i = start; i < end; i++) {
        cout << s[i];
    }
}
void solve() {
    cin >> s;
    int len = strlen(s), f = 0;
    for (int i = 0; i < len; ++i) {
        if (s[i] != s[len - 1 - i]) {
            f = i;
            break;
        }
        if (i == len / 2) {
            cout << s << "\n";
            return;
        }
    }
    for (int i = 0; i < f; i++) {
        cout << s[i];
    }
    Manachar(f,f + len - 2 * f);
    for (int i = f + len - 2 * f; i < len; i++) {
        cout << s[i];
    }
    cout << "\n";
    return ;
}
int main() {
    std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 1e6 + 5;
const int INF = 0x3f3f3f3f;
char str[MAXN << 1],s[MAXN];
int p[MAXN << 1];
void manacher(int left, int right) {
    int ind = 0;
    str[ind++] = '@';
    str[ind++] = '#';
    for (int i = left; i < right; i++) {
        str[ind++] = s[i];
        str[ind++] = '#';
    }
    str[ind] = '\0';
    int mid = 0,mx = 0,c = 0,r = 0;
    p[0] = 0;
    for (int i = 1; i < ind; i++) {
        p[i] = mx > i ? min(p[(mid << 1) - i],mx - i) : 1;
        while (str[i + p[i]] == str[i - p[i]]) {
            p[i]++;
        }
        if (p[i] + i > mx) {
            mx = p[i] + i;
            mid = i;
        }
        if (p[i] > r) {
            int start = (i - p[i]) / 2, end = start + p[i] - 1;
            if (start == 0 || end == right - left) {
                r = p[i];
                c = i;
            }
        }
    }
    int start = (c - r) / 2 + left, end = start + r - 1;
    for (int i = start; i < end; i++) {
        putchar(s[i]);
    }
}
void solve() {
    scanf("%s",&s);
    int len = strlen(s) - 1;
    for (int i = 0,j = len; i < j; i++,j--) {
        if (s[i] != s[j]) {
            for (int k = 0; k < i; k++) {
                putchar(s[k]);
            }
            manacher(i,j + 1);
            for (int k = j + 1; k <= len; k++) {
                putchar(s[k]);
            }
            putchar('\n');
            return ;
        }
    }
    printf("%s\n",s);
}

int main() {
    int T;
    scanf("%d",&T);
    while (T--) {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 50;
int p[maxn << 1];
void Manachar(string &s,int left, int right) {
    memset(p, 0, sizeof(p));
    string t = "$#";
    int len = s.size();
    for (int i = left; i < right; ++i) {
        t += s[i];
        t += '#';
    }
    len = t.size();
    int mx = 0, id = 0, rcen = 0, rlen = 0, ans = 0;
    for (int i = 0; i < len; ++i) {
        p[i] = mx > i ? min(p[2 * id - i], mx - i) : 1;
        while (t[i - p[i]] == t[i + p[i]]) p[i]++;
        if (p[i] + i > mx) {
            mx = p[i] + i;
            id = i;
        }
        if (rlen < p[i]) {
            int start = (i - p[i]) / 2, end = start + p[i] - 1;
            if (start == 0 || end == right - left) {
                rlen = p[i];
                rcen = i;
            }
        }
    }
    int start = (rcen - rlen) / 2 + left, end = start + rlen - 1;
    for (int i = start; i < end; i++) {
        cout << s[i];
    }
}
void solve() {
    string s;
    cin >> s;
    int len = s.size(), f = 0;
    for (int i = 0; i < len; ++i) {
        if (s[i] != s[len - 1 - i]) {
            f = i;
            break;
        }
        if (i == len / 2) {
            cout << s << "\n";
            return;
        }
    }
    for (int i = 0; i < f; i++) {
        cout << s[i];
    }
    Manachar(s,f,f + len - 2 * f);
    for (int i = f + len - 2 * f; i < len; i++) {
        cout << s[i];
    }
    cout << "\n";
    return ;
}
int main() {
    std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 1e6 + 5;
const int INF = 0x3f3f3f3f;
char str[MAXN << 1],s[MAXN];
int p[MAXN << 1];
void manacher(int left, int right) {
    int ind = 0;
    str[ind++] = '@';
    str[ind++] = '#';
    for (int i = left; i < right; i++) {
        str[ind++] = s[i];
        str[ind++] = '#';
    }
    str[ind] = '\0';
    int mid = 0,mx = 0,c = 0,r = 0;
    p[0] = 0;
    for (int i = 1; i < ind; i++) {
        p[i] = mx > i ? min(p[(mid << 1) - i],mx - i) : 1;
        while (str[i + p[i]] == str[i - p[i]]) {
            p[i]++;
        }
        if (p[i] + i > mx) {
            mx = p[i] + i;
            mid = i;
        }
        if (p[i] > r) {
            int start = (i - p[i]) / 2, end = start + p[i] - 1;
            if (start == 0 || end == right - left) {
                r = p[i];
                c = i;
            }
        }
    }
    int start = (c - r) / 2 + left, end = start + r - 1;
    for (int i = start; i < end; i++) {
        putchar(s[i]);
    }
}
void solve() {
    scanf("%s",&s);
    int len = strlen(s) - 1;
    if (len == 0) {
        printf("%c\n",s[0]);
        return ;
    }
    for (int i = 0,j = len; i < j; i++,j--) {
        if (s[i] != s[j]) {
            for (int k = 0; k < i; k++) {
                putchar(s[k]);
            }
            manacher(i,j + 1);
            for (int k = j + 1; k <= len; k++) {
                putchar(s[k]);
            }
            putchar('\n');
            return ;
        }
    }
    for (int i = 0; i <= len; i++) {
        putchar(s[i]);
    }
    putchar('\n');
}

int main() {
    int T;
    scanf("%d",&T);
    while (T--) {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 1e6 + 5;
const int INF = 0x3f3f3f3f;
char str[MAXN << 1],s[MAXN];
int p[MAXN << 1];
void manacher(int left, int right) {
    int ind = 0;
    str[ind++] = '@';
    str[ind++] = '#';
    for (int i = left; i < right; i++) {
        str[ind++] = s[i];
        str[ind++] = '#';
    }
    str[ind++] = '\0';
    int mid = 0,mx = 0,c = 0,r = 0;
    p[0] = 0;
    for (int i = 1; i < ind; i++) {
        p[i] = mx > i ? min(p[(mid << 1) - i],mx - i) : 1;
        while (str[i + p[i]] == str[i - p[i]]) {
            p[i]++;
        }
        if (p[i] + i > mx) {
            mx = p[i] + i;
            mid = i;
        }
        if (p[i] > r) {
            int start = (i - p[i]) / 2, end = start + p[i] - 1;
            if (start == 0 || end == right - left) {
                r = p[i];
                c = i;
            }
        }
    }
    int start = (c - r) / 2 + left, end = start + r - 1;
    for (int i = start; i < end; i++) {
        putchar(s[i]);
    }
}
void solve() {
    scanf("%s",&s);
    int len = strlen(s) - 1;
    if (len == 0) {
        printf("%c\n",s[0]);
        return ;
    }
    for (int i = 0,j = len; i <= j; i++,j--) {
        if (s[i] != s[j] || i == j) {
            for (int k = 0; k < i; k++) {
                putchar(s[k]);
            }
            manacher(i,j + 1);
            for (int k = j + 1; k <= len; k++) {
                putchar(s[k]);
            }
            putchar('\n');
            return ;
        }
    }
}

int main() {
    int T;
    scanf("%d",&T);
    while (T--) {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 1e6 + 5;
const int INF = 0x3f3f3f3f;
char str[MAXN << 1],s[MAXN];
int p[MAXN << 1];
void manacher(int left, int right) {
    int ind = 0;
    str[ind++] = '@';
    str[ind++] = '#';
    for (int i = left; i < right; i++) {
        str[ind++] = s[i];
        str[ind++] = '#';
    }
    str[ind++] = '\0';
    int mid = 0,mx = 0,c = 0,r = 0;
    p[0] = 0;
    for (int i = 1; i < ind; i++) {
        p[i] = mx > i ? min(p[(mid << 1) - i],mx - i) : 1;
        while (str[i + p[i]] == str[i - p[i]]) {
            p[i]++;
        }
        if (p[i] + i > mx) {
            mx = p[i] + i;
            mid = i;
        }
        if (p[i] > r) {
            int start = (i - p[i]) / 2, end = start + p[i] - 1;
            if (start == 0 || end == right - left) {
                r = p[i];
                c = i;
            }
        }
    }
    int start = (c - r) / 2 + left, end = start + r - 1;
    for (int i = start; i < end; i++) {
        putchar(s[i]);
    }
}
void solve() {
    scanf("%s",&s);
    int len = strlen(s) - 1;
    if (len == 0) {
        printf("%c\n",s[0]);
        return ;
    }
    for (int i = 0,j = len; i < j; i++,j--) {
        if (s[i] != s[j]) {
            for (int k = 0; k < i; k++) {
                putchar(s[k]);
            }
            manacher(i,j + 1);
            for (int k = j + 1; k <= len; k++) {
                putchar(s[k]);
            }
            putchar('\n');
            return ;
        }
    }
    for (int i = 0; i <= len; i++) {
        putchar(s[i]);
    }
    putchar('\n');
}

int main() {
    int T;
    scanf("%d",&T);
    while (T--) {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 50;
int p[maxn << 1];
void Manachar(string &s,int left, int right) {
    memset(p, 0, sizeof(p));
    string t = "$#";
    int len = s.size();
    for (int i = left; i < right; ++i) {
        t += s[i];
        t += '#';
    }
    len = t.size();
    int mx = 0, id = 0, rcen = 0, rlen = 0, ans = 0;
    for (int i = 0; i < len; ++i) {
        p[i] = mx > i ? min(p[2 * id - i], mx - i) : 1;
        while (t[i - p[i]] == t[i + p[i]]) p[i]++;
        if (p[i] + i > mx) {
            mx = p[i] + i;
            id = i;
        }
        if (rlen < p[i]) {
            int start = (i - p[i]) / 2, end = start + p[i] - 1;
            if (start == 0 || end == right - left) {
                rlen = p[i];
                rcen = i;
            }
        }
    }
    int start = (rcen - rlen) / 2, end = start + rlen - 1;
    for (int i = start; i < end; i++) {
        cout << s[i];
    }
}
void solve() {
    string s;
    cin >> s;
    int len = s.size(), f = 0;
    for (int i = 0; i < len; ++i) {
        if (s[i] != s[len - 1 - i]) {
            f = i;
            break;
        }
        if (i == len / 2) {
            cout << s << "\n";
            return;
        }
    }
    for (int i = 0; i < f; i++) {
        cout << s[i];
    }
    Manachar(s,f,f + len - 2 * f);
    for (int i = f + len - 2 * f; i < len; i++) {
        cout << s[i];
    }
    cout << "\n";
    return ;
}
int main() {
    std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 1e6 + 5;
const int INF = 0x3f3f3f3f;
char str[MAXN << 1],s[MAXN];
int p[MAXN << 1];
void manacher(int left, int right) {
    int ind = 0;
    str[ind++] = '@';
    str[ind++] = '#';
    for (int i = left; i < right; i++) {
        str[ind++] = s[i];
        str[ind++] = '#';
    }
    str[ind] = '\0';
    int mid = 0,mx = 0,c = 0,r = 0;
    p[0] = 0;
    for (int i = 1; i < ind; i++) {
        p[i] = mx > i ? min(p[(mid << 1) - i],mx - i) : 1;
        while (str[i + p[i]] == str[i - p[i]]) {
            p[i]++;
        }
        if (p[i] + i > mx) {
            mx = p[i] + i;
            mid = i;
        }
        if (p[i] > r) {
            int start = (i - p[i]) / 2, end = start + p[i] - 1;
            if (start == 0 || end == right - left) {
                r = p[i];
                c = i;
            }
        }
    }
    int start = (c - r) / 2 + left, end = start + r - 1;
    for (int i = start; i < end; i++) {
        putchar(s[i]);
    }
}
void solve() {
    scanf("%s",&s);
    int len = strlen(s) - 1;
    for (int i = 0,j = len; i <= j; i++,j--) {
        if (i == j) {
            printf("%s\n",s);
        }
        if (s[i] != s[j]) {
            for (int k = 0; k < i; k++) {
                putchar(s[k]);
            }
            manacher(i,j + 1);
            for (int k = j + 1; k <= len; k++) {
                putchar(s[k]);
            }
            putchar('\n');
            return ;
        }
    }
}

int main() {
    int T;
    scanf("%d",&T);
    while (T--) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
#define db(x) cout << (x) << '\n';
#define all(v) (v).begin(),(v).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pii;
typedef pair<double,ll> pdi;
typedef pair<string,ll> psi;
typedef pair<ll,string> pls;
typedef pair<ll,pii> pip;
typedef pair<pii,pii> ppp;

template<typename charT>
struct SuffixArray
{
	int n;
	vector<int> sa, rank, lcp;

	SuffixArray(const basic_string<charT> &s) :
		n(s.length() + 1), sa(n), rank(n), lcp(n)
	{
		vector<int> _sa(n), bucket(n);

		iota(sa.rbegin(), sa.rend(), 0);
		sort(next(sa.begin()), sa.end(),
			[&](int i, int j) { return s[i] < s[j]; });

		for (int i = 1, j = 0; i < n; ++i)
		{
			rank[sa[i]] = rank[sa[i - 1]] +
						  (i == 1 || s[sa[i - 1]] < s[sa[i]]);
			if (rank[sa[i]] != rank[sa[i - 1]])
				bucket[++j] = i;
		}

		for (int len = 1; len <= n; len += len)
		{
			for (int i = 0, j; i < n; ++i)
			{
				if ((j = sa[i] - len) < 0) j += n;
				_sa[bucket[rank[j]]++] = j;
			}

			sa[_sa[bucket[0] = 0]] = 0;

			for (int i = 1, j = 0; i < n; ++i)
			{
				if (rank[_sa[i]] != rank[_sa[i - 1]] ||
					rank[_sa[i] + len] != rank[_sa[i - 1] + len])
					bucket[++j] = i;

				sa[_sa[i]] = j;
			}

			copy(sa.begin(), sa.end(), rank.begin());
			sa.swap(_sa);

			if (rank[sa[n - 1]] == n - 1) break;
		}

		for (int i = 0, j = rank[lcp[0] = 0], k = 0; i < n - 1; ++i, ++k)
			while (k >= 0 && s[i] != s[sa[j - 1] + k])
				lcp[j] = k--, j = rank[sa[j] + 1];
	}
};
#define MAXN 500005

int rmq[MAXN][20],lg[MAXN];
int pos[MAXN];

int query(int l,int r)
{
    if(l>r)
        swap(l,r);
    l++;
    int po=lg[r-l+1];
    return min(rmq[l][po],rmq[r-(1<<po)+1][po]);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    lg[0]=-1;
    for(int i=1;i<MAXN;i++)
        lg[i]=lg[i-1]+!(i&(i-1));

    int t;
    cin >> t;
    while(t--)
    {
        string s;
        cin >> s;
        int ma=0,n=s.size();
        for(int i=0;i<n/2;i++)
        {
            if(s[i]==s[n-i-1])
                ma++;
            else
                break;
        }
        string sss=s;
        reverse(all(sss));

        string ss=s+"#"+sss;

        SuffixArray<char> sa(ss);

        for(int i=0;i<sa.lcp.size();i++)
            rmq[i][0]=sa.lcp[i];
        for(int i=1;i<=lg[sa.lcp.size()];i++)
            for(int j=0;j+(1<<i)<=sa.lcp.size();j++)
                rmq[j][i]=min(rmq[j][i-1],rmq[j+(1<<(i-1))][i-1]);

//        db(ss)
//        db(ma)
        for(int i=0;i<sa.sa.size();i++)
        {
            pos[sa.sa[i]]=i;
//            cout << sa.sa[i] << ' ' << sa.lcp[i] << '\n';
        }
//        db("")

        pii res=pii(1,1);
        for(int i=0;i<n/2;i++)
        {
            if(ma>i)
                res=max(res,pii(2*(i+1),1));
            else
            {
                int faltan=i+1-ma;
                int p1=ma;
                int p2=2*n-(i+faltan);

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*(i+1),1));
            }
        }

        for(int i=0;i<(n+1)/2;i++)
        {
            if(ma>=i)
                res=max(res,pii(2*i+1,1));
            else
            {
                int faltan=i-ma;
                int p1=ma;
                int p2=2*n-(i+faltan);

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*i+1,1));
            }
        }

        for(int i=0;i<n/2;i++)
        {
            if(ma>i)
                res=max(res,pii(2*(i+1),1));
            else
            {
                int faltan=i+1-ma;
                int p1=n-1-i-faltan;
                int p2=n+ma+1;

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*(i+1),2));
            }
        }

        for(int i=0;i<(n+1)/2;i++)
        {
            if(ma>=i)
                res=max(res,pii(2*i+1,1));
            else
            {
                int faltan=i-ma;
                int p1=n-1-i-faltan;
                int p2=n+ma+1;

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*i+1,2));
            }
        }

//        cout << res.first << ' ' << res.second << '\n';
        string ress="";
        ress.resize(res.first);
        int cont=0;
        if(res.second==1)
        {
            for(int i=0;i<res.first/2;i++)
            {
//                cout << s[i];
//                printf("%c",s[i]);
                ress[cont]=s[i];
                cont++;
            }
            if(res.first&1)
            {
//                cout << s[res.first/2];
//                printf("%c",s[res.first/2]);
                ress[cont]=s[res.first/2];
                cont++;
            }
            for(int i=0;i<res.first/2;i++)
            {
//                cout << s[res.first/2-i-1];
//                printf("%c",s[res.first/2-i-1]);
                ress[cont]=s[res.first/2-i-1];
                cont++;
            }
//            cout << '\n';
//            printf("\n");
        }
        else
        {
            for(int i=0;i<res.first/2;i++)
            {
//                cout << s[n-1-i];
//                printf("%c",s[n-1-i]);
                ress[cont]=s[n-1-i];
                cont++;
            }
            if(res.first&1)
            {
//                cout << s[n-1-res.first/2];
//                printf("%c",s[n-1-res.first/2]);
                ress[cont]=s[n-1-res.first/2];
                cont++;
            }
            for(int i=0;i<res.first/2;i++)
            {
//                cout << s[n-res.first/2+i];
//                printf("%c",s[n-res.first/2+i]);
                ress[cont]=s[n-res.first/2+i];
                cont++;
            }
//            printf("\n");
//            cout << '\n';
        }
        db(ress)
    }

    return 0;
}
/**
1
acbba
**/


#include <bits/stdc++.h>
#define db(x) cout << (x) << '\n';
#define all(v) (v).begin(),(v).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pii;
typedef pair<double,ll> pdi;
typedef pair<string,ll> psi;
typedef pair<ll,string> pls;
typedef pair<ll,pii> pip;
typedef pair<pii,pii> ppp;

template<typename charT>
struct SuffixArray
{
	int n;
	vector<int> sa, rank, lcp;

	SuffixArray(const basic_string<charT> &s) :
		n(s.length() + 1), sa(n), rank(n), lcp(n)
	{
		vector<int> _sa(n), bucket(n);

		iota(sa.rbegin(), sa.rend(), 0);
		sort(next(sa.begin()), sa.end(),
			[&](int i, int j) { return s[i] < s[j]; });

		for (int i = 1, j = 0; i < n; ++i)
		{
			rank[sa[i]] = rank[sa[i - 1]] +
						  (i == 1 || s[sa[i - 1]] < s[sa[i]]);
			if (rank[sa[i]] != rank[sa[i - 1]])
				bucket[++j] = i;
		}

		for (int len = 1; len <= n; len += len)
		{
			for (int i = 0, j; i < n; ++i)
			{
				if ((j = sa[i] - len) < 0) j += n;
				_sa[bucket[rank[j]]++] = j;
			}

			sa[_sa[bucket[0] = 0]] = 0;

			for (int i = 1, j = 0; i < n; ++i)
			{
				if (rank[_sa[i]] != rank[_sa[i - 1]] ||
					rank[_sa[i] + len] != rank[_sa[i - 1] + len])
					bucket[++j] = i;

				sa[_sa[i]] = j;
			}

			copy(sa.begin(), sa.end(), rank.begin());
			sa.swap(_sa);

			if (rank[sa[n - 1]] == n - 1) break;
		}

		for (int i = 0, j = rank[lcp[0] = 0], k = 0; i < n - 1; ++i, ++k)
			while (k >= 0 && s[i] != s[sa[j - 1] + k])
				lcp[j] = k--, j = rank[sa[j] + 1];
	}
};
#define MAXN 500005

int rmq[MAXN][20],lg[MAXN];
int pos[MAXN];

int query(int l,int r)
{
    if(l>r)
        swap(l,r);
    l++;
    int po=lg[r-l+1];
    return min(rmq[l][po],rmq[r-(1<<po)+1][po]);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    lg[0]=-1;
    for(int i=1;i<MAXN;i++)
        lg[i]=lg[i-1]+!(i&(i-1));

    int t;
    cin >> t;
    while(t--)
    {
        string s;
        cin >> s;
        int ma=0,n=s.size();
        for(int i=0;i<n/2;i++)
        {
            if(s[i]==s[n-i-1])
                ma++;
            else
                break;
        }
        string sss=s;
        reverse(all(sss));

        string ss=s+"#"+sss;

        SuffixArray<char> sa(ss);

        for(int i=0;i<sa.lcp.size();i++)
            rmq[i][0]=sa.lcp[i];
        for(int i=1;i<=lg[sa.lcp.size()];i++)
            for(int j=0;j+(1<<i)<=sa.lcp.size();j++)
                rmq[j][i]=min(rmq[j][i-1],rmq[j+(1<<(i-1))][i-1]);

//        db(ss)
//        db(ma)
        for(int i=0;i<sa.sa.size();i++)
        {
            pos[sa.sa[i]]=i;
//            cout << sa.sa[i] << ' ' << sa.lcp[i] << '\n';
        }
//        db("")

        pii res=pii(1,1);
        for(int i=0;i<n/2;i++)
        {
            if(ma>i)
                res=max(res,pii(2*(i+1),1));
            else
            {
                int faltan=i+1-ma;
                int p1=ma;
                int p2=2*n-(i+faltan);

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*(i+1),1));
            }
        }

        for(int i=0;i<(n+1)/2;i++)
        {
            if(ma>=i)
                res=max(res,pii(2*i+1,1));
            else
            {
                int faltan=i-ma;
                int p1=ma;
                int p2=2*n-(i+faltan);

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*i+1,1));
            }
        }

        for(int i=0;i<n/2;i++)
        {
            if(ma>i)
                res=max(res,pii(2*(i+1),1));
            else
            {
                int faltan=i+1-ma;
                int p1=n-1-i-faltan;
                int p2=n+ma+1;

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*(i+1),2));
            }
        }

        for(int i=0;i<(n+1)/2;i++)
        {
            if(ma>=i)
                res=max(res,pii(2*i+1,1));
            else
            {
                int faltan=i-ma;
                int p1=n-1-i-faltan;
                int p2=n+ma+1;

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*i+1,2));
            }
        }

//        cout << res.first << ' ' << res.second << '\n';
        if(res.second==1)
        {
            for(int i=0;i<res.first/2;i++)
//                cout << s[i];
                printf("%c",s[i]);
            if(res.first&1)
//                cout << s[res.first/2];
                printf("%c",s[res.first/2]);
            for(int i=0;i<res.first/2;i++)
//                cout << s[res.first/2-i-1];
                printf("%c",s[res.first/2-i-1]);
//            cout << '\n';
            printf("\n");
        }
        else
        {
            for(int i=0;i<res.first/2;i++)
//                cout << s[n-1-i];
                printf("%c",s[n-1-i]);
            if(res.first&1)
//                cout << s[n-1-res.first/2];
                printf("%c",s[n-1-res.first/2]);
            for(int i=0;i<res.first/2;i++)
//                cout << s[n-res.first/2+i];
                printf("%c",s[n-res.first/2+i]);
            printf("\n");
//            cout << '\n';
        }
    }

    return 0;
}
/**
1
acbba
**/


#include <bits/stdc++.h>
#define db(x) cout << (x) << '\n';
#define all(v) (v).begin(),(v).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pii;
typedef pair<double,ll> pdi;
typedef pair<string,ll> psi;
typedef pair<ll,string> pls;
typedef pair<ll,pii> pip;
typedef pair<pii,pii> ppp;

template<typename charT>
struct SuffixArray
{
	int n;
	vector<int> sa, rank, lcp;

	SuffixArray(const basic_string<charT> &s) :
		n(s.length() + 1), sa(n), rank(n), lcp(n)
	{
		vector<int> _sa(n), bucket(n);

		iota(sa.rbegin(), sa.rend(), 0);
		sort(next(sa.begin()), sa.end(),
			[&](int i, int j) { return s[i] < s[j]; });

		for (int i = 1, j = 0; i < n; ++i)
		{
			rank[sa[i]] = rank[sa[i - 1]] +
						  (i == 1 || s[sa[i - 1]] < s[sa[i]]);
			if (rank[sa[i]] != rank[sa[i - 1]])
				bucket[++j] = i;
		}

		for (int len = 1; len <= n; len += len)
		{
			for (int i = 0, j; i < n; ++i)
			{
				if ((j = sa[i] - len) < 0) j += n;
				_sa[bucket[rank[j]]++] = j;
			}

			sa[_sa[bucket[0] = 0]] = 0;

			for (int i = 1, j = 0; i < n; ++i)
			{
				if (rank[_sa[i]] != rank[_sa[i - 1]] ||
					rank[_sa[i] + len] != rank[_sa[i - 1] + len])
					bucket[++j] = i;

				sa[_sa[i]] = j;
			}

			copy(sa.begin(), sa.end(), rank.begin());
			sa.swap(_sa);

			if (rank[sa[n - 1]] == n - 1) break;
		}

		for (int i = 0, j = rank[lcp[0] = 0], k = 0; i < n - 1; ++i, ++k)
			while (k >= 0 && s[i] != s[sa[j - 1] + k])
				lcp[j] = k--, j = rank[sa[j] + 1];
	}
};
#define MAXN 3000005

int rmq[MAXN][24],lg[MAXN];
int pos[MAXN];

int query(int l,int r)
{
    if(l>r)
        swap(l,r);
    l++;
    int po=lg[r-l+1];
    return min(rmq[l][po],rmq[r-(1<<po)+1][po]);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    cout.tie(0);

    lg[0]=-1;
    for(int i=1;i<MAXN;i++)
        lg[i]=lg[i-1]+!(i&(i-1));

    int t;
    cin >> t;
    while(t--)
    {
        string s;
        cin >> s;
        int ma=0,n=s.size();
        for(int i=0;i<n/2;i++)
        {
            if(s[i]==s[n-i-1])
                ma++;
            else
                break;
        }
        string sss=s;
        reverse(all(sss));

        string ss=s+"#"+sss;

        SuffixArray<char> sa(ss);

        for(int i=0;i<sa.lcp.size();i++)
            rmq[i][0]=sa.lcp[i];
        for(int i=1;i<=lg[sa.lcp.size()];i++)
            for(int j=0;j+(1<<i)<=sa.lcp.size();j++)
                rmq[j][i]=min(rmq[j][i-1],rmq[j+(1<<(i-1))][i-1]);

//        db(ss)
//        db(ma)
        for(int i=0;i<sa.sa.size();i++)
        {
            pos[sa.sa[i]]=i;
//            cout << sa.sa[i] << ' ' << sa.lcp[i] << '\n';
        }
//        db("")

        pii res=pii(1,1);
        for(int i=0;i<n/2;i++)
        {
            if(ma>i)
                res=max(res,pii(2*(i+1),1));
            else
            {
                int faltan=i+1-ma;
                int p1=ma;
                int p2=2*n-(i+faltan);

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*(i+1),1));
            }
        }

        for(int i=0;i<(n+1)/2;i++)
        {
            if(ma>=i)
                res=max(res,pii(2*i+1,1));
            else
            {
                int faltan=i-ma;
                int p1=ma;
                int p2=2*n-(i+faltan);

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*i+1,1));
            }
        }

        for(int i=0;i<n/2;i++)
        {
            if(ma>i)
                res=max(res,pii(2*(i+1),1));
            else
            {
                int faltan=i+1-ma;
                int p1=n-1-i-faltan;
                int p2=n+ma+1;

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*(i+1),2));
            }
        }

        for(int i=0;i<(n+1)/2;i++)
        {
            if(ma>=i)
                res=max(res,pii(2*i+1,1));
            else
            {
                int faltan=i-ma;
                int p1=n-1-i-faltan;
                int p2=n+ma+1;

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*i+1,2));
            }
        }

//        cout << res.first << ' ' << res.second << '\n';
        string ress="";
        ress.resize(res.first);
        int cont=0;
        if(res.second==1)
        {
            for(int i=0;i<res.first/2;i++)
            {
//                cout << s[i];
//                printf("%c",s[i]);
                ress[cont]=s[i];
                cont++;
            }
            if(res.first&1)
            {
//                cout << s[res.first/2];
//                printf("%c",s[res.first/2]);
                ress[cont]=s[res.first/2];
                cont++;
            }
            for(int i=0;i<res.first/2;i++)
            {
//                cout << s[res.first/2-i-1];
//                printf("%c",s[res.first/2-i-1]);
                ress[cont]=s[res.first/2-i-1];
                cont++;
            }
//            cout << '\n';
//            printf("\n");
        }
        else
        {
            for(int i=0;i<res.first/2;i++)
            {
//                cout << s[n-1-i];
//                printf("%c",s[n-1-i]);
                ress[cont]=s[n-1-i];
                cont++;
            }
            if(res.first&1)
            {
//                cout << s[n-1-res.first/2];
//                printf("%c",s[n-1-res.first/2]);
                ress[cont]=s[n-1-res.first/2];
                cont++;
            }
            for(int i=0;i<res.first/2;i++)
            {
//                cout << s[n-res.first/2+i];
//                printf("%c",s[n-res.first/2+i]);
                ress[cont]=s[n-res.first/2+i];
                cont++;
            }
//            printf("\n");
//            cout << '\n';
        }
        db(ress)
    }

    return 0;
}
/**
1
acbba
**/


#include <bits/stdc++.h>
#define db(x) cout << (x) << '\n';
#define all(v) (v).begin(),(v).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pii;
typedef pair<double,ll> pdi;
typedef pair<string,ll> psi;
typedef pair<ll,string> pls;
typedef pair<ll,pii> pip;
typedef pair<pii,pii> ppp;

template<typename charT>
struct SuffixArray
{
	int n;
	vector<int> sa, rank, lcp;

	SuffixArray(const basic_string<charT> &s) :
		n(s.length() + 1), sa(n), rank(n), lcp(n)
	{
		vector<int> _sa(n), bucket(n);

		iota(sa.rbegin(), sa.rend(), 0);
		sort(next(sa.begin()), sa.end(),
			[&](int i, int j) { return s[i] < s[j]; });

		for (int i = 1, j = 0; i < n; ++i)
		{
			rank[sa[i]] = rank[sa[i - 1]] +
						  (i == 1 || s[sa[i - 1]] < s[sa[i]]);
			if (rank[sa[i]] != rank[sa[i - 1]])
				bucket[++j] = i;
		}

		for (int len = 1; len <= n; len += len)
		{
			for (int i = 0, j; i < n; ++i)
			{
				if ((j = sa[i] - len) < 0) j += n;
				_sa[bucket[rank[j]]++] = j;
			}

			sa[_sa[bucket[0] = 0]] = 0;

			for (int i = 1, j = 0; i < n; ++i)
			{
				if (rank[_sa[i]] != rank[_sa[i - 1]] ||
					rank[_sa[i] + len] != rank[_sa[i - 1] + len])
					bucket[++j] = i;

				sa[_sa[i]] = j;
			}

			copy(sa.begin(), sa.end(), rank.begin());
			sa.swap(_sa);

			if (rank[sa[n - 1]] == n - 1) break;
		}

		for (int i = 0, j = rank[lcp[0] = 0], k = 0; i < n - 1; ++i, ++k)
			while (k >= 0 && s[i] != s[sa[j - 1] + k])
				lcp[j] = k--, j = rank[sa[j] + 1];
	}
};
#define MAXN 500005

int rmq[MAXN][20],lg[MAXN];
int pos[MAXN];

int query(int l,int r)
{
    if(l>r)
        swap(l,r);
    l++;
    int po=lg[r-l+1];
    return min(rmq[l][po],rmq[r-(1<<po)+1][po]);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    lg[0]=-1;
    for(int i=1;i<MAXN;i++)
        lg[i]=lg[i-1]+!(i&(i-1));

    int t;
    cin >> t;
    while(t--)
    {
        string s;
        cin >> s;
        int ma=0,n=s.size();
        for(int i=0;i<n/2;i++)
        {
            if(s[i]==s[n-i-1])
                ma++;
            else
                break;
        }
        string sss=s;
        reverse(all(sss));

        string ss=s+"#"+sss;

        SuffixArray<char> sa(ss);

        for(int i=0;i<sa.lcp.size();i++)
            rmq[i][0]=sa.lcp[i];
        for(int i=1;i<=lg[sa.lcp.size()];i++)
            for(int j=0;j+(1<<i)<=sa.lcp.size();j++)
                rmq[j][i]=min(rmq[j][i-1],rmq[j+(1<<(i-1))][i-1]);

//        db(ss)
//        db(ma)
        for(int i=0;i<sa.sa.size();i++)
        {
            pos[sa.sa[i]]=i;
//            cout << sa.sa[i] << ' ' << sa.lcp[i] << '\n';
        }
//        db("")

        pii res=pii(1,1);
        for(int i=0;i<n/2;i++)
        {
            if(ma>i)
                res=max(res,pii(2*(i+1),1));
            else
            {
                int faltan=i+1-ma;
                int p1=ma;
                int p2=2*n-(i+faltan);

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*(i+1),1));
            }
        }

        for(int i=0;i<(n+1)/2;i++)
        {
            if(ma>=i)
                res=max(res,pii(2*i+1,1));
            else
            {
                int faltan=i-ma;
                int p1=ma;
                int p2=2*n-(i+faltan);

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*i+1,1));
            }
        }

        for(int i=0;i<n/2;i++)
        {
            if(ma>i)
                res=max(res,pii(2*(i+1),1));
            else
            {
                int faltan=i+1-ma;
                int p1=n-1-i-faltan;
                int p2=n+ma+1;

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*(i+1),2));
            }
        }

        for(int i=0;i<(n+1)/2;i++)
        {
            if(ma>=i)
                res=max(res,pii(2*i+1,1));
            else
            {
                int faltan=i-ma;
                int p1=n-1-i-faltan;
                int p2=n+ma+1;

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*i+1,2));
            }
        }

//        cout << res.first << ' ' << res.second << '\n';
        if(res.second==1)
        {
            for(int i=0;i<res.first/2;i++)
                cout << s[i];
            if(res.first&1)
                cout << s[res.first/2];
            for(int i=0;i<res.first/2;i++)
                cout << s[res.first/2-i-1];
            cout << '\n';
        }
        else
        {
            for(int i=0;i<res.first/2;i++)
                cout << s[n-1-i];
            if(res.first&1)
                cout << s[n-1-res.first/2];
            for(int i=0;i<res.first/2;i++)
                cout << s[n-res.first/2+i];
            cout << '\n';
        }
    }

    return 0;
}
/**
1
acbba
**/


#include <bits/stdc++.h>
#define db(x) cout << (x) << '\n';
#define all(v) (v).begin(),(v).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pii;
typedef pair<double,ll> pdi;
typedef pair<string,ll> psi;
typedef pair<ll,string> pls;
typedef pair<ll,pii> pip;
typedef pair<pii,pii> ppp;

template<typename charT>
struct SuffixArray
{
	int n;
	vector<int> sa, rank, lcp;

	SuffixArray(const basic_string<charT> &s) :
		n(s.length() + 1), sa(n), rank(n), lcp(n)
	{
		vector<int> _sa(n), bucket(n);

		iota(sa.rbegin(), sa.rend(), 0);
		sort(next(sa.begin()), sa.end(),
			[&](int i, int j) { return s[i] < s[j]; });

		for (int i = 1, j = 0; i < n; ++i)
		{
			rank[sa[i]] = rank[sa[i - 1]] +
						  (i == 1 || s[sa[i - 1]] < s[sa[i]]);
			if (rank[sa[i]] != rank[sa[i - 1]])
				bucket[++j] = i;
		}

		for (int len = 1; len <= n; len += len)
		{
			for (int i = 0, j; i < n; ++i)
			{
				if ((j = sa[i] - len) < 0) j += n;
				_sa[bucket[rank[j]]++] = j;
			}

			sa[_sa[bucket[0] = 0]] = 0;

			for (int i = 1, j = 0; i < n; ++i)
			{
				if (rank[_sa[i]] != rank[_sa[i - 1]] ||
					rank[_sa[i] + len] != rank[_sa[i - 1] + len])
					bucket[++j] = i;

				sa[_sa[i]] = j;
			}

			copy(sa.begin(), sa.end(), rank.begin());
			sa.swap(_sa);

			if (rank[sa[n - 1]] == n - 1) break;
		}

		for (int i = 0, j = rank[lcp[0] = 0], k = 0; i < n - 1; ++i, ++k)
			while (k >= 0 && s[i] != s[sa[j - 1] + k])
				lcp[j] = k--, j = rank[sa[j] + 1];
	}
};
#define MAXN 2000005

int rmq[MAXN][21],lg[MAXN];
int pos[MAXN];

int query(int l,int r)
{
    if(l>r)
        swap(l,r);
    l++;
    int po=lg[r-l+1];
    return min(rmq[l][po],rmq[r-(1<<po)+1][po]);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    cout.tie(0);

    lg[0]=-1;
    for(int i=1;i<MAXN;i++)
        lg[i]=lg[i-1]+!(i&(i-1));

    int t;
    cin >> t;
    while(t--)
    {
        string s;
        cin >> s;
        int ma=0,n=s.size();
        for(int i=0;i<n/2;i++)
        {
            if(s[i]==s[n-i-1])
                ma++;
            else
                break;
        }
        string sss=s;
        reverse(all(sss));

        string ss=s+"#"+sss;

        SuffixArray<char> sa(ss);

        for(int i=0;i<sa.lcp.size();i++)
            rmq[i][0]=sa.lcp[i];
        for(int i=1;i<=lg[sa.lcp.size()];i++)
            for(int j=0;j+(1<<i)<=sa.lcp.size();j++)
                rmq[j][i]=min(rmq[j][i-1],rmq[j+(1<<(i-1))][i-1]);

//        db(ss)
//        db(ma)
        for(int i=0;i<sa.sa.size();i++)
        {
            pos[sa.sa[i]]=i;
//            cout << sa.sa[i] << ' ' << sa.lcp[i] << '\n';
        }
//        db("")

        pii res=pii(1,1);
        for(int i=0;i<n/2;i++)
        {
            if(ma>i)
                res=max(res,pii(2*(i+1),1));
            else
            {
                int faltan=i+1-ma;
                int p1=ma;
                int p2=2*n-(i+faltan);

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*(i+1),1));
            }
        }

        for(int i=0;i<(n+1)/2;i++)
        {
            if(ma>=i)
                res=max(res,pii(2*i+1,1));
            else
            {
                int faltan=i-ma;
                int p1=ma;
                int p2=2*n-(i+faltan);

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*i+1,1));
            }
        }

        for(int i=0;i<n/2;i++)
        {
            if(ma>i)
                res=max(res,pii(2*(i+1),1));
            else
            {
                int faltan=i+1-ma;
                int p1=n-1-i-faltan;
                int p2=n+ma+1;

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*(i+1),2));
            }
        }

        for(int i=0;i<(n+1)/2;i++)
        {
            if(ma>=i)
                res=max(res,pii(2*i+1,1));
            else
            {
                int faltan=i-ma;
                int p1=n-1-i-faltan;
                int p2=n+ma+1;

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*i+1,2));
            }
        }

//        cout << res.first << ' ' << res.second << '\n';
        string ress="";
        ress.resize(res.first);
        int cont=0;
        if(res.second==1)
        {
            for(int i=0;i<res.first/2;i++)
            {
//                cout << s[i];
//                printf("%c",s[i]);
                ress[cont]=s[i];
                cont++;
            }
            if(res.first&1)
            {
//                cout << s[res.first/2];
//                printf("%c",s[res.first/2]);
                ress[cont]=s[res.first/2];
                cont++;
            }
            for(int i=0;i<res.first/2;i++)
            {
//                cout << s[res.first/2-i-1];
//                printf("%c",s[res.first/2-i-1]);
                ress[cont]=s[res.first/2-i-1];
                cont++;
            }
//            cout << '\n';
//            printf("\n");
        }
        else
        {
            for(int i=0;i<res.first/2;i++)
            {
//                cout << s[n-1-i];
//                printf("%c",s[n-1-i]);
                ress[cont]=s[n-1-i];
                cont++;
            }
            if(res.first&1)
            {
//                cout << s[n-1-res.first/2];
//                printf("%c",s[n-1-res.first/2]);
                ress[cont]=s[n-1-res.first/2];
                cont++;
            }
            for(int i=0;i<res.first/2;i++)
            {
//                cout << s[n-res.first/2+i];
//                printf("%c",s[n-res.first/2+i]);
                ress[cont]=s[n-res.first/2+i];
                cont++;
            }
//            printf("\n");
//            cout << '\n';
        }
        db(ress)
    }

    return 0;
}
/**
1
acbba
**/


#include <bits/stdc++.h>
#define db(x) cout << (x) << '\n';
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pii;
typedef pair<double,ll> pdi;
typedef pair<string,ll> psi;
typedef pair<ll,string> pls;
typedef pair<ll,pii> pip;
typedef pair<pii,pii> ppp;

vector<int> manacher(const string &s)
{
	int n = 2 * s.length();
	vector<int> rad(n);

	for (int i = 0, j = 0, k; i < n; i += k, j = max(j - k, 0))
	{
		for (; i >= j && i + j + 1 < n
			&& s[(i - j) / 2] == s[(i + j + 1) / 2]; ++j);
		rad[i] = j;
		for (k = 1; i >= k &&
			rad[i] >= k && rad[i - k] != rad[i] - k; ++k)
			rad[i + k] = min(rad[i - k], rad[i] - k);
	}

	return rad;
}

bool is_pal(const vector<int> &rad, int b, int e)
{
	int n = rad.size() / 2;
	return b >= 0 && e < n && rad[b + e] >= e - b + 1;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    while(t--)
    {
        string s;
        cin >> s;
        vector<int> v=manacher(s);
        int n=s.size();

        int ma=0;
        while(ma<n/2 && s[ma]==s[n-1-ma])
            ma++;

        pii res=pii(1,0);
        for(int i=0;i<n;i++)
        {
            int cant=min(n-i-1,i);
            if(cant<=ma)
                res=max(res,pii(cant*2+1,i));
            else
            {
                int def=cant-ma;
                if(is_pal(v,i-def,i+def))
                    res=max(res,pii(cant*2+1,i));
            }
        }

        for(int i=0;i<n-1;i++)
        {
            int cant=min(n-i-1,i+1);
            if(cant<=ma)
                res=max(res,pii(cant*2,i));
            else
            {
                int def=cant-ma;
                int l=i-def;
                int r=i+def;
                if(i+1<=n-i)
                    l++;
                else
                    r--;
                if(is_pal(v,l,r))
                    res=max(res,pii(cant*2,i));
            }
        }

        if(res.first&1)
        {
            if(res.second>n-res.second-1)
            {
                reverse(s.begin(),s.end());
                res.second=n-res.second-1;
            }
            for(int i=0;i<res.second;i++)
                cout << s[i];
            cout << s[res.second];
            for(int i=0;i<res.second;i++)
                cout << s[res.second-i-1];
            cout << '\n';
        }
        else
        {
            if(res.second+1>n-res.second-1)
            {
                reverse(s.begin(),s.end());
                res.second=n-res.second-2;
            }

            for(int i=0;i<=res.second;i++)
                cout << s[i];
            for(int i=0;i<=res.second;i++)
                cout << s[res.second-i];
            cout << '\n';
        }
    }

    return 0;
}


#include <bits/stdc++.h>
#define db(x) cout << (x) << '\n';
#define all(v) (v).begin(),(v).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pii;
typedef pair<double,ll> pdi;
typedef pair<string,ll> psi;
typedef pair<ll,string> pls;
typedef pair<ll,pii> pip;
typedef pair<pii,pii> ppp;

template<typename charT>
struct SuffixArray
{
	int n;
	vector<int> sa, rank, lcp;

	SuffixArray(const basic_string<charT> &s) :
		n(s.length() + 1), sa(n), rank(n), lcp(n)
	{
		vector<int> _sa(n), bucket(n);

		iota(sa.rbegin(), sa.rend(), 0);
		sort(next(sa.begin()), sa.end(),
			[&](int i, int j) { return s[i] < s[j]; });

		for (int i = 1, j = 0; i < n; ++i)
		{
			rank[sa[i]] = rank[sa[i - 1]] +
						  (i == 1 || s[sa[i - 1]] < s[sa[i]]);
			if (rank[sa[i]] != rank[sa[i - 1]])
				bucket[++j] = i;
		}

		for (int len = 1; len <= n; len += len)
		{
			for (int i = 0, j; i < n; ++i)
			{
				if ((j = sa[i] - len) < 0) j += n;
				_sa[bucket[rank[j]]++] = j;
			}

			sa[_sa[bucket[0] = 0]] = 0;

			for (int i = 1, j = 0; i < n; ++i)
			{
				if (rank[_sa[i]] != rank[_sa[i - 1]] ||
					rank[_sa[i] + len] != rank[_sa[i - 1] + len])
					bucket[++j] = i;

				sa[_sa[i]] = j;
			}

			copy(sa.begin(), sa.end(), rank.begin());
			sa.swap(_sa);

			if (rank[sa[n - 1]] == n - 1) break;
		}

		for (int i = 0, j = rank[lcp[0] = 0], k = 0; i < n - 1; ++i, ++k)
			while (k >= 0 && s[i] != s[sa[j - 1] + k])
				lcp[j] = k--, j = rank[sa[j] + 1];
	}
};
#define MAXN 2000005

int rmq[MAXN][21],lg[MAXN];
int pos[MAXN];

int query(int l,int r)
{
    if(l>r)
        swap(l,r);
    l++;
    int po=lg[r-l+1];
    return min(rmq[l][po],rmq[r-(1<<po)+1][po]);
}

char cc[MAXN];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    lg[0]=-1;
    for(int i=1;i<MAXN;i++)
        lg[i]=lg[i-1]+!(i&(i-1));

    int t;
//    cin >> t;
    scanf("%d",&t);
    while(t--)
    {
        string s;
//        cin >> s;

        scanf("%1000005s",cc);
        s=cc;

        int ma=0,n=s.size();
        for(int i=0;i<n/2;i++)
        {
            if(s[i]==s[n-i-1])
                ma++;
            else
                break;
        }
        string sss=s;
        reverse(all(sss));

        string ss=s+"#"+sss;

        SuffixArray<char> sa(ss);

        for(int i=0;i<sa.lcp.size();i++)
            rmq[i][0]=sa.lcp[i];
        for(int i=1;i<=lg[sa.lcp.size()];i++)
            for(int j=0;j+(1<<i)<=sa.lcp.size();j++)
                rmq[j][i]=min(rmq[j][i-1],rmq[j+(1<<(i-1))][i-1]);

//        db(ss)
//        db(ma)
        for(int i=0;i<sa.sa.size();i++)
        {
            pos[sa.sa[i]]=i;
//            cout << sa.sa[i] << ' ' << sa.lcp[i] << '\n';
        }
//        db("")

        pii res=pii(1,1);
        for(int i=0;i<n/2;i++)
        {
            if(ma>i)
                res=max(res,pii(2*(i+1),1));
            else
            {
                int faltan=i+1-ma;
                int p1=ma;
                int p2=2*n-(i+faltan);

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*(i+1),1));
            }
        }

        for(int i=0;i<(n+1)/2;i++)
        {
            if(ma>=i)
                res=max(res,pii(2*i+1,1));
            else
            {
                int faltan=i-ma;
                int p1=ma;
                int p2=2*n-(i+faltan);

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*i+1,1));
            }
        }

        for(int i=0;i<n/2;i++)
        {
            if(ma>i)
                res=max(res,pii(2*(i+1),1));
            else
            {
                int faltan=i+1-ma;
                int p1=n-1-i-faltan;
                int p2=n+ma+1;

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*(i+1),2));
            }
        }

        for(int i=0;i<(n+1)/2;i++)
        {
            if(ma>=i)
                res=max(res,pii(2*i+1,1));
            else
            {
                int faltan=i-ma;
                int p1=n-1-i-faltan;
                int p2=n+ma+1;

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*i+1,2));
            }
        }

//        cout << res.first << ' ' << res.second << '\n';
        string ress="";
        ress.resize(res.first);
        int cont=0;
        if(res.second==1)
        {
            for(int i=0;i<res.first/2;i++)
            {
//                cout << s[i];
//                printf("%c",s[i]);
                ress[cont]=s[i];
                cont++;
            }
            if(res.first&1)
            {
//                cout << s[res.first/2];
//                printf("%c",s[res.first/2]);
                ress[cont]=s[res.first/2];
                cont++;
            }
            for(int i=0;i<res.first/2;i++)
            {
//                cout << s[res.first/2-i-1];
//                printf("%c",s[res.first/2-i-1]);
                ress[cont]=s[res.first/2-i-1];
                cont++;
            }
//            cout << '\n';
//            printf("\n");
        }
        else
        {
            for(int i=0;i<res.first/2;i++)
            {
//                cout << s[n-1-i];
//                printf("%c",s[n-1-i]);
                ress[cont]=s[n-1-i];
                cont++;
            }
            if(res.first&1)
            {
//                cout << s[n-1-res.first/2];
//                printf("%c",s[n-1-res.first/2]);
                ress[cont]=s[n-1-res.first/2];
                cont++;
            }
            for(int i=0;i<res.first/2;i++)
            {
//                cout << s[n-res.first/2+i];
//                printf("%c",s[n-res.first/2+i]);
                ress[cont]=s[n-res.first/2+i];
                cont++;
            }
//            printf("\n");
//            cout << '\n';
        }
//        db(ress)
        printf("%s\n",ress.c_str());
//        cout << ress << '\n';
    }

    return 0;
}
/**
1
acbba
**/


#include <bits/stdc++.h>
#define db(x) cout << (x) << '\n';
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pii;
typedef pair<double,ll> pdi;
typedef pair<string,ll> psi;
typedef pair<ll,string> pls;
typedef pair<ll,pii> pip;
typedef pair<pii,pii> ppp;

vector<int> manacher(const string &s)
{
	int n = 2 * s.length();
	vector<int> rad(n);

	for (int i = 0, j = 0, k; i < n; i += k, j = max(j - k, 0))
	{
		for (; i >= j && i + j + 1 < n
			&& s[(i - j) / 2] == s[(i + j + 1) / 2]; ++j);
		rad[i] = j;
		for (k = 1; i >= k &&
			rad[i] >= k && rad[i - k] != rad[i] - k; ++k)
			rad[i + k] = min(rad[i - k], rad[i] - k);
	}

	return rad;
}

bool is_pal(const vector<int> &rad, int b, int e)
{
	int n = rad.size() / 2;
	return b >= 0 && e < n && rad[b + e] >= e - b + 1;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    while(t--)
    {
        string s;
        cin >> s;
        vector<int> v=manacher(s);
        int n=s.size();

        int ma=0;
        while(ma<n/2 && s[ma]==s[n-1-ma])
            ma++;

        pii res=pii(1,0);
        for(int i=0;i<n;i++)
        {
            int cant=min(n-i-1,i);
            if(cant<=ma)
                res=max(res,pii(cant*2+1,i));
            else
            {
                int def=cant-ma;
                if(is_pal(v,i-def,i+def))
                    res=max(res,pii(cant*2+1,i));
            }
        }

        for(int i=0;i<n-1;i++)
        {
            int cant=min(n-i-1,i+1);
            if(cant<=ma)
                res=max(res,pii(cant*2,i));
            else
            {
                int def=cant-ma;
                int l=i-def+1;
                int r=i+def;
                if(is_pal(v,l,r))
                    res=max(res,pii(cant*2,i));
            }
        }

        if(res.first&1)
        {
            if(res.second>n-res.second-1)
            {
                reverse(s.begin(),s.end());
                res.second=n-res.second-1;
            }
            for(int i=0;i<res.second;i++)
                cout << s[i];
            cout << s[res.second];
            for(int i=0;i<res.second;i++)
                cout << s[res.second-i-1];
            cout << '\n';
        }
        else
        {
            if(res.second+1>n-res.second-1)
            {
                reverse(s.begin(),s.end());
                res.second=n-res.second-2;
            }

            for(int i=0;i<=res.second;i++)
                cout << s[i];
            for(int i=0;i<=res.second;i++)
                cout << s[res.second-i];
            cout << '\n';
        }
    }

    return 0;
}


#include <bits/stdc++.h>
#define db(x) cout << (x) << '\n';
#define all(v) (v).begin(),(v).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pii;
typedef pair<double,ll> pdi;
typedef pair<string,ll> psi;
typedef pair<ll,string> pls;
typedef pair<ll,pii> pip;
typedef pair<pii,pii> ppp;

template<typename charT>
struct SuffixArray
{
	int n;
	vector<int> sa, rank, lcp;

	SuffixArray(const basic_string<charT> &s) :
		n(s.length() + 1), sa(n), rank(n), lcp(n)
	{
		vector<int> _sa(n), bucket(n);

		iota(sa.rbegin(), sa.rend(), 0);
		sort(next(sa.begin()), sa.end(),
			[&](int i, int j) { return s[i] < s[j]; });

		for (int i = 1, j = 0; i < n; ++i)
		{
			rank[sa[i]] = rank[sa[i - 1]] +
						  (i == 1 || s[sa[i - 1]] < s[sa[i]]);
			if (rank[sa[i]] != rank[sa[i - 1]])
				bucket[++j] = i;
		}

		for (int len = 1; len <= n; len += len)
		{
			for (int i = 0, j; i < n; ++i)
			{
				if ((j = sa[i] - len) < 0) j += n;
				_sa[bucket[rank[j]]++] = j;
			}

			sa[_sa[bucket[0] = 0]] = 0;

			for (int i = 1, j = 0; i < n; ++i)
			{
				if (rank[_sa[i]] != rank[_sa[i - 1]] ||
					rank[_sa[i] + len] != rank[_sa[i - 1] + len])
					bucket[++j] = i;

				sa[_sa[i]] = j;
			}

			copy(sa.begin(), sa.end(), rank.begin());
			sa.swap(_sa);

			if (rank[sa[n - 1]] == n - 1) break;
		}

		for (int i = 0, j = rank[lcp[0] = 0], k = 0; i < n - 1; ++i, ++k)
			while (k >= 0 && s[i] != s[sa[j - 1] + k])
				lcp[j] = k--, j = rank[sa[j] + 1];
	}
};
#define MAXN 500005

int rmq[MAXN][20],lg[MAXN];
int pos[MAXN];

int query(int l,int r)
{
    if(l>r)
        swap(l,r);
    l++;
    int po=lg[r-l+1];
    return min(rmq[l][po],rmq[r-(1<<po)+1][po]);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    cout.tie(0);

    lg[0]=-1;
    for(int i=1;i<MAXN;i++)
        lg[i]=lg[i-1]+!(i&(i-1));

    int t;
    cin >> t;
    while(t--)
    {
        string s;
        cin >> s;
        int ma=0,n=s.size();
        for(int i=0;i<n/2;i++)
        {
            if(s[i]==s[n-i-1])
                ma++;
            else
                break;
        }
        string sss=s;
        reverse(all(sss));

        string ss=s+"#"+sss;

        SuffixArray<char> sa(ss);

        for(int i=0;i<sa.lcp.size();i++)
            rmq[i][0]=sa.lcp[i];
        for(int i=1;i<=lg[sa.lcp.size()];i++)
            for(int j=0;j+(1<<i)<=sa.lcp.size();j++)
                rmq[j][i]=min(rmq[j][i-1],rmq[j+(1<<(i-1))][i-1]);

//        db(ss)
//        db(ma)
        for(int i=0;i<sa.sa.size();i++)
        {
            pos[sa.sa[i]]=i;
//            cout << sa.sa[i] << ' ' << sa.lcp[i] << '\n';
        }
//        db("")

        pii res=pii(1,1);
        for(int i=0;i<n/2;i++)
        {
            if(ma>i)
                res=max(res,pii(2*(i+1),1));
            else
            {
                int faltan=i+1-ma;
                int p1=ma;
                int p2=2*n-(i+faltan);

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*(i+1),1));
            }
        }

        for(int i=0;i<(n+1)/2;i++)
        {
            if(ma>=i)
                res=max(res,pii(2*i+1,1));
            else
            {
                int faltan=i-ma;
                int p1=ma;
                int p2=2*n-(i+faltan);

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*i+1,1));
            }
        }

        for(int i=0;i<n/2;i++)
        {
            if(ma>i)
                res=max(res,pii(2*(i+1),1));
            else
            {
                int faltan=i+1-ma;
                int p1=n-1-i-faltan;
                int p2=n+ma+1;

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*(i+1),2));
            }
        }

        for(int i=0;i<(n+1)/2;i++)
        {
            if(ma>=i)
                res=max(res,pii(2*i+1,1));
            else
            {
                int faltan=i-ma;
                int p1=n-1-i-faltan;
                int p2=n+ma+1;

                if(query(pos[p1],pos[p2])>=faltan)
                    res=max(res,pii(2*i+1,2));
            }
        }

//        cout << res.first << ' ' << res.second << '\n';
        string ress="";
        ress.resize(res.first);
        int cont=0;
        if(res.second==1)
        {
            for(int i=0;i<res.first/2;i++)
            {
//                cout << s[i];
//                printf("%c",s[i]);
                ress[cont]=s[i];
                cont++;
            }
            if(res.first&1)
            {
//                cout << s[res.first/2];
//                printf("%c",s[res.first/2]);
                ress[cont]=s[res.first/2];
                cont++;
            }
            for(int i=0;i<res.first/2;i++)
            {
//                cout << s[res.first/2-i-1];
//                printf("%c",s[res.first/2-i-1]);
                ress[cont]=s[res.first/2-i-1];
                cont++;
            }
//            cout << '\n';
//            printf("\n");
        }
        else
        {
            for(int i=0;i<res.first/2;i++)
            {
//                cout << s[n-1-i];
//                printf("%c",s[n-1-i]);
                ress[cont]=s[n-1-i];
                cont++;
            }
            if(res.first&1)
            {
//                cout << s[n-1-res.first/2];
//                printf("%c",s[n-1-res.first/2]);
                ress[cont]=s[n-1-res.first/2];
                cont++;
            }
            for(int i=0;i<res.first/2;i++)
            {
//                cout << s[n-res.first/2+i];
//                printf("%c",s[n-res.first/2+i]);
                ress[cont]=s[n-res.first/2+i];
                cont++;
            }
//            printf("\n");
//            cout << '\n';
        }
        db(ress)
    }

    return 0;
}
/**
1
acbba
**/


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>

using namespace std;

#define ff first
#define ss second
#define pii pair<int, int>
#define pb push_back
#define ll long long
#define sz size()

const int N = 1e6 + 5;
const ll MOD = 998244353;

struct Hash {
  struct base {
    string s; int b, mod;
    vector<int> hash, p;
    void init(string &_s, int _b, int _mod) { // b > 26, prime.
      s = _s; b = _b, mod = _mod;
      hash.resize(s.size());
      p.resize(s.size());
      hash[0] = s[0] - 'A' + 1; p[0] = 1;
      for(int i = 1; i < s.size(); ++i) {
        hash[i] = (ll) hash[i - 1] * b % mod;
        hash[i] += s[i] - 'A' + 1;
        if(hash[i] >= mod) hash[i] -= mod;
        p[i] = (ll) p[i - 1] * b % mod;
      }
    }
    int get(int l, int r) {
      int ret = hash[r];
      if(l) ret -= (ll) hash[l - 1] * p[r - l + 1] % mod;
      if(ret < 0) ret += mod;
      return ret;
    }
  } h[1];
  void init(string &s) {
    h[0].init(s, 31, 1e9+7);
  }
  int get(int l, int r) {
    return h[0].get(l, r);
  }
};

char s[N];

string get(string s){

  Hash H1, H2;
  H1.init(s);
  reverse(s.begin(), s.end());
  H2.init(s);

  string ret = "", now = "";
  int n = s.size();

  now += s[n - 1];
  ret = now;
  for(int i = 1, j = n - 2; i < n; i++, j--){
    now += s[j];
    int len = i + 1;
    int f1, f2, t1, t2;
    if(len & 1){
      f1 = 0, t1 = i/2 - 1;
      f2 = n - 1 - i, t2 = n - 2 - (i/2);
    }
    else {
      f1 = 0, t1 = i/2;
      f2 = n - 1 - i, t2 = n - 2 - (i/2);
    }
    if(H1.get(f1, t1) == H2.get(f2, t2))ret = now;
  }
  return ret;
}

int main() {

  int t;
  scanf("%d",&t);

  while(t--){
    scanf("%s",s);
    int n = strlen(s);
    int matchTo = -1;
    for(int i = 0, j = n - 1; i < n; i++, j--){
      if(s[i] != s[j])break;
      matchTo = i;
    }

    if(matchTo == n - 1)printf("%s\n", s);
    else {
      string A, B1 = "", B2 = "", D = "";
      for(int i = 0; i <= matchTo; i++)A += s[i];

      for(int i = matchTo + 1; i < n - (matchTo + 1); i++)D += s[i];
      B1 = get(D);
      // cout << "1 " << D << ' ' << B1 << endl;
      reverse(D.begin(), D.end());
      // cout << "2 " << D << ' ' << B2 << endl;
      B2 = get(D);

      if(B2.sz > B1.sz){
        A += B2;
      }
      else A += B1;

      for(int j = n - (matchTo + 1); j < n; j++)A += s[j];
      printf("%s\n", A.c_str());
    }
  }

  return 0;
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>

using namespace std;

#define ff first
#define ss second
#define pii pair<int, int>
#define pb push_back
#define ll long long
#define sz size()

const int N = 1e6 + 5;
const ll MOD = 998244353;

struct Hash {
  struct base {
    string s; int b, mod;
    vector<int> hash, p;
    void init(string &_s, int _b, int _mod) { // b > 26, prime.
      s = _s; b = _b, mod = _mod;
      hash.resize(s.size());
      p.resize(s.size());
      hash[0] = s[0] - 'A' + 1; p[0] = 1;
      for(int i = 1; i < s.size(); ++i) {
        hash[i] = (ll) hash[i - 1] * b % mod;
        hash[i] += s[i] - 'A' + 1;
        if(hash[i] >= mod) hash[i] -= mod;
        p[i] = (ll) p[i - 1] * b % mod;
      }
    }
    int get(int l, int r) {
      int ret = hash[r];
      if(l) ret -= (ll) hash[l - 1] * p[r - l + 1] % mod;
      if(ret < 0) ret += mod;
      return ret;
    }
  } h[1];
  void init(string &s) {
    h[0].init(s, 31, 1e9+7);
  }
  int get(int l, int r) {
    return h[0].get(l, r);
  }
};

char s[N];

string get(string &s){

  Hash H1, H2;
  H1.init(s);
  reverse(s.begin(), s.end());
  H2.init(s);

  string ret = "", now = "";
  int n = s.size();

  now += s[n - 1];
  ret = now;
  for(int i = 1, j = n - 2; i < n; i++, j--){
    now += s[j];
    int len = i + 1;
    int f1, f2, t1, t2;
    if(len & 1){
      f1 = 0, t1 = i/2 - 1;
      f2 = n - 1 - i, t2 = n - 2 - (i/2);
    }
    else {
      f1 = 0, t1 = i/2;
      f2 = n - 1 - i, t2 = n - 2 - (i/2);
    }
    if(H1.get(f1, t1) == H2.get(f2, t2))ret = now;
  }
  return ret;
}

int main() {

  int t;
  scanf("%d",&t);

  while(t--){
    scanf("%s",s);
    int n = strlen(s);
    int matchTo = -1;
    for(int i = 0, j = n - 1; i < n; i++, j--){
      if(s[i] != s[j])break;
      matchTo = i;
    }

    if(matchTo == n - 1)printf("%s\n", s);
    else {
      string A, B1 = "", B2 = "", D = "";
      for(int i = 0; i <= matchTo; i++)A += s[i];

      for(int i = matchTo + 1; i < n - (matchTo + 1); i++)D += s[i];
      B1 = get(D);
      // cout << "1 " << D << ' ' << B1 << endl;
      reverse(D.begin(), D.end());
      // cout << "2 " << D << ' ' << B2 << endl;
      B2 = get(D);

      if(B2.sz > B1.sz){
        A += B2;
      }
      else A += B1;

      for(int j = n - (matchTo + 1); j < n; j++)A += s[j];
      printf("%s\n", A.c_str());
    }
  }

  return 0;
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>

using namespace std;

#define ff first
#define ss second
#define pii pair<int, int>
#define pb push_back
#define ll long long
#define sz size()

const int N = 1e6 + 5;
const ll MOD = 998244353;

struct Hash {
  struct base {
    string s; int b, mod;
    vector<int> hash, p;
    void init(string &_s, int _b, int _mod) { // b > 26, prime.
      s = _s; b = _b, mod = _mod;
      hash.resize(s.size());
      p.resize(s.size());
      hash[0] = s[0] - 'A' + 1; p[0] = 1;
      for(int i = 1; i < s.size(); ++i) {
        hash[i] = (ll) hash[i - 1] * b % mod;
        hash[i] += s[i] - 'A' + 1;
        if(hash[i] >= mod) hash[i] -= mod;
        p[i] = (ll) p[i - 1] * b % mod;
      }
    }
    int get(int l, int r) {
      int ret = hash[r];
      if(l) ret -= (ll) hash[l - 1] * p[r - l + 1] % mod;
      if(ret < 0) ret += mod;
      return ret;
    }
  } h[2];
  void init(string &s) {
    h[0].init(s, 29, 1e9+7);
    h[1].init(s, 41, 1e9+9);
  }
  pair<int, int> get(int l, int r) {
    return { h[0].get(l, r), h[1].get(l, r) };
  }
};

char s[N];

string get(string s){

  Hash H1, H2;
  H1.init(s);
  reverse(s.begin(), s.end());
  H2.init(s);

  int n = s.size();
  int k = n;
  for(int i = n - 1, j = 0; i >= 1; i--, j++){
    int len = i + 1;
    int f1, f2, t1, t2;
    if(len & 1){
      f1 = 0, t1 = i/2 - 1;
      f2 = n - 1 - i, t2 = n - 2 - (i/2);
    }
    else {
      f1 = 0, t1 = i/2;
      f2 = n - 1 - i, t2 = n - 2 - (i/2);
    }
    if(H1.get(f1, t1) == H2.get(f2, t2))break;
    k--;
  }
  string ret = "";
  for(int i = n - 1, j = 0; j < k; i--, j++) ret += s[i];
  return ret;
}

int main() {

  int t;
  scanf("%d",&t);

  while(t--){
    scanf("%s",s);
    int n = strlen(s);
    int matchTo = -1;
    for(int i = 0, j = n - 1; i < n; i++, j--){
      if(s[i] != s[j])break;
      matchTo = i;
    }

    if(matchTo == n - 1)printf("%s\n", s);
    else {
      string A, B1 = "", B2 = "", D = "";
      for(int i = 0; i <= matchTo; i++)A += s[i];

      for(int i = matchTo + 1; i < n - (matchTo + 1); i++)D += s[i];
      B1 = get(D);
      // cout << "1 " << D << ' ' << B1 << endl;
      reverse(D.begin(), D.end());
      // cout << "2 " << D << ' ' << B2 << endl;
      B2 = get(D);

      if(B2.sz > B1.sz){
        A += B2;
      }
      else A += B1;

      for(int j = n - (matchTo + 1); j < n; j++)A += s[j];
      printf("%s\n", A.c_str());
    }
  }

  return 0;
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>

using namespace std;

#define ff first
#define ss second
#define pii pair<int, int>
#define pb push_back
#define ll long long
#define sz size()

const int N = 1e6 + 5;
const ll MOD = 998244353;

char s[N], S[N];
int pi[N];

string get(string t) {

    int len = t.sz;
    for(int i = 0; i < len; i++)S[i] = t[i];
    S[len] = 0;

 // append reverse_of(S) with S itself
 for(int i = len-1, j = len; i >= 0; --i, ++j)
 {
     S[j] = S[i];
 }

 // KMP pre-processing Begins
 pi[0] = -1; // pi[0] is undefined

 for(int i = 0; i < len*2; ++i)
 {
     int k = pi[i];

     while(k >= 0 && S[k]!=S[i])
     k = pi[k];

     pi[i+1] = k+1;
 }
 // KMP pre-processing Ends

 int L = min(len,pi[len*2]); // length of the longest palindrome-prefix

  string ret = "";

  for(int i = 0; i < L; i++)ret += t[i];

  return ret;

 // memset(P,false,sizeof(P)); // set all P[k] to false

 // while(L >= 0)
 // {
 //     P[L] = true;
 //     L = pi[L];
 // }
}

int main() {

  int t;
  scanf("%d",&t);

  while(t--){
    scanf("%s",s);
    int n = strlen(s);
    int matchTo = -1;
    for(int i = 0, j = n - 1; i < n; i++, j--){
      if(s[i] != s[j])break;
      matchTo = i;
    }

    if(matchTo == n - 1)printf("%s\n", s);
    else {
      string A, B1 = "", B2 = "", D = "";
      for(int i = 0; i <= matchTo; i++)A += s[i];

      for(int i = matchTo + 1; i < n - (matchTo + 1); i++)D += s[i];
      B1 = get(D);
      // cout << "1 " << D << ' ' << B1 << endl;
      reverse(D.begin(), D.end());
      // cout << "2 " << D << ' ' << B2 << endl;
      B2 = get(D);

      if(B2.sz > B1.sz){
        A += B2;
      }
      else A += B1;

      for(int j = n - (matchTo + 1); j < n; j++)A += s[j];
      printf("%s\n", A.c_str());
    }
  }

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ff first
#define ss second
#define pii pair<int, int>
#define pb push_back
#define ll long long
#define sz size()

const int N = 1e6 + 5;
const ll MOD = 998244353;

struct Hash {
  struct base {
    string s; int b, mod;
    vector<int> hash, p;
    void init(string &_s, int _b, int _mod) { // b > 26, prime.
      s = _s; b = _b, mod = _mod;
      hash.resize(s.size());
      p.resize(s.size());
      hash[0] = s[0] - 'A' + 1; p[0] = 1;
      for(int i = 1; i < s.size(); ++i) {
        hash[i] = (ll) hash[i - 1] * b % mod;
        hash[i] += s[i] - 'A' + 1;
        if(hash[i] >= mod) hash[i] -= mod;
        p[i] = (ll) p[i - 1] * b % mod;
      }
    }
    int get(int l, int r) {
      int ret = hash[r];
      if(l) ret -= (ll) hash[l - 1] * p[r - l + 1] % mod;
      if(ret < 0) ret += mod;
      return ret;
    }
  } h[2];
  void init(string &s) {
    h[0].init(s, 29, 1e9+7);
    h[1].init(s, 31, 1e9+9);
  }
  pair<int, int> get(int l, int r) {
    return { h[0].get(l, r), h[1].get(l, r) };
  }
};

char s[N];

string get(string s){

  Hash H1, H2;
  H1.init(s);
  reverse(s.begin(), s.end());
  H2.init(s);

  string ret = "", now = "";
  int n = s.size();

  now += s[n - 1];
  ret = now;
  for(int i = 1, j = n - 2; i < n; i++, j--){
    now += s[j];
    int len = i + 1;
    int f1, f2, t1, t2;
    if(len & 1){
      f1 = 0, t1 = i/2 - 1;
      f2 = n - 1 - i, t2 = n - 2 - (i/2);
    }
    else {
      f1 = 0, t1 = i/2;
      f2 = n - 1 - i, t2 = n - 2 - (i/2);
    }
    if(H1.get(f1, t1) == H2.get(f2, t2))ret = now;
  }
  return ret;
}

int main() {

  int t;
  scanf("%d",&t);

  while(t--){
    scanf("%s",s);
    int n = strlen(s);
    int matchTo = -1;
    for(int i = 0, j = n - 1; i < n; i++, j--){
      if(s[i] != s[j])break;
      matchTo = i;
    }

    if(matchTo == n - 1)printf("%s\n", s);
    else {
      string A, B1 = "", B2 = "", C = "", D = "";
      for(int i = 0; i <= matchTo; i++)A.pb(s[i]);

      for(int i = matchTo + 1; i < n - (matchTo + 1); i++)D.pb(s[i]);
      B1 = get(D);
      // cout << "1 " << D << ' ' << B1 << endl;
      reverse(D.begin(), D.end());
      // cout << "2 " << D << ' ' << B2 << endl;
      B2 = get(D);

      if(B2.sz > B1.sz){
        A += B2;
      }
      else A += B1;

      for(int j = n - 1, i = 0; i <= matchTo; i++, j--)C.push_back(s[i]);
      reverse(C.begin(), C.end());
      A += C;
      printf("%s\n", A.c_str());
    }
  }

  return 0;
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>

using namespace std;

#define ff first
#define ss second
#define pii pair<int, int>
#define pb push_back
#define ll long long
#define sz size()

const int N = 1e6 + 5;
const ll MOD = 998244353;

char s[N], S[2 * N];
int pi[2 * N];

string get(string t) {

    int len = t.sz;
    for(int i = 0; i < len; i++)S[i] = t[i];
    S[len] = 0;

 // append reverse_of(S) with S itself
 for(int i = len-1, j = len; i >= 0; --i, ++j)
 {
     S[j] = S[i];
 }
 for(int i = 0; i <= 2 * len; i++)pi[i] = 0;
 // KMP pre-processing Begins
 pi[0] = -1; // pi[0] is undefined

 for(int i = 0; i < len*2; ++i)
 {
     int k = pi[i];

     while(k >= 0 && S[k]!=S[i])
     k = pi[k];

     pi[i+1] = k+1;
 }
 // KMP pre-processing Ends

 int L = min(len,pi[len*2]); // length of the longest palindrome-prefix

  string ret = "";

  for(int i = 0; i < L; i++)ret += t[i];

  return ret;
}

int main() {

  int t;
  scanf("%d",&t);

  while(t--){
    scanf("%s",s);
    int n = strlen(s);
    int matchTo = -1;
    for(int i = 0, j = n - 1; i < n; i++, j--){
      if(s[i] != s[j])break;
      matchTo = i;
    }

    if(matchTo == n - 1)printf("%s\n", s);
    else {
      string A, B1 = "", B2 = "", D = "";
      for(int i = 0; i <= matchTo; i++)A += s[i];

      for(int i = matchTo + 1; i < n - (matchTo + 1); i++)D += s[i];
      B1 = get(D);
      // cout << "1 " << D << ' ' << B1 << endl;
      reverse(D.begin(), D.end());
      // cout << "2 " << D << ' ' << B2 << endl;
      B2 = get(D);

      if(B2.sz > B1.sz){
        A += B2;
      }
      else A += B1;

      for(int j = n - (matchTo + 1); j < n; j++)A += s[j];
      printf("%s\n", A.c_str());
    }
  }

  return 0;
}


#include <cstdio>
#include <string.h>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main() {
  int n, m, a[200000], l[200000], r[200000];
   scanf("%d %d", &n, &m);
  int b;
  for(int i = 0; i<n; i++) {
      scanf("%d", &a[i]);
      if(a[i] == -1) {
        b+=1;
      }
  }
  for(int i = 0; i<m; i++) {
      scanf("%d %d", &l[i], &r[i]);
      if( b < (r[i] - l[i] +1) / 2 || (r[i] - l[i] + 1) % 2 == 1) {
        printf("0\n");
      } else {
        printf("1\n");
      }
  }
  return 0;
}


#include<iostream>
using namespace std;
int A, n, m, l, r;
long long int mus, man;
bool B[200001];
int main()
{
	cin>>n>>m;
	for(int i=1; i<=n; i++)
	{
		cin>>A;
		if(A>0) mus++;
		else man++;
	}
	for(int i=1; i<=m; i++)
	{
		cin>>l>>r;
		if(((r-l+1)/2)<=mus && ((r-l+1)/2)<=man && (r-l+1)%2==0) B[i]=1;
		else B[i]=0;
	}
	for(int i=1; i<=m; i++)	cout<<B[i]<<"\n";
	return 0;
}


#include<iostream>
using namespace std;
int n, m, l, r, mus, man;
bool B[200001];
short A;
int main()
{
	cin>>n>>m;
	for(int i=1; i<=n; i++)
	{
		cin>>A;
		if(A>0) mus++;
		else man++;
	}
	for(int i=1; i<=m; i++)
	{
		cin>>l>>r;
		if(((r-l+1)/2)<=mus && ((r-l+1)/2)<=man && (r-l+1)%2==0) B[i]=1;
		else B[i]=0;
	}
	for(int i=1; i<=m; i++)	cout<<B[i]<<"\n";
	return 0;
}


#include<iostream>
using namespace std;
int A, n, m, l, r;
long long int mus, man;
bool B[200001];
int main()
{
	cin>>n>>m;
	for(int i=1; i<=n; i++)
	{
		cin>>A;
		if(A>0) mus++;
		else man++;
	}
	for(int i=1; i<=m; i++)
	{
		cin>>l>>r;
		if(((r-l+1)/2)<=mus && ((r-l+1)/2)<=man && (r-l+1)%2==0) cout<<"1\n";//B[i]=1;
		else cout<<"1\n"; //B[i]=0;
	}
	//for(int i=1; i<=m; i++)	cout<<B[i]<<"\n";
	return 0;
}


#include<iostream>
using namespace std;
int A, l, r;
long long int mus, man, n, m;
bool B[200001];
int main()
{
	cin>>n>>m;
	for(int i=1; i<=n; i++)
	{
		cin>>A;
		if(A>0) mus++;
		else man++;
	}
	for(int i=1; i<=m; i++)
	{
		cin>>l>>r;
		if(((r-l+1)/2)<=mus && ((r-l+1)/2)<=man && (r-l+1)%2==0) B[i]=1;
		else B[i]=0;
	}
	for(int i=1; i<=m; i++)	cout<<B[i]<<"\n";
	return 0;
}


#include<iostream>
using namespace std;
int A, n, m, l, r;
long long int mus, man;
bool B[200001];
int main()
{
	cin>>n>>m;
	for(int i=1; i<=n; i++)
	{
		cin>>A;
		if(A>0) mus++;
		else man++;
	}
	for(int i=1; i<=m; i++)
	{
		cin>>l>>r;
		if(((r-l+1)/2)<=mus && ((r-l+1)/2)<=man && (r-l)%2!=0) B[i]=1;
		else B[i]=0;
	}
	for(int i=1; i<=m; i++)	cout<<B[i]<<"\n";
	return 0;
}


#include<iostream>
using namespace std;
int A, n, m, l, r;
long long int mus, man;
bool B[200001];
int main()
{
	cin>>n>>m;
	for(int i=1; i<=n; i++)
	{
		cin>>A;
		if(A>0) mus++;
		else man++;
	}
	for(int i=1; i<=m; i++)
	{
		cin>>l>>r;
		if(((r-l+1)/2)<=mus && ((r-l+1)/2)<=man && (r-l+1)%2==0) cout<<"1\n";//B[i]=1;
		else cout<<"0\n"; //B[i]=0;
	}
	//for(int i=1; i<=m; i++)	cout<<B[i]<<"\n";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int A, n, m, l, r, mus, man, k;
int main()
{
	cin>>n>>m;
	for(int i=1; i<=n; i++)
	{
		cin>>A;
		if(A>0) mus++;
		else man++;
	}
	k = min(mus, man);
	for(int i=1; i<=m; i++)
	{
		cin>>l>>r;
		if((r-l+1)/2<=k && (r-l+1)%2==0 && k!=0) cout<<(true)<<"\n";
		else cout<<(false)<<"\n";
	}
	return 0;
}


#include<iostream>
using namespace std;
long long int A, n, m, l, r, mus, man, k;
bool B[200001];
int main()
{
	cin>>n>>m;
	for(int i=1; i<=n; i++)
	{
		cin>>A;
		if(A>0) mus++;
		else man++;
	}
	k = min(mus, man);
	for(int i=1; i<=m; i++)
	{
		cin>>l>>r;
		if((r-l+1)/2<=k && (r-l+1)%2==0 && k!=0) B[i]=true;
		else B[i]=false;
	}
	for(int i=1; i<=m; i++)	cout<<B[i]<<"\n";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long int A, n, m, l, r, mus, man, k;
int main()
{
	cin>>n>>m;
	for(int i=1; i<=n; i++)
	{
		cin>>A;
		if(A>0) mus++;
		else man++;
	}
	k = min(mus, man);
	for(int i=1; i<=m; i++)
	{
		cin>>l>>r;
		if((r-l+1)/2<=k && (r-l+1)%2==0 && k!=0) cout<<(true)<<"\n";
		else cout<<(false)<<"\n";
	}
	return 0;
}


#include<iostream>
using namespace std;
int mus, man, A[200001], n, m, l, r, B[200001];
int main()
{
	cin>>n>>m;
	for(int i=1; i<=n; i++)
	{
		cin>>A[i];
		if(A[i]>0) mus++;
		else man++;
	}
	for(int i=1; i<=m; i++)
	{
		cin>>l>>r;
		if((r-l+1)<=mus+man && (r-l+1)%2==0) B[i]=1;
		else B[i]=0;
	}
	for(int i=1; i<=m; i++)	cout<<B[i]<<"\n";
	return 0;
}


#include<iostream>
using namespace std;
int A, n, m, l, r, B[200001];
long long int mus, man;
int main()
{
	cin>>n>>m;
	for(int i=1; i<=n; i++)
	{
		cin>>A;
		if(A>0) mus++;
		else man++;
	}
	for(int i=1; i<=m; i++)
	{
		cin>>l>>r;
		if((r-l+1)/2<=mus && (r-l+1)/2<=man && (r-l+1)%2==0) B[i]=1;
		else B[i]=0;
	}
	for(int i=1; i<=m; i++)	cout<<B[i]<<"\n";
	return 0;
}


#include<iostream>
using namespace std;
int mus, man, A[200001], n, m, l, r, B[200001];
int main()
{
	cin>>n>>m;
	for(int i=1; i<=n; i++)
	{
		cin>>A[i];
		if(A[i]>0) mus++;
		else man++;
	}
	for(int i=1; i<=m; i++)
	{
		cin>>l>>r;
		if(((r-l+1)/2)<=mus+man && (r-l+1)%2==0) B[i]=1;
		else B[i]=0;
	}
	for(int i=1; i<=m; i++)	cout<<B[i]<<"\n";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int A, n, m, l, r, mus, k;
int main()
{
	cin>>n>>m;
	for(int i=1; i<=n; i++)
	{
		scanf("%d", &A);
		if(A>0) mus++;
	}
	k = min(mus, n-mus);
	for(int i=1; i<=m; i++)
	{
		scanf("%d %d", &l, &r);
		if((r-l+1)/2<=k && (r-l+1)%2==0 && k!=0) printf("%d \n", 1);
		else printf("%d \n", 0);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int A, n, m, l, r, mus, k;
int main()
{
	cin>>n>>m;
	for(int i=1; i<=n; i++)
	{
		cin>>A;
		if(A>0) mus++;
	}
	k = min(mus, n-mus);
	for(int i=1; i<=m; i++)
	{
		cin>>l>>r;
		if((r-l+1)/2<=k && (r-l+1)%2==0 && k!=0) cout<<"1\n";
		else cout<<"0\n";
	}
	return 0;
}


#include<iostream>
using namespace std;
long long int A, n, m, l, r, mus, man, k;
bool B[200001];
int main()
{
	cin>>n>>m;
	for(int i=1; i<=n; i++)
	{
		cin>>A;
		if(A>0) mus++;
		else man++;
	}
	k = min(mus, man);
	for(int i=1; i<=m; i++)
	{
		cin>>l>>r;
		if((r-l+1)/2<=k && (r-l+1)%2==0) B[i]=true;
		else B[i]=false;
	}
	for(int i=1; i<=m; i++)	cout<<B[i]<<"\n";
	return 0;
}


#include<iostream>
using namespace std;
long long int n, m, mus, man;
bool B[200001];
short A;
int l, r;
int main()
{
	cin>>n>>m;
	for(int i=1; i<=n; i++)
	{
		cin>>A;
		if(A>0) mus++;
		else man++;
	}
	for(int i=1; i<=m; i++)
	{
		cin>>l>>r;
		if(((r-l+1)/2)<=mus && ((r-l+1)/2)<=man && (r-l+1)%2==0) B[i]=1;
		else B[i]=0;
	}
	for(int i=1; i<=m; i++)	cout<<B[i]<<"\n";
	return 0;
}


#include <stdio.h>
#include <math.h>
int main()
{
	int n,m;
	int c1,c2,x;
	int k,t,i;
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		c1 = c2 = 0;
		for(i = 0;i<n;i++)
		{
			scanf("%d",&x);
			if(x == 1)
				c1++;
			else if(x==-1)
				c2++;
		}
		for(i = 0;i<m;i++)
		{
			scanf("%d %d",&k,&t);
			if((k-t)%2 == 0)
				printf("0\n");
			else
			{
				if(abs(c1-c2)>(k-t))
					printf("1\n");
				else
					printf("0\n");
			}
		}

	}
	return 0;
}


#include<stdio.h>
int s[2000001];
int main()
{
    int n, m, a, b, t=0;
    int count1=0, count2=0;
    scanf("%d %d", &n, &m);
    for(int i=0; i<n; i++)
    {
        scanf("%d", &s[i]);
        if(s[i]==-1)
            count1++;
        else
            count2++;
    }
    if(count1>count2)
        t = count2;
    else
        t = count1;
    while(m--)
    {
        scanf("%d %d", &a, &b);
        int p = b-a+1;
        if(p%2!=0)
            printf("0\n");
        else
        {
            if(p/2<=t)
                printf("1\n");
            else
                printf("0\n");
        }
    }

    return 0;
}


#include<stdio.h>
#include<math.h>
int s[10001];
int main()
{
    int n, m, a, b, t=0;
    int count1=0, count2=0;
    while(scanf("%d %d", &n, &m)!=EOF)
    {
    for(int i=0; i<n; i++)
    {
        scanf("%d", &s[i]);
        if(s[i]==-1)
            count1++;
        else
            count2++;
    }
    if(count1>count2)
        t = count2;
    else
        t = count1;
    while(m--)
    {
        scanf("%d %d", &a, &b);
        int p = b-a+1;
        if(p%2!=0)
            printf("0\n");
        else
        {
            if(p/2<=t)
                printf("1\n");
            else
                printf("%0\n");
        }
    }
    }
    return 0;
}


#include<stdio.h>
#include<math.h>
int s[10001];
int main()
{
    int n, m, a, b, t=0;
    int count1=0, count2=0;
    scanf("%d %d", &n, &m);
    for(int i=0; i<n; i++)
    {
        scanf("%d", &s[i]);
        if(s[i]==-1)
            count1++;
        else
            count2++;
    }
    if(count1>count2)
        t = count2;
    else
        t = count1;
    while(m--)
    {
        scanf("%d %d", &a, &b);
        int p = b-a+1;
        if(p%2!=0)
            printf("0\n");
        else
        {
            if(p/2<=t)
                printf("1\n");
            else
                printf("%0\n");
        }
    }
    return 0;
}


#include <stdio.h>
#include <string.h>

#define M 200

#define min c1<c2?c1:c2
int a[M];
int main(void)
{
    memset(a,0,sizeof(a));
    int n,m,i,w;
    int l,r,c1=0,c2=0;
    scanf("%d %d",&n,&m);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]==-1)
            c1++;
        else
            c2++;
    }
    while(m--)
    {
        scanf("%d %d",&l,&r);
        w=r-l+1;
        if(w%2==0&&w/2<=min)
        {
            printf("1\n");
        }
        else
            printf("0\n");
    }
    return 0;
}


#include<stdio.h>
#include<math.h>
int main()
{
	int x,f,e,a;
	long long n,m,i,l,k,g;
     while(scanf("%lld%lld",&n,&m)!=EOF)
	 {
	     l=0;
		 k=0;
	for(i=1;i<=n;i++)
	{
		scanf("%d",&a);
	    if(a==1)
		{
			l++;
		}
		else
		{
			k++;
		}
	}
	 if(l>=k)
	 {
		 g=k;
	 }
	 else
	 {
		 g=l;
	 }
	for(i=1;i<=m;i++)
	{
		scanf("%d%d",&e,&f);
		x=f-e+1;
	if(x%2==1)
	{
		printf("0\n");
	}
	else
	{
	if(x<=2*g)
	{
		printf("1\n");
	}
	else
	{
		printf("0\n");
	}
	}
	 }
	 }
		return 0;
	 }


#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
using namespace std;

int main()
{
    int n,m,i,j,a[100000],x,y,sum,s,t;
    scanf("%d%d",&n,&m);
    s=0;t=0;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]==1) s++;
        if(a[i]==-1) t++;
    }
    for(i=0;i<m;i++)
    {
        scanf("%d%d",&x,&y);
        if((y-x+1)%2!=0) printf("0\n");
        else if(((y-x+1)/2)<=s&&((y-x+1)/2)<=t)
        printf("1\n");
    }
}


#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
/*struct node
{
    int data;
    struct node *next;
};
struct node*lianbiao(int n)
{
    struct node *head,*p,*tail;
    head=(struct node*)malloc(sizeof(struct node));
    head->next=NULL;
    tail=head;
    for(int i=1;i<=n;i++)
    {
        p=(struct node*)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next=NULL;
        tail->next=p;
        tail=p;
    }
       return head;
}
int main()
{
    int n;
     struct node *head,*p;
    scanf("%d",&n);
     head=lianbiao(n);
     p=head;
    while(p->next!=NULL)
     {
         if(p->next->next!=NULL)
         printf("%d ",p->next->data);
         else printf("%d\n",p->next->data);
            p=p->next;
     }
}
struct node
{
    int data;
    struct node*next;
};
struct node*lianbiao(int n)
{
    struct node*head,*p;
    head=(struct node*)malloc(sizeof(struct node));
    head->next=NULL;
    for(int i=1; i<=n; i++)
    {
        p=(struct node*)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next=head->next;
        head->next=p;
    }
    return head;
}
int main()
{
    int n;

    struct node*head,*p;
    scanf("%d",&n);
    head=lianbiao(n);
    p=head;
    while(p->next!=NULL)
    {
        if(p->next->next!=NULL)
            printf("%d ",p->next->data);
        else printf("%d\n",p->next->data);
        p=p->next;
    }
}*/
int main()
{
    int n,m,i,j,a[100000],x,y,sum,s,t;
    scanf("%d%d",&n,&m);
    s=0;t=0;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]==1) s++;
        if(a[i]==-1) t++;
    }
    for(i=0;i<m;i++)
    {
        scanf("%d%d",&x,&y);
        if((y-x+1)%2!=0) printf("0\n");
        else if(((y-x+1)/2)<=s&&((y-x+1)/2)<=t)
        printf("1\n");
            else printf("0\n");
    }
}


#include <stdio.h>
#include <math.h>
int main()
{
	long n,m;
	int c1,c2;
	long x;
	long k,t,i;
	while(scanf("%ld %ld",&n,&m)!=EOF)
	{
		c1 = c2 = 0;
		for(i = 0;i<n;i++)
		{
			scanf("%ld",&x);
			if(x == 1)
				c1++;
			else if(x==-1)
				c2++;
		}
		for(i = 0;i<m;i++)
		{
			scanf("%ld %ld",&k,&t);
			if((k-t)%2 == 0)
				printf("0\n");
			else
			{
				if(abs(c1-c2)>(k-t))
					printf("1\n");
				else
					printf("0\n");
			}
		}

	}
	return 0;
}


#include<stdio.h>
#include<math.h>
int s[10001];
int main()
{
    int n, m, a, b, t=0;
    int count1=0, count2=0;
    while(scanf("%d %d", &n, &m)!=EOF)
    {
    for(int i=0; i<n; i++)
    {
        scanf("%d", &s[i]);
        if(s[i]==-1)
            count1++;
        else
            count2++;
    }
    if(count1>count2)
        t = count2;
    else
        t = count1;
    while(m--)
    {
        scanf("%d %d", &a, &b);
        int p = b-a+1;
        if(p%2!=0)
            printf("0\n");
        else
        {
            if(p/2<=t)
                printf("1\n");
            else
                printf("0\n");
        }
    }
    }
    return 0;
}


#include <iostream>
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
using namespace std;
int main()
{
    int n,m,i,j;
    int a[100000];
    int e,f;
    while(~scanf("%d%d",&n,&m))
    {
        int x=0,y=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            if(a[i]==-1)
                ++x;
            if(a[i]==1)
                ++y;

        }
        while(m--)
        {
            scanf("%d%d",&e,&f);
            int d=f-e+1;
            if(d%2!=0)
                printf("0\n");
            else
            {
                if(x>=d/2&&y>=d/2)
                {
                    printf("1\n");
                }
                else
                    printf("0\n");
            }
        }
    }
    return 0;
}


#include<stdio.h>
#include<math.h>
int s[10001];
int main()
{
    int n, m, a, b, t=0;
    int count1=0, count2=0;
    scanf("%d %d", &n, &m);
    {
    for(int i=0; i<n; i++)
    {
        scanf("%d", &s[i]);
        if(s[i]==-1)
            count1++;
        else
            count2++;
    }
    if(count1>count2)
        t = count2;
    else
        t = count1;
    while(m--)
    {
        scanf("%d %d", &a, &b);
        int p = b-a+1;
        if(p%2!=0)
            printf("0\n");
        else
        {
            if(p/2<=t)
                printf("1\n");
            else
                printf("0\n");
        }
        printf("\n");
    }

    return 0;
}


#include<bits/stdc++.h>
#include<stdio.h>
#include<iostream>
#include<sstream>
#include<bitset>
#include<utility>
using namespace std;

#define pi1         3.1415927
#define pi2         2.0*acos(0.0)
#define pi3         acos(-1.0)
#define MOD         1000000007
#define EPS         1e-11
#define PRIME       999998727899999
#define MAX         100005
#define UMAX        9223372036854775807
#define MAXARRAY    2000002
#define imax        2147483647
#define imin        -2147483648

#define so      sizeof
#define ll      long long
#define ull     unsigned long long
#define nl      '\n'
#define xx      first
#define yy      second
#define bg      begin()
#define en      end()
#define pb      push_back
#define mp      make_pair
#define ss      stringstream

#define clr(arr) memset(arr, 0, sizeof(arr))
#define SET(arr) memset(arr, -1, sizeof(arr))

#define sf          scanf
#define si(n)       scanf("%d", &n)
#define sl(n)       scanf("%lld", &n)
#define slf(n)      scanf("%lf", &n)
#define sc(n)       scanf("%c", &n)
#define si2(x, y)   scanf("%d %d", &x, &y)
#define sl2(x, y)   scanf("%lld %lld", &x, &y)
#define slf2(x, y)  scanf("%lf %lf", &x, &y)

#define pf      printf
#define pi(n)   printf("%d\n", n)
#define pl(n)   printf("%lld\n", n)
#define plf(n)  printf("%lf\n", n)
#define pc(n)   printf("%c\n", n)

#define READ()  freopen("input.txt", "r", stdin)
#define WRITE() freopen("output.txt", "w", stdout)
#define BOOST   std::ios_base::sync_with_stdio(0);

#define FOR(i,n)       for(i=0; i<n; i++)
#define LOOP(i,j,k)    for(i=j; i<k; i++)
#define REV(i,j,k)     for(i=j; i>=k; i--)

typedef pair<int,int>   pii;
typedef pair<ll,ll>     pll;
typedef vector<int>     vi;
typedef vector<ll>      vl;
typedef vector<pii>     vii;
typedef vector<pll>     vll;
typedef stack<int>      si;
typedef stack<ll>       sl;
typedef queue<int>      qi;
typedef queue<ll>       ql;
typedef set<int>        sti;
typedef set<ll>         stl;

template<class T> T GCD(T a, T b) {while(b){b^=a^=b^=a%=b;} return a;}
template<class T> T LCM(T a, T b) {T x=(a*b)/GCD(a, b); return x;}
template<class T> T sumdigit(T n) {if (n == 0){return 0;} return (n % 10 + sumdigit(n / 10));}
template<class T> T maxarray(T *arr) {T n=sizeof(arr); T x=*max_element(arr, arr+n); return x;} ///z=maxarray(a, n)
template<class T> T minarray(T *arr) {T n=sizeof(arr); T x=*min_element(arr, arr+n); return x;} ///z=minarray(a, n)
template<class T> double triangle(T a, T b, T c) {double s, area; s=(a+b+c)/2; area=sqrt(fabs(s*(s-a)*(s-b)*(s-c))); return area;}///any datatype
double triangle(double a, double b, double c) {double s, area; s=(a+b+c)/2; area=sqrt(fabs(s*(s-a)*(s-b)*(s-c))); return area;}///double datatype
bool isprime(ll n) {if(n==2){return true;} if(n<2 || n%2==0){return false;}for(ll i=3;i*i<=n;i+=2){if(n%i==0) {return false;}} return true;}
ll bigmod(ll a,ll b) {if(b==0){return 1%MOD;} ll x=bigmod(a,b/2); x=(x*x)%MOD; if(b%2==1){x=(x*a)%MOD;} return x;}///a-base, b-power (a^b)%MOD
ll countone(ull n) {int i; ull one=1,count=0; FOR(i,64){ll bi=one;bi&=n;if(bi) count++;one<<=1;} return count;}

///Graph Directional Array [4 dir]
//ll X[4]={0,0,1,-1};
//ll Y[4]={1,-1,0,0};
///Graph Directional Array [8 dir]
//ll X[8]={0,0,1,-1,-1,1,1,-1};
//ll Y[8]={1,-1,0,0,-1,-1,1,1};
///Knight Directional Array
//ll X[8]={1, 1, 2, 2, -1, -1, -2, -2};
//ll Y[8]={2, -2, 1, -1, 2, -2, 1, -1};

int main()
{
    ll n, m, l, r, ans, i, count=0, k, minus=0, plus=0;
    si2(n, m);
    ll a[n], b[m];
    ll sum[n];
    FOR(i, n)
    {
        si(a[i]);
//        if(i==0)
//        {
//            sum[i]=a[i];
//        }
//        else if(i>0)
//        {
//            sum[i]=sum[i-1]+a[i];
//        }
//        if(a[i]==1)
//        {
//            count++;
//        }
//        count=count+(a[i]==1);
        if(a[i]<0)
        {
            minus++;
        }
        else if(a[i]>0)
        {
            plus++;
        }
    }
    if(minus>plus)
    {
        count=plus;
    }
    else
    {
        count=minus;
    }
    count*=2;
    FOR(i, m)
    {
        cin>>l>>r;
//        r--;
//        l-=2;
//        if(l<0)
//        {
//            l=0;
//        }
//        ans=sum[r]-sum[l];
//        if(ans==0)
//        {
//            cout<<"1"<<nl;
//        }
//        else
//        {
//            cout<<"0"<<nl;
//        }
        ans=r-l+1;
        if((ans>count)||(ans%2==1))
        {
           cout<<'0'<<nl;
        }
        else
        {
            cout<<'1'<<nl;
        }
    }
    return 0;
}