#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MAX=1e9;
ll mod=998244353 ;
#define setbits(x) __builtin_popcount(x)
#define setbitsll(x) __builtin_popcountll(x)
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define se second
#define fi first
#define maxe max_element
#define mine min_element
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
#define debug4(x,y,z,w) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\t"<<#w<<" :: "<<w<<endl;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define boost  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
//#define SIZE 5000 + 1

vector<int> P(2000001);
long long expo(ll x,ll n,ll M)
{
    if(n==0)
        return 1;
    else if(n%2 == 0)        //n is even
        return expo((x*x)%M,n/2,M)%M;
    else                             //n is odd
        return (x*expo((x*x)%M,(n-1)/2,M))%M;

}

string convertToNewString(const string &s) {
    string newString = "@";

    for (int i = 0; i < s.size(); i++) {
        newString += "#" + s.substr(i, 1);
    }

    newString += "#$";
    return newString;
}

string longestPalindromeSubstring(const string &s) {
    for(int i=0;i<=(2*s.length()+1);i++)P[i]=0;
    string Q = convertToNewString(s);
    //debug(Q);
    int c = 0, r = 0;                // current center, right limit

    for (int i = 1; i < Q.size() - 1; i++) {
        // find the corresponding letter in the palidrome subString
        int iMirror = c - (i - c);

        if(r > i) {
            P[i] = min(r - i, P[iMirror]);
        }

        // expanding around center i
        while (Q[i + 1 + P[i]] == Q[i - 1 - P[i]]){
            P[i]++;
        }

        // Update c,r in case if the palindrome centered at i expands past r,
        if (i + P[i] > r) {
            c = i;              // next center = i
            r = i + P[i];
        }
    }

    // Find the longest palindrome length in p.

    int maxPalindrome = 0;
    int centerIndex = 0;
 int c1=0;
    for (int i = 1; i < Q.size() - 1; i++) {
        if (P[i] > maxPalindrome) {
            if(Q[i]=='#')
            {
                if(P[i]==(2*c1))
                {
                    maxPalindrome = P[i];
            centerIndex = i;
                }
            }
            else
            {
                if(P[i]==(2*c1+1))
                {
                    maxPalindrome = P[i];
            centerIndex = i;
                }
            }

        }
        if(Q[i]!='#' && Q[i]!='$')
            c1++;
    }

    //cout << maxPalindrome << "\n";
    if(maxPalindrome==0)return "";
    return s.substr( (centerIndex - 1 - maxPalindrome) / 2, maxPalindrome);
}

vector<int> fastLongestPalindromes(string str) {
    int i=0,j,d,s,e,lLen,palLen=0;
    vector<int> res;
    while (i < str.length()) {
        if (i > palLen && str[i-palLen-1] == str[i]) {
            palLen += 2; i++; continue;
        }
        res.push_back(palLen);
        s = res.size()-2;
        e = s-palLen;
        bool b = true;
        for (j=s; j>e; j--) {
            d = j-e-1;
            if (res[j] == d) { palLen = d; b = false; break; }
            res.push_back(min(d, res[j]));
        }
        if (b) { palLen = 1; i++; }
    }
    res.push_back(palLen);
    lLen = res.size();
    s = lLen-2;
    e = s-(2*str.length()+1-lLen);
    for (i=s; i>e; i--) { d = i-e-1; res.push_back(min(d, res[i])); }
    return res;
}
//vector<ll> visi(200001,0);
void solve()
{
  int t;
  cin>>t;string s;
  while(t--)
  {
      cin>>s;
      int n=s.length();
      string ans="";
      int i;
      for(i=0;i<(n/2);i++)
      {
          if(s[i]==s[n-1-i])
            ans=ans+s[i];
          else
            break;
      }
      string  tt=s.substr(i,n-(2*ans.length()));
     string s1="",s2="";
     //cout<<ans;
     //debug(tt);
      vector<int> re=fastLongestPalindromes(tt);
      /*for(int i=0;i<(tt.length()/2);i++)
        swap(tt[i],tt[tt.length()-1-i]);
      s2=longestPalindromeSubstring(tt);
      if(s1.length()>s2.length())
        cout<<s1;
      else
        cout<<s2;
        for(int i=0;i<(ans.length()/2);i++)
        swap(ans[i],ans[ans.length()-1-i]);
        cout<<ans<<endl;*/
        ll tata=0;string mid="";
        for(int i=0;i<re.size();i++)
        {
            if(i==re[i])
               tata=i;
        }
        mid=tt.substr(0,tata);
                tata=0;
        reverse(tt.begin(),tt.end());
        re=fastLongestPalindromes(tt);
        for(int i=0;i<re.size();i++)
        {
            if(i==re[i])
                tata=i;
        }
        if(tata>mid.length())
            mid=tt.substr(0,tata);
        cout<<ans<<mid;
        reverse(ans.begin(),ans.end());
        cout<<ans<<endl;
          }

}

