/*================= *\
|  ID : aman__guta__ |
|      LANG: C++     |
\*==================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mk make_pair
#define fs first
#define sc second
#define mod 1000000007
#define sp <<" "<<
#define JOKER ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
int spf[10000001];
long long int fac[200005];
void sieve(int n)
{
    spf[1]=1;
    for(int i=2;i<=n;i++) spf[i]=i;
    for(int i=4;i<=n;i+=2) spf[i]=2;
    for(int i=3;i*i<=n;i++)
    {
        if(spf[i]==i)
        {
            for(int j=i*i;j<=n;j+=i)
            {
                if(spf[j]==j) spf[j]=i;
            }
        }
    }
}
long long int power(long long int x, long long int y, long long int p)
{
    long long int res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
long long int modInverse(long long int n, long long int p)
{
    return power(n, p-2, p);
}
long long int nCrModPFermat(long long int n, long long int r, long long int p)
{
   if (r==0)
      return 1;
    return (fac[n]* modInverse(fac[r], p) % p *
            modInverse(fac[n-r], p) % p) % p;
}
int lps(string s)
{
    int n=s.size();
    // cout<<s<<endl;
    int lps[n];
    lps[0]=0;
    int i=1;
    int j=0;
    while(i<n)
    {
        if(s[i]==s[j])
        {
            j++;
            lps[i]=j;
            i++;
        }
        else
        {
            if(j)
            {
                j=lps[j-1];
            }
            else
            {
                lps[i]=0;
                i++;
            }

        }
    }
    // cout<<s<<"--"<<lps[n-1]<<endl;
    return lps[n-1];
}
main()
{
    JOKER
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        int n=s.size();
        int lo=0;
        int hi=s.size()-1;
        string a="";
        while(lo<hi)
        {
            if(s[lo]==s[hi])
            {
                a+=s[lo];
                lo++;
                hi--;
            }
            else
                break;
        }
        string b=a;
        reverse(b.begin(),b.end());
        int x=a.size();
        string s1="\0";
        for(int i=x; i<n-x; i++)
            s1+=s[i];
        string s2=s1;
        reverse(s2.begin(),s2.end());
        x=s1.size();
        int k1=min(x,lps(s1+s2));
        int k2=min(x,lps(s2+s1));
        // cout<<k1 sp k2<<endl;
        if(k1>=k2)
        {
            cout<<a;
            for(int i=0; i<k1; i++)
                cout<<s1[i];
            cout<<b<<endl;
        }
        else
        {
            cout<<a;
            for(int i=0; i<k2; i++)
                cout<<s2[i];
            cout<<b<<endl;
        }
    }
    cerr<<"Time elapsed : "<<clock()*1000.0/CLOCKS_PER_SEC<<"ms"<<'\n';
    return 0;
}


/*================= *\
|  ID : aman__guta__ |
|      LANG: C++     |
\*==================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mk make_pair
#define fs first
#define sc second
#define mod 1000000007
#define sp <<" "<<
#define JOKER ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
int spf[10000001];
long long int fac[200005];
void sieve(int n)
{
    spf[1]=1;
    for(int i=2;i<=n;i++) spf[i]=i;
    for(int i=4;i<=n;i+=2) spf[i]=2;
    for(int i=3;i*i<=n;i++)
    {
        if(spf[i]==i)
        {
            for(int j=i*i;j<=n;j+=i)
            {
                if(spf[j]==j) spf[j]=i;
            }
        }
    }
}
long long int power(long long int x, long long int y, long long int p)
{
    long long int res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
long long int modInverse(long long int n, long long int p)
{
    return power(n, p-2, p);
}
long long int nCrModPFermat(long long int n, long long int r, long long int p)
{
   if (r==0)
      return 1;
    return (fac[n]* modInverse(fac[r], p) % p *
            modInverse(fac[n-r], p) % p) % p;
}
string s;
int longestPrefixSuffix()
{
    int n = s.length();
    int lps[n];
    lps[0] = 0;
    int len = 0;
    int i = 1;
    while (i < n)
    {
        if (s[i] == s[len])
        {
            len++;
            lps[i] = len;
            i++;
        }
        else // (pat[i] != pat[len])
        {
            if (len != 0)
            {
                len = lps[len-1];
            }
            else // if (len == 0)
            {
                lps[i] = 0;
                i++;
            }
        }
    }
    int res = lps[n-1];
}
main()
{
    JOKER
    int t;
    cin>>t;
    while(t--)
    {
        cin>>s;
        int i=0;
        int j=s.size()-1;
        string a="",b="";
        while(i<j)
        {
            if(s[i]==s[j])
            {
                a+=s[i];
                b+=s[j];
                i++;
                j--;
            }
            else break;
        }
        string s1="";
        for(i=(int)a.size(); i<(int)s.size()-(int)b.size(); i++)
        {
            s1+=s[i];
        }
        s=s1;
        reverse(s1.begin(),s1.end());
        s+=s1;
        int x=s1.size();
        x=min(x,longestPrefixSuffix());
        s=s1;
        reverse(s1.begin(),s1.end());
        s+=s1;
        int y=s1.size();
        y=min(y,longestPrefixSuffix());
        reverse(b.begin(),b.end());
        cout<<a sp b<<endl;
        cout<<s1<<endl;
        cout<<x sp y<<endl;
        cout<<endl;
        if(x>=y)
        {

            cout<<a;
            for(int i=0; i<x; i++)
                cout<<s1[i];
            cout<<b<<endl;
        }
        else
        {
            cout<<a;
            for(int i=(int)s1.size()-1; i>(int)s1.size()-1-y; i--)
                cout<<s1[i];
            cout<<b<<endl;
        }
    }
    cerr<<"Time elapsed : "<<clock()*1000.0/CLOCKS_PER_SEC<<"ms"<<'\n';
    return 0;
}


/*================= *\
|  ID : aman__guta__ |
|      LANG: C++     |
\*==================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mk make_pair
#define fs first
#define sc second
#define mod 1000000007
#define sp <<" "<<
#define JOKER ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
int spf[10000001];
long long int fac[200005];
void sieve(int n)
{
    spf[1]=1;
    for(int i=2;i<=n;i++) spf[i]=i;
    for(int i=4;i<=n;i+=2) spf[i]=2;
    for(int i=3;i*i<=n;i++)
    {
        if(spf[i]==i)
        {
            for(int j=i*i;j<=n;j+=i)
            {
                if(spf[j]==j) spf[j]=i;
            }
        }
    }
}
long long int power(long long int x, long long int y, long long int p)
{
    long long int res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
long long int modInverse(long long int n, long long int p)
{
    return power(n, p-2, p);
}
long long int nCrModPFermat(long long int n, long long int r, long long int p)
{
   if (r==0)
      return 1;
    return (fac[n]* modInverse(fac[r], p) % p *
            modInverse(fac[n-r], p) % p) % p;
}
string s;
int longestPrefixSuffix()
{
    int n = s.size();
    int lps[n]={};
    lps[0] = 0;
    int len = 0;
    int i = 1;
    while (i < n)
    {
        if (s[i] == s[len])
        {
            len++;
            lps[i] = len;
            i++;
        }
        else // (pat[i] != pat[len])
        {
            if (len != 0)
            {
                len = lps[len-1];
            }
            else // if (len == 0)
            {
                lps[i] = 0;
                i++;
            }
        }
    }
    int res = lps[n-1];
    return res;
}
main()
{
    JOKER
    int t;
    cin>>t;
    while(t--)
    {
        cin>>s;
        int i=0;
        int j=s.size()-1;
        string a="",b="";
        while(i<j)
        {
            if(s[i]==s[j])
            {
                a+=s[i];
                b+=s[j];
                i++;
                j--;
            }
            else break;
        }
        string s1="";
        for(i=(int)a.size(); i<(int)s.size()-(int)b.size(); i++)
        {
            s1+=s[i];
        }
        s=s1;
        reverse(s1.begin(),s1.end());
        s+=s1;
        int x=(int)s1.size();
        // cout<<s<<endl;
        // cout<<longestPrefixSuffix()<<endl;
        x=min(x,longestPrefixSuffix());
        s=s1;
        reverse(s1.begin(),s1.end());
        s+=s1;
        int y=(int)s1.size();
        // cout<<s<<endl;
        // cout<<longestPrefixSuffix()<<endl;
        y=min(y,longestPrefixSuffix());
        reverse(b.begin(),b.end());
        if(x>=y)
        {

            cout<<a;
            for(int i=0; i<x; i++)
                cout<<s1[i];
            cout<<b<<endl;
        }
        else
        {
            cout<<a;
            for(int i=(int)s1.size()-1; i>(int)s1.size()-1-y; i--)
                cout<<s1[i];
            cout<<b<<endl;
        }
    }
    cerr<<"Time elapsed : "<<clock()*1000.0/CLOCKS_PER_SEC<<"ms"<<'\n';
    return 0;
}


/*================= *\
|  ID : aman__guta__ |
|      LANG: C++     |
\*==================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mk make_pair
#define fs first
#define sc second
#define mod 1000000007
#define sp <<" "<<
#define JOKER ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
int spf[10000001];
long long int fac[200005];
void sieve(int n)
{
    spf[1]=1;
    for(int i=2;i<=n;i++) spf[i]=i;
    for(int i=4;i<=n;i+=2) spf[i]=2;
    for(int i=3;i*i<=n;i++)
    {
        if(spf[i]==i)
        {
            for(int j=i*i;j<=n;j+=i)
            {
                if(spf[j]==j) spf[j]=i;
            }
        }
    }
}
long long int power(long long int x, long long int y, long long int p)
{
    long long int res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
long long int modInverse(long long int n, long long int p)
{
    return power(n, p-2, p);
}
long long int nCrModPFermat(long long int n, long long int r, long long int p)
{
   if (r==0)
      return 1;
    return (fac[n]* modInverse(fac[r], p) % p *
            modInverse(fac[n-r], p) % p) % p;
}
int lps(string s)
{
    int n=s.size();
    // cout<<s<<endl;
    int lps[n];
    lps[0]=0;
    int i=1;
    int j=0;
    while(i<n)
    {
        if(s[i]==s[j])
        {
            j++;
            lps[i]=j;
            i++;
        }
        else
        {
            if(j)
            {
                j=lps[j-1];
            }
            else
            {
                lps[i]=0;
                i++;
            }

        }
    }
    // cout<<s<<"--"<<lps[n-1]<<endl;
    return min(n/2,lps[n-1]);
}
main()
{
    JOKER
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        int n=s.size();
        int lo=0;
        int hi=s.size()-1;
        string a="\0";
        while(lo<hi)
        {
            if(s[lo]==s[hi])
            {
                a+=s[lo];
                lo++;
                hi--;
            }
            else
                break;
        }
        string b=a;
        reverse(b.begin(),b.end());
        int x=a.size();
        string s1="\0";
        for(int i=x; i<n-x; i++)
        {
            s1+=s[i];
        }
        string s2=s1;
        reverse(s2.begin(),s2.end());
        int k1=lps(s1+s2);
        int k2=lps(s2+s1);
        // cout<<k1 sp k2<<endl;
        if(k1>=k2)
        {
            cout<<a;
            for(int i=0; i<k1; i++)
                cout<<s1[i];
            cout<<b<<endl;
        }
        else
        {
            cout<<a;
            for(int i=0; i<k2; i++)
                cout<<s2[i];
            cout<<b<<endl;
        }
    }
    cerr<<"Time elapsed : "<<clock()*1000.0/CLOCKS_PER_SEC<<"ms"<<'\n';
    return 0;
}


/*================= *\
|  ID : aman__guta__ |
|      LANG: C++     |
\*==================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mk make_pair
#define fs first
#define sc second
#define mod 1000000007
#define sp <<" "<<
#define JOKER ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
int spf[10000001];
long long int fac[200005];
void sieve(int n)
{
    spf[1]=1;
    for(int i=2;i<=n;i++) spf[i]=i;
    for(int i=4;i<=n;i+=2) spf[i]=2;
    for(int i=3;i*i<=n;i++)
    {
        if(spf[i]==i)
        {
            for(int j=i*i;j<=n;j+=i)
            {
                if(spf[j]==j) spf[j]=i;
            }
        }
    }
}
long long int power(long long int x, long long int y, long long int p)
{
    long long int res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
long long int modInverse(long long int n, long long int p)
{
    return power(n, p-2, p);
}
long long int nCrModPFermat(long long int n, long long int r, long long int p)
{
   if (r==0)
      return 1;
    return (fac[n]* modInverse(fac[r], p) % p *
            modInverse(fac[n-r], p) % p) % p;
}
string s;
int longestPrefixSuffix()
{
    int n = s.length();
    int lps[n];
    lps[0] = 0;
    int len = 0;
    int i = 1;
    while (i < n)
    {
        if (s[i] == s[len])
        {
            len++;
            lps[i] = len;
            i++;
        }
        else // (pat[i] != pat[len])
        {
            if (len != 0)
            {
                len = lps[len-1];
            }
            else // if (len == 0)
            {
                lps[i] = 0;
                i++;
            }
        }
    }
    int res = lps[n-1];
}
main()
{
    JOKER
    int t;
    cin>>t;
    while(t--)
    {
        cin>>s;
        int i=0;
        int j=s.size()-1;
        string a="",b="";
        while(i<j)
        {
            if(s[i]==s[j])
            {
                a+=s[i];
                b+=s[j];
                i++;
                j--;
            }
            else break;
        }
        string s1="";
        for(i=(int)a.size(); i<(int)s.size()-(int)b.size(); i++)
        {
            s1+=s[i];
        }
        s=s1;
        reverse(s1.begin(),s1.end());
        s+=s1;
        int x=s1.size();
        cout<<s<<endl;
        x=min(x,longestPrefixSuffix());
        s=s1;
        reverse(s1.begin(),s1.end());
        s+=s1;
        int y=s1.size();
        cout<<s<<endl;
        y=min(y,longestPrefixSuffix());
        reverse(b.begin(),b.end());
        // cout<<a sp b<<endl;
        // cout<<s1<<endl;
        // cout<<x sp y<<endl;
        // cout<<endl;
        if(x>=y)
        {

            cout<<a;
            for(int i=0; i<x; i++)
                cout<<s1[i];
            cout<<b<<endl;
        }
        else
        {
            cout<<a;
            for(int i=(int)s1.size()-1; i>(int)s1.size()-1-y; i--)
                cout<<s1[i];
            cout<<b<<endl;
        }
    }
    cerr<<"Time elapsed : "<<clock()*1000.0/CLOCKS_PER_SEC<<"ms"<<'\n';
    return 0;
}


/*================= *\
|  ID : aman__guta__ |
|      LANG: C++     |
\*==================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mk make_pair
#define fs first
#define sc second
#define mod 1000000007
#define sp <<" "<<
#define JOKER ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
int spf[10000001];
long long int fac[200005];
void sieve(int n)
{
    spf[1]=1;
    for(int i=2;i<=n;i++) spf[i]=i;
    for(int i=4;i<=n;i+=2) spf[i]=2;
    for(int i=3;i*i<=n;i++)
    {
        if(spf[i]==i)
        {
            for(int j=i*i;j<=n;j+=i)
            {
                if(spf[j]==j) spf[j]=i;
            }
        }
    }
}
long long int power(long long int x, long long int y, long long int p)
{
    long long int res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
long long int modInverse(long long int n, long long int p)
{
    return power(n, p-2, p);
}
long long int nCrModPFermat(long long int n, long long int r, long long int p)
{
   if (r==0)
      return 1;
    return (fac[n]* modInverse(fac[r], p) % p *
            modInverse(fac[n-r], p) % p) % p;
}
int dp[2005][2005];
int check(string s)
{
    int i=0,j=s.size()-1;
    while(i<=j)
    {
        if(s[i]==s[j])
        {
            // cout<<s[i] sp s[j]<<endl;
            i++;
            j--;
        }
        else
            return 0;
    }
    return 1;
}
string s;
int longestPrefixSuffix()
{
    int n = s.length();
    int lps[n];
    lps[0] = 0;
    int len = 0;
    int i = 1;
    while (i < n)
    {
        if (s[i] == s[len])
        {
            len++;
            lps[i] = len;
            i++;
        }
        else // (pat[i] != pat[len])
        {
            if (len != 0)
            {
                len = lps[len-1];
            }
            else // if (len == 0)
            {
                lps[i] = 0;
                i++;
            }
        }
    }
    int res = lps[n-1];
}
main()
{
    JOKER
    int t;
    cin>>t;
    while(t--)
    {
        cin>>s;
        int i=0;
        int j=s.size()-1;
        string a="",b="";
        while(i<j)
        {
            if(s[i]==s[j])
            {
                a+=s[i];
                b+=s[j];
                i++;
                j--;
            }
            else break;
        }
        // cout<<a sp b<<endl;
        string s1="";
        for(i=a.size(); i<s.size()-b.size(); i++)
        {
            s1+=s[i];
        }
        s=s1;
        reverse(s1.begin(),s1.end());
        s+=s1;
        int x=s1.size();
        x=min(x,longestPrefixSuffix());

        s=s1;
        reverse(s1.begin(),s1.end());
        s+=s1;
        int y=s1.size();
        y=min(y,longestPrefixSuffix());
        // cout<<s<<endl;
        // cout<<y<<endl;
        if(x>=y)
        {

            cout<<a;
            for(int i=0; i<x; i++)
                cout<<s1[i];
            cout<<b<<endl;
        }
        else
        {
            cout<<a;
            for(int i=s1.size()-1; i>s1.size()-1-y; i--)
                cout<<s1[i];
            cout<<b<<endl;
        }
    }
    cerr<<"Time elapsed : "<<clock()*1000.0/CLOCKS_PER_SEC<<"ms"<<'\n';
    return 0;
}


/*================= *\
|  ID : aman__guta__ |
|      LANG: C++     |
\*==================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mk make_pair
#define fs first
#define sc second
#define mod 1000000007
#define sp <<" "<<
#define JOKER ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
int spf[10000001];
long long int fac[200005];
void sieve(int n)
{
    spf[1]=1;
    for(int i=2;i<=n;i++) spf[i]=i;
    for(int i=4;i<=n;i+=2) spf[i]=2;
    for(int i=3;i*i<=n;i++)
    {
        if(spf[i]==i)
        {
            for(int j=i*i;j<=n;j+=i)
            {
                if(spf[j]==j) spf[j]=i;
            }
        }
    }
}
long long int power(long long int x, long long int y, long long int p)
{
    long long int res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
long long int modInverse(long long int n, long long int p)
{
    return power(n, p-2, p);
}
long long int nCrModPFermat(long long int n, long long int r, long long int p)
{
   if (r==0)
      return 1;
    return (fac[n]* modInverse(fac[r], p) % p *
            modInverse(fac[n-r], p) % p) % p;
}
string s;
int longestPrefixSuffix()
{
    int n = s.length();
    int lps[n];
    lps[0] = 0;
    int len = 0;
    int i = 1;
    while (i < n)
    {
        if (s[i] == s[len])
        {
            len++;
            lps[i] = len;
            i++;
        }
        else // (pat[i] != pat[len])
        {
            if (len != 0)
            {
                len = lps[len-1];
            }
            else // if (len == 0)
            {
                lps[i] = 0;
                i++;
            }
        }
    }
    int res = lps[n-1];
}
main()
{
    JOKER
    int t;
    cin>>t;
    while(t--)
    {
        cin>>s;
        int i=0;
        int j=s.size()-1;
        string a="",b="";
        while(i<j)
        {
            if(s[i]==s[j])
            {
                a+=s[i];
                b+=s[j];
                i++;
                j--;
            }
            else break;
        }
        string s1="";
        for(i=(int)a.size(); i<(int)s.size()-(int)b.size(); i++)
        {
            s1+=s[i];
        }
        s=s1;
        reverse(s1.begin(),s1.end());
        s+=s1;
        int x=s1.size();
        cout<<s<<endl;
        cout<<longestPrefixSuffix()<<endl;
        x=min(x,longestPrefixSuffix());
        s=s1;
        reverse(s1.begin(),s1.end());
        s+=s1;
        int y=s1.size();
        cout<<s<<endl;
        cout<<longestPrefixSuffix()<<endl;
        y=min(y,longestPrefixSuffix());
        reverse(b.begin(),b.end());
        if(x>=y)
        {

            cout<<a;
            for(int i=0; i<x; i++)
                cout<<s1[i];
            cout<<b<<endl;
        }
        else
        {
            cout<<a;
            for(int i=(int)s1.size()-1; i>(int)s1.size()-1-y; i--)
                cout<<s1[i];
            cout<<b<<endl;
        }
    }
    cerr<<"Time elapsed : "<<clock()*1000.0/CLOCKS_PER_SEC<<"ms"<<'\n';
    return 0;
}


/*================= *\
|  ID : aman__guta__ |
|      LANG: C++     |
\*==================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mk make_pair
#define fs first
#define sc second
#define mod 1000000007
#define sp <<" "<<
#define JOKER ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
int spf[10000001];
long long int fac[200005];
void sieve(int n)
{
    spf[1]=1;
    for(int i=2;i<=n;i++) spf[i]=i;
    for(int i=4;i<=n;i+=2) spf[i]=2;
    for(int i=3;i*i<=n;i++)
    {
        if(spf[i]==i)
        {
            for(int j=i*i;j<=n;j+=i)
            {
                if(spf[j]==j) spf[j]=i;
            }
        }
    }
}
long long int power(long long int x, long long int y, long long int p)
{
    long long int res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
long long int modInverse(long long int n, long long int p)
{
    return power(n, p-2, p);
}
long long int nCrModPFermat(long long int n, long long int r, long long int p)
{
   if (r==0)
      return 1;
    return (fac[n]* modInverse(fac[r], p) % p *
            modInverse(fac[n-r], p) % p) % p;
}
int dp[2005][2005];
int check(string s)
{
    int i=0,j=s.size()-1;
    while(i<=j)
    {
        if(s[i]==s[j])
        {
            // cout<<s[i] sp s[j]<<endl;
            i++;
            j--;
        }
        else
            return 0;
    }
    return 1;
}
string s;
int longestPrefixSuffix()
{
    int n = s.length();
    int lps[n];
    lps[0] = 0;
    int len = 0;
    int i = 1;
    while (i < n)
    {
        if (s[i] == s[len])
        {
            len++;
            lps[i] = len;
            i++;
        }
        else // (pat[i] != pat[len])
        {
            if (len != 0)
            {
                len = lps[len-1];
            }
            else // if (len == 0)
            {
                lps[i] = 0;
                i++;
            }
        }
    }
    int res = lps[n-1];
}
main()
{
    JOKER
    int t;
    cin>>t;
    while(t--)
    {
        cin>>s;
        int i=0;
        int j=s.size()-1;
        string a="",b="";
        while(i<j)
        {
            if(s[i]==s[j])
            {
                a+=s[i];
                b+=s[j];
                i++;
                j--;
            }
            else break;
        }
        string s1="";
        for(i=(int)a.size(); i<(int)s.size()-(int)b.size(); i++)
        {
            s1+=s[i];
        }
        s=s1;
        reverse(s1.begin(),s1.end());
        s+=s1;
        int x=s1.size();
        x=min(x,longestPrefixSuffix());
        s=s1;
        reverse(s1.begin(),s1.end());
        s+=s1;
        int y=s1.size();
        y=min(y,longestPrefixSuffix());
        reverse(b.begin(),b.end());
        if(x>=y)
        {

            cout<<a;
            for(int i=0; i<x; i++)
                cout<<s1[i];
            cout<<b<<endl;
        }
        else
        {
            cout<<a;
            for(int i=(int)s1.size()-1; i>(int)s1.size()-1-y; i--)
                cout<<s1[i];
            cout<<b<<endl;
        }
    }
    cerr<<"Time elapsed : "<<clock()*1000.0/CLOCKS_PER_SEC<<"ms"<<'\n';
    return 0;
}


/*================= *\
|  ID : aman__guta__ |
|      LANG: C++     |
\*==================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mk make_pair
#define fs first
#define sc second
#define mod 1000000007
#define sp <<" "<<
#define JOKER ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
int spf[10000001];
long long int fac[200005];
void sieve(int n)
{
    spf[1]=1;
    for(int i=2;i<=n;i++) spf[i]=i;
    for(int i=4;i<=n;i+=2) spf[i]=2;
    for(int i=3;i*i<=n;i++)
    {
        if(spf[i]==i)
        {
            for(int j=i*i;j<=n;j+=i)
            {
                if(spf[j]==j) spf[j]=i;
            }
        }
    }
}
long long int power(long long int x, long long int y, long long int p)
{
    long long int res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
long long int modInverse(long long int n, long long int p)
{
    return power(n, p-2, p);
}
long long int nCrModPFermat(long long int n, long long int r, long long int p)
{
   if (r==0)
      return 1;
    return (fac[n]* modInverse(fac[r], p) % p *
            modInverse(fac[n-r], p) % p) % p;
}
int lps(string s)
{
    int n=s.size();
    // cout<<s<<endl;
    int lps[n];
    lps[0]=0;
    int i=1;
    int j=0;
    while(i<n)
    {
        if(s[i]==s[j])
        {
            j++;
            lps[i]=j;
            i++;
        }
        else
        {
            if(j)
            {
                j=lps[j-1];
            }
            else
            {
                lps[i]=0;
                i++;
            }

        }
    }
    // cout<<s<<"--"<<lps[n-1]<<endl;
    return min(n/2,lps[n-1]);
}
main()
{
    JOKER
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        int n=s.size();
        int lo=0;
        int hi=s.size()-1;
        string a="\0";
        while(lo<hi)
        {
            if(s[lo]==s[hi])
            {
                a+=s[lo];
                lo++;
                hi--;
            }
            else
                break;
        }
        string b=a;
        // cout<<a sp b<<endl;
        reverse(b.begin(),b.end());
        int x=a.size();
        string s1="\0";
        for(int i=x; i<n-x; i++)
        {
            s1+=s[i];
        }
        string s2=s1;
        reverse(s2.begin(),s2.end());
        // cout<<s1 sp s2<<endl;
        int k1=lps(s1+"54"+s2);
        int k2=lps(s2+"54"+s1);
        // cout<<k1 sp k2<<endl;
        if(k1>=k2)
        {
            cout<<a;
            for(int i=0; i<k1; i++)
                cout<<s1[i];
            cout<<b<<endl;
        }
        else
        {
            cout<<a;
            for(int i=0; i<k2; i++)
                cout<<s2[i];
            cout<<b<<endl;
        }
    }
    cerr<<"Time elapsed : "<<clock()*1000.0/CLOCKS_PER_SEC<<"ms"<<'\n';
    return 0;
}


/*================= *\
|  ID : aman__guta__ |
|      LANG: C++     |
\*==================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mk make_pair
#define fs first
#define sc second
#define mod 1000000007
#define sp <<" "<<
#define JOKER ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
int spf[10000001];
long long int fac[200005];
void sieve(int n)
{
    spf[1]=1;
    for(int i=2;i<=n;i++) spf[i]=i;
    for(int i=4;i<=n;i+=2) spf[i]=2;
    for(int i=3;i*i<=n;i++)
    {
        if(spf[i]==i)
        {
            for(int j=i*i;j<=n;j+=i)
            {
                if(spf[j]==j) spf[j]=i;
            }
        }
    }
}
long long int power(long long int x, long long int y, long long int p)
{
    long long int res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
long long int modInverse(long long int n, long long int p)
{
    return power(n, p-2, p);
}
long long int nCrModPFermat(long long int n, long long int r, long long int p)
{
   if (r==0)
      return 1;
    return (fac[n]* modInverse(fac[r], p) % p *
            modInverse(fac[n-r], p) % p) % p;
}
int lps(string s)
{
    int n=s.size();
    // cout<<s<<endl;
    int lps[n];
    lps[0]=0;
    int i=1;
    int j=0;
    while(i<n)
    {
        if(s[i]==s[j])
        {
            j++;
            lps[i]=j;
            i++;
        }
        else
        {
            if(j)
            {
                j=lps[j-1];
            }
            else
            {
                lps[i]=0;
                i++;
            }

        }
    }
    // cout<<s<<"--"<<lps[n-1]<<endl;
    return lps[n-1];
}
main()
{
    JOKER
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        int n=s.size();
        int lo=0;
        int hi=s.size()-1;
        string a="";
        while(lo<hi)
        {
            if(s[lo]==s[hi])
            {
                a+=s[lo];
                lo++;
                hi--;
            }
            else
                break;
        }
        string b=a;
        reverse(b.begin(),b.end());
        int x=a.size();
        string s1="";
        for(int i=x; i<n-x; i++)
            s1+=s[i];
        string s2=s1;
        reverse(s2.begin(),s2.end());
        x=s1.size();
        int k1=min(x,lps(s1+s2));
        int k2=min(x,lps(s2+s1));
        // cout<<k1 sp k2<<endl;
        if(k1>=k2)
        {
            cout<<a;
            for(int i=0; i<k1; i++)
                cout<<s1[i];
            cout<<b<<endl;
        }
        else
        {
            cout<<a;
            for(int i=0; i<k2; i++)
                cout<<s2[i];
            cout<<b<<endl;
        }
    }
    cerr<<"Time elapsed : "<<clock()*1000.0/CLOCKS_PER_SEC<<"ms"<<'\n';
    return 0;
}


/*================= *\
|  ID : aman__guta__ |
|      LANG: C++     |
\*==================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mk make_pair
#define fs first
#define sc second
#define mod 1000000007
#define sp <<" "<<
#define JOKER ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
int spf[10000001];
long long int fac[200005];
void sieve(int n)
{
    spf[1]=1;
    for(int i=2;i<=n;i++) spf[i]=i;
    for(int i=4;i<=n;i+=2) spf[i]=2;
    for(int i=3;i*i<=n;i++)
    {
        if(spf[i]==i)
        {
            for(int j=i*i;j<=n;j+=i)
            {
                if(spf[j]==j) spf[j]=i;
            }
        }
    }
}
long long int power(long long int x, long long int y, long long int p)
{
    long long int res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
long long int modInverse(long long int n, long long int p)
{
    return power(n, p-2, p);
}
long long int nCrModPFermat(long long int n, long long int r, long long int p)
{
   if (r==0)
      return 1;
    return (fac[n]* modInverse(fac[r], p) % p *
            modInverse(fac[n-r], p) % p) % p;
}
int dp[2005][2005];
int check(string s)
{
    int i=0,j=s.size()-1;
    while(i<=j)
    {
        if(s[i]==s[j])
        {
            // cout<<s[i] sp s[j]<<endl;
            i++;
            j--;
        }
        else
            return 0;
    }
    return 1;
}
string s;
int longestPrefixSuffix()
{
    int n = s.length();
    int lps[n];
    lps[0] = 0;
    int len = 0;
    int i = 1;
    while (i < n)
    {
        if (s[i] == s[len])
        {
            len++;
            lps[i] = len;
            i++;
        }
        else // (pat[i] != pat[len])
        {
            if (len != 0)
            {
                len = lps[len-1];
            }
            else // if (len == 0)
            {
                lps[i] = 0;
                i++;
            }
        }
    }
    int res = lps[n-1];
}
main()
{
    JOKER
    int t;
    cin>>t;
    while(t--)
    {
        cin>>s;
        int i=0;
        int j=s.size()-1;
        string a="",b="";
        while(i<j)
        {
            if(s[i]==s[j])
            {
                a+=s[i];
                b+=s[j];
                i++;
                j--;
            }
            else break;
        }
        // cout<<a sp b<<endl;
        string s1="";
        for(i=(int)a.size(); i<(int)s.size()-(int)b.size(); i++)
        {
            s1+=s[i];
        }
        s=s1;
        reverse(s1.begin(),s1.end());
        s+=s1;
        int x=s1.size();
        x=min(x,longestPrefixSuffix());

        s=s1;
        reverse(s1.begin(),s1.end());
        s+=s1;
        int y=s1.size();
        y=min(y,longestPrefixSuffix());
        // cout<<s<<endl;
        // cout<<y<<endl;
        if(x>=y)
        {

            cout<<a;
            for(int i=0; i<x; i++)
                cout<<s1[i];
            cout<<b<<endl;
        }
        else
        {
            cout<<a;
            for(int i=(int)s1.size()-1; i>(int)s1.size()-1-y; i--)
                cout<<s1[i];
            cout<<b<<endl;
        }
    }
    cerr<<"Time elapsed : "<<clock()*1000.0/CLOCKS_PER_SEC<<"ms"<<'\n';
    return 0;
}


/*================= *\
|  ID : aman__guta__ |
|      LANG: C++     |
\*==================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mk make_pair
#define fs first
#define sc second
#define mod 1000000007
#define sp <<" "<<
#define JOKER ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
int spf[10000001];
long long int fac[200005];
void sieve(int n)
{
    spf[1]=1;
    for(int i=2;i<=n;i++) spf[i]=i;
    for(int i=4;i<=n;i+=2) spf[i]=2;
    for(int i=3;i*i<=n;i++)
    {
        if(spf[i]==i)
        {
            for(int j=i*i;j<=n;j+=i)
            {
                if(spf[j]==j) spf[j]=i;
            }
        }
    }
}
long long int power(long long int x, long long int y, long long int p)
{
    long long int res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
long long int modInverse(long long int n, long long int p)
{
    return power(n, p-2, p);
}
long long int nCrModPFermat(long long int n, long long int r, long long int p)
{
   if (r==0)
      return 1;
    return (fac[n]* modInverse(fac[r], p) % p *
            modInverse(fac[n-r], p) % p) % p;
}
string s;
int longestPrefixSuffix()
{
    int n = s.length();
    int lps[n];
    lps[0] = 0;
    int len = 0;
    int i = 1;
    while (i < n)
    {
        if (s[i] == s[len])
        {
            len++;
            lps[i] = len;
            i++;
        }
        else // (pat[i] != pat[len])
        {
            if (len != 0)
            {
                len = lps[len-1];
            }
            else // if (len == 0)
            {
                lps[i] = 0;
                i++;
            }
        }
    }
    int res = lps[n-1];
}
main()
{
    JOKER
    int t;
    cin>>t;
    while(t--)
    {
        cin>>s;
        int i=0;
        int j=s.size()-1;
        string a="",b="";
        while(i<j)
        {
            if(s[i]==s[j])
            {
                a+=s[i];
                b+=s[j];
                i++;
                j--;
            }
            else break;
        }
        string s1="";
        for(i=(int)a.size(); i<(int)s.size()-(int)b.size(); i++)
        {
            s1+=s[i];
        }
        s=s1;
        reverse(s1.begin(),s1.end());
        s+=s1;
        int x=s1.size();
        x=min(x,longestPrefixSuffix());
        s=s1;
        reverse(s1.begin(),s1.end());
        s+=s1;
        int y=s1.size();
        y=min(y,longestPrefixSuffix());
        reverse(b.begin(),b.end());
        cout<<a sp b<endl;
        cout<<s1<<endl;
        cout<<x sp y<<endl;
        cout<<endl;
        if(x>=y)
        {

            cout<<a;
            for(int i=0; i<x; i++)
                cout<<s1[i];
            cout<<b<<endl;
        }
        else
        {
            cout<<a;
            for(int i=(int)s1.size()-1; i>(int)s1.size()-1-y; i--)
                cout<<s1[i];
            cout<<b<<endl;
        }
    }
    cerr<<"Time elapsed : "<<clock()*1000.0/CLOCKS_PER_SEC<<"ms"<<'\n';
    return 0;
}


/*================= *\
|  ID : aman__guta__ |
|      LANG: C++     |
\*==================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mk make_pair
#define fs first
#define sc second
#define mod 1000000007
#define sp <<" "<<
#define JOKER ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
int spf[10000001];
long long int fac[200005];
void sieve(int n)
{
    spf[1]=1;
    for(int i=2;i<=n;i++) spf[i]=i;
    for(int i=4;i<=n;i+=2) spf[i]=2;
    for(int i=3;i*i<=n;i++)
    {
        if(spf[i]==i)
        {
            for(int j=i*i;j<=n;j+=i)
            {
                if(spf[j]==j) spf[j]=i;
            }
        }
    }
}
long long int power(long long int x, long long int y, long long int p)
{
    long long int res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
long long int modInverse(long long int n, long long int p)
{
    return power(n, p-2, p);
}
long long int nCrModPFermat(long long int n, long long int r, long long int p)
{
   if (r==0)
      return 1;
    return (fac[n]* modInverse(fac[r], p) % p *
            modInverse(fac[n-r], p) % p) % p;
}
int lps(string s)
{
    int n=s.size();
    // cout<<s<<endl;
    int lps[n];
    lps[0]=0;
    int i=1;
    int j=0;
    while(i<n)
    {
        if(s[i]==s[j])
        {
            j++;
            lps[i]=j;
            i++;
        }
        else
        {
            if(j)
            {
                j=lps[j-1];
            }
            else
            {
                lps[i]=0;
                i++;
            }

        }
    }
    // cout<<s<<"--"<<lps[n-1]<<endl;
    return lps[n-1];
}
main()
{
    JOKER
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        int n=s.size();
        int lo=0;
        int hi=s.size()-1;
        string a="\0";
        while(lo<hi)
        {
            if(s[lo]==s[hi])
            {
                a+=s[lo];
                lo++;
                hi--;
            }
            else
                break;
        }
        string b=a;
        reverse(b.begin(),b.end());
        int x=a.size();
        string s1="\0";
        int y=0;
        for(int i=x; i<n-x; i++)
        {
            s1+=s[i];
            y++;
        }
        string s2=s1;
        reverse(s2.begin(),s2.end());
        x=s1.size();
        int k1=min(y,lps(s1+s2));
        int k2=min(y,lps(s2+s1));
        // cout<<k1 sp k2<<endl;
        if(k1>=k2)
        {
            cout<<a;
            for(int i=0; i<k1; i++)
                cout<<s1[i];
            cout<<b<<endl;
        }
        else
        {
            cout<<a;
            for(int i=0; i<k2; i++)
                cout<<s2[i];
            cout<<b<<endl;
        }
    }
    cerr<<"Time elapsed : "<<clock()*1000.0/CLOCKS_PER_SEC<<"ms"<<'\n';
    return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#define all(a) a.begin(),a.end()
#define forr(i,p,n) for(int i=p;i<n;i++)
using namespace std;
typedef long long ll;
//priority_queue<ll> pq;
bool pal(string s){
  ll j=s.size()-1;
  ll sw=0;
  for(ll i=0;i<s.size()/2;i++){
    if(s[i]!=s[j]){sw=1;break;}j--;}
  if(sw)return false;
  else return true;
}
//unordered_map<string,ll> mp,mp1;
int main()
{
  //ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie();
    ll a,b,c,d,n,t,m,k;
    scanf("%lld ",&t);
    while(t--){
      //mp1.clear();
      string s;
      //unordered_map<string,ll> mp,mp1;
      cin>>s;
      if(pal(s)){
	cout<<s<<endl;
	continue;
      }
      ll tam=s.size();
      string s1,s2,s3;
      s1.clear();
      ll ans=0;
      ll sw=0;
      ll l=0;
       string ss="",ss1="",aux="";
       ll j=tam-1;
      forr(i,0,tam/2+1){
        if(s[i]!=s[j]){
	  l=i;
	  break;
	}
	j--;
      }
       j=l-1;
      forr(i,0,l){
	ss+=s[i];
	ss1+=s[j];
	j--;
      }
      //cout<<ss<<" "<<ss1<<endl;
      // s1.clear();
      ll r=tam-1-l;
      forr(i,l,r+1){
	s1+=s[i];
	if(pal(s1)){
	  aux=s1;
	}
      }
      //cout<<aux<<endl;
      s1.clear();
      for(int i=r=min(tam-1,r);i>=l;i--){
	//	cout<<s1<<endl;
	s1+=s[i];
	if(pal(s1)){
	  if(s1.size()>aux.size()){
	    aux=s1;
	  }
	}
      }
      cout<<ss<<aux<<ss1<<endl;
    }
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#define all(a) a.begin(),a.end()
#define forr(i,p,n) for(int i=p;i<n;i++)
using namespace std;
typedef long long ll;
//priority_queue<ll> pq;
bool pal(string s){
  ll j=s.size()-1;
  ll sw=0;
  for(ll i=0;i<s.size()/2;i++){
    if(s[i]!=s[j]){sw=1;break;}j--;}
  if(sw)return false;
  else return true;
}
string man(string x){
  string t="#";ll tam=x.size();
  forr(i,0,tam){
    t+=x[i];t+="#";
  }
  ll p[tam*2+2];
  ll c=0,r=-1,rad;
  for(ll i=0;i<tam*2;i++){
    if(i<=r){
      rad=min(p[2*c-i],r-i);
    }
    else rad=0;
    while(i+rad<t.size() && i-rad>=0 && t[i-rad]==t[i+rad])rad++;
    p[i]=rad;
    if(i+rad-1>r){
      c=i;
      r=i+rad-1;
    }
  }/*
  cout<<t<<endl;
  forr(i,0,tam*2){
    cout<<p[i]-1;
  }
  cout<<endl;*/
  ll maxi=0,pos=0;
  forr(i,0,tam*2){
    if(i>0){
      ll h=p[i]-1;
      if(i-h==0 or i+h>=tam*2-1){
	if(maxi<p[i]){
	  maxi=p[i]-1;
	  pos=i;
	  //   cout<<maxi<<" "<<pos<<endl;
	}
      }
    }
  }
  string ans;
  for(int i=pos-maxi;i<=pos+maxi;i++){
    if(t[i]!='#')ans+=t[i];
  }
  // cout<<ans<<endl;
  return ans;
}
//unordered_map<string,ll> mp,mp1;
int main()
{
  //ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie();
    ll a,b,c,d,n,t,m,k;
    scanf("%lld ",&t);
    while(t--){
      //mp1.clear();
      string s;
      //unordered_map<string,ll> mp,mp1;
      cin>>s;
      if(pal(s)){
	cout<<s<<endl;
	continue;
      }
      ll tam=s.size();
      string s1,s2,s3;
      s1.clear();
      ll ans=0;
      ll sw=0;
      ll l=0,r=s.size();
       string ss="",ss1="",aux="";
       ll j=tam-1;
      forr(i,0,tam/2+1){
        if(s[i]!=s[j]){
	  l=i;r=j;
	  break;
	}
	j--;
      }
       j=l-1;
      forr(i,0,l){
	ss+=s[i];
	ss1+=s[j];
	j--;
      }
      //cout<<ss<<" "<<ss1<<endl;
      // s1.clear();
      //cout<<l<<" "<<r<<endl;
      ll h=abs(l-r)+1;
      s2=s.substr(l,h);
      //cout<<s2<<endl;
      s2=man(s2);
      cout<<ss<<s2<<ss1<<endl;
    }
}


