#include <bits/stdc++.h>

#ifdef ONLINE_JUDGE
#define endl "\n"
#endif
using namespace std;
typedef unsigned long long int lu;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> v;
typedef vector<v> vv;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
const ll MOD = 1e9 + 7;
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
#define yes cout << "YES\n";
#define no cout << "NO\n";
#define yesno(f) \
    if (f)       \
        yes else no;
#define noo \
    { no return; }
#define amax(x, y) \
    if (y > x)     \
        x = y;
#define amin(x, y) \
    if (y < x)     \
        x = y;
#define sz(x) (ll) x.size()
#define sq(x) ((x) * (x))

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

template<typename T = ll>
vector<T> ga(ll n, bool oneIndexed = false) {
    vector<T> a = vector<T>(n + oneIndexed);

    for (ll i = 0; i < n; i++) {
        T p;
        cin >> p;
        a[i + oneIndexed] = p;
    }

    return move(a);
}

ll n;
string s;

ll len = 1;
pll x = {1, 0};

ll maxCommon = 0;

//vector<int> findLongestPalindromicString(string &s) {
//    int N = s.size();
//    if (N == 0)
//        return vector<int>(0);
//    N = 2 * N + 1; //Position count
//    vector<int> L(N); //LPS Length Array
//    L[0] = 0;
//    L[1] = 1;
//    int C = 1; //centerPosition
//    int R = 2; //centerRightPosition
//    int i = 0; //currentRightPosition
//    int iMirror; //currentLeftPosition
//    int maxLPSLength = 0;
//    int maxLPSCenterPosition = 0;
//    int start = -1;
//    int end = -1;
//    int diff = -1;
//
//    //Uncomment it to print LPS Length array
//    //printf("%d %d ", L[0], L[1]);
//    for (i = 2; i < N; i++) {
//        //get currentLeftPosition iMirror for currentRightPosition i
//        iMirror = 2 * C - i;
//        L[i] = 0;
//        diff = R - i;
//        //If currentRightPosition i is within centerRightPosition R
//        if (diff > 0)
//            L[i] = min(L[iMirror], diff);
//
//        //Attempt to expand palindrome centered at currentRightPosition i
//        //Here for odd positions, we compare characters and
//        //if match then increment LPS Length by ONE
//        //If even position, we just increment LPS by ONE without
//        //any character comparison
//        while (((i + L[i]) < N && (i - L[i]) > 0) &&
//               (((i + L[i] + 1) % 2 == 0) ||
//                (s[(i + L[i] + 1) / 2] == s[(i - L[i] - 1) / 2]))) {
//            L[i]++;
//        }
//
//        if (L[i] > maxLPSLength)  // Track maxLPSLength
//        {
//            maxLPSLength = L[i];
//            maxLPSCenterPosition = i;
//        }
//
//        //If palindrome centered at currentRightPosition i
//        //expand beyond centerRightPosition R,
//        //adjust centerPosition C based on expanded palindrome.
//        if (i + L[i] > R) {
//            C = i;
//            R = i + L[i];
//        }
//    }
//
//}

class KMPstring {
public:
    string pattern;
    v lps;

    explicit KMPstring(const string &pattern) {
        this->pattern = pattern;
        ll m = pattern.size();
        lps = v(m + 1, 0);
        ll i = 0, j = -1;
        lps[0] = -1;
        while (i < m) {
            while (j >= 0 && pattern[i] != pattern[j])
                j = lps[j];
            i++, j++;
            lps[i] = j;
        }
    }

    v match(const string &text) {
        ll n = text.size(), m = pattern.size();
        v matches, m_length(n);
        ll i = 0, j = 0;
        while (i < n) {
            while (j >= 0 && text[i] != pattern[j])
                j = lps[j];
            i++, j++;
            m_length[i - 1] = j;
            if (j == m) {
                matches.push_back(i - m);
                j = lps[j];
            }
        }
        return move(matches);  // or m_length
    }
};

void solve() {
    cin >> s;
    n = sz(s);

    maxCommon = 0;
    while (maxCommon < n) {
        if (s[maxCommon] == s[n - maxCommon - 1]) {
            maxCommon++;
        } else break;
    }
    x = {maxCommon, maxCommon};

    string s2;
    for (ll i = maxCommon; i < n; i++) {
        s2 += s[i];
    }
    s2 += "#";
    for (ll i = n - 1; i >= maxCommon; i--) {
        s2 += s[i];
    }

    KMPstring kmp(s2);
    ll prefixExtra = kmp.lps.back();

    s2 = "";
    for (ll i = n - maxCommon - 1; i >= 0; i--) {
        s2 += s[i];
    }
    s2 += "#";

    for (ll i = 0; i <= n - maxCommon - 1; i++) {
        s2 += s[i];
    }
    KMPstring k2(s2);
    ll suffixExtra = k2.lps.back();

    KMPstring k3(s);
    ll d1 = k3.lps.back();
    string s4 = s;
    reverse(all(s4));
    KMPstring k4(s4);
    ll d2 = k4.lps.back();

    if (d1 > 2 * maxCommon) {
        x = {d1, 0};
    }
    if (d2 > d1 && d2 > 2 * maxCommon) {
        x = {0, d2};
    }
    len = x.first + x.second;

    ll p1 = 2 * maxCommon + prefixExtra;
    ll p2 = 2 * maxCommon + suffixExtra;
    if (len >= n) {
        x = {n / 2, n / 2};
        if (n & 1) {
            x.first++;
        }
        goto end;
    }

    if (p1 <= n && p1 > p2 && p1 > len) x.first += prefixExtra;
    else if (p2 <= n && p2 > len) x.second += suffixExtra;

    end:;
    for (ll i = 0; i < x.first; i++) cout << s[i];
    for (ll i = n - x.second; i < n; i++) cout << s[i];
    cout << endl;
}

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}


#include <bits/stdc++.h>

#ifdef ONLINE_JUDGE
#define endl "\n"
#endif
using namespace std;
typedef unsigned long long int lu;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> v;
typedef vector<v> vv;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
const ll MOD = 1e9 + 7;
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
#define yes cout << "YES\n";
#define no cout << "NO\n";
#define yesno(f) \
    if (f)       \
        yes else no;
#define noo \
    { no return; }
#define amax(x, y) \
    if (y > x)     \
        x = y;
#define amin(x, y) \
    if (y < x)     \
        x = y;
#define sz(x) (ll) x.size()
#define sq(x) ((x) * (x))

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

template<typename T = ll>
vector<T> ga(ll n, bool oneIndexed = false) {
    vector<T> a = vector<T>(n + oneIndexed);

    for (ll i = 0; i < n; i++) {
        T p;
        cin >> p;
        a[i + oneIndexed] = p;
    }

    return move(a);
}

ll n;
string s;

ll len = 1;
pll x = {1, 0};

ll maxCommon = 0;

//vector<int> findLongestPalindromicString(string &s) {
//    int N = s.size();
//    if (N == 0)
//        return vector<int>(0);
//    N = 2 * N + 1; //Position count
//    vector<int> L(N); //LPS Length Array
//    L[0] = 0;
//    L[1] = 1;
//    int C = 1; //centerPosition
//    int R = 2; //centerRightPosition
//    int i = 0; //currentRightPosition
//    int iMirror; //currentLeftPosition
//    int maxLPSLength = 0;
//    int maxLPSCenterPosition = 0;
//    int start = -1;
//    int end = -1;
//    int diff = -1;
//
//    //Uncomment it to print LPS Length array
//    //printf("%d %d ", L[0], L[1]);
//    for (i = 2; i < N; i++) {
//        //get currentLeftPosition iMirror for currentRightPosition i
//        iMirror = 2 * C - i;
//        L[i] = 0;
//        diff = R - i;
//        //If currentRightPosition i is within centerRightPosition R
//        if (diff > 0)
//            L[i] = min(L[iMirror], diff);
//
//        //Attempt to expand palindrome centered at currentRightPosition i
//        //Here for odd positions, we compare characters and
//        //if match then increment LPS Length by ONE
//        //If even position, we just increment LPS by ONE without
//        //any character comparison
//        while (((i + L[i]) < N && (i - L[i]) > 0) &&
//               (((i + L[i] + 1) % 2 == 0) ||
//                (s[(i + L[i] + 1) / 2] == s[(i - L[i] - 1) / 2]))) {
//            L[i]++;
//        }
//
//        if (L[i] > maxLPSLength)  // Track maxLPSLength
//        {
//            maxLPSLength = L[i];
//            maxLPSCenterPosition = i;
//        }
//
//        //If palindrome centered at currentRightPosition i
//        //expand beyond centerRightPosition R,
//        //adjust centerPosition C based on expanded palindrome.
//        if (i + L[i] > R) {
//            C = i;
//            R = i + L[i];
//        }
//    }
//
//}

class KMPstring {
public:
    string pattern;
    v lps;

    explicit KMPstring(const string &pattern) {
        this->pattern = pattern;
        ll m = pattern.size();
        lps = v(m + 1, 0);
        ll i = 0, j = -1;
        lps[0] = -1;
        while (i < m) {
            while (j >= 0 && pattern[i] != pattern[j])
                j = lps[j];
            i++, j++;
            lps[i] = j;
        }
    }