int main(){
    boost;
  solve();
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MAX=1e9;
ll mod=998244353 ;
#define setbits(x) __builtin_popcount(x)
#define setbitsll(x) __builtin_popcountll(x)
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define se second
#define fi first
#define maxe max_element
#define mine min_element
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
#define debug4(x,y,z,w) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\t"<<#w<<" :: "<<w<<endl;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define boost  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
//#define SIZE 5000 + 1

vector<int> P(2000001);
long long expo(ll x,ll n,ll M)
{
    if(n==0)
        return 1;
    else if(n%2 == 0)        //n is even
        return expo((x*x)%M,n/2,M)%M;
    else                             //n is odd
        return (x*expo((x*x)%M,(n-1)/2,M))%M;

}

string convertToNewString(const string &s) {
    string newString = "@";

    for (int i = 0; i < s.size(); i++) {
        newString += "#" + s.substr(i, 1);
    }

    newString += "#$";
    return newString;
}

string longestPalindromeSubstring(const string &s) {
    for(int i=0;i<=(2*s.length()+1);i++)P[i]=0;
    string Q = convertToNewString(s);
    //debug(Q);
    int c = 0, r = 0;                // current center, right limit

    for (int i = 1; i < Q.size() - 1; i++) {
        // find the corresponding letter in the palidrome subString
        int iMirror = c - (i - c);

        if(r > i) {
            P[i] = min(r - i, P[iMirror]);
        }

        // expanding around center i
        while (Q[i + 1 + P[i]] == Q[i - 1 - P[i]]){
            P[i]++;
        }

        // Update c,r in case if the palindrome centered at i expands past r,
        if (i + P[i] > r) {
            c = i;              // next center = i
            r = i + P[i];
        }
    }

    // Find the longest palindrome length in p.

    int maxPalindrome = 0;
    int centerIndex = 0;
 int c1=0;
    for (int i = 1; i < Q.size() - 1; i++) {
        if (P[i] > maxPalindrome) {
            if(Q[i]=='#')
            {
                if(P[i]==(2*c1))
                {
                    maxPalindrome = P[i];
            centerIndex = i;
                }
            }
            else
            {
                if(P[i]==(2*c1+1))
                {
                    maxPalindrome = P[i];
            centerIndex = i;
                }
            }

        }
        if(Q[i]!='#' && Q[i]!='$')
            c1++;
    }

    //cout << maxPalindrome << "\n";
    return s.substr( (centerIndex - 1 - maxPalindrome) / 2, maxPalindrome);
}

//vector<ll> visi(200001,0);
void solve()
{
  int t;
  cin>>t;string s;
  while(t--)
  {
      cin>>s;
      int n=s.length();
      string ans="";
      int i;
      for(i=0;i<(n/2);i++)
      {
          if(s[i]==s[n-1-i])
            ans=ans+s[i];
          else
            break;
      }
      string temp="";
      for(int i=ans.length()-1;i>-1;i--)
        temp+=ans[i];
      int till=2*temp.length();
      string  tt=s.substr(i,n-till);
     string s1="",s2="";
      s1=longestPalindromeSubstring(tt);
      for(int i=0;i<(tt.length()/2);i++)
        swap(tt[i],tt[tt.length()-1-i]);
      s2=longestPalindromeSubstring(tt);
      if(s1.length()>s2.length())
        cout<<ans<<s1<<temp;
      else
        cout<<ans<<s2<<temp;
        cout<<endl;
  }

}

int main(){
    boost;
  solve();
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MAX=1e9;
ll mod=998244353 ;
#define setbits(x) __builtin_popcount(x)
#define setbitsll(x) __builtin_popcountll(x)
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define se second
#define fi first
#define maxe max_element
#define mine min_element
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
#define debug4(x,y,z,w) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\t"<<#w<<" :: "<<w<<endl;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define boost  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
//#define SIZE 5000 + 1

long long expo(ll x,ll n,ll M)
{
    if(n==0)
        return 1;
    else if(n%2 == 0)        //n is even
        return expo((x*x)%M,n/2,M)%M;
    else                             //n is odd
        return (x*expo((x*x)%M,(n-1)/2,M))%M;

}

string convertToNewString(const string &s) {
    string newString = "@";

    for (int i = 0; i < s.size(); i++) {
        newString += "#" + s.substr(i, 1);
    }

    newString += "#$";
    return newString;
}

string longestPalindromeSubstring(const string &s) {
    vector<int> P(2*s.length()+2,0);
    string Q = convertToNewString(s);
    //debug(Q);
    int c = 0, r = 0;                // current center, right limit

    for (int i = 1; i < Q.size() - 1; i++) {
        // find the corresponding letter in the palidrome subString
        int iMirror = c - (i - c);

        if(r > i) {
            P[i] = min(r - i, P[iMirror]);
        }

        // expanding around center i
        while (Q[i + 1 + P[i]] == Q[i - 1 - P[i]]){
            P[i]++;
        }

        // Update c,r in case if the palindrome centered at i expands past r,
        if (i + P[i] > r) {
            c = i;              // next center = i
            r = i + P[i];
        }
    }

    // Find the longest palindrome length in p.

    int maxPalindrome = 0;
    int centerIndex = 0;
 int c1=0;
    for (int i = 1; i < Q.size() - 1; i++) {
        if (P[i] > maxPalindrome) {
            if(Q[i]=='#')
            {
                if(P[i]==(2*c1))
                {
                    maxPalindrome = P[i];
            centerIndex = i;
                }
            }
            else
            {
                if(P[i]==(2*c1+1))
                {
                    maxPalindrome = P[i];
            centerIndex = i;
                }
            }

        }
        if(Q[i]!='#' && Q[i]!='$')
            c1++;
    }

    //cout << maxPalindrome << "\n";
    return s.substr( (centerIndex - 1 - maxPalindrome) / 2, maxPalindrome);
}

//vector<ll> visi(200001,0);
void solve()
{
  ll t;
  cin>>t;string s;
  while(t--)
  {
      cin>>s;
      ll n=s.length();
      string ans="";
      int i;
      for(i=0;i<(n/2);i++)
      {
          if(s[i]==s[n-1-i])
            ans=ans+s[i];
          else
            break;
      }
      string temp=ans;
      reverse(temp.begin(),temp.end());
      ll till=2*temp.length();
      string  tt=s.substr(i,n-till);
     // debug(tt);
     string s1="",s2="";
      if(tt.length())
      s1=longestPalindromeSubstring(tt);
      reverse(tt.begin(),tt.end());
      if(tt.length())
      s2=longestPalindromeSubstring(tt);
      if(s1.length()>s2.length())
        ans=ans+s1;
      else
        ans=ans+s2;
      ans=ans+temp;
      cout<<ans<<endl;
  }

}

int main(){
    boost;
  solve();
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MAX=1e9;
ll mod=998244353 ;
#define setbits(x) __builtin_popcount(x)
#define setbitsll(x) __builtin_popcountll(x)
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define se second
#define fi first
#define maxe max_element
#define mine min_element
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
#define debug4(x,y,z,w) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\t"<<#w<<" :: "<<w<<endl;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define boost  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
//#define SIZE 5000 + 1

vector<int> P(2000001);
long long expo(ll x,ll n,ll M)
{
    if(n==0)
        return 1;
    else if(n%2 == 0)        //n is even
        return expo((x*x)%M,n/2,M)%M;
    else                             //n is odd
        return (x*expo((x*x)%M,(n-1)/2,M))%M;

}

string convertToNewString(const string &s) {
    string newString = "@";

    for (int i = 0; i < s.size(); i++) {
        newString += "#" + s.substr(i, 1);
    }

    newString += "#$";
    return newString;
}

string longestPalindromeSubstring(const string &s) {
    for(int i=0;i<=(2*s.length()+1);i++)P[i]=0;
    string Q = convertToNewString(s);
    //debug(Q);
    int c = 0, r = 0;                // current center, right limit

    for (int i = 1; i < Q.size() - 1; i++) {
        // find the corresponding letter in the palidrome subString
        int iMirror = c - (i - c);

        if(r > i) {
            P[i] = min(r - i, P[iMirror]);
        }

        // expanding around center i
        while (Q[i + 1 + P[i]] == Q[i - 1 - P[i]]){
            P[i]++;
        }

        // Update c,r in case if the palindrome centered at i expands past r,
        if (i + P[i] > r) {
            c = i;              // next center = i
            r = i + P[i];
        }
    }

    // Find the longest palindrome length in p.

    int maxPalindrome = 0;
    int centerIndex = 0;
 int c1=0;
    for (int i = 1; i < Q.size() - 1; i++) {
        if (P[i] > maxPalindrome) {
            if(Q[i]=='#')
            {
                if(P[i]==(2*c1))
                {
                    maxPalindrome = P[i];
            centerIndex = i;
                }
            }
            else
            {
                if(P[i]==(2*c1+1))
                {
                    maxPalindrome = P[i];
            centerIndex = i;
                }
            }

        }
        if(Q[i]!='#' && Q[i]!='$')
            c1++;
    }

    //cout << maxPalindrome << "\n";
    if(maxPalindrome==0)return "";
    return s.substr( (centerIndex - 1 - maxPalindrome) / 2, maxPalindrome);
}

vector<int> fastLongestPalindromes(string str) {
    int i=0,j,d,s,e,lLen,palLen=0;
    vector<int> res;
    while (i < str.length()) {
        if (i > palLen && str[i-palLen-1] == str[i]) {
            palLen += 2; i++; continue;
        }
        res.push_back(palLen);
        s = res.size()-2;
        e = s-palLen;
        bool b = true;
        for (j=s; j>e; j--) {
            d = j-e-1;
            if (res[j] == d) { palLen = d; b = false; break; }
            res.push_back(min(d, res[j]));
        }
        if (b) { palLen = 1; i++; }
    }
    res.push_back(palLen);
    lLen = res.size();
    s = lLen-2;
    e = s-(2*str.length()+1-lLen);
    for (i=s; i>e; i--) { d = i-e-1; res.push_back(min(d, res[i])); }
    return res;
}
//vector<ll> visi(200001,0);
void solve()
{
  int t;
  cin>>t;string s;
  while(t--)
  {
      cin>>s;
      int n=s.length();
      string ans="";
      int i;
      for(i=0;i<(n/2);i++)
      {
          if(s[i]==s[n-1-i])
            ans=ans+s[i];
          else
            break;
      }
      string  tt=s.substr(i,n-(2*ans.length()));
     string s1="",s2="";
     cout<<ans;
     //debug(tt);
      vector<int> re=fastLongestPalindromes(tt);
      /*for(int i=0;i<(tt.length()/2);i++)
        swap(tt[i],tt[tt.length()-1-i]);
      s2=longestPalindromeSubstring(tt);
      if(s1.length()>s2.length())
        cout<<s1;
      else
        cout<<s2;
        for(int i=0;i<(ans.length()/2);i++)
        swap(ans[i],ans[ans.length()-1-i]);
        cout<<ans<<endl;*/
        ll tata=0;string mid="";
        for(int i=0;i<re.size();i++)
        {
            if(i==re[i])
               tata=i;
        }
        mid=tt.substr(0,tata);
                tata=0;
        reverse(tt.begin(),tt.end());
        re=fastLongestPalindromes(tt);
        for(int i=0;i<re.size();i++)
        {
            if(i==re[i])
                tata=i;
        }
        if(tata>mid.length())
            mid=tt.substr(0,tata);
        cout<<ans<<mid;
        reverse(ans.begin(),ans.end());
        cout<<ans<<endl;
          }

}

int main(){
    boost;
  solve();
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MAX=1e9;
ll mod=998244353 ;
#define setbits(x) __builtin_popcount(x)
#define setbitsll(x) __builtin_popcountll(x)
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define se second
#define fi first
#define maxe max_element
#define mine min_element
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
#define debug4(x,y,z,w) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\t"<<#w<<" :: "<<w<<endl;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define boost  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
//#define SIZE 5000 + 1

long long expo(ll x,ll n,ll M)
{
    if(n==0)
        return 1;
    else if(n%2 == 0)        //n is even
        return expo((x*x)%M,n/2,M)%M;
    else                             //n is odd
        return (x*expo((x*x)%M,(n-1)/2,M))%M;

}

string convertToNewString(const string &s) {
    string newString = "@";

    for (int i = 0; i < s.size(); i++) {
        newString += "#" + s.substr(i, 1);
    }

    newString += "#$";
    return newString;
}

string longestPalindromeSubstring(const string &s) {
    vector<int> P(2*s.length()+2,0);
    string Q = convertToNewString(s);
    //debug(Q);
    int c = 0, r = 0;                // current center, right limit

    for (int i = 1; i < Q.size() - 1; i++) {
        // find the corresponding letter in the palidrome subString
        int iMirror = c - (i - c);

        if(r > i) {
            P[i] = min(r - i, P[iMirror]);
        }

        // expanding around center i
        while (Q[i + 1 + P[i]] == Q[i - 1 - P[i]]){
            P[i]++;
        }

        // Update c,r in case if the palindrome centered at i expands past r,
        if (i + P[i] > r) {
            c = i;              // next center = i
            r = i + P[i];
        }
    }

    // Find the longest palindrome length in p.

    int maxPalindrome = 0;
    int centerIndex = 0;
 int c1=0;
    for (int i = 1; i < Q.size() - 1; i++) {
        if (P[i] > maxPalindrome) {
            if(Q[i]=='#')
            {
                if(P[i]==(2*c1))
                {
                    maxPalindrome = P[i];
            centerIndex = i;
                }
            }
            else
            {
                if(P[i]==(2*c1+1))
                {
                    maxPalindrome = P[i];
            centerIndex = i;
                }
            }

        }
        if(Q[i]!='#' && Q[i]!='$')
            c1++;
    }

    //cout << maxPalindrome << "\n";
    return s.substr( (centerIndex - 1 - maxPalindrome) / 2, maxPalindrome);
}

//vector<ll> visi(200001,0);
void solve()
{
  ll t;
  cin>>t;string s;
  while(t--)
  {
      cin>>s;
      ll n=s.length();
      string ans="";
      int i;
      for(i=0;i<(n/2);i++)
      {
          if(s[i]==s[n-1-i])
            ans=ans+s[i];
          else
            break;
      }
      string temp="";
      for(int i=ans.length()-1;i>-1;i--)
        temp+=ans[i];
      //reverse(temp.begin(),temp.end());
      ll till=2*temp.length();
      string  tt=s.substr(i,n-till);
     // debug(tt);
     string s1="",s2="";
      if(tt.length())
      s1=longestPalindromeSubstring(tt);
      for(int i=0;i<(tt.length()/2);i++)
        swap(tt[i],tt[tt.length()-1-i]);
     // reverse(tt.begin(),tt.end());
      if(tt.length())
      s2=longestPalindromeSubstring(tt);
      if(s1.length()>s2.length())
        ans=ans+s1;
      else
        ans=ans+s2;
      ans=ans+temp;
      cout<<ans<<endl;
  }

}

int main(){
    boost;
  solve();
}


//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#include<bits/stdc++.h>
using namespace std;

/* abbr */
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define msv(x, v) memset(x, v, sizeof(x))
#define ms(x) memset(x, 0, sizeof(x))

/* typedef for long long */

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;

/* typedef for other */

typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;

/* typedef for point */

//typedef pii point;
//#define x first
//#define y second

/* typedef for vector */

typedef vector<int> vi;
typedef vector<ll> vl;

/* constant number */

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double PI = acos(-1.0);
const double EPS = 1e-8;
const int MOD = 998244353;

template <typename T>
void cmin(T &x, T y) {
    if(y < x)
        x = y;
}

template <typename T>
void cmax(T &x, T y) {
    if(y > x)
        x = y;
}

#define dbg(x) cerr << "Line " << __LINE__ << ": " << #x " = " << (x) << endl

ll qpow(ll x, ll n, ll mod = MOD) {
    ll res = 1;
    while(n) {
        if(n & 1)
            res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

struct PAM {

    static const int MAXN = 1e6 + 10;

    char s[MAXN];
    int ch[MAXN][26], fail[MAXN], len[MAXN];
    int cnt, slen, lst;

    void Init() {
        ms(ch[0]), fail[0] = 1, len[0] = 0;
        ms(ch[1]), fail[1] = 0, len[1] = -1;
        cnt = 1, slen = 0, lst = 0;
    }

    int NewNode(int l) {
        int o = ++cnt;
        ms(ch[o]), fail[o] = -1, len[o] = l;
        return o;
    }

    int Fail(int u) {
        while(s[slen - len[u] - 1] != s[slen])
            u = fail[u];
        return u;
    }

    void Extend(char c) {
        s[++slen] = c;
        int u = Fail(lst);
        if(ch[u][c - 'a'] == 0) {
            int o = NewNode(len[u] + 2), v = Fail(fail[u]);
            fail[o] = ch[v][c - 'a'], ch[u][c - 'a'] = o;
        }
        lst = ch[u][c - 'a'];
    }

} pam;

namespace Solver {

    void InitOnce() {
#ifdef LOCAL
        freopen("A.txt", "r", stdin);
#endif // LOCAL
        int t;
        scanf("%d", &t);
    }

    char s[1000005];
    char t[1000005];
    char ans[1000005];

    void Read() {
        int res = scanf("%s", s + 1);
        if(res == -1)
            exit(0);
    }

    void Solve() {
        int n = strlen(s + 1);
        for(int i = 0; i <= n + 1; ++i) {
            t[i] = 0;
            ans[i] = 0;
        }
        bool suc = 1;
        for(int i = 1; i <= n; ++i) {
            if(s[i] != s[n + 1 - i]) {
                suc = 0;
                break;
            }
        }
        if(suc) {
            puts(s + 1);
            return;
        }
        int len = 0;
        int L = 1, R = n;
        while(s[L] == s[R]) {
            ans[++len] = s[L];
            ++L;
            --R;
        }
        int m = 0;
        for(int i = L; i <= R; ++i)
            t[++m] = s[i];
        pam.Init();
        for(int i = 1; i <= m; ++i)
            pam.Extend(t[i]);
        int l1 = pam.len[pam.lst];
        pam.Init();
        for(int i = 1; i <= m; ++i)
            pam.Extend(t[m + 1 - i]);
        int l2 = pam.len[pam.lst];
        //dbg(l1), dbg(l2);
        for(int i = 1; i <= len; ++i)
            putchar(ans[i]);
        if(l1 >= l2) {
            for(int i = m; i >= m - l1 + 1; --i)
                putchar(t[i]);
        } else {
            for(int i = 1; i <= l2; ++i)
                putchar(t[i]);
        }
        for(int i = 1; i <= len; ++i)
            putchar(ans[len + 1 - i]);
        puts("");
        return;
    }

}

int main() {
    Solver::InitOnce();
    while(true) {
        Solver::Read();
        Solver::Solve();
    }
    return 0;
}


//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#include<bits/stdc++.h>
using namespace std;

/* abbr */
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define msv(x, v) memset(x, v, sizeof(x))
#define ms(x) memset(x, 0, sizeof(x))

/* typedef for long long */

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;

/* typedef for other */

typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;

/* typedef for point */

//typedef pii point;
//#define x first
//#define y second

/* typedef for vector */

typedef vector<int> vi;
typedef vector<ll> vl;

/* constant number */

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double PI = acos(-1.0);
const double EPS = 1e-8;
const int MOD = 998244353;

template <typename T>
void cmin(T &x, T y) {
    if(y < x)
        x = y;
}

template <typename T>
void cmax(T &x, T y) {
    if(y > x)
        x = y;
}

#define dbg(x) cerr << "Line " << __LINE__ << ": " << #x " = " << (x) << endl

ll qpow(ll x, ll n, ll mod = MOD) {
    ll res = 1;
    while(n) {
        if(n & 1)
            res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

namespace Solver {

    void InitOnce() {
#ifdef LOCAL
        freopen("A.txt", "r", stdin);
#endif // LOCAL
        int t;
        scanf("%d", &t);
    }

    char s[1000005];
    char t[1000005];

    void Read() {
        int res = scanf("%s", s + 1);
        if(res == -1)
            exit(0);
    }

    int m;
    char ans[1000005];
    ll hash1[1000005];
    ll hash2[1000005];
    ll BASE[1000005];

    ll hash1LR(int L, int R) {
        //printf("hash1: [%d,%d]\n", L, R);
        if(L < 1 || R > m)
            return -775;
        ll res = (hash1[R] - hash1[L - 1] * BASE[R - L + 1] % MOD + MOD) % MOD;
        //dbg(res);
        return res;
    }

    ll hash2LR(int L, int R) {
        //printf("hash2: [%d,%d]\n", L, R);
        if(L < 1 || R > m)
            return -886;
        ll res = (hash2[L] - hash2[R + 1] * BASE[R - L + 1] % MOD + MOD) % MOD;
        //dbg(res);
        return res;
    }

    void Solve() {
        int n = strlen(s + 1);
        for(int i = 1; i <= n + 1; ++i) {
            t[i] = 0;
            ans[i] = 0;
        }
        bool suc = 1;
        for(int i = 1; i <= n; ++i) {
            if(s[i] != s[n + 1 - i]) {
                suc = 0;
                break;
            }
        }
        if(suc) {
            puts(s + 1);
            return;
        }
        int len = 0;
        int L = 1, R = n;
        while(s[L] == s[R]) {
            ans[++len] = s[L];
            ++L;
            --R;
        }
        //printf("[%d,%d]\n", L, R);
        m = 0;
        for(int i = L; i <= R; ++i)
            t[++m] = s[i];
        //printf("m=%d\n", m);
        BASE[0] = 1;
        BASE[1] = 1331;
        for(int i = 2; i <= m; ++i)
            BASE[i] = BASE[i - 1] * BASE[1] % MOD;

        for(int i = 1; i <= m; ++i)
            hash1[i] = (hash1[i - 1] * BASE[1] + t[i]) % MOD;
        hash2[m + 1] = 0;
        for(int i = m; i >= 1; --i)
            hash2[i] = (hash2[i + 1] * BASE[1] + t[i]) % MOD;
        int ansL = 0, ansR = -1;
        for(int i = 1; i <= m; ++i) {
            {
                int l = (i - 1) * 2 + 1;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(1, i - 1) == hash2LR(i + 1, l)) {
                        ansL = 1, ansR = l;
                    }
                }
            } {
                int l = i * 2;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(1, i) == hash2LR(i + 1, l)) {
                        ansL = 1, ansR = l;
                    }
                }
            }
        }
        for(int i = m; i >= 1; --i) {
            {
                int l = (m - i) * 2 + 1;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(m - l + 1, i - 1) == hash2LR(i + 1, m)) {
                        ansL = m - l + 1, ansR = m;
                    }
                }
            } {
                int l = (m + 1 - i) * 2;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(m - l + 1, i - 1) == hash2LR(i, m)) {
                        ansL = m - l + 1, ansR = m;
                    }
                }
            }
        }
        //printf("[%d,%d]\n", ansL, ansR);
        for(int i = 1; i <= len; ++i)
            putchar(ans[i]);
        for(int i = ansL; i <= ansR; ++i)
            putchar(t[i]);
        for(int i = 1; i <= len; ++i)
            putchar(ans[len + 1 - i]);
        puts("");
        return;
    }

}