#include <bits/stdc++.h>
#define all(a) a.begin(),a.end()
#define allr(a) a.rbegin(),a.rend()
#define F first
#define S second
#define INF 3000000000
#define inf -10000000
#define pb push_back
#define forr(i,p,n) for(int i=p;i<n;i++)
#define mpk make_pair
#define upb(a,s) upper_bound(a.begin(),a.end(),s)
#define lwb(a,s) lower_bound(a.begin(),a.end(),s)
#define D(x) cout << #x " = " << (x) << endl
#define mem(a,val) memset(a,val,sizeof a)
#define ulti(a) for(int i=0;i<a.size();i++)cout<<a[i]<<" "
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
//priority_queue<ll> pq;
bool pal(string s){
  ll j=s.size()-1;
  ll sw=0;
  for(ll i=0;i<s.size()/2;i++){
    if(s[i]!=s[j]){sw=1;break;}j--;}
  if(sw)return false;
  else return true;
}
unordered_map<string,ll> mp,mp1;
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie();
    ll a,b,c,d,n,t,m,k;
    cin>>t;
    while(t--){
      mp1.clear();
      string s;
      //unordered_map<string,ll> mp,mp1;
      cin>>s;
      if(pal(s)){
	cout<<s<<endl;
	continue;
      }
      ll tam=s.size();
      string s1,s2,s3;
      for(int i=tam-1;i>=0;i--){
	s1+=s[i];
	mp1[s1]=1;
      }
      s1.clear();
      ll ans=0;
      ll sw=0;
      ll l=0;
      string ss="",ss1="",aux="";
      forr(i,0,tam){
	s1+=s[i];
	//cout<<s1<<endl;
	if(mp1[s1]==1){
	  if(s1.size()*2<=tam){
	    ss=s1;
	    ss1=ss;
	    ans=s1.size()*2;
	    reverse(all(ss1));
	    l=i+1;
	    continue;
	  }
	}
      }
      //cout<<ss<<" "<<ss1<<endl;
      s1.clear();
      ll r=tam-1-l;
      forr(i,l,r+1){
	s1+=s[i];
	if(pal(s1)){
	  aux=s1;
	}
      }
      //cout<<aux<<endl;
      s1.clear();
      for(int i=r=min(tam-1,r);i>=l;i--){
	//	cout<<s1<<endl;
	s1+=s[i];
	if(pal(s1)){
	  if(s1.size()>aux.size()){
	    aux=s1;
	  }
	}
      }
      cout<<ss<<aux<<ss1<<endl;
    }
}