    v match(const string &text) {
        ll n = text.size(), m = pattern.size();
        v matches, m_length(n);
        ll i = 0, j = 0;
        while (i < n) {
            while (j >= 0 && text[i] != pattern[j])
                j = lps[j];
            i++, j++;
            m_length[i - 1] = j;
            if (j == m) {
                matches.push_back(i - m);
                j = lps[j];
            }
        }
        return move(matches);  // or m_length
    }
};

void solve() {
    cin >> s;
    n = sz(s);

    maxCommon = 0;
    while (maxCommon < n) {
        if (s[maxCommon] == s[n - maxCommon - 1]) {
            maxCommon++;
        } else break;
    }

    string s2;
    for (ll i = maxCommon; i < n; i++) {
        s2 += s[i];
    }
    s2 += "#";
    for (ll i = n - 1; i >= maxCommon; i--) {
        s2 += s[i];
    }

    KMPstring kmp(s2);
    ll prefixExtra = kmp.lps.back();

    s2 = "";
    for (ll i = n - maxCommon - 1; i >= 0; i--) {
        s2 += s[i];
    }
    s2 += "#";

    for (ll i = 0; i <= n - maxCommon - 1; i++) {
        s2 += s[i];
    }
    KMPstring k2(s2);
    ll suffixExtra = k2.lps.back();

    KMPstring k3(s);
    ll d1 = k3.lps.back();
    string s4 = s;
    reverse(all(s4));
    KMPstring k4(s4);
    ll d2 = k4.lps.back();

    pll x1 = {maxCommon + prefixExtra, maxCommon};
    pll x2 = {maxCommon, suffixExtra + maxCommon};
    pll x3 = {d1, 0};
    pll x4 = {0, d2};

    vpll vec = {x1, x2, x3, x4};
    len = 0;
    for (auto z : vec) {
        ll sum = z.first + z.second;
        if (sum > len && sum <= n) {
            len = sum;
            x = z;
        }
    }

    if (len >= n) {
        x = {n / 2, n / 2};
        if (n & 1) {
            x.first++;
        }
        goto end;
    }

    end:;
    for (ll i = 0; i < x.first; i++) cout << s[i];
    for (ll i = n - x.second; i < n; i++) cout << s[i];
    cout << endl;
}

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}


#include <bits/stdc++.h>

#ifdef ONLINE_JUDGE
#define endl "\n"
#endif
using namespace std;
typedef unsigned long long int lu;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> v;
typedef vector<v> vv;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
const ll MOD = 1e9 + 7;
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
#define yes cout << "YES\n";
#define no cout << "NO\n";
#define yesno(f) \
    if (f)       \
        yes else no;
#define noo \
    { no return; }
#define amax(x, y) \
    if (y > x)     \
        x = y;
#define amin(x, y) \
    if (y < x)     \
        x = y;
#define sz(x) (ll) x.size()
#define sq(x) ((x) * (x))

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

template<typename T = ll>
vector<T> ga(ll n, bool oneIndexed = false) {
    vector<T> a = vector<T>(n + oneIndexed);

    for (ll i = 0; i < n; i++) {
        T p;
        cin >> p;
        a[i + oneIndexed] = p;
    }

    return move(a);
}

ll n;
string s;

ll len = 1;
pll x = {1, 0};

ll maxCommon = 0;

//vector<int> findLongestPalindromicString(string &s) {
//    int N = s.size();
//    if (N == 0)
//        return vector<int>(0);
//    N = 2 * N + 1; //Position count
//    vector<int> L(N); //LPS Length Array
//    L[0] = 0;
//    L[1] = 1;
//    int C = 1; //centerPosition
//    int R = 2; //centerRightPosition
//    int i = 0; //currentRightPosition
//    int iMirror; //currentLeftPosition
//    int maxLPSLength = 0;
//    int maxLPSCenterPosition = 0;
//    int start = -1;
//    int end = -1;
//    int diff = -1;
//
//    //Uncomment it to print LPS Length array
//    //printf("%d %d ", L[0], L[1]);
//    for (i = 2; i < N; i++) {
//        //get currentLeftPosition iMirror for currentRightPosition i
//        iMirror = 2 * C - i;
//        L[i] = 0;
//        diff = R - i;
//        //If currentRightPosition i is within centerRightPosition R
//        if (diff > 0)
//            L[i] = min(L[iMirror], diff);
//
//        //Attempt to expand palindrome centered at currentRightPosition i
//        //Here for odd positions, we compare characters and
//        //if match then increment LPS Length by ONE
//        //If even position, we just increment LPS by ONE without
//        //any character comparison
//        while (((i + L[i]) < N && (i - L[i]) > 0) &&
//               (((i + L[i] + 1) % 2 == 0) ||
//                (s[(i + L[i] + 1) / 2] == s[(i - L[i] - 1) / 2]))) {
//            L[i]++;
//        }
//
//        if (L[i] > maxLPSLength)  // Track maxLPSLength
//        {
//            maxLPSLength = L[i];
//            maxLPSCenterPosition = i;
//        }
//
//        //If palindrome centered at currentRightPosition i
//        //expand beyond centerRightPosition R,
//        //adjust centerPosition C based on expanded palindrome.
//        if (i + L[i] > R) {
//            C = i;
//            R = i + L[i];
//        }
//    }
//
//}

class KMPstring {
public:
    string pattern;
    v lps;

    explicit KMPstring(const string &pattern) {
        this->pattern = pattern;
        ll m = pattern.size();
        lps = v(m + 1, 0);
        ll i = 0, j = -1;
        lps[0] = -1;
        while (i < m) {
            while (j >= 0 && pattern[i] != pattern[j])
                j = lps[j];
            i++, j++;
            lps[i] = j;
        }
    }

    v match(const string &text) {
        ll n = text.size(), m = pattern.size();
        v matches, m_length(n);
        ll i = 0, j = 0;
        while (i < n) {
            while (j >= 0 && text[i] != pattern[j])
                j = lps[j];
            i++, j++;
            m_length[i - 1] = j;
            if (j == m) {
                matches.push_back(i - m);
                j = lps[j];
            }
        }
        return move(matches);  // or m_length
    }
};

void solve() {
    cin >> s;
    n = sz(s);

    maxCommon = 0;
    while (maxCommon < n) {
        if (s[maxCommon] == s[n - maxCommon - 1]) {
            maxCommon++;
        } else break;
    }

    len = maxCommon * 2;

    string s2;
    for (ll i = maxCommon; i < n; i++) {
        s2 += s[i];
    }

    for (ll i = n - 1; i >= maxCommon; i--) {
        s2 += s[i];
    }

    KMPstring kmp(s2);
    ll prefixExtra = kmp.lps.back();

    s2 = "";
    for (ll i = n - maxCommon - 1; i >= 0; i--) {
        s2 += s[i];
    }

    for (ll i = 0; i <= n - maxCommon - 1; i++) {
        s2 += s[i];
    }
    KMPstring k2(s2);
    ll suffixExtra = k2.lps.back();

    if (len >= n) {
        x = {n / 2, n / 2};
        if (n & 1) {
            x.first++;
        }
        goto end;
    }

    x = {maxCommon, maxCommon};
    if (2 * maxCommon + prefixExtra <= n && prefixExtra > suffixExtra) x.first += prefixExtra;
    else if (2 * maxCommon + suffixExtra <= n) x.second += suffixExtra;

    end:;
    for (ll i = 0; i < x.first; i++) cout << s[i];
    for (ll i = n - x.second; i < n; i++) cout << s[i];
    cout << endl;
}

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}


#include <bits/stdc++.h>

#ifdef ONLINE_JUDGE
#define endl "\n"
#endif
using namespace std;
typedef unsigned long long int lu;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> v;
typedef vector<v> vv;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
const ll MOD = 1e9 + 7;
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
#define yes cout << "YES\n";
#define no cout << "NO\n";
#define yesno(f) \
    if (f)       \
        yes else no;
#define noo \
    { no return; }
#define amax(x, y) \
    if (y > x)     \
        x = y;
#define amin(x, y) \
    if (y < x)     \
        x = y;
#define sz(x) (ll) x.size()
#define sq(x) ((x) * (x))

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

template<typename T = ll>
vector<T> ga(ll n, bool oneIndexed = false) {
    vector<T> a = vector<T>(n + oneIndexed);

    for (ll i = 0; i < n; i++) {
        T p;
        cin >> p;
        a[i + oneIndexed] = p;
    }

    return move(a);
}

ll n;
string s;

ll len = 1;
pll x = {1, 0};

ll maxCommon = 0;