int main() {
    Solver::InitOnce();
    while(true) {
        Solver::Read();
        Solver::Solve();
    }
    return 0;
}


//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#include<bits/stdc++.h>
using namespace std;

/* abbr */
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define msv(x, v) memset(x, v, sizeof(x))
#define ms(x) memset(x, 0, sizeof(x))

/* typedef for long long */

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;

/* typedef for other */

typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;

/* typedef for point */

//typedef pii point;
//#define x first
//#define y second

/* typedef for vector */

typedef vector<int> vi;
typedef vector<ll> vl;

/* constant number */

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double PI = acos(-1.0);
const double EPS = 1e-8;
const int MOD = 998244353;

template <typename T>
void cmin(T &x, T y) {
    if(y < x)
        x = y;
}

template <typename T>
void cmax(T &x, T y) {
    if(y > x)
        x = y;
}

#define dbg(x) cerr << "Line " << __LINE__ << ": " << #x " = " << (x) << endl

ll qpow(ll x, ll n, ll mod = MOD) {
    ll res = 1;
    while(n) {
        if(n & 1)
            res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

namespace Solver {

    void InitOnce() {
#ifdef LOCAL
        freopen("A.txt", "r", stdin);
#endif // LOCAL
        int t;
        scanf("%d", &t);
    }

    char s[1000005];
    char t[1000005];

    void Read() {
        int res = scanf("%s", s + 1);
        if(res == -1)
            exit(0);
    }

    int m;
    char ans[1000005];
    ll hash1[1000005];
    ll hash11[1000005];
    ll hash2[1000005];
    ll hash22[1000005];
    ll BASE[1000005];
    ll BASE2[1000005];

    ll hash1LR(int L, int R) {
        //printf("hash1: [%d,%d]\n", L, R);
        if(L < 1 || R > m || L > R)
            return -775;
        ll res = (hash1[R] - hash1[L - 1] * BASE[R - L + 1] % MOD + MOD) % MOD;
        //dbg(res);
        return res;
    }

    ll hash2LR(int L, int R) {
        //printf("hash2: [%d,%d]\n", L, R);
        if(L < 1 || R > m || L > R)
            return -886;
        ll res = (hash2[L] - hash2[R + 1] * BASE[R - L + 1] % MOD + MOD) % MOD;
        //dbg(res);
        return res;
    }

    ll hash11LR(int L, int R) {
        //printf("hash1: [%d,%d]\n", L, R);
        if(L < 1 || R > m || L > R)
            return -775;
        ll res = (hash11[R] - hash11[L - 1] * BASE2[R - L + 1] % MOD + MOD) % MOD;
        //dbg(res);
        return res;
    }

    ll hash22LR(int L, int R) {
        //printf("hash2: [%d,%d]\n", L, R);
        if(L < 1 || R > m || L > R)
            return -886;
        ll res = (hash22[L] - hash22[R + 1] * BASE2[R - L + 1] % MOD + MOD) % MOD;
        //dbg(res);
        return res;
    }

    void Solve() {
        int n = strlen(s + 1);
        for(int i = 1; i <= n + 1; ++i) {
            t[i] = 0;
            ans[i] = 0;
        }
        bool suc = 1;
        for(int i = 1; i <= n; ++i) {
            if(s[i] != s[n + 1 - i]) {
                suc = 0;
                break;
            }
        }
        if(suc) {
            puts(s + 1);
            return;
        }
        int len = 0;
        int L = 1, R = n;
        while(s[L] == s[R]) {
            ans[++len] = s[L];
            ++L;
            --R;
        }
        //printf("[%d,%d]\n", L, R);
        m = 0;
        for(int i = L; i <= R; ++i)
            t[++m] = s[i];
        //printf("m=%d\n", m);
        BASE[0] = 1;
        BASE[1] = 2337;
        for(int i = 2; i <= m; ++i)
            BASE[i] = BASE[i - 1] * BASE[1] % MOD;
        BASE2[1] = 3337;
        for(int i = 2; i <= m; ++i)
            BASE2[i] = BASE2[i - 1] * BASE2[1] % MOD;

        for(int i = 1; i <= m; ++i)
            hash1[i] = (hash1[i - 1] * BASE[1] + t[i]) % MOD;
        hash2[m + 1] = 0;
        for(int i = m; i >= 1; --i)
            hash2[i] = (hash2[i + 1] * BASE[1] + t[i]) % MOD;

        for(int i = 1; i <= m; ++i)
            hash11[i] = (hash11[i - 1] * BASE2[1] + t[i]) % MOD;
        hash22[m + 1] = 0;
        for(int i = m; i >= 1; --i)
            hash22[i] = (hash22[i + 1] * BASE2[1] + t[i]) % MOD;
        int ansL = 0, ansR = -100;
        for(int i = 1; i <= m; ++i) {
            {
                int l = (i - 1) * 2 + 1;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(1, i) == hash2LR(i, l) && hash11LR(1, i) == hash22LR(i, l)) {
                        ansL = 1, ansR = l;
                    }
                }
            } {
                int l = i * 2;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(1, i) == hash2LR(i + 1, l) && hash11LR(1, i) == hash22LR(i + 1, l)) {
                        ansL = 1, ansR = l;
                    }
                }
            }
        }
        for(int i = m; i >= 1; --i) {
            {
                int l = (m - i) * 2 + 1;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(m - l + 1, i) == hash2LR(i, m) && hash11LR(m - l + 1, i) == hash22LR(i, m)) {
                        ansL = m - l + 1, ansR = m;
                    }
                }
            } {
                int l = (m + 1 - i) * 2;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(m - l + 1, i - 1) == hash2LR(i, m) && hash11LR(m - l + 1, i - 1) == hash22LR(i, m)) {
                        ansL = m - l + 1, ansR = m;
                    }
                }
            }
        }
        //printf("[%d,%d]\n", ansL, ansR);
        for(int i = 1; i <= len; ++i)
            putchar(ans[i]);
        for(int i = ansL; i <= ansR; ++i)
            putchar(t[i]);
        for(int i = 1; i <= len; ++i)
            putchar(ans[len + 1 - i]);
        puts("");
        return;
    }

}

int main() {
    Solver::InitOnce();
    while(true) {
        Solver::Read();
        Solver::Solve();
    }
    return 0;
}


//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#include<bits/stdc++.h>
using namespace std;

/* abbr */
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define msv(x, v) memset(x, v, sizeof(x))
#define ms(x) memset(x, 0, sizeof(x))

/* typedef for long long */

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;

/* typedef for other */

typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;

/* typedef for point */

//typedef pii point;
//#define x first
//#define y second

/* typedef for vector */

typedef vector<int> vi;
typedef vector<ll> vl;

/* constant number */

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double PI = acos(-1.0);
const double EPS = 1e-8;
const int MOD = 998244353;

template <typename T>
void cmin(T &x, T y) {
    if(y < x)
        x = y;
}

template <typename T>
void cmax(T &x, T y) {
    if(y > x)
        x = y;
}

#define dbg(x) cerr << "Line " << __LINE__ << ": " << #x " = " << (x) << endl

ll qpow(ll x, ll n, ll mod = MOD) {
    ll res = 1;
    while(n) {
        if(n & 1)
            res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

namespace Solver {

    void InitOnce() {
#ifdef LOCAL
        freopen("A.txt", "r", stdin);
#endif // LOCAL
        int t;
        scanf("%d", &t);
    }

    char s[1000005];
    char t[1000005];

    void Read() {
        int res = scanf("%s", s + 1);
        if(res == -1)
            exit(0);
    }

    int m;
    char ans[1000005];
    ll hash1[1000005];
    ll hash2[1000005];
    ll BASE[1000005];

    ll hash1LR(int L, int R) {
        //printf("hash1: [%d,%d]\n", L, R);
        if(R > m)
            return -775;
        ll res = (hash1[R] - hash1[L - 1] * BASE[R - L + 1] % MOD + MOD) % MOD;
        //dbg(res);
        return res;
    }

    ll hash2LR(int L, int R) {
        //printf("hash2: [%d,%d]\n", L, R);
        if(R > m)
            return -886;
        ll res = (hash2[L] - hash2[R + 1] * BASE[R - L + 1] % MOD + MOD) % MOD;
        //dbg(res);
        return res;
    }

    void Solve() {
        int n = strlen(s + 1);
        for(int i = 1; i <= n + 1; ++i) {
            t[i] = 0;
            ans[i] = 0;
        }
        bool suc = 1;
        for(int i = 1; i <= n; ++i) {
            if(s[i] != s[n + 1 - i]) {
                suc = 0;
                break;
            }
        }
        if(suc) {
            puts(s + 1);
            return;
        }
        int len = 0;
        int L = 1, R = n;
        while(s[L] == s[R]) {
            ans[++len] = s[L];
            ++L;
            --R;
        }
        //printf("[%d,%d]\n", L, R);
        m = 0;
        for(int i = L; i <= R; ++i)
            t[++m] = s[i];
        //printf("m=%d\n", m);
        BASE[0] = 1;
        BASE[1] = 1331;
        for(int i = 2; i <= m; ++i)
            BASE[i] = BASE[i - 1] * BASE[1] % MOD;

        for(int i = 1; i <= m; ++i)
            hash1[i] = (hash1[i - 1] * BASE[1] + t[i]) % MOD;
        hash2[m + 1] = 0;
        for(int i = m; i >= 1; --i)
            hash2[i] = (hash2[i + 1] * BASE[1] + t[i]) % MOD;
        int ansL = 0, ansR = -1;
        for(int i = 1; i <= m; ++i) {
            {
                int l = (i - 1) * 2 + 1;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(1, i - 1) == hash2LR(i + 1, l)) {
                        ansL = 1, ansR = l;
                    }
                }
            } {
                int l = i * 2;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(1, i) == hash2LR(i + 1, l)) {
                        ansL = 1, ansR = l;
                    }
                }
            }
        }
        for(int i = m; i >= 1; --i) {
            {
                int l = (m - i) * 2 + 1;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(m - l + 1, i - 1) == hash2LR(i + 1, m)) {
                        ansL = m - l + 1, ansR = m;
                    }
                }
            } {
                int l = (m + 1 - i) * 2;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(m - l + 1, i - 1) == hash2LR(i, m)) {
                        ansL = m - l + 1, ansR = m;
                    }
                }
            }
        }
        //printf("[%d,%d]\n", ansL, ansR);
        for(int i = 1; i <= len; ++i)
            putchar(ans[i]);
        for(int i = ansL; i <= ansR; ++i)
            putchar(t[i]);
        for(int i = 1; i <= len; ++i)
            putchar(ans[len + 1 - i]);
        puts("");
        return;
    }

}