#include <bits/stdc++.h>
#define all(a) a.begin(),a.end()
#define allr(a) a.rbegin(),a.rend()
#define F first
#define S second
#define INF 3000000000
#define inf -10000000
#define pb push_back
#define forr(i,p,n) for(int i=p;i<n;i++)
#define mpk make_pair
#define upb(a,s) upper_bound(a.begin(),a.end(),s)
#define lwb(a,s) lower_bound(a.begin(),a.end(),s)
#define D(x) cout << #x " = " << (x) << endl
#define mem(a,val) memset(a,val,sizeof a)
#define ulti(a) for(int i=0;i<a.size();i++)cout<<a[i]<<" "
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
//priority_queue<ll> pq;
bool pal(string s){
  ll j=s.size()-1;
  ll sw=0;
  for(ll i=0;i<s.size()/2;i++){
    if(s[i]!=s[j]){sw=1;break;}j--;}
  if(sw)return false;
  else return true;
}
bool fun(string s,string pre){
  ll j=0,sw=0;
  for(int i=s.size()-1;i>=0;i--){
    if(j==pre.size())break;
    if(s[i]!=pre[j]){sw=1;break;}
    j++;
  }
  if(sw)return false;
  else return true;
}
//unordered_map<string,ll> mp,mp1;
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie();
    ll a,b,c,d,n,t,m,k;
    cin>>t;
    while(t--){
      //mp1.clear();
      string s;
      //unordered_map<string,ll> mp,mp1;
      cin>>s;
      if(pal(s)){
	cout<<s<<endl;
	continue;
      }
      ll tam=s.size();
      string s1,s2,s3;
      s1.clear();
      ll ans=0;
      ll sw=0;
      ll l=0;
       string ss="",ss1="",aux="";
      forr(i,0,tam){
	s1+=s[i];
	//cout<<s1<<endl;
	if(fun(s,s1)){
	  if(s1.size()*2<=tam){
	    ss=s1;
	    ss1=ss;
	    ans=s1.size()*2;
	    //reverse(all(ss1));
	    l=i+1;
	    continue;
	  }
	}
      }
      //cout<<ss<<" "<<ss1<<endl;
      s1.clear();
      ll r=tam-1-l;
      forr(i,l,r+1){
	s1+=s[i];
	if(pal(s1)){
	  aux=s1;
	}
      }
      //cout<<aux<<endl;
      s1.clear();
      for(int i=r=min(tam-1,r);i>=l;i--){
	//	cout<<s1<<endl;
	s1+=s[i];
	if(pal(s1)){
	  if(s1.size()>aux.size()){
	    aux=s1;
	  }
	}
      }
      reverse(all(s1));
      cout<<ss<<aux<<ss1<<endl;
    }
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#define all(a) a.begin(),a.end()
#define pb push_back
#define forr(i,p,n) for(int i=p;i<n;i++)
using namespace std;
typedef long long ll;
//priority_queue<ll> pq;
bool pal(string s){
  ll j=s.size()-1;
  ll sw=0;
  for(ll i=0;i<s.size()/2;i++){
    if(s[i]!=s[j]){sw=1;break;}j--;}
  if(sw)return false;
  else return true;
}
bool fun(string s,string pre){
  ll j=0,sw=0;
  for(int i=s.size()-1;i>=0;i--){
    if(j==pre.size())break;
    if(s[i]!=pre[j]){sw=1;break;}
    j++;
  }
  if(sw)return false;
  else return true;
}
//unordered_map<string,ll> mp,mp1;
int main()
{
  //ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie();
    ll a,b,c,d,n,t,m,k;
    scanf("%lld ",&t);
    while(t--){
      //mp1.clear();
      string s;
      //unordered_map<string,ll> mp,mp1;
      cin>>s;
      if(pal(s)){
	cout<<s<<endl;
	continue;
      }
      ll tam=s.size();
      string s1,s2,s3;
      s1.clear();
      ll ans=0;
      ll sw=0;
      ll l=0;
       string ss="",ss1="",aux="";
      forr(i,0,tam/2){
	s1+=s[i];
	//cout<<s1<<endl;
	if(fun(s,s1)){
	  if(s1.size()*2<=tam){
	    ss=s1;
	    ss1=ss;
	    ans=s1.size()*2;
	    //reverse(all(ss1));
	    l=i+1;
	    continue;
	  }
	}
      }
      //cout<<ss<<" "<<ss1<<endl;
      s1.clear();
      ll r=tam-1-l;
      forr(i,l,r+1){
	s1+=s[i];
	if(pal(s1)){
	  aux=s1;
	}
      }
      //cout<<aux<<endl;
      s1.clear();
      for(int i=r=min(tam-1,r);i>=l;i--){
	//	cout<<s1<<endl;
	s1+=s[i];
	if(pal(s1)){
	  if(s1.size()>aux.size()){
	    aux=s1;
	  }
	}
      }
      reverse(all(ss1));
      cout<<ss<<aux<<ss1<<endl;
    }
}