//vector<int> findLongestPalindromicString(string &s) {
//    int N = s.size();
//    if (N == 0)
//        return vector<int>(0);
//    N = 2 * N + 1; //Position count
//    vector<int> L(N); //LPS Length Array
//    L[0] = 0;
//    L[1] = 1;
//    int C = 1; //centerPosition
//    int R = 2; //centerRightPosition
//    int i = 0; //currentRightPosition
//    int iMirror; //currentLeftPosition
//    int maxLPSLength = 0;
//    int maxLPSCenterPosition = 0;
//    int start = -1;
//    int end = -1;
//    int diff = -1;
//
//    //Uncomment it to print LPS Length array
//    //printf("%d %d ", L[0], L[1]);
//    for (i = 2; i < N; i++) {
//        //get currentLeftPosition iMirror for currentRightPosition i
//        iMirror = 2 * C - i;
//        L[i] = 0;
//        diff = R - i;
//        //If currentRightPosition i is within centerRightPosition R
//        if (diff > 0)
//            L[i] = min(L[iMirror], diff);
//
//        //Attempt to expand palindrome centered at currentRightPosition i
//        //Here for odd positions, we compare characters and
//        //if match then increment LPS Length by ONE
//        //If even position, we just increment LPS by ONE without
//        //any character comparison
//        while (((i + L[i]) < N && (i - L[i]) > 0) &&
//               (((i + L[i] + 1) % 2 == 0) ||
//                (s[(i + L[i] + 1) / 2] == s[(i - L[i] - 1) / 2]))) {
//            L[i]++;
//        }
//
//        if (L[i] > maxLPSLength)  // Track maxLPSLength
//        {
//            maxLPSLength = L[i];
//            maxLPSCenterPosition = i;
//        }
//
//        //If palindrome centered at currentRightPosition i
//        //expand beyond centerRightPosition R,
//        //adjust centerPosition C based on expanded palindrome.
//        if (i + L[i] > R) {
//            C = i;
//            R = i + L[i];
//        }
//    }
//
//}

class KMPstring {
public:
    string pattern;
    v lps;

    explicit KMPstring(const string &pattern) {
        this->pattern = pattern;
        ll m = pattern.size();
        lps = v(m + 1, 0);
        ll i = 0, j = -1;
        lps[0] = -1;
        while (i < m) {
            while (j >= 0 && pattern[i] != pattern[j])
                j = lps[j];
            i++, j++;
            lps[i] = j;
        }
    }

    v match(const string &text) {
        ll n = text.size(), m = pattern.size();
        v matches, m_length(n);
        ll i = 0, j = 0;
        while (i < n) {
            while (j >= 0 && text[i] != pattern[j])
                j = lps[j];
            i++, j++;
            m_length[i - 1] = j;
            if (j == m) {
                matches.push_back(i - m);
                j = lps[j];
            }
        }
        return move(matches);  // or m_length
    }
};

void solve() {
    cin >> s;
    n = sz(s);

    maxCommon = 0;
    while (maxCommon < n) {
        if (s[maxCommon] == s[n - maxCommon - 1]) {
            maxCommon++;
        } else break;
    }

    string s2;
    for (ll i = maxCommon; i < n; i++) {
        s2 += s[i];
    }
    s2 += "#";
    for (ll i = n - 1; i >= maxCommon; i--) {
        s2 += s[i];
    }

    KMPstring kmp(s2);
    ll prefixExtra = kmp.lps.back();

    s2 = "";
    for (ll i = n - maxCommon - 1; i >= 0; i--) {
        s2 += s[i];
    }
    s2 += "#";

    for (ll i = 0; i <= n - maxCommon - 1; i++) {
        s2 += s[i];
    }
    KMPstring k2(s2);
    ll suffixExtra = k2.lps.back();

    KMPstring k3(s);
    ll d1 = k3.lps.back();
    string s4 = s;
    reverse(all(s4));
    KMPstring k4(s4);
    ll d2 = k4.lps.back();

    pll x1 = {maxCommon + prefixExtra, maxCommon};
    pll x2 = {maxCommon, suffixExtra + maxCommon};
    pll x3 = {d1, 0};
    pll x4 = {0, d2};

    vpll vec = {x1, x2, x3, x4};
    len = 0;
    for (auto z : vec) {
        if (z.first + z.second > len) {
            len = z.first + z.second;
            x = z;
        }
    }

    if (len >= n) {
        x = {n / 2, n / 2};
        if (n & 1) {
            x.first++;
        }
        goto end;
    }

    end:;
    for (ll i = 0; i < x.first; i++) cout << s[i];
    for (ll i = n - x.second; i < n; i++) cout << s[i];
    cout << endl;
}

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}


// forked from ninja_28
// for testing kmp

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define ll long long int
#define lli long long int
#define v vector<long long>
#define llu unsigned long long int
#define fr(va, beg, end) for (lli va = beg; va < end; va++)
#define pb push_back
#define rt return 0
#define endln "\n"
#define all(x) x.begin(), x.end()
#define sz(x) (lli)(x.size())
const lli MOD = 1e9 + 7;
// cout << fixed << setprecision(0) << pi <<endl;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
//              tree_order_statistics_node_update>
//     new_data_set;
// for multiset
// typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
//              tree_order_statistics_node_update>
//     new_data_set;
// order_of_key(val): returns the number of values less than val
// find_by_order(k): returns an iterator to the kth largest element (0-based)

class KMPstring {

   public:
    string pattern;
    v lps;
    explicit KMPstring(const string &pattern) {
        this->pattern = pattern;
        ll m = pattern.size();
        lps = v(m + 1, 0);
        ll i = 0, j = -1;
        lps[0] = -1;
        while (i < m) {
            while (j >= 0 && pattern[i] != pattern[j])
                j = lps[j];
            i++, j++;
            lps[i] = j;
        }
    }
    v match(const string &text) {
        ll n = text.size(), m = pattern.size();
        v matches, m_length(n);
        ll i = 0, j = 0;
        while (i < n) {
            while (j >= 0 && text[i] != pattern[j])
                j = lps[j];
            i++, j++;
            m_length[i - 1] = j;
            if (j == m) {
                matches.push_back(i - m);
                j = lps[j];
            }
        }
        return move(matches);  // or m_length
    }
};

ll longest_palindrome_prefix(const string &x){
    string y = x;
    reverse(all(y))
    KMPString k(x + "#" + y);
    return k.lps.back();
}

void solve() {
    ll n, a;
    string st;
    cin>>st;
    lli beg = 0,end = st.length()-1;
    string part1="";

    while(beg <= end){
        if(st[beg] == st[end]){
            part1.pb(st[beg]);
            beg++;
            end--;
        }
        else{
            break;
        }
    }

    if(st[beg] == st[end]){
        cout<<st<<endl;
        return;
    }

    string st1 = st.substr(beg,end-beg+1);
    string st2 = st1;
    reverse(all(st2));
    string part3;

    lli aa = longest_palindrome_prefix(st1);
    lli bb = longest_palindrome_prefix(st2);

    if(aa > bb){
        part3 = st.substr(beg,aa);
    }
    else{
        part3 = st.substr(end-bb+1,bb);
    }

    string part2 = part1;
    reverse(part2.begin(),part2.end());
    string an = part1 + part3 + part2;
    cout<<an<<endl;
}

int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    lli t; cin >> t; while(t--)
    solve();
    rt;
}


// forked from ninja_28
// for testing kmp

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define ll long long int
#define lli long long int
#define v vector<long long>
#define llu unsigned long long int
#define fr(va, beg, end) for (lli va = beg; va < end; va++)
#define pb push_back
#define rt return 0
#define endln "\n"
#define all(x) x.begin(), x.end()
#define sz(x) (lli)(x.size())
const lli MOD = 1e9 + 7;
// cout << fixed << setprecision(0) << pi <<endl;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
//              tree_order_statistics_node_update>
//     new_data_set;
// for multiset
// typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
//              tree_order_statistics_node_update>
//     new_data_set;
// order_of_key(val): returns the number of values less than val
// find_by_order(k): returns an iterator to the kth largest element (0-based)

class KMPstring {

   public:
    string pattern;
    v lps;
    explicit KMPstring(const string &pattern) {
        this->pattern = pattern;
        ll m = pattern.size();
        lps = v(m + 1, 0);
        ll i = 0, j = -1;
        lps[0] = -1;
        while (i < m) {
            while (j >= 0 && pattern[i] != pattern[j])
                j = lps[j];
            i++, j++;
            lps[i] = j;
        }
    }
    v match(const string &text) {
        ll n = text.size(), m = pattern.size();
        v matches, m_length(n);
        ll i = 0, j = 0;
        while (i < n) {
            while (j >= 0 && text[i] != pattern[j])
                j = lps[j];
            i++, j++;
            m_length[i - 1] = j;
            if (j == m) {
                matches.push_back(i - m);
                j = lps[j];
            }
        }
        return move(matches);  // or m_length
    }
};

ll longest_palindrome_prefix(const string &x){
    string y = x;
    reverse(all(y));
    KMPString k(x + "#" + y);
    return k.lps.back();
}

void solve() {
    ll n, a;
    string st;
    cin>>st;
    lli beg = 0,end = st.length()-1;
    string part1="";

    while(beg <= end){
        if(st[beg] == st[end]){
            part1.pb(st[beg]);
            beg++;
            end--;
        }
        else{
            break;
        }
    }

    if(st[beg] == st[end]){
        cout<<st<<endl;
        return;
    }

    string st1 = st.substr(beg,end-beg+1);
    string st2 = st1;
    reverse(all(st2));
    string part3;

    lli aa = longest_palindrome_prefix(st1);
    lli bb = longest_palindrome_prefix(st2);

    if(aa > bb){
        part3 = st.substr(beg,aa);
    }
    else{
        part3 = st.substr(end-bb+1,bb);
    }

    string part2 = part1;
    reverse(part2.begin(),part2.end());
    string an = part1 + part3 + part2;
    cout<<an<<endl;
}

int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    lli t; cin >> t; while(t--)
    solve();
    rt;
}