int main() {
    Solver::InitOnce();
    while(true) {
        Solver::Read();
        Solver::Solve();
    }
    return 0;
}


//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#include<bits/stdc++.h>
using namespace std;

/* abbr */
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define msv(x, v) memset(x, v, sizeof(x))
#define ms(x) memset(x, 0, sizeof(x))

/* typedef for long long */

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;

/* typedef for other */

typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;

/* typedef for point */

//typedef pii point;
//#define x first
//#define y second

/* typedef for vector */

typedef vector<int> vi;
typedef vector<ll> vl;

/* constant number */

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double PI = acos(-1.0);
const double EPS = 1e-8;
const int MOD = 998244353;

template <typename T>
void cmin(T &x, T y) {
    if(y < x)
        x = y;
}

template <typename T>
void cmax(T &x, T y) {
    if(y > x)
        x = y;
}

#define dbg(x) cerr << "Line " << __LINE__ << ": " << #x " = " << (x) << endl

ll qpow(ll x, ll n, ll mod = MOD) {
    ll res = 1;
    while(n) {
        if(n & 1)
            res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

namespace Solver {

    void InitOnce() {
#ifdef LOCAL
        freopen("A.txt", "r", stdin);
#endif // LOCAL
        int t;
        //scanf("%d", &t);
    }

    char s[200005];
    char t[200005];

    void Read() {
        int res = scanf("%s", s + 1);
        if(res == -1)
            exit(0);
    }

    int m;
    char ans[200005];
    ll hash1[200005];
    ll hash2[200005];
    ll BASE[200005];

    ll hash1LR(int L, int R) {
        //printf("hash1: [%d,%d]\n", L, R);
        if(R > m)
            return -775;
        ll res = (hash1[R] - hash1[L - 1] * BASE[R - L + 1] % MOD + MOD) % MOD;
        //dbg(res);
        return res;
    }

    ll hash2LR(int L, int R) {
        //printf("hash2: [%d,%d]\n", L, R);
        if(R > m)
            return -886;
        ll res = (hash2[L] - hash2[R + 1] * BASE[R - L + 1] % MOD + MOD) % MOD;
        //dbg(res);
        return res;
    }

    void Solve() {
        int n = strlen(s + 1);
        for(int i = 1; i <= n + 1; ++i) {
            t[i] = 0;
            ans[i] = 0;
        }
        bool suc = 1;
        for(int i = 1; i <= n; ++i) {
            if(s[i] != s[n + 1 - i]) {
                suc = 0;
                break;
            }
        }
        if(suc) {
            puts(s + 1);
            return;
        }
        int len = 0;
        int L = 1, R = n;
        while(s[L] == s[R]) {
            ans[++len] = s[L];
            ++L;
            --R;
        }
        //printf("[%d,%d]\n", L, R);
        m = 0;
        for(int i = L; i <= R; ++i)
            t[++m] = s[i];
        //printf("m=%d\n", m);
        BASE[0] = 1;
        BASE[1] = 1331;
        for(int i = 2; i <= m; ++i)
            BASE[i] = BASE[i - 1] * BASE[1] % MOD;

        for(int i = 1; i <= m; ++i)
            hash1[i] = (hash1[i - 1] * BASE[1] + t[i]) % MOD;
        hash2[m + 1] = 0;
        for(int i = m; i >= 1; --i)
            hash2[i] = (hash2[i + 1] * BASE[1] + t[i]) % MOD;
        int ansL = 0, ansR = -1;
        for(int i = 1; i <= m; ++i) {
            {
                int l = (i - 1) * 2 + 1;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(1, i - 1) == hash2LR(i + 1, l)) {
                        ansL = 1, ansR = l;
                    }
                }
            } {
                int l = i * 2;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(1, i) == hash2LR(i + 1, l)) {
                        ansL = 1, ansR = l;
                    }
                }
            }
        }
        for(int i = m; i >= 1; --i) {
            {
                int l = (m - i) * 2 + 1;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(m - l + 1, i - 1) == hash2LR(i + 1, m)) {
                        ansL = m - l + 1, ansR = m;
                    }
                }
            } {
                int l = (m + 1 - i) * 2;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(m - l + 1, i - 1) == hash2LR(i, m)) {
                        ansL = m - l + 1, ansR = m;
                    }
                }
            }
        }
        //printf("[%d,%d]\n", ansL, ansR);
        for(int i = 1; i <= len; ++i)
            putchar(ans[i]);
        for(int i = ansL; i <= ansR; ++i)
            putchar(t[i]);
        for(int i = 1; i <= len; ++i)
            putchar(ans[len + 1 - i]);
        puts("");
        return;
    }

}

int main() {
    Solver::InitOnce();
    while(true) {
        Solver::Read();
        Solver::Solve();
    }
    return 0;
}


//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#include<bits/stdc++.h>
using namespace std;

/* abbr */
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define msv(x, v) memset(x, v, sizeof(x))
#define ms(x) memset(x, 0, sizeof(x))

/* typedef for long long */

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;

/* typedef for other */

typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;

/* typedef for point */

//typedef pii point;
//#define x first
//#define y second

/* typedef for vector */

typedef vector<int> vi;
typedef vector<ll> vl;

/* constant number */

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double PI = acos(-1.0);
const double EPS = 1e-8;
const int MOD = 998244353;

template <typename T>
void cmin(T &x, T y) {
    if(y < x)
        x = y;
}

template <typename T>
void cmax(T &x, T y) {
    if(y > x)
        x = y;
}

#define dbg(x) cerr << "Line " << __LINE__ << ": " << #x " = " << (x) << endl