#include <bits/stdc++.h>
#define all(a) a.begin(),a.end()
#define allr(a) a.rbegin(),a.rend()
#define F first
#define S second
#define INF 3000000000
#define inf -10000000
#define pb push_back
#define forr(i,p,n) for(int i=p;i<n;i++)
#define mpk make_pair
#define upb(a,s) upper_bound(a.begin(),a.end(),s)
#define lwb(a,s) lower_bound(a.begin(),a.end(),s)
#define D(x) cout << #x " = " << (x) << endl
#define mem(a,val) memset(a,val,sizeof a)
#define ulti(a) for(int i=0;i<a.size();i++)cout<<a[i]<<" "
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
//priority_queue<ll> pq;
bool pal(string s){
  ll j=s.size()-1;
  ll sw=0;
  for(ll i=0;i<s.size()/2;i++){
    if(s[i]!=s[j]){sw=1;break;}j--;}
  if(sw)return false;
  else return true;
}
unordered_map<string,ll> mp,mp1;
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie();
    ll a,b,c,d,n,t,m,k;
    cin>>t;
    while(t--){
      string s;
      //unordered_map<string,ll> mp,mp1;
      cin>>s;
      if(pal(s)){
	cout<<s<<endl;
	continue;
      }
      ll tam=s.size();
      string s1,s2,s3;
      for(int i=tam-1;i>=0;i--){
	s1+=s[i];
	mp1[s1]=1;
      }
      s1.clear();
      ll ans=0;
      ll sw=0;
      ll l=0;
      string ss="",ss1="",aux="";
      forr(i,0,tam){
	s1+=s[i];
	//cout<<s1<<endl;
	if(mp1[s1]==1){
	  if(s1.size()*2<=tam){
	    ss=s1;
	    ss1=ss;
	    ans=s1.size()*2;
	    reverse(all(ss1));
	    l=i+1;
	    continue;
	  }
	}
      }
      //cout<<ss<<" "<<ss1<<endl;
      s1.clear();
      ll r=tam-1-l;
      forr(i,l,r+1){
	s1+=s[i];
	if(pal(s1)){
	  aux=s1;
	}
      }
      //cout<<aux<<endl;
      s1.clear();
      for(int i=r=min(tam-1,r);i>=l;i--){
	//	cout<<s1<<endl;
	s1+=s[i];
	if(pal(s1)){
	  if(s1.size()>aux.size()){
	    aux=s1;
	  }
	}
      }
      cout<<ss<<aux<<ss1<<endl;
    }
}


