//https://blog.csdn.net/hesorchen#include<map>#include<set>#include<list>#include<queue>#include<deque>#include<cmath>#include<stack>#include<vector>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>usingnamespace std;#define endl '\n'#define PI cos(-1)#define ll long long#define INF 0x3f3f3f3f#define mod 1000000009#define lowbit(abcd) (abcd & (-abcd))char before[1000010];char after[2000010];int R[2000010];intmain(){int t;
cin >> t;while(t--){scanf("%s", before);int len =strlen(before);int s =-1;for(int i =0; i < len; i++)if(before[i]!= before[len - i -1]){
s = i -1;break;}
after[0]='$';
after[1]='#';int ct =2;for(int i = s +1; i < len - s -1; i++){
after[ct++]= before[i];
after[ct++]='#';}
after[ct]='@';int pos =0;int maxx =0;for(int i =1; i < ct; i++){if(i < maxx)
R[i]=min(maxx - i, R[2* pos - i]);else
R[i]=1;while(after[i + R[i]]== after[i - R[i]])
R[i]++;if(R[i]+ i > maxx){
maxx = R[i]+ i;
pos = i;}}int maxxx =-1;int l, r;
l = r =-1;// cout << after << endl;for(int i =1; i < ct; i++){// cout << i << ' ' << R[i] << ' ' << ct << ' ' << endl;if(R[i]>= maxxx &&(i - R[i]==0|| i + R[i]-1== ct -1)){
l = i - R[i]+1;
r = i + R[i]-1;
maxxx = R[i];}}for(int i =0; i <= s; i++)printf("%c", before[i]);// cout << endl;for(int i = l; i <= r; i++)if(after[i]=='#')continue;elseprintf("%c", after[i]);// cout << endl;for(int i = len - s -1; i <= len -1; i++)printf("%c", before[i]);
cout << endl;}return0;}