ll qpow(ll x, ll n, ll mod = MOD) {
    ll res = 1;
    while(n) {
        if(n & 1)
            res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

struct StringHashing {

    static const int MAXN = 1000005;

    int n;
    int bse[3] = {13, 1331, 2333};
    int mod[3] = {998244353, 1000000007, 1000000009};
    int bsk[MAXN][3], hsh1[MAXN][3], hsh2[MAXN][3];

    void Init(char *s) {
        n = strlen(s + 1);
        for(int t = 0; t < 3; ++t) {
            bsk[0][t] = 1, hsh1[0][t] = 0, hsh2[n + 1][t] = 0;
            for(int i = 1; i <= n; ++i) {
                bsk[i][t] = (1LL * bsk[i - 1][t] * bse[t]) % mod[t];
                hsh1[i][t] = (1LL * hsh1[i - 1][t] * bse[t] + s[i]) % mod[t];
                hsh2[n + 1 - i][t] = (1LL * hsh2[n + 1 - i + 1][t] * bse[t] + s[n + 1 - i]) % mod[t];
            }
        }
    }

    int HashCode1(int L, int R, int t) {
        int res = (hsh1[R][t] - 1LL * hsh1[L - 1][t] * bsk[R - L + 1][t]) % mod[t];
        res += (res < 0) ? mod[t] : 0;
        return res;
    }

    int HashCode2(int L, int R, int t) {
        int res = (hsh2[L][t] - 1LL * hsh2[R + 1][t] * bsk[R - L + 1][t]) % mod[t];
        res += (res < 0) ? mod[t] : 0;
        return res;
    }

    bool isEqual(int L1, int R1, int L2, int R2) {
        if(L1 < 1 || R1 > n || L2 < 1 || R2 > n)
            return 0;
        if(L1 > R1 && L2 > R2)
            return 1;
        for(int t = 0; t < 3; ++t) {
            if(HashCode1(L1, R1, t) != HashCode1(L2, R2, t))
                return 0;
        }
        return 1;
    }

    bool isReverseEqual(int L1, int R1, int L2, int R2) {
        if(L1 < 1 || R1 > n || L2 < 1 || R2 > n)
            return 0;
        if(L1 > R1 && L2 > R2)
            return 1;
        for(int t = 0; t < 3; ++t) {
            if(HashCode1(L1, R1, t) != HashCode2(L2, R2, t))
                return 0;
        }
        return 1;
    }

} sh;

namespace Solver {

    void InitOnce() {
#ifdef LOCAL
        freopen("A.txt", "r", stdin);
#endif // LOCAL
        int t;
        scanf("%d", &t);
    }

    char s[1000005];
    char t[1000005];
    char ans[1000005];

    void Read() {
        int res = scanf("%s", s + 1);
        if(res == -1)
            exit(0);
    }

    void Solve() {
        int n = strlen(s + 1);
        for(int i = 0; i <= n + 1; ++i) {
            t[i] = 0;
            ans[i] = 0;
        }
        bool suc = 1;
        for(int i = 1; i <= n; ++i) {
            if(s[i] != s[n + 1 - i]) {
                suc = 0;
                break;
            }
        }
        if(suc) {
            puts(s + 1);
            return;
        }
        int len = 0;
        int L = 1, R = n;
        while(s[L] == s[R]) {
            ans[++len] = s[L];
            ++L;
            --R;
        }
        int m = 0;
        for(int i = L; i <= R; ++i)
            t[++m] = s[i];
        sh.Init(t);
        int ansL = 0, ansR = -1;
        for(int i = 1; i <= m; ++i) {
            int l = (i - 1) * 2 + 1;
            if(l > ansR - ansL + 1) {
                if(sh.isReverseEqual(1, i, i, l))
                    ansL = 1, ansR = l;
            }
            l = i * 2;
            if(l > ansR - ansL + 1) {
                if(sh.isReverseEqual(1, i, i + 1, l))
                    ansL = 1, ansR = l;
            }
        }
        for(int i = m; i >= 1; --i) {
            int l = (m - i) * 2 + 1;
            if(l > ansR - ansL + 1) {
                if(sh.isReverseEqual(m - l + 1, i, i, m))
                    ansL = m - l + 1, ansR = m;
            }
            l = (m + 1 - i) * 2;
            if(l > ansR - ansL + 1) {
                if(sh.isReverseEqual(m - l + 1, i - 1, i, m))
                    ansL = m - l + 1, ansR = m;
            }
        }
        for(int i = 1; i <= len; ++i)
            putchar(ans[i]);
        for(int i = ansL; i <= ansR; ++i)
            putchar(t[i]);
        for(int i = 1; i <= len; ++i)
            putchar(ans[len + 1 - i]);
        puts("");
        return;
    }

}

int main() {
    Solver::InitOnce();
    while(true) {
        Solver::Read();
        Solver::Solve();
    }
    return 0;
}


//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#include<bits/stdc++.h>
using namespace std;

/* abbr */
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define msv(x, v) memset(x, v, sizeof(x))
#define ms(x) memset(x, 0, sizeof(x))

/* typedef for long long */

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;

/* typedef for other */

typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;

/* typedef for point */

//typedef pii point;
//#define x first
//#define y second

/* typedef for vector */

typedef vector<int> vi;
typedef vector<ll> vl;

/* constant number */

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double PI = acos(-1.0);
const double EPS = 1e-8;
const int MOD = 998244353;

template <typename T>
void cmin(T &x, T y) {
    if(y < x)
        x = y;
}

template <typename T>
void cmax(T &x, T y) {
    if(y > x)
        x = y;
}

#define dbg(x) cerr << "Line " << __LINE__ << ": " << #x " = " << (x) << endl

ll qpow(ll x, ll n, ll mod = MOD) {
    ll res = 1;
    while(n) {
        if(n & 1)
            res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

struct StringHashing {

    static const int MAXN = 1000005;

    int n;
    int bse[3] = {13, 1331, 2333};
    int mod[3] = {998244353, 1000000007, 1000000009};
    int bsk[MAXN][3], hsh1[MAXN][3], hsh2[MAXN][3];

    void Init(char *s) {
        n = strlen(s + 1);
        for(int t = 0; t < 3; ++t) {
            bsk[0][t] = 1;
            for(int i = 1; i <= n; ++i)
                bsk[i][t] = (1LL * bsk[i - 1][t] * bse[t]) % mod[t];
            hsh1[0][t] = 0;
            for(int i = 1; i <= n; ++i)
                hsh1[i][t] = (1LL * hsh1[i - 1][t] * bse[t] + s[i]) % mod[t];
            hsh2[n + 1][t] = 0;
            for(int i = n; i >= 1; --i)
                hsh2[i][t] = (1LL * hsh2[i + 1][t] * bse[t] + s[i]) % mod[t];
        }
    }

    int HashCode1(int L, int R, int t) {
        int res = (hsh1[R][t] - 1LL * hsh1[L - 1][t] * bsk[R - L + 1][t]) % mod[t];
        if(res < 0)
            res += mod[t];
        return res;
    }

    int HashCode2(int L, int R, int t) {
        int res = (hsh2[L][t] - 1LL * hsh2[R + 1][t] * bsk[R - L + 1][t]) % mod[t];
        if(res < 0)
            res += mod[t];
        return res;
    }

    bool isEqual(int L1, int R1, int L2, int R2) {
        if(L1 < 1 || R1 > n || L2 < 1 || R2 > n)
            return 0;
        if(L1 > R1 && L2 > R2)
            return 1;
        for(int t = 0; t < 3; ++t) {
            if(HashCode1(L1, R1, t) != HashCode1(L2, R2, t))
                return 0;
        }
        return 1;
    }

    bool isReverseEqual(int L1, int R1, int L2, int R2) {
        if(L1 < 1 || R1 > n || L2 < 1 || R2 > n)
            return 0;
        if(L1 > R1 && L2 > R2)
            return 1;
        for(int t = 0; t < 3; ++t) {
            if(HashCode1(L1, R1, t) != HashCode2(L2, R2, t))
                return 0;
        }
        return 1;
    }

} sh;

namespace Solver {

    void InitOnce() {
#ifdef LOCAL
        freopen("A.txt", "r", stdin);
#endif // LOCAL
        int t;
        scanf("%d", &t);
    }

    char s[1000005];
    char t[1000005];
    char ans[1000005];

    void Read() {
        int res = scanf("%s", s + 1);
        if(res == -1)
            exit(0);
    }

    void Solve() {
        int n = strlen(s + 1);
        for(int i = 0; i <= n + 1; ++i) {
            t[i] = 0;
            ans[i] = 0;
        }
        bool suc = 1;
        for(int i = 1; i <= n; ++i) {
            if(s[i] != s[n + 1 - i]) {
                suc = 0;
                break;
            }
        }
        if(suc) {
            puts(s + 1);
            return;
        }
        int len = 0;
        int L = 1, R = n;
        while(s[L] == s[R]) {
            ans[++len] = s[L];
            ++L;
            --R;
        }
        int m = 0;
        for(int i = L; i <= R; ++i)
            t[++m] = s[i];
        sh.Init(t);
        int ansL = 0, ansR = -1;
        for(int i = 1; i <= m; ++i) {
            int l = (i - 1) * 2 + 1;
            if(l > ansR - ansL + 1) {
                if(sh.isReverseEqual(1, i, i, l))
                    ansL = 1, ansR = l;
            }
            l = i * 2;
            if(l > ansR - ansL + 1) {
                if(sh.isReverseEqual(1, i, i + 1, l))
                    ansL = 1, ansR = l;
            }
        }
        for(int i = m; i >= 1; --i) {
            int l = (m - i) * 2 + 1;
            if(l > ansR - ansL + 1) {
                if(sh.isReverseEqual(m - l + 1, i, i, m))
                    ansL = m - l + 1, ansR = m;
            }
            l = (m + 1 - i) * 2;
            if(l > ansR - ansL + 1) {
                if(sh.isReverseEqual(m - l + 1, i - 1, i, m))
                    ansL = m - l + 1, ansR = m;
            }
        }
        for(int i = 1; i <= len; ++i)
            putchar(ans[i]);
        for(int i = ansL; i <= ansR; ++i)
            putchar(t[i]);
        for(int i = 1; i <= len; ++i)
            putchar(ans[len + 1 - i]);
        puts("");
        return;
    }

}

int main() {
    Solver::InitOnce();
    while(true) {
        Solver::Read();
        Solver::Solve();
    }
    return 0;
}


//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#include<bits/stdc++.h>
using namespace std;

/* abbr */
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define msv(x, v) memset(x, v, sizeof(x))
#define ms(x) memset(x, 0, sizeof(x))

/* typedef for long long */

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;

/* typedef for other */

typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;

/* typedef for point */

//typedef pii point;
//#define x first
//#define y second

/* typedef for vector */

typedef vector<int> vi;
typedef vector<ll> vl;

/* constant number */

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double PI = acos(-1.0);
const double EPS = 1e-8;
const int MOD = 998244353;

template <typename T>
void cmin(T &x, T y) {
    if(y < x)
        x = y;
}

template <typename T>
void cmax(T &x, T y) {
    if(y > x)
        x = y;
}

#define dbg(x) cerr << "Line " << __LINE__ << ": " << #x " = " << (x) << endl

ll qpow(ll x, ll n, ll mod = MOD) {
    ll res = 1;
    while(n) {
        if(n & 1)
            res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

namespace Solver {

    void InitOnce() {
#ifdef LOCAL
        freopen("A.txt", "r", stdin);
#endif // LOCAL
        int t;
        scanf("%d", &t);
    }

    char s[1000005];
    char t[1000005];

    void Read() {
        int res = scanf("%s", s + 1);
        if(res == -1)
            exit(0);
    }

    int m;
    char ans[1000005];
    ll hash1[1000005];
    ll hash2[1000005];
    ll BASE[1000005];

    ll hash1LR(int L, int R) {
        //printf("hash1: [%d,%d]\n", L, R);
        if(L < 1 || R > m || L > R)
            return -775;
        ll res = (hash1[R] - hash1[L - 1] * BASE[R - L + 1] % MOD + MOD) % MOD;
        //dbg(res);
        return res;
    }

    ll hash2LR(int L, int R) {
        //printf("hash2: [%d,%d]\n", L, R);
        if(L < 1 || R > m || L > R)
            return -886;
        ll res = (hash2[L] - hash2[R + 1] * BASE[R - L + 1] % MOD + MOD) % MOD;
        //dbg(res);
        return res;
    }

    void Solve() {
        int n = strlen(s + 1);
        for(int i = 1; i <= n + 1; ++i) {
            t[i] = 0;
            ans[i] = 0;
        }
        bool suc = 1;
        for(int i = 1; i <= n; ++i) {
            if(s[i] != s[n + 1 - i]) {
                suc = 0;
                break;
            }
        }
        if(suc) {
            puts(s + 1);
            return;
        }
        int len = 0;
        int L = 1, R = n;
        while(s[L] == s[R]) {
            ans[++len] = s[L];
            ++L;
            --R;
        }
        //printf("[%d,%d]\n", L, R);
        m = 0;
        for(int i = L; i <= R; ++i)
            t[++m] = s[i];
        //printf("m=%d\n", m);
        BASE[0] = 1;
        BASE[1] = 1331;
        for(int i = 2; i <= m; ++i)
            BASE[i] = BASE[i - 1] * BASE[1] % MOD;

        for(int i = 1; i <= m; ++i)
            hash1[i] = (hash1[i - 1] * BASE[1] + t[i]) % MOD;
        hash2[m + 1] = 0;
        for(int i = m; i >= 1; --i)
            hash2[i] = (hash2[i + 1] * BASE[1] + t[i]) % MOD;
        int ansL = 0, ansR = -1;
        for(int i = 1; i <= m; ++i) {
            {
                int l = (i - 1) * 2 + 1;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(1, i) == hash2LR(i, l)) {
                        ansL = 1, ansR = l;
                    }
                }
            } {
                int l = i * 2;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(1, i) == hash2LR(i + 1, l)) {
                        ansL = 1, ansR = l;
                    }
                }
            }
        }
        for(int i = m; i >= 1; --i) {
            {
                int l = (m - i) * 2 + 1;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(m - l + 1, i) == hash2LR(i, m)) {
                        ansL = m - l + 1, ansR = m;
                    }
                }
            } {
                int l = (m + 1 - i) * 2;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(m - l + 1, i - 1) == hash2LR(i, m)) {
                        ansL = m - l + 1, ansR = m;
                    }
                }
            }
        }
        //printf("[%d,%d]\n", ansL, ansR);
        for(int i = 1; i <= len; ++i)
            putchar(ans[i]);
        for(int i = ansL; i <= ansR; ++i)
            putchar(t[i]);
        for(int i = 1; i <= len; ++i)
            putchar(ans[len + 1 - i]);
        puts("");
        return;
    }

}

int main() {
    Solver::InitOnce();
    while(true) {
        Solver::Read();
        Solver::Solve();
    }
    return 0;
}


//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#include<bits/stdc++.h>
using namespace std;

/* abbr */
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define msv(x, v) memset(x, v, sizeof(x))
#define ms(x) memset(x, 0, sizeof(x))

/* typedef for long long */

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;

/* typedef for other */

typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;

/* typedef for point */

//typedef pii point;
//#define x first
//#define y second

/* typedef for vector */

typedef vector<int> vi;
typedef vector<ll> vl;

/* constant number */

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double PI = acos(-1.0);
const double EPS = 1e-8;
const int MOD = 998244353;

template <typename T>
void cmin(T &x, T y) {
    if(y < x)
        x = y;
}

template <typename T>
void cmax(T &x, T y) {
    if(y > x)
        x = y;
}

#define dbg(x) cerr << "Line " << __LINE__ << ": " << #x " = " << (x) << endl

ll qpow(ll x, ll n, ll mod = MOD) {
    ll res = 1;
    while(n) {
        if(n & 1)
            res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

namespace Solver {

    void InitOnce() {
#ifdef LOCAL
        freopen("A.txt", "r", stdin);
#endif // LOCAL
        int t;
        scanf("%d", &t);
    }

    char s[200005];
    char t[200005];

    void Read() {
        int res = scanf("%s", s + 1);
        if(res == -1)
            exit(0);
    }

    int m;
    char ans[200005];
    ll hash1[200005];
    ll hash2[200005];
    ll BASE[200005];

    ll hash1LR(int L, int R) {
        //printf("hash1: [%d,%d]\n", L, R);
        if(R > m)
            return -775;
        ll res = (hash1[R] - hash1[L - 1] * BASE[R - L + 1] % MOD + MOD) % MOD;
        //dbg(res);
        return res;
    }

    ll hash2LR(int L, int R) {
        //printf("hash2: [%d,%d]\n", L, R);
        if(R > m)
            return -886;
        ll res = (hash2[L] - hash2[R + 1] * BASE[R - L + 1] % MOD + MOD) % MOD;
        //dbg(res);
        return res;
    }

    void Solve() {
        int n = strlen(s + 1);
        for(int i = 1; i <= n + 1; ++i) {
            t[i] = 0;
            ans[i] = 0;
        }
        bool suc = 1;
        for(int i = 1; i <= n; ++i) {
            if(s[i] != s[n + 1 - i]) {
                suc = 0;
                break;
            }
        }
        if(suc) {
            puts(s + 1);
            return;
        }
        int len = 0;
        int L = 1, R = n;
        while(s[L] == s[R]) {
            ans[++len] = s[L];
            ++L;
            --R;
        }
        //printf("[%d,%d]\n", L, R);
        m = 0;
        for(int i = L; i <= R; ++i)
            t[++m] = s[i];
        //printf("m=%d\n", m);
        BASE[0] = 1;
        BASE[1] = 1331;
        for(int i = 2; i <= m; ++i)
            BASE[i] = BASE[i - 1] * BASE[1] % MOD;

        for(int i = 1; i <= m; ++i)
            hash1[i] = (hash1[i - 1] * BASE[1] + t[i]) % MOD;
        hash2[m + 1] = 0;
        for(int i = m; i >= 1; --i)
            hash2[i] = (hash2[i + 1] * BASE[1] + t[i]) % MOD;
        int ansL = 0, ansR = -1;
        for(int i = 1; i <= m; ++i) {
            {
                int l = (i - 1) * 2 + 1;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(1, i - 1) == hash2LR(i + 1, l)) {
                        ansL = 1, ansR = l;
                    }
                }
            } {
                int l = i * 2;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(1, i) == hash2LR(i + 1, l)) {
                        ansL = 1, ansR = l;
                    }
                }
            }
        }
        for(int i = m; i >= 1; --i) {
            {
                int l = (m - i) * 2 + 1;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(m - l + 1, i - 1) == hash2LR(i + 1, m)) {
                        ansL = m - l + 1, ansR = m;
                    }
                }
            } {
                int l = (m + 1 - i) * 2;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(m - l + 1, i - 1) == hash2LR(i, m)) {
                        ansL = m - l + 1, ansR = m;
                    }
                }
            }
        }
        //printf("[%d,%d]\n", ansL, ansR);
        for(int i = 1; i <= len; ++i)
            putchar(ans[i]);
        for(int i = ansL; i <= ansR; ++i)
            putchar(t[i]);
        for(int i = 1; i <= len; ++i)
            putchar(ans[len + 1 - i]);
        puts("");
        return;
    }

}

int main() {
    Solver::InitOnce();
    while(true) {
        Solver::Read();
        Solver::Solve();
    }
    return 0;
}


//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#include<bits/stdc++.h>
using namespace std;

/* abbr */
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define msv(x, v) memset(x, v, sizeof(x))
#define ms(x) memset(x, 0, sizeof(x))

/* typedef for long long */

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;

/* typedef for other */

typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;

/* typedef for point */

//typedef pii point;
//#define x first
//#define y second

/* typedef for vector */

typedef vector<int> vi;
typedef vector<ll> vl;