#include <bits/stdc++.h>
#define all(a) a.begin(),a.end()
#define allr(a) a.rbegin(),a.rend()
#define F first
#define S second
#define INF 3000000000
#define inf -10000000
#define pb push_back
#define forr(i,p,n) for(int i=p;i<n;i++)
#define mpk make_pair
#define upb(a,s) upper_bound(a.begin(),a.end(),s)
#define lwb(a,s) lower_bound(a.begin(),a.end(),s)
#define D(x) cout << #x " = " << (x) << endl
#define mem(a,val) memset(a,val,sizeof a)
#define ulti(a) for(int i=0;i<a.size();i++)cout<<a[i]<<" "
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
//priority_queue<ll> pq;
bool pal(string s){
  ll j=s.size()-1;
  ll sw=0;
  for(ll i=0;i<s.size()/2;i++){
    if(s[i]!=s[j]){sw=1;break;}j--;}
  if(sw)return false;
  else return true;
}
bool fun(string s,string pre){
  ll j=0,sw=0;
  for(int i=s.size()-1;i>=0;i--){
    if(j==pre.size())break;
    if(s[i]!=pre[j]){sw=1;break;}
    j++;
  }
  if(sw)return false;
  else return true;
}
//unordered_map<string,ll> mp,mp1;
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie();
    ll a,b,c,d,n,t,m,k;
    cin>>t;
    while(t--){
      //mp1.clear();
      string s;
      //unordered_map<string,ll> mp,mp1;
      cin>>s;
      if(pal(s)){
	cout<<s<<endl;
	continue;
      }
      ll tam=s.size();
      string s1,s2,s3;
      s1.clear();
      ll ans=0;
      ll sw=0;
      ll l=0;
       string ss="",ss1="",aux="";
      forr(i,0,tam){
	s1+=s[i];
	//cout<<s1<<endl;
	if(fun(s,s1)){
	  if(s1.size()*2<=tam){
	    ss=s1;
	    ss1=ss;
	    ans=s1.size()*2;
	    //reverse(all(ss1));
	    l=i+1;
	    continue;
	  }
	}
      }
      //cout<<ss<<" "<<ss1<<endl;
      s1.clear();
      ll r=tam-1-l;
      forr(i,l,r+1){
	s1+=s[i];
	if(pal(s1)){
	  aux=s1;
	}
      }
      //cout<<aux<<endl;
      s1.clear();
      for(int i=r=min(tam-1,r);i>=l;i--){
	//	cout<<s1<<endl;
	s1+=s[i];
	if(pal(s1)){
	  if(s1.size()>aux.size()){
	    aux=s1;
	  }
	}
      }
      reverse(all(ss1));
      cout<<ss<<aux<<ss1<<endl;
    }
}