#include<bits/stdc++.h>
using namespace std;
const int mod=45007;
string s;int n,T;bool xx[1000005];
int pw[1000005],fr[1000005],bk[1000005];
bool check(int a,int b,int x,int y){
	int fi=((fr[b]-fr[a]*pw[b-a])%mod+mod)%mod;
	int sc=((bk[x]-bk[y]*pw[y-x])%mod+mod)%mod;
	return fi==sc;
}
int main(){
	cin>>T;pw[0]=1;
	for(int i=1;i<1000003;i++)pw[i]=pw[i-1]*26%mod;
	while(T--){
		cin>>s;
		n=s.size();
		fr[0]=0;bk[n]=0;
		for(int i=0;i<n;i++)fr[i+1]=(fr[i]*26+s[i]-'a')%mod;
		for(int i=n;i>0;i--)bk[i-1]=(bk[i]*26+s[i-1]-'a')%mod;
		for(int i=0;i<=n;i++)xx[i]=0;
		for(int i=0;i<=n;i++){
			xx[i]=1;
			if(s[i]!=s[n-1-i])break;
		}
		int res=0;
		pair<int,int>ans=make_pair(-1,-1);
		for(int i=0;i<n;i ++){
			int l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(check(i,i+mid,i-mid+1,i+1))
					l=mid;
				else
					r=mid-1;
				if(check(i,i+l+1,i-l,i+1))l++;
				if(!check(i,i+r,i-r+1,i+1))r--;
			}
			int realans=l*2-1,hwcf=i-l+1,hwcb=i+l-1;
			if(check(i,i+r,i-r+1,i+1)){
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
			l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(i-mid>=0&&check(i,i+mid,i-mid,i))
					l=mid;
				else
					r=mid-1;
				if(r-l<=5)if(i-l-1>=0&&check(i,i+l+1,i-l-1,i))l++;
				if(r-l<=5)if(i-r<0||!check(i,i+r,i-r,i))r--;
			}realans=l*2,hwcf=i-l,hwcb=i+l-1;
			if(i-r>=0&&check(i,i+r,i-r,i))
			{
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
		}
		for(int i=0;i<(res-ans.second+ans.first-1)/2;i++)cout<<s[i];
		for(int i=ans.first;i<=ans.second;i++)cout<<s[i];
		for(int i=n-(res-ans.second+ans.first-1)/2;i<n;i++)cout<<s[i];
		cout<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
const int mod=10007;
string s;int n,T;bool xx[1000005];
int pw[1000005],fr[1000005],bk[1000005];
bool check(int a,int b,int x,int y){
	int fi=((fr[b]-fr[a]*pw[b-a])%mod+mod)%mod;
	int sc=((bk[x]-bk[y]*pw[y-x])%mod+mod)%mod;
	return fi==sc;
}
int main(){
	cin>>T;pw[0]=1;
	for(int i=1;i<1000003;i++)pw[i]=pw[i-1]*26%mod;
	while(T--){
		cin>>s;
		n=s.size();
		fr[0]=0;bk[n]=0;
		for(int i=0;i<n;i++)fr[i+1]=(fr[i]*26+s[i]-'a')%mod;
		for(int i=n;i>0;i--)bk[i-1]=(bk[i]*26+s[i-1]-'a')%mod;
		for(int i=0;i<=n;i++)xx[i]=0;
		for(int i=0;i<=n;i++){
			xx[i]=1;
			if(s[i]!=s[n-1-i])break;
		}
		int res=0;
		pair<int,int>ans=make_pair(-1,-1);
		for(int i=0;i<n;i ++){
			int l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(check(i,i+mid,i-mid+1,i+1))
					l=mid;
				else
					r=mid-1;
				if(check(i,i+l+1,i-l,i+1))l++;
				if(!check(i,i+r,i-r+1,i+1))r--;
			}
			int realans=l*2-1,hwcf=i-l+1,hwcb=i+l-1;
			if(check(i,i+r,i-r+1,i+1)){
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
			l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(i-mid>=0&&check(i,i+mid,i-mid,i))
					l=mid;
				else
					r=mid-1;
				if(r-l<=5)if(i-l-1>=0&&check(i,i+l+1,i-l-1,i))l++;
				if(r-l<=5)if(i-r<0||!check(i,i+r,i-r,i))r--;
			}realans=l*2,hwcf=i-l,hwcb=i+l-1;
			if(i-r>=0&&check(i,i+r,i-r,i))
			{
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
		}
		for(int i=0;i<(res-ans.second+ans.first-1)/2;i++)cout<<s[i];
		for(int i=ans.first;i<=ans.second;i++)cout<<s[i];
		for(int i=n-(res-ans.second+ans.first-1)/2;i<n;i++)cout<<s[i];
		cout<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
const int mod=19260817;
string s;int n,T;bool xx[1000003];
int pw[1000003],fr[1000003],bk[1000003];
bool check(int a,int b,int x,int y){
	long long fi=fr[a];fi*=pw[b-a];fi-=fr[b];fi%=mod;fi+=mod;fi%=mod;
	long long sc=bk[y];sc*=pw[y-x];sc-=bk[x];sc%=mod;sc+=mod;sc%=mod;
	return fi==sc;
}
int main(){
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
	cin>>T;pw[0]=1;
	for(int i=1;i<1000003;i++)pw[i]=pw[i-1]*26%mod;
	while(T--){
		cin>>s;
		n=s.size();
		fr[0]=0;bk[n]=0;
		for(int i=0;i<n;i++)fr[i+1]=(fr[i]*26+s[i]-'a')%mod;
		for(int i=n;i>0;i--)bk[i-1]=(bk[i]*26+s[i-1]-'a')%mod;
		for(int i=0;i<=n;i++)xx[i]=0;
		for(int i=0;i<=n;i++){
			xx[i]=1;
			if(s[i]!=s[n-1-i])break;
		}
		int res=0;
		pair<int,int>ans=make_pair(-1,-1);
		for(int i=0;i<n;i++){
			int l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(check(i,i+mid,i-mid+1,i+1))
					l=mid;
				else
					r=mid-1;
				if(r-l<=5)if(check(i,i+l+1,i-l,i+1))l++;
				if(r-l<=5)if(!check(i,i+r,i-r+1,i+1))r--;
			}
			int realans=l*2-1,hwcf=i-l+1,hwcb=i+l-1;
			if(check(i,i+r,i-r+1,i+1)){
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
			l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(i-mid>=0&&check(i,i+mid,i-mid,i))
					l=mid;
				else
					r=mid-1;
				if(r-l<=5)if(i-l-1>=0&&check(i,i+l+1,i-l-1,i))l++;
				if(r-l<=5)if(i-r<0||!check(i,i+r,i-r,i))r--;
			}realans=l*2,hwcf=i-l,hwcb=i+l-1;
			if(i-r>=0&&check(i,i+r,i-r,i))
			{
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
		}
		for(int i=0;i<(res-ans.second+ans.first-1)/2;i++)cout<<s[i];
		for(int i=ans.first;i<=ans.second;i++)cout<<s[i];
		for(int i=n-(res-ans.second+ans.first-1)/2;i<n;i++)cout<<s[i];
		cout<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
string s;int n,T;bool xx[1000003];
long long pw[1000003],fr[1000003],bk[1000003];
bool check(long long a,long long b,long long x,long long y){
	long long fi=((fr[b]-fr[a]*pw[b-a])%mod+mod)%mod;
	long long sc=((bk[x]-bk[y]*pw[y-x])%mod+mod)%mod;
	return fi==sc;
}
int main(){
	cin>>T;pw[0]=1;
	for(int i=1;i<1000003;i++)pw[i]=pw[i-1]*26%mod;
	while(T--){
		cin>>s;
		n=s.size();
		fr[0]=0;bk[n]=0;
		for(int i=0;i<n;i++)fr[i+1]=(fr[i]*26+s[i]-'a')%mod;
		for(int i=n;i>0;i--)bk[i-1]=(bk[i]*26+s[i-1]-'a')%mod;
		for(int i=0;i<=n;i++)xx[i]=0;
		for(int i=0;i<=n;i++){
			xx[i]=1;
			if(s[i]!=s[n-1-i])break;
		}
		int res=0;
		pair<int,int>ans=make_pair(-1,-1);
		for(int i=0;i<n;i++){
			int l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(check(i,i+mid,i-mid+1,i+1))
					l=mid;
				else
					r=mid-1;
				if(check(i,i+l+1,i-l,i+1))l++;
				if(!check(i,i+r,i-r+1,i+1))r--;
			}
			int realans=l*2-1,hwcf=i-l+1,hwcb=i+l-1;
			if(check(i,i+r,i-r+1,i+1)){
				int x=min(hwcf,n-1-hwcb);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
			l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(check(i,i+mid,i-mid+1,i+1)||(i-mid>=0&&check(i,i+mid,i-mid,i)))
					l=mid;
				else
					r=mid-1;
				if(i-l-1>=0&&check(i,i+l+1,i-l-1,i))l++;
				if(i-r<0||!check(i,i+r,i-r,i))r--;
			}realans=l*2,hwcf=i-l,hwcb=i+l-1;
			if(i-r>=0&&check(i,i+r,i-r,i))
			{
				int x=min(hwcf,n-1-hwcb);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
		}
		for(int i=0;i<(res-ans.second+ans.first-1)/2;i++)cout<<s[i];
		for(int i=ans.first;i<=ans.second;i++)cout<<s[i];
		for(int i=n-(res-ans.second+ans.first-1)/2;i<n;i++)cout<<s[i];
		cout<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
const int mod=19260817;
string s;int n,T;bool xx[1000003];
int pw[1000003],fr[1000003],bk[1000003];
bool check(int a,int b,int x,int y){
	long long fi=fr[a];fi*=pw[b-a];fi-=fr[b];fi%=mod;fi+=mod;fi%=mod;
	long long sc=bk[y];sc*=pw[y-x];sc-=bk[x];sc%=mod;sc+=mod;sc%=mod;
	return fi==sc;
}
int main(){
	cin>>T;pw[0]=1;
	for(int i=1;i<1000003;i++)pw[i]=pw[i-1]*26%mod;
	while(T--){
		cin>>s;
		n=s.size();
		fr[0]=0;bk[n]=0;
		for(int i=0;i<n;i++)fr[i+1]=(fr[i]*26+s[i]-'a')%mod;
		for(int i=n;i>0;i--)bk[i-1]=(bk[i]*26+s[i-1]-'a')%mod;
		for(int i=0;i<=n;i++)xx[i]=0;
		for(int i=0;i<=n;i++){
			xx[i]=1;
			if(s[i]!=s[n-1-i])break;
		}
		int res=0;
		pair<int,int>ans=make_pair(-1,-1);
		for(int i=0;i<n;i++){
			int l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(check(i,i+mid,i-mid+1,i+1))
					l=mid;
				else
					r=mid-1;
				if(r-l<=5)if(check(i,i+l+1,i-l,i+1))l++;
				if(r-l<=5)if(!check(i,i+r,i-r+1,i+1))r--;
			}
			int realans=l*2-1,hwcf=i-l+1,hwcb=i+l-1;
			if(check(i,i+r,i-r+1,i+1)){
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
			l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(i-mid>=0&&check(i,i+mid,i-mid,i))
					l=mid;
				else
					r=mid-1;
				if(r-l<=5)if(i-l-1>=0&&check(i,i+l+1,i-l-1,i))l++;
				if(r-l<=5)if(i-r<0||!check(i,i+r,i-r,i))r--;
			}realans=l*2,hwcf=i-l,hwcb=i+l-1;
			if(i-r>=0&&check(i,i+r,i-r,i))
			{
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
		}
		for(int i=0;i<(res-ans.second+ans.first-1)/2;i++)cout<<s[i];
		for(int i=ans.first;i<=ans.second;i++)cout<<s[i];
		for(int i=n-(res-ans.second+ans.first-1)/2;i<n;i++)cout<<s[i];
		cout<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
const int mod=10007;
char s[1000003];int n,T;bool xx[1000003];
int pw[1000003],fr[1000003],bk[1000003];
inline bool check(int a,int b,int x,int y){
	int fi=((fr[b]-fr[a]*pw[b-a])%mod+mod)%mod;
	int sc=((bk[x]-bk[y]*pw[y-x])%mod+mod)%mod;
	return fi==sc;
}
int main(){
//	freopen("a.in","r",stdin);
//	freopen("a.out","w",stdout);
	cin>>T;pw[0]=1;
	for(int i=1;i<1000003;i++)pw[i]=pw[i-1]*26%mod;
	while(T--){
		scanf("%s",&s);
		n=strlen(s);
		fr[0]=0;bk[n]=0;
		for(int i=0;i<n;i++)fr[i+1]=(fr[i]*26+s[i]-'a')%mod;
		for(int i=n;i>0;i--)bk[i-1]=(bk[i]*26+s[i-1]-'a')%mod;
		for(int i=0;i<=n;i++)xx[i]=0;
		for(int i=0;i<=n;i++){
			xx[i]=1;
			if(s[i]!=s[n-1-i])break;
		}
		int res=0;
		pair<int,int>ans=make_pair(-1,-1);
		for(int i=0;i<n;i++){
			int l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(check(i,i+mid,i-mid+1,i+1))
					l=mid;
				else
					r=mid-1;
				if(r-l<=10)if(check(i,i+l+1,i-l,i+1))l++;
				if(r-l<=10)if(!check(i,i+r,i-r+1,i+1))r--;
			}
			int realans=l*2-1,hwcf=i-l+1,hwcb=i+l-1;
			if(check(i,i+r,i-r+1,i+1)){
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
			l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(i-mid>=0&&check(i,i+mid,i-mid,i))
					l=mid;
				else
					r=mid-1;
				if(r-l<=10)if(check(i,i+l+1,i-l-1,i))l++;
				if(r-l<=10)if(!check(i,i+r,i-r,i))r--;
			}realans=l*2,hwcf=i-l,hwcb=i+l-1;
			if(i-r>=0&&check(i,i+r,i-r,i))
			{
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
		}
		for(int i=0;i<(res-ans.second+ans.first-1)/2;i++)putchar(s[i]);
		for(int i=ans.first;i<=ans.second;i++)putchar(s[i]);
		for(int i=n-(res-ans.second+ans.first-1)/2;i<n;i++)putchar(s[i]);
		printf("\n");
	}
}


#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
string s;int n,T;bool xx[1000003];
long long pw[1000003],fr[1000003],bk[1000003];
bool check(long long a,long long b,long long x,long long y){
	long long fi=((fr[b]-fr[a]*pw[b-a])%mod+mod)%mod;
	long long sc=((bk[x]-bk[y]*pw[y-x])%mod+mod)%mod;
	return fi==sc;
}
int main(){
	cin>>T;pw[0]=1;
	for(int i=1;i<1000003;i++)pw[i]=pw[i-1]*26%mod;
	while(T--){
		cin>>s;
		n=s.size();
		fr[0]=0;bk[n]=0;
		for(int i=0;i<n;i++)fr[i+1]=(fr[i]*26+s[i]-'a')%mod;
		for(int i=n;i>0;i--)bk[i-1]=(bk[i]*26+s[i-1]-'a')%mod;
		for(int i=0;i<=n;i++)xx[i]=0;
		for(int i=0;i<=n;i++){
			xx[i]=1;
			if(s[i]!=s[n-1-i])break;
		}
		int res=0;
		pair<int,int>ans=make_pair(-1,-1);
		for(int i=0;i<n;i++){
			int l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(check(i,i+mid,i-mid+1,i+1)||(i-mid>=0&&check(i,i+mid,i-mid,i)))
					l=mid;
				else
					r=mid-1;
				if(check(i,i+l+1,i-l,i+1)||(i-l-1>=0&&check(i,i+l+1,i-l-1,i)))l++;
				if(!(check(i,i+r,i-r+1,i+1)||(i-r>=0&&check(i,i+r,i-r,i))))r--;
			}
			int realans=l*2-1,hwcf=i-l+1,hwcb=i+l-1;
			if(check(i,i+r,i-r+1,i+1)){
				int x=min(hwcf,n-1-hwcb);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
			if(i-r>=0&&check(i,i+r,i-r,i))
			{
				realans++,hwcf--;
				int x=min(hwcf,n-1-hwcb);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
		}
		for(int i=0;i<(res-ans.second+ans.first-1)/2;i++)cout<<s[i];
		for(int i=ans.first;i<=ans.second;i++)cout<<s[i];
		for(int i=n-(res-ans.second+ans.first-1)/2;i<n;i++)cout<<s[i];
		cout<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
const int mod=10007;
string s;int n,T;bool xx[1000003];
int pw[1000003],fr[1000003],bk[1000003];
bool check(long long a,long long b,long long x,long long y){
	int fi=((fr[b]-fr[a]*pw[b-a])%mod+mod)%mod;
	int sc=((bk[x]-bk[y]*pw[y-x])%mod+mod)%mod;
	return fi==sc;
}
int main(){
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
	cin>>T;pw[0]=1;
	for(int i=1;i<1000003;i++)pw[i]=pw[i-1]*26%mod;
	while(T--){
		cin>>s;
		n=s.size();
		fr[0]=0;bk[n]=0;
		for(int i=0;i<n;i++)fr[i+1]=(fr[i]*26+s[i]-'a')%mod;
		for(int i=n;i>0;i--)bk[i-1]=(bk[i]*26+s[i-1]-'a')%mod;
		for(int i=0;i<=n;i++)xx[i]=0;
		for(int i=0;i<=n;i++){
			xx[i]=1;
			if(s[i]!=s[n-1-i])break;
		}
		int res=0;
		pair<int,int>ans=make_pair(-1,-1);
		for(int i=0;i<n;i++){
			int l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(check(i,i+mid,i-mid+1,i+1))
					l=mid;
				else
					r=mid-1;
				if(check(i,i+l+1,i-l,i+1))l++;
				if(!check(i,i+r,i-r+1,i+1))r--;
			}
			int realans=l*2-1,hwcf=i-l+1,hwcb=i+l-1;
			if(check(i,i+r,i-r+1,i+1)){
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
			l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(i-mid>=0&&check(i,i+mid,i-mid,i))
					l=mid;
				else
					r=mid-1;
				if(check(i,i+l+1,i-l-1,i))l++;
				if(!check(i,i+r,i-r,i))r--;
			}realans=l*2,hwcf=i-l,hwcb=i+l-1;
			if(i-r>=0&&check(i,i+r,i-r,i))
			{
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
		}
		for(int i=0;i<(res-ans.second+ans.first-1)/2;i++)cout<<s[i];
		for(int i=ans.first;i<=ans.second;i++)cout<<s[i];
		for(int i=n-(res-ans.second+ans.first-1)/2;i<n;i++)cout<<s[i];
		cout<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
const int mod=1000000007;
string s;int n,T;bool xx[1000003];
long long pw[1000003],fr[1000003],bk[1000003];
bool check(long long a,long long b,long long x,long long y){
	long long fi=((fr[b]-fr[a]*pw[b-a])%mod+mod)%mod;
	long long sc=((bk[x]-bk[y]*pw[y-x])%mod+mod)%mod;
	return fi==sc;
}
int main(){
	cin>>T;pw[0]=1;
	for(int i=1;i<1000003;i++)pw[i]=pw[i-1]*26%mod;
	while(T--){
		cin>>s;
		n=s.size();
		fr[0]=0;bk[n]=0;
		for(int i=0;i<n;i++)fr[i+1]=(fr[i]*26+s[i]-'a')%mod;
		for(int i=n;i>0;i--)bk[i-1]=(bk[i]*26+s[i-1]-'a')%mod;
		for(int i=0;i<=n;i++)xx[i]=0;
		for(int i=0;i<=n;i++){
			xx[i]=1;
			if(s[i]!=s[n-1-i])break;
		}
		int res=0;
		pair<int,int>ans=make_pair(-1,-1);
		for(int i=0;i<n;i++){
			int l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(check(i,i+mid,i-mid+1,i+1))
					l=mid;
				else
					r=mid-1;
				if(check(i,i+l+1,i-l,i+1))l++;
				if(!check(i,i+r,i-r+1,i+1))r--;
			}
			int realans=l*2-1,hwcf=i-l+1,hwcb=i+l-1;
			if(check(i,i+r,i-r+1,i+1)){
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
			l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(check(i,i+mid,i-mid+1,i+1)||(i-mid>=0&&check(i,i+mid,i-mid,i)))
					l=mid;
				else
					r=mid-1;
				if(i-l-1>=0&&check(i,i+l+1,i-l-1,i))l++;
				if(i-r<0||!check(i,i+r,i-r,i))r--;
			}realans=l*2,hwcf=i-l,hwcb=i+l-1;
			if(i-r>=0&&check(i,i+r,i-r,i))
			{
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
		}
		for(int i=0;i<(res-ans.second+ans.first-1)/2;i++)cout<<s[i];
		for(int i=ans.first;i<=ans.second;i++)cout<<s[i];
		for(int i=n-(res-ans.second+ans.first-1)/2;i<n;i++)cout<<s[i];
		cout<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
string s;int n,T;bool xx[1000003];
long long pw[1000003],fr[1000003],bk[1000003];
bool check(long long a,long long b,long long x,long long y){
	long long fi=((fr[b]-fr[a]*pw[b-a])%mod+mod)%mod;
	long long sc=((bk[x]-bk[y]*pw[y-x])%mod+mod)%mod;
	return fi==sc;
}
int main(){
	cin>>T;pw[0]=1;
	for(int i=1;i<1000003;i++)pw[i]=pw[i-1]*26%mod;
	while(T--){
		cin>>s;
		n=s.size();
		fr[0]=0;bk[n]=0;
		for(int i=0;i<n;i++)fr[i+1]=(fr[i]*26+s[i]-'a')%mod;
		for(int i=n;i>0;i--)bk[i-1]=(bk[i]*26+s[i-1]-'a')%mod;
		for(int i=0;i<=n;i++)xx[i]=0;
		for(int i=0;i<=n;i++){
			xx[i]=1;
			if(s[i]!=s[n-1-i])break;
		}
		int res=0;
		pair<int,int>ans=make_pair(-1,-1);
		for(int i=0;i<n;i++){
			int l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(check(i,i+mid,i-mid+1,i+1))
					l=mid;
				else
					r=mid-1;
				if(check(i,i+l+1,i-l,i+1))l++;
				if(!check(i,i+r,i-r+1,i+1))r--;
			}
			int realans=l*2-1,hwcf=i-l+1,hwcb=i+l-1;
			if(check(i,i+r,i-r+1,i+1)){
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
			l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(i-mid>=0&&check(i,i+mid,i-mid,i))
					l=mid;
				else
					r=mid-1;
				if(r-l<=5)if(i-l-1>=0&&check(i,i+l+1,i-l-1,i))l++;
				if(r-l<=5)if(i-r<0||!check(i,i+r,i-r,i))r--;
			}realans=l*2,hwcf=i-l,hwcb=i+l-1;
			if(i-r>=0&&check(i,i+r,i-r,i))
			{
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
		}
		for(int i=0;i<(res-ans.second+ans.first-1)/2;i++)cout<<s[i];
		for(int i=ans.first;i<=ans.second;i++)cout<<s[i];
		for(int i=n-(res-ans.second+ans.first-1)/2;i<n;i++)cout<<s[i];
		cout<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
string s;int n,T;bool xx[1000003];
long long pw[1000003],fr[1000003],bk[1000003];
bool check(long long a,long long b,long long x,long long y){
	long long fi=((fr[b]-fr[a]*pw[b-a])%mod+mod)%mod;
	long long sc=((bk[x]-bk[y]*pw[y-x])%mod+mod)%mod;
	return fi==sc;
}
int main(){
	cin>>T;pw[0]=1;
	for(int i=1;i<1000003;i++)pw[i]=pw[i-1]*26%mod;
	while(T--){
		cin>>s;
		n=s.size();
		fr[0]=0;bk[n]=0;
		for(int i=0;i<n;i++)fr[i+1]=(fr[i]*26+s[i]-'a')%mod;
		for(int i=n;i>0;i--)bk[i-1]=(bk[i]*26+s[i-1]-'a')%mod;
		for(int i=0;i<=n;i++)xx[i]=0;
		for(int i=0;i<=n;i++){
			xx[i]=1;
			if(s[i]!=s[n-1-i])break;
		}
		int res=0;
		pair<int,int>ans=make_pair(-1,-1);
		for(int i=0;i<n;i++){
			int l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(check(i,i+mid,i-mid+1,i+1))
					l=mid;
				else
					r=mid-1;
				if(check(i,i+l+1,i-l,i+1))l++;
				if(!check(i,i+r,i-r+1,i+1))r--;
			}
			int realans=l*2-1,hwcf=i-l+1,hwcb=i+l-1;
			if(check(i,i+r,i-r+1,i+1)){
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
			l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(i-mid>=0&&check(i,i+mid,i-mid,i))
					l=mid;
				else
					r=mid-1;
				if(i-l-1>=0&&check(i,i+l+1,i-l-1,i))l++;
				if(i-r<0||!check(i,i+r,i-r,i))r--;
			}realans=l*2,hwcf=i-l,hwcb=i+l-1;
			if(i-r>=0&&check(i,i+r,i-r,i))
			{
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
		}
		for(int i=0;i<(res-ans.second+ans.first-1)/2;i++)cout<<s[i];
		for(int i=ans.first;i<=ans.second;i++)cout<<s[i];
		for(int i=n-(res-ans.second+ans.first-1)/2;i<n;i++)cout<<s[i];
		cout<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
string s;int n,T,xx[10000003];
long long pw[1000003],fr[1000003],bk[1000003];
bool check(long long a,long long b,long long x,long long y){
	long long fi=((fr[b]-fr[a]*pw[b-a])%mod+mod)%mod;
	long long sc=((bk[x]-bk[y]*pw[y-x])%mod+mod)%mod;
	return fi==sc;
}
int main(){
	cin>>T;pw[0]=1;
	for(int i=1;i<1000003;i++)pw[i]=pw[i-1]*26%mod;
	while(T--){
		cin>>s;
		n=s.size();
		fr[0]=0;bk[n]=0;
		for(int i=0;i<n;i++)fr[i+1]=(fr[i]*26+s[i]-'a')%mod;
		for(int i=n;i>0;i--)bk[i-1]=(bk[i]*26+s[i-1]-'a')%mod;
		for(int i=0;i<=n+1;i++)xx[i]=0;
		for(int i=0;i<n/2;i++){
			if(s[i]!=s[n-1-i])break;
			xx[i]=i+1;xx[n-1-i]=i+1;
		}
		int res=0;
		pair<int,int>ans=make_pair(-1,-1);
		for(int i=0;i<n;i++){
			int l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(check(i,i+mid,i-mid+1,i+1)||(i-mid>=0&&check(i,i+mid,i-mid,i)))
					l=mid;
				else
					r=mid-1;
				if(check(i,i+l+1,i-l,i+1)||(i-l-1>=0&&check(i,i+l+1,i-l-1,i)))l++;
				if(!(check(i,i+r,i-r+1,i+1)||(i-r>=0&&check(i,i+r,i-r,i))))r--;
			}
			int realans=l*2-1,hwcf=i-l+1,hwcb=i+l-1;
			if(i-r>=0&&check(i,i+r,i-r,i))realans++,hwcf--;
			if(res<realans+max(xx[hwcf-1],xx[hwcb+1])*2)
				res=realans+max(xx[hwcf-1],xx[hwcb+1])*2,
				ans=make_pair(hwcf,hwcb);
		}
		for(int i=0;i<(res-ans.second+ans.first-1)/2;i++)cout<<s[i];
		for(int i=ans.first;i<=ans.second;i++)cout<<s[i];
		for(int i=n-(res-ans.second+ans.first-1)/2;i<n;i++)cout<<s[i];
		cout<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
string s;int n,T;bool xx[1000003];
long long pw[1000003],fr[1000003],bk[1000003];
bool check(long long a,long long b,long long x,long long y){
	long long fi=((fr[b]-fr[a]*pw[b-a])%mod+mod)%mod;
	long long sc=((bk[x]-bk[y]*pw[y-x])%mod+mod)%mod;
	return fi==sc;
}
int main(){
	cin>>T;pw[0]=1;
	for(int i=1;i<1000003;i++)pw[i]=pw[i-1]*26%mod;
	while(T--){
		cin>>s;
		n=s.size();
		fr[0]=0;bk[n]=0;
		for(int i=0;i<n;i++)fr[i+1]=(fr[i]*26+s[i]-'a')%mod;
		for(int i=n;i>0;i--)bk[i-1]=(bk[i]*26+s[i-1]-'a')%mod;
		for(int i=0;i<=n;i++)xx[i]=0;
		for(int i=0;i<=n;i++){
			xx[i]=1;
			if(s[i]!=s[n-1-i])break;
		}
		int res=0;
		pair<int,int>ans=make_pair(-1,-1);
		for(int i=0;i<n;i++){
			int l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(check(i,i+mid,i-mid+1,i+1)||(i-mid>=0&&check(i,i+mid,i-mid,i)))
					l=mid;
				else
					r=mid-1;
				if(check(i,i+l+1,i-l,i+1)||(i-l-1>=0&&check(i,i+l+1,i-l-1,i)))l++;
				if(!(check(i,i+r,i-r+1,i+1)||(i-r>=0&&check(i,i+r,i-r,i))))r--;
			}
			int realans=l*2-1,hwcf=i-l+1,hwcb=i+l-1;
			if(i-r>=0&&check(i,i+r,i-r,i))realans++,hwcf--;
			int x=min(hwcf,n-1-hwcb);
			if(xx[x])
				if(res<realans+x*2)
					res=realans+x*2,
					ans=make_pair(hwcf,hwcb);
		}
		for(int i=0;i<(res-ans.second+ans.first-1)/2;i++)cout<<s[i];
		for(int i=ans.first;i<=ans.second;i++)cout<<s[i];
		for(int i=n-(res-ans.second+ans.first-1)/2;i<n;i++)cout<<s[i];
		cout<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
string s;int n,T;bool xx[1000003];
long long pw[1000003],fr[1000003],bk[1000003];
bool check(long long a,long long b,long long x,long long y){
	long long fi=((fr[b]-fr[a]*pw[b-a])%mod+mod)%mod;
	long long sc=((bk[x]-bk[y]*pw[y-x])%mod+mod)%mod;
	return fi==sc;
}
int main(){
	cin>>T;pw[0]=1;
	for(int i=1;i<1000003;i++)pw[i]=pw[i-1]*26%mod;
	while(T--){
		cin>>s;
		n=s.size();
		fr[0]=0;bk[n]=0;
		for(int i=0;i<n;i++)fr[i+1]=(fr[i]*26+s[i]-'a')%mod;
		for(int i=n;i>0;i--)bk[i-1]=(bk[i]*26+s[i-1]-'a')%mod;
		for(int i=0;i<=n;i++)xx[i]=0;
		for(int i=0;i<=n;i++){
			xx[i]=1;
			if(s[i]!=s[n-1-i])break;
		}
		int res=0;
		pair<int,int>ans=make_pair(-1,-1);
		for(int i=0;i<n;i++){
			int l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(check(i,i+mid,i-mid+1,i+1)||(i-mid>=0&&check(i,i+mid,i-mid,i)))
					l=mid;
				else
					r=mid-1;
				if(check(i,i+l+1,i-l,i+1)||(i-l-1>=0&&check(i,i+l+1,i-l-1,i)))l++;
				if(!(check(i,i+r,i-r+1,i+1)||(i-r>=0&&check(i,i+r,i-r,i))))r--;
			}
			int realans=l*2-1,hwcf=i-l+1,hwcb=i+l-1;
			if(i-r>=0&&check(i,i+r,i-r,i))realans++,hwcf--;
			int x=min(hwcf,n-1-hwcb);
			if(xx[x])
				if(hwcf-1>=0&&res<realans+x*2)
					res=realans+x*2,
					ans=make_pair(hwcf,hwcb);
		}
		for(int i=0;i<(res-ans.second+ans.first-1)/2;i++)cout<<s[i];
		for(int i=ans.first;i<=ans.second;i++)cout<<s[i];
		for(int i=n-(res-ans.second+ans.first-1)/2;i<n;i++)cout<<s[i];
		cout<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
string s;int n,T;bool xx[1000003];
long long pw[1000003],fr[1000003],bk[1000003];
bool check(long long a,long long b,long long x,long long y){
	long long fi=((fr[b]-fr[a]*pw[b-a])%mod+mod)%mod;
	long long sc=((bk[x]-bk[y]*pw[y-x])%mod+mod)%mod;
	return fi==sc;
}
int main(){
	cin>>T;pw[0]=1;
	for(int i=1;i<1000003;i++)pw[i]=pw[i-1]*26%mod;
	while(T--){
		cin>>s;
		n=s.size();
		fr[0]=0;bk[n]=0;
		for(int i=0;i<n;i++)fr[i+1]=(fr[i]*26+s[i]-'a')%mod;
		for(int i=n;i>0;i--)bk[i-1]=(bk[i]*26+s[i-1]-'a')%mod;
		for(int i=0;i<=n;i++)xx[i]=0;
		for(int i=0;i<=n;i++){
			xx[i]=1;
			if(s[i]!=s[n-1-i])break;
		}
		int res=0;
		pair<int,int>ans=make_pair(-1,-1);
		for(int i=0;i<n;i++){
			int l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(check(i,i+mid,i-mid+1,i+1))
					l=mid;
				else
					r=mid-1;
				if(check(i,i+l+1,i-l,i+1))l++;
				if(!check(i,i+r,i-r+1,i+1))r--;
			}
			int realans=l*2-1,hwcf=i-l+1,hwcb=i+l-1;
			if(check(i,i+r,i-r+1,i+1)){
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
			l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(check(i,i+mid,i-mid+1,i+1)||(i-mid>=0&&check(i,i+mid,i-mid,i)))
					l=mid;
				else
					r=mid-1;
				if(i-l-1>=0&&check(i,i+l+1,i-l-1,i))l++;
				if(i-r<0||!check(i,i+r,i-r,i))r--;
			}realans=l*2,hwcf=i-l,hwcb=i+l-1;
			if(i-r>=0&&check(i,i+r,i-r,i))
			{
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
		}
		for(int i=0;i<(res-ans.second+ans.first-1)/2;i++)cout<<s[i];
		for(int i=ans.first;i<=ans.second;i++)cout<<s[i];
		for(int i=n-(res-ans.second+ans.first-1)/2;i<n;i++)cout<<s[i];
		cout<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
const int mod=10007;
char s[1000003];int n,T;bool xx[1000003];
int pw[1000003],fr[1000003],bk[1000003];
inline bool check(int a,int b,int x,int y){
	int fi=((fr[b]-fr[a]*pw[b-a])%mod+mod)%mod;
	int sc=((bk[x]-bk[y]*pw[y-x])%mod+mod)%mod;
	return fi==sc;
}
int main(){
//	freopen("a.in","r",stdin);
//	freopen("a.out","w",stdout);
	cin>>T;pw[0]=1;
	for(int i=1;i<1000003;i++)pw[i]=pw[i-1]*26%mod;
	while(T--){
		scanf("%s",&s);
		n=strlen(s);
		fr[0]=0;bk[n]=0;
		for(int i=0;i<n;i++)fr[i+1]=(fr[i]*26+s[i]-'a')%mod;
		for(int i=n;i>0;i--)bk[i-1]=(bk[i]*26+s[i-1]-'a')%mod;
		for(int i=0;i<=n;i++)xx[i]=0;
		for(int i=0;i<=n;i++){
			xx[i]=1;
			if(s[i]!=s[n-1-i])break;
		}
		int res=0;
		pair<int,int>ans=make_pair(-1,-1);
		for(int i=0;i<n;i++){
			int l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(check(i,i+mid,i-mid+1,i+1))
					l=mid;
				else
					r=mid-1;
				if(r-l<=50)if(check(i,i+l+1,i-l,i+1))l++;
				if(r-l<=50)if(!check(i,i+r,i-r+1,i+1))r--;
			}
			int realans=l*2-1,hwcf=i-l+1,hwcb=i+l-1;
			if(check(i,i+r,i-r+1,i+1)){
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
			l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(i-mid>=0&&check(i,i+mid,i-mid,i))
					l=mid;
				else
					r=mid-1;
				if(r-l<=50)if(check(i,i+l+1,i-l-1,i))l++;
				if(r-l<=50)if(!check(i,i+r,i-r,i))r--;
			}realans=l*2,hwcf=i-l,hwcb=i+l-1;
			if(i-r>=0&&check(i,i+r,i-r,i))
			{
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
		}
		for(int i=0;i<(res-ans.second+ans.first-1)/2;i++)putchar(s[i]);
		for(int i=ans.first;i<=ans.second;i++)putchar(s[i]);
		for(int i=n-(res-ans.second+ans.first-1)/2;i<n;i++)putchar(s[i]);
		printf("\n");
	}
}


#include<bits/stdc++.h>
using namespace std;
const int mod=45007;
string s;int n,T;bool xx[1000005];
int pw[1000005],fr[1000005],bk[1000005];
bool check(int a,int b,int x,int y){
	int fi=((fr[b]-fr[a]*pw[b-a])%mod+mod)%mod;
	int sc=((bk[x]-bk[y]*pw[y-x])%mod+mod)%mod;
	return fi==sc;
}
int main(){
	cin>>T;pw[0]=1;
	for(int i=1;i<1000003;i++)pw[i]=pw[i-1]*27%mod;
	while(T--){
		cin>>s;
		n=s.size();
		fr[0]=0;bk[n]=0;
		for(int i=0;i<n;i++)fr[i+1]=(fr[i]*27+s[i]-'a'+1)%mod;
		for(int i=n;i>0;i--)bk[i-1]=(bk[i]*27+s[i-1]-'a'+1)%mod;
		for(int i=0;i<=n;i++)xx[i]=0;
		for(int i=0;i<=n;i++){
			xx[i]=1;
			if(s[i]!=s[n-1-i])break;
		}
		int res=0;
		pair<int,int>ans=make_pair(-1,-1);
		for(int i=0;i<n;i ++){
			int l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(check(i,i+mid,i-mid+1,i+1))
					l=mid;
				else
					r=mid-1;
				if(check(i,i+l+1,i-l,i+1))l++;
				if(!check(i,i+r,i-r+1,i+1))r--;
			}
			int realans=l*2-1,hwcf=i-l+1,hwcb=i+l-1;
			if(check(i,i+r,i-r+1,i+1)){
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
			l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(i-mid>=0&&check(i,i+mid,i-mid,i))
					l=mid;
				else
					r=mid-1;
				if(r-l<=5)if(i-l-1>=0&&check(i,i+l+1,i-l-1,i))l++;
				if(r-l<=5)if(i-r<0||!check(i,i+r,i-r,i))r--;
			}realans=l*2,hwcf=i-l,hwcb=i+l-1;
			if(i-r>=0&&check(i,i+r,i-r,i))
			{
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
		}
		for(int i=0;i<(res-ans.second+ans.first-1)/2;i++)cout<<s[i];
		for(int i=ans.first;i<=ans.second;i++)cout<<s[i];
		for(int i=n-(res-ans.second+ans.first-1)/2;i<n;i++)cout<<s[i];
		cout<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
const int mod=10007;
char s[1000003];int n,T;bool xx[1000003];
int pw[1000003],fr[1000003],bk[1000003];
inline bool check(int a,int b,int x,int y){
	int fi=((fr[b]-fr[a]*pw[b-a])%mod+mod)%mod;
	int sc=((bk[x]-bk[y]*pw[y-x])%mod+mod)%mod;
	return fi==sc;
}
int main(){
//	freopen("a.in","r",stdin);
//	freopen("a.out","w",stdout);
	cin>>T;pw[0]=1;
	for(int i=1;i<1000003;i++)pw[i]=pw[i-1]*26%mod;
	while(T--){
		scanf("%s",&s);
		n=strlen(s);
		fr[0]=0;bk[n]=0;
		for(int i=0;i<n;i++)fr[i+1]=(fr[i]*26+s[i]-'a')%mod;
		for(int i=n;i>0;i--)bk[i-1]=(bk[i]*26+s[i-1]-'a')%mod;
		for(int i=0;i<=n;i++)xx[i]=0;
		for(int i=0;i<=n;i++){
			xx[i]=1;
			if(s[i]!=s[n-1-i])break;
		}
		int res=0;w
		pair<int,int>ans=make_pair(-1,-1);
		for(int i=0;i<n;i++){
			int l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(check(i,i+mid,i-mid+1,i+1))
					l=mid;
				else
					r=mid-1;
				if(r-l<=10)if(check(i,i+l+1,i-l,i+1))l++;
				if(r-l<=10)if(!check(i,i+r,i-r+1,i+1))r--;
			}
			int realans=l*2-1,hwcf=i-l+1,hwcb=i+l-1;
			if(check(i,i+r,i-r+1,i+1)){
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
			l=1,r=min(i+1,n-i);
			while(l!=r){
				int mid=(l+r)/2;
				if(i-mid>=0&&check(i,i+mid,i-mid,i))
					l=mid;
				else
					r=mid-1;
				if(r-l<=10)if(check(i,i+l+1,i-l-1,i))l++;
				if(r-l<=10)if(!check(i,i+r,i-r,i))r--;
			}realans=l*2,hwcf=i-l,hwcb=i+l-1;
			if(i-r>=0&&check(i,i+r,i-r,i))
			{
				int x=min(hwcf,n-hwcb-1);
				if(xx[x])
					if(res<realans+x*2)
						res=realans+x*2,
						ans=make_pair(hwcf,hwcb);
			}
		}
		for(int i=0;i<(res-ans.second+ans.first-1)/2;i++)putchar(s[i]);
		for(int i=ans.first;i<=ans.second;i++)putchar(s[i]);
		for(int i=n-(res-ans.second+ans.first-1)/2;i<n;i++)putchar(s[i]);
		printf("\n");
	}
}


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

#include <bits/stdc++.h>

using namespace std;

long long hshf[2][1000005], hshr[2][1000005];
long long sd[2] = {93, 93};
long long pows[2][1000005];
const long long MOD = 1e9+7;

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

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


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

#include <bits/stdc++.h>

using namespace std;

long long hshf[2][1000005], hshr[2][1000005];
long long sd[2] = {1331, 69};
long long wesleysasshole[10] = {2843, 6581,7753,139,4889,7583,389,8273,1433,9203};
long long pows[2][1000005];
const long long MOD = 1e9+7;

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

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


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

#include <bits/stdc++.h>

using namespace std;

long long hshf[2][1000005], hshr[2][1000005];
long long sd[2] = {1331, 69};
vector<long long> wesleysasshole[10] = {2843, 6581,7753,139,4889,7583,389,8273,1433,9203};
long long pows[2][1000005];
const long long MOD = 1e9+7;

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

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


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

#include <bits/stdc++.h>

using namespace std;

long long hshf[2][1000005], hshr[2][1000005];
long long sd[2] = {31, 29};
long long pows[2][1000005];
const long long MOD = 1e9+7;

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

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


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

#include <bits/stdc++.h>

using namespace std;

long long hshf[2][1000005], hshr[2][1000005];
long long sd[2] = {1331, 69};
long long wesleysasshole[10] = {2843, 6581,7753,139,4889,7583,389,8273,1433,9203};
long long pows[2][1000005];
const long long MOD = 1e9+7;

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

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


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

#include <bits/stdc++.h>

using namespace std;

long long hshf[2][1000005], hshr[2][1000005];
long long sd[2] = {1331, 69};
long long wesleysasshole[10] = {2843, 6581,7753,139,4889,7583,389,8273,1433,9203};
long long pows[2][1000005];
const long long MOD = 1e9+7;

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

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


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

#include <bits/stdc++.h>

using namespace std;

long long hshf[2][1000005], hshr[2][1000005];
long long sd[2] = {43, 131};
long long pows[2][1000005];
const long long MOD = 1e9+7;

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

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


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

#include <bits/stdc++.h>

using namespace std;

long long hshf[2][1000005], hshr[2][1000005];
long long sd[2] = {1331, 69};
vector<long long> wesleysasshole = {2843, 6581,7753,139,4889,7583,389,8273,1433,9203};
long long pows[2][1000005];
const long long MOD = 1e9+7;

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

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


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

#include <bits/stdc++.h>

using namespace std;

long long hshf[2][1000005], hshr[2][1000005];
long long sd[2] = {1331, 2997};
long long pows[2][1000005];
const long long MOD = 1e9+7;

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

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


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

#include <bits/stdc++.h>

using namespace std;

long long hshf[2][1000005], hshr[2][1000005];
long long sd[2] = {43, 129};
long long pows[2][1000005];
const long long MOD = 1e9+7;

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

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


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

#include <bits/stdc++.h>

using namespace std;

long long hshf[2][1000005], hshr[2][1000005];
long long sd[2] = {1331, 29};
vector<long long> wesleysasshole = {1000000007};
long long pows[2][1000005];
long long MOD = 1e9+7;

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

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

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

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


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

#include <bits/stdc++.h>

using namespace std;

long long hshf[2][1000005], hshr[2][1000005];
long long sd[2] = {97, 129};
long long pows[2][1000005];
const long long MOD = 1e9+7;

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

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


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

#include <bits/stdc++.h>

using namespace std;

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

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

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

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

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


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

#include <bits/stdc++.h>

using namespace std;

long long hshf[2][1000005], hshr[2][1000005];
long long sd[2] = {1331, 69};
long long wesleysasshole[10] = {2843, 6581,7753,139,4889,7583,389,8273,1433,9203};
long long pows[2][1000005];
const long long MOD = 1e9+7;

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

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