/* constant number */

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double PI = acos(-1.0);
const double EPS = 1e-8;
const int MOD = 998244353;

template <typename T>
void cmin(T &x, T y) {
    if(y < x)
        x = y;
}

template <typename T>
void cmax(T &x, T y) {
    if(y > x)
        x = y;
}

#define dbg(x) cerr << "Line " << __LINE__ << ": " << #x " = " << (x) << endl

ll qpow(ll x, ll n, ll mod = MOD) {
    ll res = 1;
    while(n) {
        if(n & 1)
            res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

namespace Solver {

    void InitOnce() {
#ifdef LOCAL
        freopen("A.txt", "r", stdin);
#endif // LOCAL
        int t;
        scanf("%d", &t);
    }

    char s[1000005];
    char t[1000005];

    void Read() {
        int res = scanf("%s", s + 1);
        if(res == -1)
            exit(0);
    }

    int m;
    char ans[1000005];
    ll hash1[1000005];
    ll hash2[1000005];
    ll BASE[1000005];

    ll hash1LR(int L, int R) {
        //printf("hash1: [%d,%d]\n", L, R);
        if(L < 1 || R > m || L > R)
            return -775;
        ll res = (hash1[R] - hash1[L - 1] * BASE[R - L + 1] % MOD + MOD) % MOD;
        //dbg(res);
        return res;
    }

    ll hash2LR(int L, int R) {
        //printf("hash2: [%d,%d]\n", L, R);
        if(L < 1 || R > m || L > R)
            return -886;
        ll res = (hash2[L] - hash2[R + 1] * BASE[R - L + 1] % MOD + MOD) % MOD;
        //dbg(res);
        return res;
    }

    void Solve() {
        int n = strlen(s + 1);
        for(int i = 1; i <= n + 1; ++i) {
            t[i] = 0;
            ans[i] = 0;
        }
        bool suc = 1;
        for(int i = 1; i <= n; ++i) {
            if(s[i] != s[n + 1 - i]) {
                suc = 0;
                break;
            }
        }
        if(suc) {
            puts(s + 1);
            return;
        }
        int len = 0;
        int L = 1, R = n;
        while(s[L] == s[R]) {
            ans[++len] = s[L];
            ++L;
            --R;
        }
        //printf("[%d,%d]\n", L, R);
        m = 0;
        for(int i = L; i <= R; ++i)
            t[++m] = s[i];
        //printf("m=%d\n", m);
        BASE[0] = 1;
        BASE[1] = 2337;
        for(int i = 2; i <= m; ++i)
            BASE[i] = BASE[i - 1] * BASE[1] % MOD;

        for(int i = 1; i <= m; ++i)
            hash1[i] = (hash1[i - 1] * BASE[1] + t[i]) % MOD;
        hash2[m + 1] = 0;
        for(int i = m; i >= 1; --i)
            hash2[i] = (hash2[i + 1] * BASE[1] + t[i]) % MOD;
        int ansL = 0, ansR = -100;
        for(int i = 1; i <= m; ++i) {
            {
                int l = (i - 1) * 2 + 1;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(1, i) == hash2LR(i, l)) {
                        ansL = 1, ansR = l;
                    }
                }
            } {
                int l = i * 2;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(1, i) == hash2LR(i + 1, l)) {
                        ansL = 1, ansR = l;
                    }
                }
            }
        }
        for(int i = m; i >= 1; --i) {
            {
                int l = (m - i) * 2 + 1;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(m - l + 1, i) == hash2LR(i, m)) {
                        ansL = m - l + 1, ansR = m;
                    }
                }
            } {
                int l = (m + 1 - i) * 2;
                if(l > ansR - ansL + 1) {
                    if(hash1LR(m - l + 1, i - 1) == hash2LR(i, m)) {
                        ansL = m - l + 1, ansR = m;
                    }
                }
            }
        }
        //printf("[%d,%d]\n", ansL, ansR);
        for(int i = 1; i <= len; ++i)
            putchar(ans[i]);
        for(int i = ansL; i <= ansR; ++i)
            putchar(t[i]);
        for(int i = 1; i <= len; ++i)
            putchar(ans[len + 1 - i]);
        puts("");
        return;
    }

}

int main() {
    Solver::InitOnce();
    while(true) {
        Solver::Read();
        Solver::Solve();
    }
    return 0;
}


#include "bits/stdc++.h"
using namespace std;
#define endl '\n'
#define int long long

int MD = 47;
int MOD = 79333;

const int MXN = 1000005;
int po[MXN];

int p(int x) {
	return po[x];
}

int long_pf(string left) {
	if (left.size() == 0) return 0;
	int m = left.size();
	int ha[m], hb[m];
	ha[0] = (left[0] - 'a');
	hb[0] = (left[0] - 'a');
	for (int i=1; i<m; i++) {
		ha[i] = ha[i-1] * MD + (left[i] - 'a');
		hb[i] = hb[i-1] + (left[i] - 'a') * p(i);
		ha[i] %= MOD;
		hb[i] %= MOD;
	}

	int a = 0;
	for (int i=0; i<m; i++) {
		if (ha[i] == hb[i]) a = i;
	}
	return a + 1; // length
}

void solve() {

	string s;
	cin >> s;
	int n = s.size();
	int side = 0;
	bool go = true;
	int i = 0, j = n-1;

	for (i=0, j=n-1; i<j && go; i++, j--) {
		if (s[i] == s[j]) {
			side++;
		}
		else {
			go = false;
			i--;
			j++;
		}
	}
	// i and j is remianing bit
	// i--; j++;
	// cout << ": " << i << " ; " << j << endl;
	string left = "";
	for (int X=i; X<=j; X++) left += s[X];
	if (i > j) {
		cout << s << endl;
		return;
	}
	string co = "";
	for (int i=left.size()-1; i>=0; i--) {
		co += left[i];
	}
	int fpr = long_pf(left);
	int fsf = long_pf(co);
	if (fpr > fsf) {
		// cout << "PR: " << endl;
		for (int j=0; j<side; j++) cout << s[j];
		for (int j=0; j<fpr; j++) cout << left[j];
		for (int j=side-1; j>=0; j--) cout << s[j];
		cout << endl;
	}
	else {
		// cout << "SF: " << endl;
		for (int j=0; j<side; j++) cout << s[j];
		for (int j=0; j<fsf; j++) cout << co[j];
		for (int j=side-1; j>=0; j--) cout << s[j];
		cout << endl;
	}

}

signed main() {

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

	po[0] = 1;
	for (int i=1; i<MXN; i++) {
		po[i] = po[i-1] * MD;
		po[i] %= MOD;
	}

	int t;
	cin >> t;
	while (t--) solve();

}


#include "bits/stdc++.h"
using namespace std;
#define endl '\n'
#define int long long

int MD = 1282817;
int MOD = 1289083;

const int MXN = 1000005;
int po[MXN];

int p(int x) {
	return po[x];
}

int long_pf(string left) {
	if (left.size() == 0) return 0;
	int m = left.size();
	int ha[m], hb[m];
	ha[0] = (left[0] - 'a');
	hb[0] = (left[0] - 'a');
	for (int i=1; i<m; i++) {
		ha[i] = ha[i-1] * MD + (left[i] - 'a');
		hb[i] = hb[i-1] + (left[i] - 'a') * p(i);
		ha[i] %= MOD;
		hb[i] %= MOD;
	}

	int a = 0;
	for (int i=0; i<m; i++) {
		if (ha[i] == hb[i]) a = i;
	}
	return a + 1; // length
}

void solve() {

	string s;
	cin >> s;
	int n = s.size();
	int side = 0;
	bool go = true;
	int i = 0, j = n-1;

	for (i=0, j=n-1; i<j && go; i++, j--) {
		if (s[i] == s[j]) {
			side++;
		}
		else {
			go = false;
			i--;
			j++;
		}
	}
	// i and j is remianing bit
	// i--; j++;
	// cout << ": " << i << " ; " << j << endl;
	string left = "";
	for (int X=i; X<=j; X++) left += s[X];
	if (i > j) {
		cout << s << endl;
		return;
	}
	string co = "";
	for (int i=left.size()-1; i>=0; i--) {
		co += left[i];
	}
	int fpr = long_pf(left);
	int fsf = long_pf(co);
	if (fpr > fsf) {
		// cout << "PR: " << endl;
		for (int j=0; j<side; j++) cout << s[j];
		for (int j=0; j<fpr; j++) cout << left[j];
		for (int j=side-1; j>=0; j--) cout << s[j];
		cout << endl;
	}
	else {
		// cout << "SF: " << endl;
		for (int j=0; j<side; j++) cout << s[j];
		for (int j=0; j<fsf; j++) cout << co[j];
		for (int j=side-1; j>=0; j--) cout << s[j];
		cout << endl;
	}

}

signed main() {

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

	po[0] = 1;
	for (int i=1; i<MXN; i++) {
		po[i] = po[i-1] * MD;
		po[i] %= MOD;
	}

	int t;
	cin >> t;
	while (t--) solve();

}


#include "bits/stdc++.h"
using namespace std;
// #define endl '\n'
#define int long long

int MD = 47;
int MOD = 79333;

int p(int x) {
	if (x == 0) return 1;
	if (x == 1) return MD;
	int h = p(x / 2);
	if (x % 2 == 0) return (h * h) % MOD;
	return (((h * h) % MOD) * MD) % MOD;
}

int long_pf(string left) {
	if (left.size() == 0) return 0;
	int m = left.size();
	int ha[m], hb[m];
	ha[0] = (left[0] - 'a');
	hb[0] = (left[0] - 'a');
	for (int i=1; i<m; i++) {
		ha[i] = ha[i-1] * MD + (left[i] - 'a');
		hb[i] = hb[i-1] + (left[i] - 'a') * p(i);
		ha[i] %= MOD;
		hb[i] %= MOD;
	}

	int a = 0;
	for (int i=0; i<m; i++) {
		if (ha[i] == hb[i]) a = i;
	}
	return a + 1; // length
}

void solve() {

	string s;
	cin >> s;
	int n = s.size();
	string side = "";
	bool go = true;
	int i = 0, j = n-1;

	for (i=0, j=n-1; i<j && go; i++, j--) {
		if (s[i] == s[j]) {
			side += s[i];
		}
		else {
			go = false;
			i--;
			j++;
		}
	}
	// i and j is remianing bit
	// i--; j++;
	// cout << ": " << i << " ; " << j << endl;
	string left = "";
	for (int X=i; X<=j; X++) left += s[X];
	if (left.empty()) {
		cout << s << endl;
		return;
	}
	string co = left;
	reverse(co.begin(), co.end());
	int fpr = long_pf(left);
	int fsf = long_pf(co);
	if (fpr > fsf) {
		// cout << "PR: " << endl;
		cout << side;
		for (int j=0; j<fpr; j++) cout << left[j];
		for (int i=side.size()-1; i>=0; i--) cout << side[i];
		cout << endl;
	}
	else {
		// cout << "SF: " << endl;
		cout << side;
		for (int j=0; j<fsf; j++) cout << co[j];
		for (int i=side.size()-1; i>=0; i--) cout << side[i];
		cout << endl;
	}

}

signed main() {

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

	int t;
	cin >> t;
	while (t--) solve();

}


#include "bits/stdc++.h"
using namespace std;
#define endl '\n'
#define int long long

int MD = 47;
int MOD = 79333;

int p(int x) {
	if (x == 0) return 1;
	if (x == 1) return MD;
	int h = p(x / 2);
	if (x % 2 == 0) return (h * h) % MOD;
	return (((h * h) % MOD) * MD) % MOD;
}

int long_pf(string left) {
	if (left.size() == 0) return 0;
	int m = left.size();
	int ha[m], hb[m];
	ha[0] = (left[0] - 'a');
	hb[0] = (left[0] - 'a');
	for (int i=1; i<m; i++) {
		ha[i] = ha[i-1] * MD + (left[i] - 'a');
		hb[i] = hb[i-1] + (left[i] - 'a') * p(i);
		ha[i] %= MOD;
		hb[i] %= MOD;
	}

	int a = 0;
	for (int i=0; i<m; i++) {
		if (ha[i] == hb[i]) a = i;
	}
	return a + 1; // length
}

void solve() {

	string s;
	cin >> s;
	int n = s.size();
	string side = "";
	bool go = true;
	int i = 0, j = n-1;

	for (i=0, j=n-1; i<j && go; i++, j--) {
		if (s[i] == s[j]) {
			side += s[i];
		}
		else {
			go = false;
			i--;
			j++;
		}
	}
	// i and j is remianing bit
	// i--; j++;
	// cout << ": " << i << " ; " << j << endl;
	string left = "";
	for (int X=i; X<=j; X++) left += s[X];
	if (left.empty()) {
		cout << s << endl;
		return;
	}
	string co = left;
	reverse(co.begin(), co.end());
	int fpr = long_pf(left);
	int fsf = long_pf(co);
	if (fpr > fsf) {
		// cout << "PR: " << endl;
		cout << side;
		for (int j=0; j<fpr; j++) cout << left[j];
		for (int i=side.size()-1; i>=0; i--) cout << side[i];
		cout << endl;
	}
	else {
		// cout << "SF: " << endl;
		cout << side;
		for (int j=0; j<fsf; j++) cout << co[j];
		for (int i=side.size()-1; i>=0; i--) cout << side[i];
		cout << endl;
	}

}