#include <bits/stdc++.h>
#define all(a) a.begin(),a.end()
#define allr(a) a.rbegin(),a.rend()
#define F first
#define S second
#define INF 3000000000
#define inf -10000000
#define pb push_back
#define forr(i,p,n) for(int i=p;i<n;i++)
#define mpk make_pair
#define upb(a,s) upper_bound(a.begin(),a.end(),s)
#define lwb(a,s) lower_bound(a.begin(),a.end(),s)
#define D(x) cout << #x " = " << (x) << endl
#define mem(a,val) memset(a,val,sizeof a)
#define ulti(a) for(int i=0;i<a.size();i++)cout<<a[i]<<" "
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
//priority_queue<ll> pq;
bool pal(string s){
  ll j=s.size()-1;
  ll sw=0;
  for(ll i=0;i<s.size()/2;i++){
    if(s[i]!=s[j]){sw=1;break;}j--;}
  if(sw)return false;
  else return true;
}
bool fun(string s,string pre){
  ll j=0,sw=0;
  for(int i=s.size()-1;i>=0;i--){
    if(j==pre.size())break;
    if(s[i]!=pre[j]){sw=1;break;}
    j++;
  }
  if(sw)return false;
  else return true;
}
//unordered_map<string,ll> mp,mp1;
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie();
    ll a,b,c,d,n,t,m,k;
    cin>>t;
    while(t--){
      //mp1.clear();
      string s;
      //unordered_map<string,ll> mp,mp1;
      cin>>s;
      if(pal(s)){
	cout<<s<<endl;
	continue;
      }
      ll tam=s.size();
      string s1,s2,s3;
      s1.clear();
      ll ans=0;
      ll sw=0;
      ll l=0;
       string ss="",ss1="",aux="";
      forr(i,0,tam){
	s1+=s[i];
	//cout<<s1<<endl;
	if(fun(s,s1)){
	  if(s1.size()*2<=tam){
	    ss=s1;
	    ss1=ss;
	    ans=s1.size()*2;
	    reverse(all(ss1));
	    l=i+1;
	    continue;
	  }
	}
      }
      //cout<<ss<<" "<<ss1<<endl;
      s1.clear();
      ll r=tam-1-l;
      forr(i,l,r+1){
	s1+=s[i];
	if(pal(s1)){
	  aux=s1;
	}
      }
      //cout<<aux<<endl;
      s1.clear();
      for(int i=r=min(tam-1,r);i>=l;i--){
	//	cout<<s1<<endl;
	s1+=s[i];
	if(pal(s1)){
	  if(s1.size()>aux.size()){
	    aux=s1;
	  }
	}
      }
      cout<<ss<<aux<<ss1<<endl;
    }
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#define all(a) a.begin(),a.end()
#define forr(i,p,n) for(int i=p;i<n;i++)
using namespace std;
typedef long long ll;
//priority_queue<ll> pq;
bool pal(string s){
  ll j=s.size()-1;
  ll sw=0;
  for(ll i=0;i<s.size()/2;i++){
    if(s[i]!=s[j]){sw=1;break;}j--;}
  if(sw)return false;
  else return true;
}
bool fun(string s,string pre){
  ll j=0,sw=0;
  for(int i=s.size()-1;i>=0;i--){
    if(j==pre.size())break;
    if(s[i]!=pre[j]){sw=1;break;}
    j++;
  }
  if(sw)return false;
  else return true;
}
//unordered_map<string,ll> mp,mp1;
int main()
{
  //ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie();
    ll a,b,c,d,n,t,m,k;
    scanf("%lld ",&t);
    while(t--){
      //mp1.clear();
      string s;
      //unordered_map<string,ll> mp,mp1;
      cin>>s;
      if(pal(s)){
	cout<<s<<endl;
	continue;
      }
      ll tam=s.size();
      string s1,s2,s3;
      s1.clear();
      ll ans=0;
      ll sw=0;
      ll l=0;
       string ss="",ss1="",aux="";
       ll j=tam-1;
      forr(i,0,tam/2+1){
        if(s[i]!=s[j]){
	  l=i;
	  break;
	}
	j--;
      }
      forr(i,0,l){
	ss+=s[i];
      }
      //cout<<ss<<" "<<ss1<<endl;
      // s1.clear();
      ll r=tam-1-l;
      forr(i,l,r+1){
	s1+=s[i];
	if(pal(s1)){
	  aux=s1;
	}
      }
      //cout<<aux<<endl;
      s1.clear();
      for(int i=r=min(tam-1,r);i>=l;i--){
	//	cout<<s1<<endl;
	s1+=s[i];
	if(pal(s1)){
	  if(s1.size()>aux.size()){
	    aux=s1;
	  }
	}
      }
      ss1=ss;
      reverse(all(ss1));
      cout<<ss<<aux<<ss1<<endl;
    }
}


#include <bits/stdc++.h>
#define all(a) a.begin(),a.end()
#define allr(a) a.rbegin(),a.rend()
#define F first
#define S second
#define INF 3000000000
#define inf -10000000
#define pb push_back
#define forr(i,p,n) for(int i=p;i<n;i++)
#define mpk make_pair
#define upb(a,s) upper_bound(a.begin(),a.end(),s)
#define lwb(a,s) lower_bound(a.begin(),a.end(),s)
#define D(x) cout << #x " = " << (x) << endl
#define mem(a,val) memset(a,val,sizeof a)
#define ulti(a) for(int i=0;i<a.size();i++)cout<<a[i]<<" "
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
//priority_queue<ll> pq;
ll gcd(ll a,ll b){
    if(a%b==0)return b;
    return gcd(b,a%b);
}
bool pal(string s){
  ll j=s.size()-1;
  ll sw=0;
  for(ll i=0;i<s.size()/2;i++){
    if(s[i]!=s[j]){sw=1;break;}j--;}
  if(sw)return false;
  else return true;
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie();
    ll a,b,c,d,n,t,m,k;
    cin>>t;
    while(t--){
      string s;
      map<string,ll> mp,mp1;
      cin>>s;
      if(pal(s)){
	cout<<s<<endl;
	continue;
      }
      ll tam=s.size();
      string s1,s2,s3;
      for(int i=tam-1;i>=0;i--){
	s1+=s[i];
	mp1[s1]=1;
      }
      s1.clear();
      ll ans=0;
      ll sw=0;
      ll l=0;
      string ss="",ss1="",aux="";
      forr(i,0,tam){
	s1+=s[i];
	//cout<<s1<<endl;
	if(mp1[s1]==1){
	  if(s1.size()*2<=tam){
	    ss=s1;
	    ss1=ss;
	    ans=s1.size()*2;
	    reverse(all(ss1));
	    l=i+1;
	    continue;
	  }
	}
      }
      //cout<<ss<<" "<<ss1<<endl;
      s1.clear();
      ll r=tam-1-l;
      forr(i,l,r+1){
	s1+=s[i];
	if(pal(s1)){
	  aux=s1;
	}
      }
      //cout<<aux<<endl;
      s1.clear();
      for(int i=r=min(tam-1,r);i>=l;i--){
	//	cout<<s1<<endl;
	s1+=s[i];
	if(pal(s1)){
	  if(s1.size()>aux.size()){
	    aux=s1;
	  }
	}
      }
      cout<<ss<<aux<<ss1<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
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;
}

void solve()
{
   // boost;
  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="";
      vector<int> re=fastLongestPalindromes(tt);
        int 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(){
    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;
      string kaki=longestPalindromeSubstring(s);
      if(kaki.length()==s.length())
      {cout<<s<<endl;continue;}
      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<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="";
      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())
        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;
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

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<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;
      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<<s1;
      else
        cout<<s2;
        for(int i=0;i<(ans.length()/2);i++)
        swap(ans[i],ans[ans.length()-1-i]);
        cout<<ans<<endl;
  }

}

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


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define boost  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)

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()
{
    boost;
  int t;
  scanf("%d",&t);string s;
  while(t--)
  {
      scanf("%s",&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()));
      vector<int> re=fastLongestPalindromes(tt);
        int 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);
        printf("%s%s",ans,mid);
        reverse(ans.begin(),ans.end());
        printf("%s\n",ans);
          }

}