signed main() {

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

	int t;
	cin >> t;
	while (t--) solve();

}


#include "bits/stdc++.h"
using namespace std;
// #define endl '\n'
#define int long long

int MD = 5693;
int MOD = 79333;

int p(int x) {
	if (x == 0) return 1;
	if (x == 1) return MD;
	int h = p(x / 2);
	if (x % 2 == 0) return (h * h) % MOD;
	return (((h * h) % MOD) * MD) % MOD;
}

int long_pf(string left) {
	if (left.size() == 0) return 0;
	int m = left.size();
	int ha[m], hb[m];
	ha[0] = (left[0] - 'a');
	hb[0] = (left[0] - 'a');
	for (int i=1; i<m; i++) {
		ha[i] = ha[i-1] * MD + (left[i] - 'a');
		hb[i] = hb[i-1] + (left[i] - 'a') * p(i);
		ha[i] %= MOD;
		hb[i] %= MOD;
	}

	int a = 0;
	for (int i=0; i<m; i++) {
		if (ha[i] == hb[i]) a = i;
	}
	return a + 1; // length
}

void solve() {

	string s;
	cin >> s;
	int n = s.size();
	string side = "";
	bool go = true;
	int i = 0, j = n-1;

	for (i=0, j=n-1; i<j && go; i++, j--) {
		if (s[i] == s[j]) {
			side += s[i];
		}
		else {
			go = false;
			i--;
			j++;
		}
	}
	// i and j is remianing bit
	// i--; j++;
	// cout << ": " << i << " ; " << j << endl;
	string left = "";
	for (int X=i; X<=j; X++) left += s[X];
	if (left.empty()) {
		cout << s << endl;
		return;
	}
	string co = left;
	reverse(co.begin(), co.end());
	int fpr = long_pf(left);
	int fsf = long_pf(co);
	if (fpr > fsf) {
		// cout << "PR: " << endl;
		cout << side;
		for (int j=0; j<fpr; j++) cout << left[j];
		for (int i=side.size()-1; i>=0; i--) cout << side[i];
		cout << endl;
	}
	else {
		// cout << "SF: " << endl;
		cout << side;
		for (int j=0; j<fsf; j++) cout << co[j];
		for (int i=side.size()-1; i>=0; i--) cout << side[i];
		cout << endl;
	}

}

signed main() {

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

	int t;
	cin >> t;
	while (t--) solve();

}


#include "bits/stdc++.h"
using namespace std;
#define endl '\n'
#define int long long

int MD = 1236737;
int MOD = 1235501;

const int MXN = 1000005;
int po[MXN];

int p(int x) {
	return po[x];
}

int long_pf(string left) {
	if (left.size() == 0) return 0;
	int m = left.size();
	int ha[m], hb[m];
	ha[0] = (left[0] - 'a');
	hb[0] = (left[0] - 'a');
	for (int i=1; i<m; i++) {
		ha[i] = ha[i-1] * MD + (left[i] - 'a');
		hb[i] = hb[i-1] + (left[i] - 'a') * p(i);
		ha[i] %= MOD;
		hb[i] %= MOD;
	}

	int a = 0;
	for (int i=0; i<m; i++) {
		if (ha[i] == hb[i]) a = i;
	}
	return a + 1; // length
}

void solve() {

	string s;
	cin >> s;
	int n = s.size();
	int side = 0;
	bool go = true;
	int i = 0, j = n-1;

	for (i=0, j=n-1; i<j && go; i++, j--) {
		if (s[i] == s[j]) {
			side++;
		}
		else {
			go = false;
			i--;
			j++;
		}
	}
	// i and j is remianing bit
	// i--; j++;
	// cout << ": " << i << " ; " << j << endl;
	string left = "";
	for (int X=i; X<=j; X++) left += s[X];
	if (i > j) {
		cout << s << endl;
		return;
	}
	string co = "";
	for (int i=left.size()-1; i>=0; i--) {
		co += left[i];
	}
	int fpr = long_pf(left);
	int fsf = long_pf(co);
	if (fpr > fsf) {
		// cout << "PR: " << endl;
		for (int j=0; j<side; j++) cout << s[j];
		for (int j=0; j<fpr; j++) cout << left[j];
		for (int j=side-1; j>=0; j--) cout << s[j];
		cout << endl;
	}
	else {
		// cout << "SF: " << endl;
		for (int j=0; j<side; j++) cout << s[j];
		for (int j=0; j<fsf; j++) cout << co[j];
		for (int j=side-1; j>=0; j--) cout << s[j];
		cout << endl;
	}

}

signed main() {

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

	po[0] = 1;
	for (int i=1; i<MXN; i++) {
		po[i] = po[i-1] * MD;
		po[i] %= MOD;
	}

	int t;
	cin >> t;
	while (t--) solve();

}


#include "bits/stdc++.h"
using namespace std;
#define endl '\n'
#define int long long

int MD = 1476173;
int MOD = 1526587;

const int MXN = 1000005;
int po[MXN];

int p(int x) {
	return po[x];
}

int long_pf(string left) {
	if (left.size() == 0) return 0;
	int m = left.size();
	int ha[m], hb[m];
	ha[0] = (left[0] - 'a');
	hb[0] = (left[0] - 'a');
	for (int i=1; i<m; i++) {
		ha[i] = ha[i-1] * MD + (left[i] - 'a');
		hb[i] = hb[i-1] + (left[i] - 'a') * p(i);
		ha[i] %= MOD;
		hb[i] %= MOD;
	}

	int a = 0;
	for (int i=0; i<m; i++) {
		if (ha[i] == hb[i]) a = i;
	}
	return a + 1; // length
}

void solve() {

	string s;
	cin >> s;
	int n = s.size();
	int side = 0;
	bool go = true;
	int i = 0, j = n-1;

	for (i=0, j=n-1; i<j && go; i++, j--) {
		if (s[i] == s[j]) {
			side++;
		}
		else {
			go = false;
			i--;
			j++;
		}
	}
	// i and j is remianing bit
	// i--; j++;
	// cout << ": " << i << " ; " << j << endl;
	string left = "";
	for (int X=i; X<=j; X++) left += s[X];
	if (i > j) {
		cout << s << endl;
		return;
	}
	string co = "";
	for (int i=left.size()-1; i>=0; i--) {
		co += left[i];
	}
	int fpr = long_pf(left);
	int fsf = long_pf(co);
	if (fpr > fsf) {
		// cout << "PR: " << endl;
		for (int j=0; j<side; j++) cout << s[j];
		for (int j=0; j<fpr; j++) cout << left[j];
		for (int j=side-1; j>=0; j--) cout << s[j];
		cout << endl;
	}
	else {
		// cout << "SF: " << endl;
		for (int j=0; j<side; j++) cout << s[j];
		for (int j=0; j<fsf; j++) cout << co[j];
		for (int j=side-1; j>=0; j--) cout << s[j];
		cout << endl;
	}

}

signed main() {

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

	po[0] = 1;
	for (int i=1; i<MXN; i++) {
		po[i] = po[i-1] * MD;
		po[i] %= MOD;
	}

	int t;
	cin >> t;
	while (t--) solve();

}


#include "bits/stdc++.h"
using namespace std;
#define endl '\n'
#define int long long

int MD = 485201;
int MOD = 998344313;

const int MXN = 1000005;
int po[MXN];
int ha[MXN];
int hb[MXN];

int p(int x) {
	return po[x];
}

int long_pf(string left) {
	if (left.size() == 0) return 0;
	int m = left.size();
	ha[0] = (left[0] - 'a');
	hb[0] = (left[0] - 'a');
	for (int i=1; i<m; i++) {
		ha[i] = ha[i-1] * MD + (left[i] - 'a');
		hb[i] = hb[i-1] + (left[i] - 'a') * p(i);
		ha[i] %= MOD;
		hb[i] %= MOD;
	}

	int a = 0;
	for (int i=0; i<m; i++) {
		if (ha[i] == hb[i]) a = i;
	}
	return a + 1; // length
}

void solve() {

	string s;
	cin >> s;
	int n = s.size();
	int side = 0;
	bool go = true;
	int i = 0, j = n-1;

	for (i=0, j=n-1; i<j && go; i++, j--) {
		if (s[i] == s[j]) {
			side++;
		}
		else {
			go = false;
			i--;
			j++;
		}
	}
	// i and j is remianing bit
	// i--; j++;
	// cout << ": " << i << " ; " << j << endl;
	string left = "";
	for (int X=i; X<=j; X++) left += s[X];
	if (i > j) {
		cout << s << endl;
		return;
	}
	string co = "";
	for (int i=left.size()-1; i>=0; i--) {
		co += left[i];
	}
	int fpr = long_pf(left);
	int fsf = long_pf(co);
	if (fpr > fsf) {
		// cout << "PR: " << endl;
		for (int j=0; j<side; j++) cout << s[j];
		for (int j=0; j<fpr; j++) cout << left[j];
		for (int j=side-1; j>=0; j--) cout << s[j];
		cout << endl;
	}
	else {
		// cout << "SF: " << endl;
		for (int j=0; j<side; j++) cout << s[j];
		for (int j=0; j<fsf; j++) cout << co[j];
		for (int j=side-1; j>=0; j--) cout << s[j];
		cout << endl;
	}

}

signed main() {

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

	po[0] = 1;
	for (int i=1; i<MXN; i++) {
		po[i] = po[i-1] * MD;
		po[i] %= MOD;
	}

	int t;
	cin >> t;
	while (t--) solve();

}


#include "bits/stdc++.h"
using namespace std;
#define endl '\n'
#define int long long

int MD = 47;
int MOD = 1001267;

const int MXN = 1000005;
int po[MXN];

int p(int x) {
	return po[x];
}

int long_pf(string left) {
	if (left.size() == 0) return 0;
	int m = left.size();
	int ha[m], hb[m];
	ha[0] = (left[0] - 'a');
	hb[0] = (left[0] - 'a');
	for (int i=1; i<m; i++) {
		ha[i] = ha[i-1] * MD + (left[i] - 'a');
		hb[i] = hb[i-1] + (left[i] - 'a') * p(i);
		ha[i] %= MOD;
		hb[i] %= MOD;
	}

	int a = 0;
	for (int i=0; i<m; i++) {
		if (ha[i] == hb[i]) a = i;
	}
	return a + 1; // length
}

void solve() {

	string s;
	cin >> s;
	int n = s.size();
	int side = 0;
	bool go = true;
	int i = 0, j = n-1;

	for (i=0, j=n-1; i<j && go; i++, j--) {
		if (s[i] == s[j]) {
			side++;
		}
		else {
			go = false;
			i--;
			j++;
		}
	}
	// i and j is remianing bit
	// i--; j++;
	// cout << ": " << i << " ; " << j << endl;
	string left = "";
	for (int X=i; X<=j; X++) left += s[X];
	if (i > j) {
		cout << s << endl;
		return;
	}
	string co = "";
	for (int i=left.size()-1; i>=0; i--) {
		co += left[i];
	}
	int fpr = long_pf(left);
	int fsf = long_pf(co);
	if (fpr > fsf) {
		// cout << "PR: " << endl;
		for (int j=0; j<side; j++) cout << s[j];
		for (int j=0; j<fpr; j++) cout << left[j];
		for (int j=side-1; j>=0; j--) cout << s[j];
		cout << endl;
	}
	else {
		// cout << "SF: " << endl;
		for (int j=0; j<side; j++) cout << s[j];
		for (int j=0; j<fsf; j++) cout << co[j];
		for (int j=side-1; j>=0; j--) cout << s[j];
		cout << endl;
	}

}

signed main() {

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

	po[0] = 1;
	for (int i=1; i<MXN; i++) {
		po[i] = po[i-1] * MD;
		po[i] %= MOD;
	}

	int t;
	cin >> t;
	while (t--) solve();

}


#include "bits/stdc++.h"
using namespace std;
#define endl '\n'
#define int long long

int MD = 1000381;
int MOD = 1027751;

const int MXN = 1000005;
int po[MXN];

int p(int x) {
	return po[x];
}

int long_pf(string left) {
	if (left.size() == 0) return 0;
	int m = left.size();
	int ha[m], hb[m];
	ha[0] = (left[0] - 'a');
	hb[0] = (left[0] - 'a');
	for (int i=1; i<m; i++) {
		ha[i] = ha[i-1] * MD + (left[i] - 'a');
		hb[i] = hb[i-1] + (left[i] - 'a') * p(i);
		ha[i] %= MOD;
		hb[i] %= MOD;
	}

	int a = 0;
	for (int i=0; i<m; i++) {
		if (ha[i] == hb[i]) a = i;
	}
	return a + 1; // length
}