int main(){
    solve();
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define boost  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)

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()
{
    boost;
  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(){
    solve();
}


#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

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<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;
      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<<s1;
      else
        cout<<s2;
        for(int i=0;i<(ans.length()/2);i++)
        swap(ans[i],ans[ans.length()-1-i]);
        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<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;
      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<<s1;
      else
        cout<<s2;
        for(int i=0;i<(ans.length()/2);i++)
        swap(ans[i],ans[ans.length()-1-i]);
        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(2*(SIZE)+1,0);
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()
{
  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;
#define pb push_back
#define int long long
#define endl "\n"
#define pii pair<int,int>
#define ff first
#define ss second
#define mii map<int,int>
#define si set<int>
#define sti stack<int>
#define qi queue<int>
#define vi vector<int>
#define pq priority_queue<int>
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const int M=1e9+7;
int longest_palindrome_prefix(string s){
    string kmprev = s;
    std::reverse(kmprev.begin(), kmprev.end());
    string kmp = s + "#" + kmprev;
    vector<int> lps(kmp.size(), 0);
    for (int i = 1; i < (int)lps.size(); ++i)
    {
        int prev_idx = lps[i - 1];
        while (prev_idx > 0 && kmp[i] != kmp[prev_idx])
        {
            prev_idx = lps[prev_idx - 1];
        }
        lps[i] = prev_idx + (kmp[i] == kmp[prev_idx] ? 1 : 0);
    }
    return lps[lps.size() - 1];
}

int32_t main(){
	IOS;
	int tt;
	cin>>tt;
	while(tt--){
		string s;
		cin>>s;
		int x=-1;
		string res1;
		for(int i=0;i<(int)s.size()/2;i++){
			if(s[i]!=s[(int)s.size()-i-1]){x=i;break;}
			res1.pb(s[i]);
		}
		if(x==-1){cout<<s<<endl; continue;}
		string t;
		for(int i=x;i<=s.size()-1-x;i++){
			t.pb(s[i]);
		}
		x=longest_palindrome_prefix(t);
		string res2;
		for(int i=0;i<x;i++) res2.pb(t[i]);
		reverse(t.begin(),t.end());
		x=longest_palindrome_prefix(t);
		string res3;
		for(int i=0;i<x;i++) res3.pb(t[i]);
		string temp;
		if(res2.size()>res3.size()) temp=res2;
		else temp=res3;
		string res4=res1;
		reverse(res1.begin(),res1.end());
		string ans=res4+temp;
		ans+=res1;
		cout<<ans<<endl;
	}
}