void solve() {

	string s;
	cin >> s;
	int n = s.size();
	int side = 0;
	bool go = true;
	int i = 0, j = n-1;

	for (i=0, j=n-1; i<j && go; i++, j--) {
		if (s[i] == s[j]) {
			side++;
		}
		else {
			go = false;
			i--;
			j++;
		}
	}
	// i and j is remianing bit
	// i--; j++;
	// cout << ": " << i << " ; " << j << endl;
	string left = "";
	for (int X=i; X<=j; X++) left += s[X];
	if (i > j) {
		cout << s << endl;
		return;
	}
	string co = "";
	for (int i=left.size()-1; i>=0; i--) {
		co += left[i];
	}
	int fpr = long_pf(left);
	int fsf = long_pf(co);
	if (fpr > fsf) {
		// cout << "PR: " << endl;
		for (int j=0; j<side; j++) cout << s[j];
		for (int j=0; j<fpr; j++) cout << left[j];
		for (int j=side-1; j>=0; j--) cout << s[j];
		cout << endl;
	}
	else {
		// cout << "SF: " << endl;
		for (int j=0; j<side; j++) cout << s[j];
		for (int j=0; j<fsf; j++) cout << co[j];
		for (int j=side-1; j>=0; j--) cout << s[j];
		cout << endl;
	}

}

signed main() {

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

	po[0] = 1;
	for (int i=1; i<MXN; i++) {
		po[i] = po[i-1] * MD;
		po[i] %= MOD;
	}

	int t;
	cin >> t;
	while (t--) solve();

}


#pragma GCC optimize("Ofast")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 998244353;
const int inf = 1e9;
typedef pair<int, int> ii;
typedef pair<int, int> i2;
typedef pair<ll, int> lli;
typedef pair<ll, ll> ll2;

#define mxm(a,b) (a)=max((a),(b))
#define mnm(a,b) (a)=min((a),(b))

//							#define ONLINE_JUDGE
int sz(int l, int r) {
	return r + 1 - l;
}
const int N = 1e6 + 10; //
const ll base = 31;
ll hashFromLeft[N], hashFromRight[N];
ll bpow[N];
string s;
int n;
//i<=j
ll sub(ll a, ll b) { return (a + mod - b) % mod; }
ll mul(ll a, ll b) {
	return ((a%mod)*(b%mod)) % mod;
}
ll hl(int i, int j) {
	if (i == 0) return hashFromLeft[j];
	ll cax = mul(hashFromLeft[i - 1], bpow[j - i + 1]);
	return sub(hashFromLeft[j], cax);
}
ll hr(int i, int j) {
	if (j == n - 1) return hashFromRight[i];
	ll cax = mul(hashFromRight[j + 1], bpow[j - i + 1]); //TO BE CHECKED
	return sub(hashFromRight[i], cax);
}
bool palin(int l, int r) {
	if (l == r) return true;
	if (l > r) return false;
	ll cax = hl(l, r);
	ll loz = hr(l, r);
	return cax == loz;
}
string concua(int l, int r) {
	if (l > r) return string("");
	if (l == r) return string("") + s[l];
	if (s[l] == s[r]) {
		string tmp = concua(l + 1, r - 1);
		return string("") + s[l] + tmp + s[r];
	}
	int k = 1, len = 1;
	for (int i = l + 1; i <= r; ++i)
		if (palin(l, i) && i - l + 1 > len) {
			len = i - l + 1;
			k = len;
		}
	for (int i = r - 1; i >= l; --i)
		if (palin(i, r) && r - i + 1 > len) {
			len = r - i + 1;
			k = -len;
		}
	string ans("");
	if (k > 0) {
		for (int i = l; i <= l + len - 1; ++i) ans += s[i];
	}
	else {
		for (int i = r - len + 1; i <= r; ++i) ans += s[i];
	}
	return ans;
}
int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	ifstream fin; fin.open("input.txt");
	ofstream fout; fout.open("output.txt");
	if (fin.is_open()) {
		cin.rdbuf(fin.rdbuf());
	}
	if (fout.is_open()) {
		cout.rdbuf(fout.rdbuf());
	}
#endif

	//MAIN CODE HERE
	//MAIN CODE HERE NHA
	//DKM NOI MAIN CODE HERE MA CO NGHE KHONG???
	int t; cin >> t;
	bpow[0] = 1LL; for (int i = 1; i < N; ++i) bpow[i] = (bpow[i - 1] * base) % mod;
	while (t--) {
		cin >> s; n = s.length();
		hashFromLeft[0] = s[0] - 'a' + 1;
		for (int i = 1; i < n; ++i) hashFromLeft[i] = (hashFromLeft[i - 1] * base + (s[i] - 'a' + 1)) % mod;
		hashFromRight[n] = 0LL;
		for (int i = n - 1; i >= 0; --i) hashFromRight[i] = (hashFromRight[i + 1] * base + (s[i] - 'a' + 1)) % mod;
		int l = 0, r = n - 1;
		while (l + 1 < r && s[l] == s[r]) {
			l++;
			r--;
		}
		string ans("");
		for (int i = 0; i < l; ++i) ans += s[i];
		string tmp = concua(l, r);
		ans += tmp;
		for (int i = r + 1; i < n; ++i) ans += s[i];
		cout << ans << '\n';
	}

#ifdef CUA
	fin.close(); fout.close();
#endif //CUA

}


#pragma GCC optimize("Ofast")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = (ll)1e9 + 7;
const int inf = 1e9;
typedef pair<int, int> ii;
typedef pair<int, int> i2;
typedef pair<ll, int> lli;
typedef pair<ll, ll> ll2;

//							#define CUA
const int N=1e6+10; //
const ll base = 29;
ll hashFromLeft[N], hashFromRight[N];
ll bpow[N];
string s;
int n;
//i<=j
ll sub(ll a, ll b) { return (a + mod - b) % mod; }
ll mul(ll a, ll b) {
	return ((a%mod)*(b%mod)) % mod;
}
ll hl(int i, int j) {
	if (i == 0) return hashFromLeft[j];
	ll cax = mul(hashFromLeft[i - 1], bpow[j - i + 1]);
	return sub(hashFromLeft[j], cax);
}
ll hr(int i, int j) {
	if (j == n - 1) return hashFromRight[i];
	ll cax = mul(hashFromRight[j + 1], bpow[j - i + 1]); //TO BE CHECKED
	return sub(hashFromRight[i], cax);
}
bool palin(int l, int r) {
	if (l == r) return true;
	if (l > r) return false;
	ll cax = hl(l, r);
	ll loz = hr(l, r);
	return cax == loz;
}
string concua(int l, int r) {
	if (l > r) return string("");
	if (l == r) return string("") + s[l];
	if (s[l] == s[r]) {
		string tmp = concua(l + 1, r - 1);
		return string("") + s[l] + tmp + s[r];
	}
	int k = 1, len = 1;
	for (int i=l+1; i<=r; ++i)
		if (palin(l, i) && i-l+1>len) {
			len = i - l + 1;
			k = len;
		}
	for (int i=r-1; i>=l; --i)
		if (palin(i, r) && r - i + 1 > len) {
			len = r - i + 1;
			k = -len;
		}
	string ans("");
	if (k > 0) {
		for (int i = l; i <= l + len-1; ++i) ans += s[i];
	}
	else {
		for (int i = r-len+1; i <= r; ++i) ans += s[i];
	}
	return ans;
}
int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifdef CUA
	ifstream fin; fin.open("input.txt");
	ofstream fout; fout.open("output.txt");
	if (fin.is_open()) {
		cin.rdbuf(fin.rdbuf());
	}
	if (fout.is_open()) {
		cout.rdbuf(fout.rdbuf());
	}
#endif //CUA

	//MAIN CODE HERE
	//MAIN CODE HERE NHA
	//DKM NOI MAIN CODE HERE MA CO NGHE KHONG???
	int t; cin >> t;
	bpow[0] = 1LL; for (int i = 1; i < N; ++i) bpow[i] = (bpow[i - 1] * base) % mod;
	while (t--) {
		cin >> s; n = s.length();
		hashFromLeft[0] = s[0] - 'a' + 1;
		for (int i = 1; i < n; ++i) hashFromLeft[i] = (hashFromLeft[i - 1] * base + (s[i] - 'a' + 1)) % mod;
		hashFromRight[n] = 0LL;
		for (int i = n-1; i >= 0; --i) hashFromRight[i] = (hashFromRight[i + 1] * base + (s[i] - 'a' + 1)) % mod;
		int l = 0, r = n - 1;
		while (l+1<r && s[l] == s[r]) {
			l++;
			r--;
		}
		string ans("");
		for (int i = 0; i < l; ++i) ans += s[i];
		string tmp = concua(l, r);
		ans += tmp;
		for (int i = r + 1; i < n; ++i) ans += s[i];
		cout << ans << '\n';
	}

#ifdef CUA
	fin.close(); fout.close();
#endif //CUA

}


#pragma GCC optimize("Ofast")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 998244353;
const int inf = 1e9;
typedef pair<int, int> ii;
typedef pair<int, int> i2;
typedef pair<ll, int> lli;
typedef pair<ll, ll> ll2;

#define mxm(a,b) (a)=max((a),(b))
#define mnm(a,b) (a)=min((a),(b))

//							#define ONLINE_JUDGE
int sz(int l, int r) {
	return r + 1 - l;
}
const int N = 1e6 + 10; //
const ll base = 37;
ll hashFromLeft[N], hashFromRight[N];
ll bpow[N];
string s;
int n;
//i<=j
ll sub(ll a, ll b) { return (a + mod - b) % mod; }
ll mul(ll a, ll b) {
	return ((a%mod)*(b%mod)) % mod;
}
ll hl(int i, int j) {
	if (i == 0) return hashFromLeft[j];
	ll cax = mul(hashFromLeft[i - 1], bpow[j - i + 1]);
	return sub(hashFromLeft[j], cax);
}
ll hr(int i, int j) {
	if (j == n - 1) return hashFromRight[i];
	ll cax = mul(hashFromRight[j + 1], bpow[j - i + 1]); //TO BE CHECKED
	return sub(hashFromRight[i], cax);
}
bool palin(int l, int r) {
	if (l == r) return true;
	if (l > r) return false;
	ll cax = hl(l, r);
	ll loz = hr(l, r);
	return cax == loz;
}
string concua(int l, int r) {
	if (l > r) return string("");
	if (l == r) return string("") + s[l];
	if (s[l] == s[r]) {
		string tmp = concua(l + 1, r - 1);
		return string("") + s[l] + tmp + s[r];
	}
	int k = 1, len = 1;
	for (int i = l + 1; i <= r; ++i)
		if (palin(l, i) && i - l + 1 > len) {
			len = i - l + 1;
			k = len;
		}
	for (int i = r - 1; i >= l; --i)
		if (palin(i, r) && r - i + 1 > len) {
			len = r - i + 1;
			k = -len;
		}
	string ans("");
	if (k > 0) {
		for (int i = l; i <= l + len - 1; ++i) ans += s[i];
	}
	else {
		for (int i = r - len + 1; i <= r; ++i) ans += s[i];
	}
	return ans;
}
int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	ifstream fin; fin.open("input.txt");
	ofstream fout; fout.open("output.txt");
	if (fin.is_open()) {
		cin.rdbuf(fin.rdbuf());
	}
	if (fout.is_open()) {
		cout.rdbuf(fout.rdbuf());
	}
#endif

	//MAIN CODE HERE
	//MAIN CODE HERE NHA
	//DKM NOI MAIN CODE HERE MA CO NGHE KHONG???
	int t; cin >> t;
	bpow[0] = 1LL; for (int i = 1; i < N; ++i) bpow[i] = (bpow[i - 1] * base) % mod;
	while (t--) {
		cin >> s; n = s.length();
		hashFromLeft[0] = s[0] - 'a' + 1;
		for (int i = 1; i < n; ++i) hashFromLeft[i] = (hashFromLeft[i - 1] * base + (s[i] - 'a' + 1)) % mod;
		hashFromRight[n] = 0LL;
		for (int i = n - 1; i >= 0; --i) hashFromRight[i] = (hashFromRight[i + 1] * base + (s[i] - 'a' + 1)) % mod;
		int l = 0, r = n - 1;
		while (l + 1 < r && s[l] == s[r]) {
			l++;
			r--;
		}
		string ans("");
		for (int i = 0; i < l; ++i) ans += s[i];
		string tmp = concua(l, r);
		ans += tmp;
		for (int i = r + 1; i < n; ++i) ans += s[i];
		cout << ans << '\n';
	}

#ifdef CUA
	fin.close(); fout.close();
#endif //CUA

}