#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <iostream>
#include <algorithm>
#define ll unsigned long long
using namespace std;
const int p=133;
char ch[1000010];
ll sum[1000010],val[1000010],sum1[1000010];
int main() {
    int t;
    scanf("%d", &t);
    while(t--){
        scanf("%s",ch+1);
        int len=strlen(ch+1),ans=0, L = 0, R = 0;
        val[0]=1;
        for(int i=1;i<=len;i++){
            val[i]=val[i-1]*131;
        }
        sum1[len+1]=0;
        for(int i=1;i<=len;i++){
            sum[i]=ch[i]-'a'+1+sum[i-1]*131;
            sum1[len-i+1]=ch[len-i+1]-'a'+1+sum1[len-i+2]*131;
        }
        int l,r,mid;
        for(int i=1;i<=len;i++){
            l=0,r=min(i-1,len-i);
            while(l<r)
            {
                mid=l+r+1>>1;
                if(sum[i-1]-sum[i-mid-1]*val[mid]==sum1[i+1]-sum1[i+mid+1]*val[mid])
                    l=mid;
                else
                    r=mid-1;
            }
            int len1 = i - l - 1;
            int len2 = len - (i + l);
            int len3 = min(len1, len2);
            if((l * 2 + 1 + 2 * len3) > ans) {
//                puts("1");
//                cout << i << " " << l << " " << len1 << " " << len2 << endl;
                if(len1 < len2) {
//                    cout << sum[len1] << " " << sum1[len1] << endl;
                    if(sum[len1] == sum1[len - len1 + 1]) {
                        ans = (l * 2) + 1 + 2 * len3;
                        L = i + l;
                        R = len - len1 + 1;
                    }
                }
                else {
                    if(sum[len2] == sum1[len - len2 + 1]) {
                        ans = (l * 2) + 1 + 2 * len3;
                        L = len2;
                        R = i - l;
                    }
                }
            }

            l=0,r=min(i-1,len-i+1);
            while (l<r)
            {
                mid=l+r+1>>1;
                if(sum[i-1]-sum[i-mid-1]*val[mid]==sum1[i]-sum1[i+mid]*val[mid])
                    l=mid;
                else
                    r=mid-1;
            }

            len1 = i - l - 1;
            len2 = len - (i + l - 1);
            len3 = min(len1, len2);
            if((l * 2 + 2 * len3)> ans) {
//                puts("2");
//                cout << i << " " << l << " " << len1 << " " << len2 << endl;
                if(len1 < len2) {
                    if(sum[len1] == sum1[len - len1 + 1]) {
                        ans = (l * 2) + 2 * len3;
                        L = i + l - 1;
                        R = len - len1 + 1;
                    }
                }
                else {
                    if(sum[len2] == sum1[len - len2 + 1]) {
                        ans = (l * 2) + 2 * len3;
                        L = len2;
                        R = i - l;
                    }
                }
            }

        }

        for(int i = 1; i <= len / 2; i++) {
            if(sum[i] == sum1[i]) {
                if(i * 2 > ans) {
                    ans = i * 2;
                    L = i;
                    R = len - i + 1;
                }
            }
        }

        if(len % 2 == 1) {
            if(sum[(len + 1) / 2] == sum1[(len + 1) / 2]) {
                ans = len;
                L = (len + 1) / 2;
                R = (len + 1) / 2 + 1;
            }
        }

        for(int i = 1; i <= L; i++) printf("%c", ch[i]);
        for(int j = R; j <= len; j++) printf("%c", ch[j]);
        puts("");
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <iostream>
#include <algorithm>
#define ll unsigned long long
using namespace std;
const int p=133;
char ch[1000010];
ll sum[1000010],val[1000010],sum1[1000010];
int main() {
    int t;
    scanf("%d", &t);
    while(t--){
        scanf("%s",ch+1);
        int len=strlen(ch+1),ans=0, L = 0, R = 0;
        val[0]=1;
        for(int i=1;i<=len;i++){
            val[i]=val[i-1]*131;
        }
        sum1[len+1]=0;
        for(int i=1;i<=len;i++){
            sum[i]=ch[i]-'a'+1+sum[i-1]*131;
            sum1[len-i+1]=ch[len-i+1]-'a'+1+sum1[len-i+2]*131;
        }
        int l,r,mid;
        for(int i=1;i<=len;i++){
            l=0,r=min(i-1,len-i);
            while(l<r)
            {
                mid=l+r+1>>1;
                if(sum[i-1]-sum[i-mid-1]*val[mid]==sum1[i+1]-sum1[i+mid+1]*val[mid])
                    l=mid;
                else
                    r=mid-1;
            }
            int len1 = i - l - 1;
            int len2 = len - (i + l);
            int len3 = min(len1, len2);
            if((l * 2 + 1 + 2 * len3) > ans) {
//                puts("1");
//                cout << i << " " << l << " " << len1 << " " << len2 << endl;
                if(len1 < len2) {
//                    cout << sum[len1] << " " << sum1[len1] << endl;
                    if(sum[len1] == sum1[len - len1 + 1]) {
                        ans = (l * 2) + 1 + 2 * len3;
                        L = i + l;
                        R = len - len1 + 1;
                    }
                }
                else {
                    if(sum[len2] == sum1[len - len2 + 1]) {
                        ans = (l * 2) + 1 + 2 * len3;
                        L = len2;
                        R = i - l;
                    }
                }
            }

            l=0,r=min(i-1,len-i+1);
            while (l<r)
            {
                mid=l+r+1>>1;
                if(sum[i-1]-sum[i-mid-1]*val[mid]==sum1[i]-sum1[i+mid]*val[mid])
                    l=mid;
                else
                    r=mid-1;
            }

            len1 = i - l - 1;
            len2 = len - (i + l - 1);
            len3 = min(len1, len2);
            if((l * 2 + 2 * len3)> ans) {
//                puts("2");
//                cout << i << " " << l << " " << len1 << " " << len2 << endl;
                if(len1 < len2) {
                    if(sum[len1] == sum1[len - len1 + 1]) {
                        ans = (l * 2) + 2 * len3;
                        L = i + l - 1;
                        R = len - len1 + 1;
                    }
                }
                else {
                    if(sum[len2] == sum1[len - len2 + 1]) {
                        ans = (l * 2) + 2 * len3;
                        L = len2;
                        R = i - l;
                    }
                }
            }

        }

        int nowl = 1, nowr = len;
        while(nowl < nowr) {
            if(ch[nowl] == ch[nowr]) {
                nowl++; nowr--;
            }
            else break;
        }

        int tmplen = (nowl - 1) * 2;
        if(nowl == nowr) tmplen++;

        if(tmplen > ans) {
            ans = tmplen;
            L = nowl - 1;
            R = nowr + 1;
            if(nowl == nowr) R--;
        }

        for(int i = 1; i <= L; i++) printf("%c", ch[i]);
        for(int j = R; j <= len; j++) printf("%c", ch[j]);
        puts("");
    }
    return 0;
}


#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;*/
#define itn int
#define all(c) c.begin(),c.end()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define ll long long
#define ull unsigned long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
using namespace std;

const ll N = 2e5 + 5, inf = 1e18, mod = 998244353;
const double PI = 3.1415926536;
int days[13] = {0, 31, 28, 31, 30, 31,30, 31, 31, 30, 31, 30, 31};

template<typename T>
ostream& operator<< (ostream& out, const vector<T>& mas) {
    for (const auto& x : mas) {
        out << x << ' ';
    }
    return out;
}

bool is_prime(ll n) {
    for (ll i = 2; i * i <= n; ++i) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

inline ll getPow(ll a, ll b) {
    ll res = 1ll, tp = a;
    while (b){
        if (b&1ll) {
            res *= tp;
        }
        tp *= tp;
        tp %= mod; res %= mod;
        b >>= 1ll;
    }
    return res;
}

ll vec_mult(const pll & t1, const pll & t2, const pll & t3) {
    const ll & x1 = t1.first, y1 = t1.second;
    const ll & x2 = t2.first, y2 = t2.second;
    const ll & x3 = t3.first, y3 = t3.second;
    return abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1));
}

void ok() {
    cout << "YES" << '\n';
}

void no() {
    cout << "NO" << '\n';
}

inline ll nxt() {
    ll x;
    cin >> x;
    return x;
}

string z_function(string s) {
    int n = s.size();
    string str = s;
    reverse(all(s));
    str += '#';
    str += s;
    int l = 0, r = 0;
    vector<int> z(n * 2 + 1);
    for (int i = n + 1; i < str.size(); i++) {
        if (r >= i) {
            z[i] = min(z[i - l], r - i + 1);
        }
        while (i + z[i] < str.size() && str[z[i]] == str[i + z[i]]) {
            z[i]++;
        }
        if (i + z[i] - 1 > r) {
            l = i;
            r = i + z[i] - 1;
        }
    }

    int ans = 0;
    for (int i = n + 1; i < str.size(); i++) {
        if (z[i] + i == str.size()) {
            ans = max(ans, z[i]);
        }
    }
    ans = min(ans, n);

    reverse(all(s));
    return s.substr(0, ans);
}

void solve() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    string s;
    cin >> s;
    int n = s.size();
    int l = 0, r = n - 1;
    while (r - l > 0 && s[l] == s[r]) {
        l++;
        r--;
    }

    string tmp = s.substr(l, r - l + 1);
    string t1 = z_function(tmp);
    reverse(all(tmp));
    string t2 = z_function(tmp);
    reverse(all(t2));
    if (t1.size() > t2.size()) {
        cout << s.substr(0, l) << t1 << s.substr(r + 1);
    } else {
        cout << s.substr(0, l) << t2 << s.substr(r + 1);
    }
    cout << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t = nxt();
    while (t--) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;*/
#define itn int
#define all(c) c.begin(),c.end()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define ll long long
#define ull unsigned long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
using namespace std;

const ll N = 2e5 + 5, inf = 1e18, mod = 998244353;
const double PI = 3.1415926536;
int days[13] = {0, 31, 28, 31, 30, 31,30, 31, 31, 30, 31, 30, 31};

template<typename T>
ostream& operator<< (ostream& out, const vector<T>& mas) {
    for (const auto& x : mas) {
        out << x << ' ';
    }
    return out;
}

bool is_prime(ll n) {
    for (ll i = 2; i * i <= n; ++i) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

inline ll getPow(ll a, ll b) {
    ll res = 1ll, tp = a;
    while (b){
        if (b&1ll) {
            res *= tp;
        }
        tp *= tp;
        tp %= mod; res %= mod;
        b >>= 1ll;
    }
    return res;
}

ll vec_mult(const pll & t1, const pll & t2, const pll & t3) {
    const ll & x1 = t1.first, y1 = t1.second;
    const ll & x2 = t2.first, y2 = t2.second;
    const ll & x3 = t3.first, y3 = t3.second;
    return abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1));
}

void ok() {
    cout << "YES" << '\n';
}

void no() {
    cout << "NO" << '\n';
}

inline int nxt() {
    int x;
    cin >> x;
    return x;
}

string z_function(const string& s) {
    int n = s.size();
    string t = s;
    reverse(all(t));
    string str = s + '#' + t;

    int l = 0, r = 0;
    vector<int> z(n * 2 + 1);
    for (int i = n + 1; i < str.size(); i++) {
        if (r >= i) {
            z[i] = min(z[i - l], r - i + 1);
        }
        while (z[i] < n && str[z[i]] == str[i + z[i]]) {
            z[i]++;
        }
        if (i + z[i] - 1 > r) {
            l = i;
            r = i + z[i] - 1;
        }
        if (z[i] + i == str.size()) {
            return s.substr(0, min(z[i], n));
        }
    }
    return string();
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int t = nxt();
    while (t--) {
        string s;
        cin >> s;
        int n = s.size();
        int l = 0, r = n - 1;
        while (l < r && s[l] == s[r]) {
            l++;
            r--;
        }

        string tmp = s.substr(l, r - l + 1);
        string t1 = z_function(tmp);
        reverse(all(tmp));
        string t2 = z_function(tmp);
        reverse(all(t2));
        if (t1.size() > t2.size()) {
            cout << s.substr(0, l) << t1 << s.substr(r + 1) << '\n';
        } else {
            cout << s.substr(0, l) << t2 << s.substr(r + 1) << '\n';
        }
    }
    return 0;
}


#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;*/
#define itn int
#define all(c) c.begin(),c.end()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define ll long long
#define ull unsigned long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
using namespace std;

const ll N = 2e5 + 5, inf = 1e18, mod = 998244353;
const double PI = 3.1415926536;
int days[13] = {0, 31, 28, 31, 30, 31,30, 31, 31, 30, 31, 30, 31};

template<typename T>
ostream& operator<< (ostream& out, const vector<T>& mas) {
    for (const auto& x : mas) {
        out << x << ' ';
    }
    return out;
}

bool is_prime(ll n) {
    for (ll i = 2; i * i <= n; ++i) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

inline ll getPow(ll a, ll b) {
    ll res = 1ll, tp = a;
    while (b){
        if (b&1ll) {
            res *= tp;
        }
        tp *= tp;
        tp %= mod; res %= mod;
        b >>= 1ll;
    }
    return res;
}

ll vec_mult(const pll & t1, const pll & t2, const pll & t3) {
    const ll & x1 = t1.first, y1 = t1.second;
    const ll & x2 = t2.first, y2 = t2.second;
    const ll & x3 = t3.first, y3 = t3.second;
    return abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1));
}

void ok() {
    cout << "YES" << '\n';
}

void no() {
    cout << "NO" << '\n';
}

inline ll nxt() {
    ll x;
    cin >> x;
    return x;
}

string z_function(const string& s) {
    int n = s.size();
    auto t = s;
    string str = t;
    reverse(all(t));
    str += '#';
    str += s;
    int l = 0, r = 0;
    vector<int> z(n * 2 + 1);
    int ans = 0;
    for (int i = n + 1; i < str.size(); i++) {
        if (r >= i) {
            z[i] = min(z[i - l], r - i + 1);
        }
        while (i + z[i] < str.size() && str[z[i]] == str[i + z[i]]) {
            z[i]++;
        }
        if (i + z[i] - 1 > r) {
            l = i;
            r = i + z[i] - 1;
        }
        if (z[i] + i == str.size()) {
            ans = max(ans, z[i]);
            ans = min(ans, n);
            return s.substr(0, ans);
        }
    return string();
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int t = nxt();
    while (t--) {
        string s;
        cin >> s;
        int n = s.size();
        int l = 0, r = n - 1;
        while (r - l > 0 && s[l] == s[r]) {
            l++;
            r--;
        }

        string tmp = s.substr(l, r - l + 1);
        string t1 = z_function(tmp);
        reverse(all(tmp));
        string t2 = z_function(tmp);
        reverse(all(t2));
        if (t1.size() > t2.size()) {
            cout << s.substr(0, l) << t1 << s.substr(r + 1) << '\n';
        } else {
            cout << s.substr(0, l) << t2 << s.substr(r + 1) << '\n';
        }
    }
    return 0;
}


#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;*/
#define itn int
#define all(c) c.begin(),c.end()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define ll long long
#define ull unsigned long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
using namespace std;

const ll N = 2e5 + 5, inf = 1e18, mod = 998244353;
const double PI = 3.1415926536;
int days[13] = {0, 31, 28, 31, 30, 31,30, 31, 31, 30, 31, 30, 31};

template<typename T>
ostream& operator<< (ostream& out, const vector<T>& mas) {
    for (const auto& x : mas) {
        out << x << ' ';
    }
    return out;
}

bool is_prime(ll n) {
    for (ll i = 2; i * i <= n; ++i) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

inline ll getPow(ll a, ll b) {
    ll res = 1ll, tp = a;
    while (b){
        if (b&1ll) {
            res *= tp;
        }
        tp *= tp;
        tp %= mod; res %= mod;
        b >>= 1ll;
    }
    return res;
}

ll vec_mult(const pll & t1, const pll & t2, const pll & t3) {
    const ll & x1 = t1.first, y1 = t1.second;
    const ll & x2 = t2.first, y2 = t2.second;
    const ll & x3 = t3.first, y3 = t3.second;
    return abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1));
}

void ok() {
    cout << "YES" << '\n';
}

void no() {
    cout << "NO" << '\n';
}

inline ll nxt() {
    ll x;
    cin >> x;
    return x;
}

string z_function(const string& s) {
    int n = s.size();
    auto t = s;
    string str = t;
    reverse(all(t));
    str += '#';
    str += s;
    int l = 0, r = 0;
    vector<int> z(n * 2 + 1);
    int ans = 0;
    for (int i = n + 1; i < str.size(); i++) {
        if (r >= i) {
            z[i] = min(z[i - l], r - i + 1);
        }
        while (i + z[i] < str.size() && str[z[i]] == str[i + z[i]]) {
            z[i]++;
        }
        if (i + z[i] - 1 > r) {
            l = i;
            r = i + z[i] - 1;
        }
        if (z[i] + i == str.size()) {
            ans = max(ans, z[i]);
            ans = min(ans, n);
            return s.substr(0, ans);
        }
    }
    return string();
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int t = nxt();
    while (t--) {
        string s;
        cin >> s;
        int n = s.size();
        int l = 0, r = n - 1;
        while (r - l > 0 && s[l] == s[r]) {
            l++;
            r--;
        }

        string tmp = s.substr(l, r - l + 1);
        string t1 = z_function(tmp);
        reverse(all(tmp));
        string t2 = z_function(tmp);
        reverse(all(t2));
        if (t1.size() > t2.size()) {
            cout << s.substr(0, l) << t1 << s.substr(r + 1) << '\n';
        } else {
            cout << s.substr(0, l) << t2 << s.substr(r + 1) << '\n';
        }
    }
    return 0;
}


#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;*/
#define itn int
#define all(c) c.begin(),c.end()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define ll long long
#define ull unsigned long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
using namespace std;

const ll N = 2e5 + 5, inf = 1e18, mod = 998244353;
const double PI = 3.1415926536;
int days[13] = {0, 31, 28, 31, 30, 31,30, 31, 31, 30, 31, 30, 31};

template<typename T>
ostream& operator<< (ostream& out, const vector<T>& mas) {
    for (const auto& x : mas) {
        out << x << ' ';
    }
    return out;
}

bool is_prime(ll n) {
    for (ll i = 2; i * i <= n; ++i) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

inline ll getPow(ll a, ll b) {
    ll res = 1ll, tp = a;
    while (b){
        if (b&1ll) {
            res *= tp;
        }
        tp *= tp;
        tp %= mod; res %= mod;
        b >>= 1ll;
    }
    return res;
}

ll vec_mult(const pll & t1, const pll & t2, const pll & t3) {
    const ll & x1 = t1.first, y1 = t1.second;
    const ll & x2 = t2.first, y2 = t2.second;
    const ll & x3 = t3.first, y3 = t3.second;
    return abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1));
}

void ok() {
    cout << "YES" << '\n';
}

void no() {
    cout << "NO" << '\n';
}

inline ll nxt() {
    ll x;
    cin >> x;
    return x;
}

string z_function(string s) {
    int n = s.size();
    string str = s;
    reverse(all(s));
    str += '#';
    str += s;
    int l = 0, r = 0;
    vector<int> z(n * 2 + 1);
    int ans = 0;
    for (int i = n + 1; i < str.size(); i++) {
        if (r >= i) {
            z[i] = min(z[i - l], r - i + 1);
        }
        while (i + z[i] < str.size() && str[z[i]] == str[i + z[i]]) {
            z[i]++;
        }
        if (i + z[i] - 1 > r) {
            l = i;
            r = i + z[i] - 1;
        }
        if (z[i] + i == str.size()) {
            ans = max(ans, z[i]);
        }
    }
    ans = min(ans, n);

    reverse(all(s));
    return s.substr(0, ans);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t = nxt();
    while (t--) {
        string s;
        cin >> s;
        int n = s.size();
        int l = 0, r = n - 1;
        while (r - l > 0 && s[l] == s[r]) {
            l++;
            r--;
        }

        string tmp = s.substr(l, r - l + 1);
        string t1 = z_function(tmp);
        reverse(all(tmp));
        string t2 = z_function(tmp);
        reverse(all(t2));
        if (t1.size() > t2.size()) {
            cout << s.substr(0, l) << t1 << s.substr(r + 1) << '\n';
        } else {
            cout << s.substr(0, l) << t2 << s.substr(r + 1) << '\n';
        }
    }
    return 0;
}


#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;*/
#define itn int
#define all(c) c.begin(),c.end()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define ll long long
#define ull unsigned long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
using namespace std;

const ll N = 2e5 + 5, inf = 1e18, mod = 998244353;
const double PI = 3.1415926536;
int days[13] = {0, 31, 28, 31, 30, 31,30, 31, 31, 30, 31, 30, 31};

template<typename T>
ostream& operator<< (ostream& out, const vector<T>& mas) {
    for (const auto& x : mas) {
        out << x << ' ';
    }
    return out;
}

bool is_prime(ll n) {
    for (ll i = 2; i * i <= n; ++i) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

inline ll getPow(ll a, ll b) {
    ll res = 1ll, tp = a;
    while (b){
        if (b&1ll) {
            res *= tp;
        }
        tp *= tp;
        tp %= mod; res %= mod;
        b >>= 1ll;
    }
    return res;
}

ll vec_mult(const pll & t1, const pll & t2, const pll & t3) {
    const ll & x1 = t1.first, y1 = t1.second;
    const ll & x2 = t2.first, y2 = t2.second;
    const ll & x3 = t3.first, y3 = t3.second;
    return abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1));
}

void ok() {
    cout << "YES" << '\n';
}

void no() {
    cout << "NO" << '\n';
}

inline int nxt() {
    int x;
    cin >> x;
    return x;
}

string z_function(const string& s, int n_) {
    int n = s.size();

    int l = 0, r = 0;
    vector<int> z(n);
    for (int i = 1; i < s.size(); i++) {
        if (r >= i) {
            z[i] = min(z[i - l], r - i + 1);
        }
        while (z[i] < n && s[z[i]] == s[i + z[i]]) {
            z[i]++;
        }
        if (i + z[i] - 1 > r) {
            l = i;
            r = i + z[i] - 1;
        }
        if (z[i] + i == s.size()) {
            return s.substr(0, min(z[i], n_));
        }
    }
    return string();
}

string getMaxAns(const string& tmp) {
    string t = tmp;
    reverse(all(t));
    string ans1 = z_function(tmp + '#' + t, tmp.size());
    string ans2 = z_function(t + '#' + tmp, tmp.size());
    reverse(all(ans2));
    if (ans1.size() > ans2.size()) {
        return ans1;
    } else {
        return ans2;
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int t = nxt();
    while (t--) {
        string s;
        cin >> s;
        int n = s.size();
        int l = 0, r = n - 1;
        while (l < r && s[l] == s[r]) {
            l++, r--;
        }

        string tmp = s.substr(l, r - l + 1);
        cout << s.substr(0, l) << getMaxAns(tmp) << s.substr(r + 1) << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;*/
#define itn int
#define all(c) c.begin(),c.end()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define ll long long
#define ull unsigned long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
using namespace std;

const ll N = 2e5 + 5, inf = 1e18, mod = 998244353;
const double PI = 3.1415926536;
int days[13] = {0, 31, 28, 31, 30, 31,30, 31, 31, 30, 31, 30, 31};

template<typename T>
ostream& operator<< (ostream& out, const vector<T>& mas) {
    for (const auto& x : mas) {
        out << x << ' ';
    }
    return out;
}

bool is_prime(ll n) {
    for (ll i = 2; i * i <= n; ++i) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

inline ll getPow(ll a, ll b) {
    ll res = 1ll, tp = a;
    while (b){
        if (b&1ll) {
            res *= tp;
        }
        tp *= tp;
        tp %= mod; res %= mod;
        b >>= 1ll;
    }
    return res;
}

ll vec_mult(const pll & t1, const pll & t2, const pll & t3) {
    const ll & x1 = t1.first, y1 = t1.second;
    const ll & x2 = t2.first, y2 = t2.second;
    const ll & x3 = t3.first, y3 = t3.second;
    return abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1));
}

void ok() {
    cout << "YES" << '\n';
}

void no() {
    cout << "NO" << '\n';
}

inline int nxt() {
    int x;
    cin >> x;
    return x;
}

string z_function(const string& s) {
    int n = s.size();
    string t = s;
    reverse(all(t));
    string str = s + '#' + t;

    int l = 0, r = 0;
    vector<int> z(n * 2 + 1);
    for (int i = 1; i < str.size(); i++) {
        if (r >= i) {
            z[i] = min(z[i - l], r - i + 1);
        }
        while (z[i] < n && str[z[i]] == str[i + z[i]]) {
            z[i]++;
        }
        if (i + z[i] - 1 > r) {
            l = i;
            r = i + z[i] - 1;
        }
        if (z[i] + i == str.size()) {
            return s.substr(0, min(z[i], n));
        }
    }
    return string();
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int t = nxt();
    while (t--) {
        string s;
        cin >> s;
        int n = s.size();
        int l = 0, r = n - 1;
        while (l < r && s[l] == s[r]) {
            l++;
            r--;
        }

        string tmp = s.substr(l, r - l + 1);
        string t1 = z_function(tmp);
        reverse(all(tmp));
        string t2 = z_function(tmp);
        reverse(all(t2));
        if (t1.size() > t2.size()) {
            cout << s.substr(0, l) << t1 << s.substr(r + 1) << '\n';
        } else {
            cout << s.substr(0, l) << t2 << s.substr(r + 1) << '\n';
        }
    }
    return 0;
}


#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;*/
#define itn int
#define all(c) c.begin(),c.end()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define ll long long
#define ull unsigned long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
using namespace std;

const ll N = 2e5 + 5, inf = 1e18, mod = 998244353;
const double PI = 3.1415926536;
int days[13] = {0, 31, 28, 31, 30, 31,30, 31, 31, 30, 31, 30, 31};

template<typename T>
ostream& operator<< (ostream& out, const vector<T>& mas) {
    for (const auto& x : mas) {
        out << x << ' ';
    }
    return out;
}

bool is_prime(ll n) {
    for (ll i = 2; i * i <= n; ++i) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

inline ll getPow(ll a, ll b) {
    ll res = 1ll, tp = a;
    while (b){
        if (b&1ll) {
            res *= tp;
        }
        tp *= tp;
        tp %= mod; res %= mod;
        b >>= 1ll;
    }
    return res;
}

ll vec_mult(const pll & t1, const pll & t2, const pll & t3) {
    const ll & x1 = t1.first, y1 = t1.second;
    const ll & x2 = t2.first, y2 = t2.second;
    const ll & x3 = t3.first, y3 = t3.second;
    return abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1));
}

void ok() {
    cout << "YES" << '\n';
}

void no() {
    cout << "NO" << '\n';
}

inline ll nxt() {
    ll x;
    cin >> x;
    return x;
}

string z_function(string s) {
    int n = s.size();
    string str = s;
    reverse(all(s));
    str += '#';
    str += s;
    int l = 0, r = 0;
    vector<int> z(n * 2 + 1);
    for (int i = n + 1; i < str.size(); i++) {
        if (r >= i) {
            z[i] = min(z[i - l], r - i + 1);
        }
        while (i + z[i] < str.size() && str[z[i]] == str[i + z[i]]) {
            z[i]++;
        }
        if (i + z[i] - 1 > r) {
            l = i;
            r = i + z[i] - 1;
        }
    }

    int ans = 0;
    for (int i = n + 1; i < str.size(); i++) {
        if (z[i] + i == str.size()) {
            ans = max(ans, z[i]);
        }
    }
    ans = min(ans, n);

    reverse(all(s));
    return s.substr(0, ans);
}

void solve() {
    string s;
    cin >> s;
    int n = s.size();
    int l = 0, r = n - 1;
    while (r - l > 0 && s[l] == s[r]) {
        l++;
        r--;
    }

    string tmp = s.substr(l, r - l + 1);
    string t1 = z_function(tmp);
    reverse(all(tmp));
    string t2 = z_function(tmp);
    reverse(all(t2));
    if (t1.size() > t2.size()) {
        cout << s.substr(0, l) << t1 << s.substr(r + 1);
    } else {
        cout << s.substr(0, l) << t2 << s.substr(r + 1);
    }
    cout << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t = nxt();
    while (t--) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;*/
#define itn int
#define all(c) c.begin(),c.end()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define ll long long
#define ull unsigned long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
using namespace std;

const ll N = 2e5 + 5, inf = 1e18, mod = 998244353;
const double PI = 3.1415926536;
int days[13] = {0, 31, 28, 31, 30, 31,30, 31, 31, 30, 31, 30, 31};

template<typename T>
ostream& operator<< (ostream& out, const vector<T>& mas) {
    for (const auto& x : mas) {
        out << x << ' ';
    }
    return out;
}

bool is_prime(ll n) {
    for (ll i = 2; i * i <= n; ++i) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

inline ll getPow(ll a, ll b) {
    ll res = 1ll, tp = a;
    while (b){
        if (b&1ll) {
            res *= tp;
        }
        tp *= tp;
        tp %= mod; res %= mod;
        b >>= 1ll;
    }
    return res;
}

ll vec_mult(const pll & t1, const pll & t2, const pll & t3) {
    const ll & x1 = t1.first, y1 = t1.second;
    const ll & x2 = t2.first, y2 = t2.second;
    const ll & x3 = t3.first, y3 = t3.second;
    return abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1));
}

void ok() {
    cout << "YES" << '\n';
}

void no() {
    cout << "NO" << '\n';
}

inline int nxt() {
    int x;
    cin >> x;
    return x;
}

string z_function(const string& s) {
    int n = s.size();
    auto t = s;
    string str = t;
    reverse(all(t));
    str += '#';
    str += t;
    int l = 0, r = 0;
    vector<int> z(n * 2 + 1);
    for (int i = n + 1; i < str.size(); i++) {
        if (r >= i) {
            z[i] = min(z[i - l], r - i + 1);
        }
        while (i + z[i] < str.size() && str[z[i]] == str[i + z[i]]) {
            z[i]++;
        }
        if (i + z[i] - 1 > r) {
            l = i;
            r = i + z[i] - 1;
        }
        if (z[i] + i == str.size()) {
            return s.substr(0, min(z[i], n));
        }
    }
    return string();
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int t = nxt();
    while (t--) {
        string s;
        cin >> s;
        int n = s.size();
        int l = 0, r = n - 1;
        while (r - l > 0 && s[l] == s[r]) {
            l++;
            r--;
        }

        string tmp = s.substr(l, r - l + 1);
        string t1 = z_function(tmp);
        reverse(all(tmp));
        string t2 = z_function(tmp);
        reverse(all(t2));
        if (t1.size() > t2.size()) {
            cout << s.substr(0, l) << t1 << s.substr(r + 1) << '\n';
        } else {
            cout << s.substr(0, l) << t2 << s.substr(r + 1) << '\n';
        }
    }
    return 0;
}


#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;*/
#define itn int
#define all(c) c.begin(),c.end()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define ll long long
#define ull unsigned long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
using namespace std;

const ll N = 2e5 + 5, inf = 1e18, mod = 998244353;
const double PI = 3.1415926536;
int days[13] = {0, 31, 28, 31, 30, 31,30, 31, 31, 30, 31, 30, 31};

template<typename T>
ostream& operator<< (ostream& out, const vector<T>& mas) {
    for (const auto& x : mas) {
        out << x << ' ';
    }
    return out;
}

bool is_prime(ll n) {
    for (ll i = 2; i * i <= n; ++i) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

inline ll getPow(ll a, ll b) {
    ll res = 1ll, tp = a;
    while (b){
        if (b&1ll) {
            res *= tp;
        }
        tp *= tp;
        tp %= mod; res %= mod;
        b >>= 1ll;
    }
    return res;
}

ll vec_mult(const pll & t1, const pll & t2, const pll & t3) {
    const ll & x1 = t1.first, y1 = t1.second;
    const ll & x2 = t2.first, y2 = t2.second;
    const ll & x3 = t3.first, y3 = t3.second;
    return abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1));
}

void ok() {
    cout << "YES" << '\n';
}

void no() {
    cout << "NO" << '\n';
}

inline ll nxt() {
    ll x;
    cin >> x;
    return x;
}

string z_function(string& s) {
    int n = s.size();
    string str = s;
    reverse(all(s));
    str += '#';
    str += s;
    int l = 0, r = 0;
    vector<int> z(n * 2 + 1);
    int ans = 0;
    reverse(all(s));
    for (int i = n + 1; i < str.size(); i++) {
        if (r >= i) {
            z[i] = min(z[i - l], r - i + 1);
        }
        while (i + z[i] < str.size() && str[z[i]] == str[i + z[i]]) {
            z[i]++;
        }
        if (i + z[i] - 1 > r) {
            l = i;
            r = i + z[i] - 1;
        }
        if (z[i] + i == str.size()) {
            ans = max(ans, z[i]);
            ans = min(ans, n);
            return s.substr(0, ans);
        }
    }
    ans = min(ans, n);
    return string();
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t = nxt();
    while (t--) {
        string s;
        cin >> s;
        int n = s.size();
        int l = 0, r = n - 1;
        while (r - l > 0 && s[l] == s[r]) {
            l++;
            r--;
        }

        string tmp = s.substr(l, r - l + 1);
        string t1 = z_function(tmp);
        reverse(all(tmp));
        string t2 = z_function(tmp);
        reverse(all(t2));
        if (t1.size() > t2.size()) {
            cout << s.substr(0, l) << t1 << s.substr(r + 1) << '\n';
        } else {
            cout << s.substr(0, l) << t2 << s.substr(r + 1) << '\n';
        }
    }
    return 0;
}


#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;*/
#define itn int
#define all(c) c.begin(),c.end()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define ll long long
#define ull unsigned long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
using namespace std;

const ll N = 2e5 + 5, inf = 1e18, mod = 998244353;
const double PI = 3.1415926536;
int days[13] = {0, 31, 28, 31, 30, 31,30, 31, 31, 30, 31, 30, 31};

template<typename T>
ostream& operator<< (ostream& out, const vector<T>& mas) {
    for (const auto& x : mas) {
        out << x << ' ';
    }
    return out;
}

bool is_prime(ll n) {
    for (ll i = 2; i * i <= n; ++i) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

inline ll getPow(ll a, ll b) {
    ll res = 1ll, tp = a;
    while (b){
        if (b&1ll) {
            res *= tp;
        }
        tp *= tp;
        tp %= mod; res %= mod;
        b >>= 1ll;
    }
    return res;
}

ll vec_mult(const pll & t1, const pll & t2, const pll & t3) {
    const ll & x1 = t1.first, y1 = t1.second;
    const ll & x2 = t2.first, y2 = t2.second;
    const ll & x3 = t3.first, y3 = t3.second;
    return abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1));
}

void ok() {
    cout << "YES" << '\n';
}

void no() {
    cout << "NO" << '\n';
}

inline ll nxt() {
    ll x;
    cin >> x;
    return x;
}

string z_function(const string& s) {
    int n = s.size();
    auto t = s;
    string str = t;
    reverse(all(t));
    str += '#';
    str += t;
    int l = 0, r = 0;
    vector<int> z(n * 2 + 1);
    int ans = 0;
    for (int i = n + 1; i < str.size(); i++) {
        if (r >= i) {
            z[i] = min(z[i - l], r - i + 1);
        }
        while (i + z[i] < str.size() && str[z[i]] == str[i + z[i]]) {
            z[i]++;
        }
        if (i + z[i] - 1 > r) {
            l = i;
            r = i + z[i] - 1;
        }
        if (z[i] + i == str.size()) {
            ans = max(ans, z[i]);
            ans = min(ans, n);
            return s.substr(0, ans);
        }
    }
    return string();
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int t = nxt();
    while (t--) {
        string s;
        cin >> s;
        int n = s.size();
        int l = 0, r = n - 1;
        while (r - l > 0 && s[l] == s[r]) {
            l++;
            r--;
        }

        string tmp = s.substr(l, r - l + 1);
        string t1 = z_function(tmp);
        reverse(all(tmp));
        string t2 = z_function(tmp);
        reverse(all(t2));
        if (t1.size() > t2.size()) {
            cout << s.substr(0, l) << t1 << s.substr(r + 1) << '\n';
        } else {
            cout << s.substr(0, l) << t2 << s.substr(r + 1) << '\n';
        }
    }
    return 0;
}


#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;*/
#define itn int
#define all(c) c.begin(),c.end()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define ll long long
#define ull unsigned long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
using namespace std;

const ll N = 2e5 + 5, inf = 1e18, mod = 998244353;
const double PI = 3.1415926536;
int days[13] = {0, 31, 28, 31, 30, 31,30, 31, 31, 30, 31, 30, 31};

template<typename T>
ostream& operator<< (ostream& out, const vector<T>& mas) {
    for (const auto& x : mas) {
        out << x << ' ';
    }
    return out;
}

bool is_prime(ll n) {
    for (ll i = 2; i * i <= n; ++i) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

inline ll getPow(ll a, ll b) {
    ll res = 1ll, tp = a;
    while (b){
        if (b&1ll) {
            res *= tp;
        }
        tp *= tp;
        tp %= mod; res %= mod;
        b >>= 1ll;
    }
    return res;
}

ll vec_mult(const pll & t1, const pll & t2, const pll & t3) {
    const ll & x1 = t1.first, y1 = t1.second;
    const ll & x2 = t2.first, y2 = t2.second;
    const ll & x3 = t3.first, y3 = t3.second;
    return abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1));
}

void ok() {
    cout << "YES" << '\n';
}

void no() {
    cout << "NO" << '\n';
}

inline ll nxt() {
    ll x;
    cin >> x;
    return x;
}

string z_function(string& s) {
    int n = s.size();
    string str = s;
    reverse(all(s));
    str += '#';
    str += s;
    int l = 0, r = 0;
    vector<int> z(n * 2 + 1);
    int ans = 0;
    for (int i = n + 1; i < str.size(); i++) {
        if (r >= i) {
            z[i] = min(z[i - l], r - i + 1);
        }
        while (i + z[i] < str.size() && str[z[i]] == str[i + z[i]]) {
            z[i]++;
        }
        if (i + z[i] - 1 > r) {
            l = i;
            r = i + z[i] - 1;
        }
        if (z[i] + i == str.size()) {
            ans = max(ans, z[i]);
        }
    }
    ans = min(ans, n);

    reverse(all(s));
    return s.substr(0, ans);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t = nxt();
    while (t--) {
        string s;
        cin >> s;
        int n = s.size();
        int l = 0, r = n - 1;
        while (r - l > 0 && s[l] == s[r]) {
            l++;
            r--;
        }

        string tmp = s.substr(l, r - l + 1);
        string t1 = z_function(tmp);
        reverse(all(tmp));
        string t2 = z_function(tmp);
        reverse(all(t2));
        if (t1.size() > t2.size()) {
            cout << s.substr(0, l) << t1 << s.substr(r + 1) << '\n';
        } else {
            cout << s.substr(0, l) << t2 << s.substr(r + 1) << '\n';
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define ld long double
#define mod 1000000007
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--)
    {
      int f=1;
      ll i,res,j;
      string a,temp,s,p="",ans1="",ans2="",v="";
      cin>>a;
      ll n1=a.size();
      for(i=0;i<((n1+1)/2);i++)
      {
          if(a[i]==a[n1-i-1])
          {
              ans1=ans1+a[i];
              if(i!=(n1/2))
              ans2=ans2+a[n1-i-1];
          }
          else
          {
              f=0;
              break;
          }
      }
      reverse(ans2.begin(),ans2.end());
      if(f)
      {
          if(n1==1)
            cout<<a<< "\n";
          else
          {
              ll l=n1/2;
          cout<<a.substr(0,l);
          cout<<a.substr(l,n1-l)<< "\n";
          }

     }
      else
      {
       cout<<ans1;
      s=a.substr(i,n1-2*i);
      temp=s;
      reverse(s.begin(),s.end());
      p=temp+'?'+s;
      ll n=p.size();
      ll lcs[n],len=0,i=1;
      lcs[0]=0;
      while(i<n)
      {
          if(p[len]==p[i])
          {
              len++;
              lcs[i]=len;
              i++;
          }
          else
          {
              if(len==0)
              {
                  lcs[i]=0;
                  i++;
              }
              else
              {
                  len=lcs[len-1];
              }
          }
      }
      v=s+'?'+temp;
      res=lcs[n-1];
      n=v.size();
      i=1;
      len=0;
      lcs[0]=0;
      while(i<n)
      {
          if(v[len]==v[i])
          {
              len++;
              lcs[i]=len;
              i++;
          }
          else
          {
              if(len==0)
              {
                  lcs[i]=0;
                  i++;
              }
              else
              {
                  len=lcs[len-1];
              }
          }
      }
      //cout<<p<< " "<<v<< "\n";
      if(res>lcs[n-1])
      cout<< p.substr(0,res);
      else
      cout<< v.substr(0,lcs[n-1]);
       cout<<ans2<< "\n";
       a.clear();
       s.clear();
      }

    }
    return 0;
}


#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define vll vector<ll>
#define pll pair<ll,ll>
#define mapll map<ll,ll>
#define endl "\n"
#define all(v) v.begin(),v.end()
using namespace std;
void swap(ll*,ll*);
void fillLps(ll lps[],string s)
{
    ll n=s.length();
    ll i=1;
    ll len=lps[0];
    while(i<n)
    {
        if(s[len]==s[i]) { lps[i]=++len; i++;}
        else
        {
            if(len==0) {lps[i]=0; i++;}
            else len=lps[len-1];
        }
    }
}
ll longestPalindromicPrefix(string s)
{
    string temp=s+'?'; reverse(all(s)); temp+=s; s=temp;
    ll n=s.length();
    ll lps[n]={0};
    ll i=0;
    fillLps(lps,s);
    return lps[n-1];
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    ll testcase; cin>>testcase;
    while(testcase--)
    {
        string s; cin>>s; ll n=s.length();
        ll i; char special; bool flag=0;
        string temp; for( i=0;i<n;i++)
        {
            if(i>n-1-i) break;
            if(i==n-1-i)  { flag=1; special=s[i];}
            else if(s[i]==s[n-1-i]) temp+=s[i];
            else break;
        }
        if(i>n-1-i) { if(temp.length()) {cout<<temp; reverse(all(temp));} if(flag) cout<<special; if(temp.length()) cout<<temp; cout<<endl; continue;}
        string str=s.substr(i,n-2*i);
        ll l1=longestPalindromicPrefix(str);
        reverse(all(str));
        ll l2=longestPalindromicPrefix(str);
        cout<<temp;
        if(l1>=l2) { string ss=s.substr(i,l1); cout<<ss; reverse(all(temp)); cout<<temp;}
        else { string ss=s.substr(n-1-i-l2+1,l2); cout<<ss; reverse(all(temp)); cout<<temp;}
        cout<<endl;
    }
}
void swap(ll &a,ll &b)
{
    a=a^b; b=a^b; a=a^b;
}


#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define ld long double
#define mod 1000000007
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--)
    {
      int f=1;
      ll i,res,j;
      string a,temp,s,p="",ans1="",ans2="",v="";
      cin>>a;
      ll n1=a.size();
      for(i=0;i<((n1+1)/2);i++)
      {
          if(a[i]==a[n1-i-1])
          {
              ans1+=a[i];
              if(i!=(n1/2))
              ans2+=a[n1-i-1];
          }
          else
          {
              f=0;
              break;
          }
      }
      if(f)
      {
          if(n1==1)
            cout<<a<< "\n";
          else
          {
              ll l=n1/2;
          cout<<a.substr(0,l);
          cout<<a.substr(l,n1-l)<< "\n";
          }

     }
      else
      {
          reverse(ans2.begin(),ans2.end());
       cout<<ans1;
      s=a.substr(i,n1-2*i);
      //cout<<i<<" "<<s<< "\n";
      temp=s;
      reverse(s.begin(),s.end());
      p=temp+'?'+s;
      ll n=p.size();
      ll lcs[n],len=0; i=1;
      lcs[0]=0;
      //cout<<p<< "\n";
      while(i<n)
      {
          if(p[len]==p[i])
          {
              len++;
              lcs[i]=len;
              i++;
          }
          else
          {
              if(len==0)
              {
                  lcs[i]=0;
                  i++;
              }
              else
              {
                  len=lcs[len-1];
              }
          }
      }
      v=s+'?'+temp;
      res=lcs[n-1];
      n=v.size();
      i=1;
      len=0;
      lcs[0]=0;
      while(i<n)
      {
          if(v[len]==v[i])
          {
              len++;
              lcs[i]=len;
              i++;
          }
          else
          {
              if(len==0)
              {
                  lcs[i]=0;
                  i++;
              }
              else
              {
                  len=lcs[len-1];
              }
          }
      }
      //cout<<p<< " "<<v<< "\n";
      if(res>lcs[n-1])
      cout<< p.substr(0,res);
      else
      cout<< v.substr(0,lcs[n-1]);
       cout<<ans2<< "\n";
       a.clear();
       s.clear();
      }

    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define ld long double
#define mod 1000000007
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--)
    {
      int f=1;
      ll i,res,j;
      string a,temp,s,p="",ans1="",ans2="";
      cin>>a;
      ll n1=a.size();
      for(i=0;i<((n1+1)/2);i++)
      {
          if(a[i]==a[n1-i-1])
          {
              ans1=ans1+a[i];
              if(i!=(n1/2))
              ans2=ans2+a[n1-i-1];
          }
          else
          {
              f=0;
              break;
          }
      }
      if(f) cout<<a<<"\n";
      else
      {
          reverse(ans2.begin(),ans2.end());
      s=a.substr(i,n1-2*i);
      temp=s;
      reverse(s.begin(),s.end());
      p=temp+'?'+s;
      ll n=p.size();
      ll lcs[n]; ll len=0; i=1;
      lcs[0]=0;
      while(i<n)
      {
          if(p[len]==p[i])
          {
              len++;
              lcs[i]=len;
              i++;
          }
          else
          {
              if(len==0)
              {
                  lcs[i]=0;
                  i++;
              }
              else
              {
                  len=lcs[len-1];
              }
          }
      }
      p=s+'?'+temp;
      res=lcs[n-1];
      i=1;
      len=0;
      while(i<n)
      {
          if(p[len]==p[i])
          {
              len++;
              lcs[i]=len;
              i++;
          }
          else
          {
              if(len==0)
              {
                  lcs[i]=0;
                  i++;
              }
              else
              {
                  len=lcs[len-1];
              }
          }
      }
       cout<<ans1;
      if(res>lcs[n-1])
      cout<< p.substr(0,res);
      else
      {
          cout<<p.substr(0,lcs[n-1]);
      }
       cout<<ans2<< "\n";
      }

    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define ld long double
#define mod 1000000007
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--)
    {
      int f=1;
      ll i,res,j;
      string a,temp,s,p,ans1,ans2,v;
      cin>>a;
      ll n1=a.size();
      for(i=0;i<((n1+1)/2);i++)
      {
          if(a[i]==a[n1-i-1])
          {
              ans1+=a[i];
              if(i!=(n1/2))
              ans2+=a[n1-i-1];
          }
          else
          {
              f=0;
              break;
          }
      }
      if(f) cout<<a<<"\n";
      else
      {
          reverse(ans2.begin(),ans2.end());
       cout<<ans1;
      s=a.substr(i,n1-2*i);
      temp=s;
      reverse(s.begin(),s.end());
      p=temp+'?'+s;
      ll n=p.size();
      ll lcs[n],len=0; i=1;
      lcs[0]=0;
      while(i<n)
      {
          if(p[len]==p[i])
          {
              len++;
              lcs[i]=len;
              i++;
          }
          else
          {
              if(len==0)
              {
                  lcs[i]=0;
                  i++;
              }
              else
              {
                  len=lcs[len-1];
              }
          }
      }
      v=s+'?'+temp;
      res=lcs[n-1];
      i=1;
      len=0;
      while(i<n)
      {
          if(v[len]==v[i])
          {
              len++;
              lcs[i]=len;
              i++;
          }
          else
          {
              if(len==0)
              {
                  lcs[i]=0;
                  i++;
              }
              else
              {
                  len=lcs[len-1];
              }
          }
      }
      //cout<<p<< " "<<v<< "\n";
      if(res>lcs[n-1])
      cout<< p.substr(0,res);
      else
      cout<< v.substr(0,lcs[n-1]);
       cout<<ans2<< "\n";
      }

    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define ld long double
#define mod 1000000007
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--)
    {
      int f=1;
      ll i,res,j;
      string a,temp,s,p="",ans1="",ans2="",v="";
      cin>>a;
      ll n1=a.size();
      for(i=0;i<((n1+1)/2);i++)
      {
          if(a[i]==a[n1-i-1])
          {
              ans1=ans1+a[i];
              if(i!=(n1/2))
              ans2=ans2+a[n1-i-1];
          }
          else
          {
              f=0;
              break;
          }
      }
      reverse(ans2.begin(),ans2.end());
      if(f)
      {
          cout<<a<<endl;
     }
      else
      {
       cout<<ans1;
      s=a.substr(i,n1-2*i);
      temp=s;
      reverse(s.begin(),s.end());
      p=temp+'?'+s;
      ll n=p.size();
      ll lcs[n],len=0,i=1;
      lcs[0]=0;
      while(i<n)
      {
          if(p[len]==p[i])
          {
              len++;
              lcs[i]=len;
              i++;
          }
          else
          {
              if(len==0)
              {
                  lcs[i]=0;
                  i++;
              }
              else
              {
                  len=lcs[len-1];
              }
          }
      }
      v=s+'?'+temp;
      res=lcs[n-1];
      n=v.size();
      i=1;
      len=0;
      lcs[0]=0;
      while(i<n)
      {
          if(v[len]==v[i])
          {
              len++;
              lcs[i]=len;
              i++;
          }
          else
          {
              if(len==0)
              {
                  lcs[i]=0;
                  i++;
              }
              else
              {
                  len=lcs[len-1];
              }
          }
      }
      //cout<<p<< " "<<v<< "\n";
      if(res>lcs[n-1])
      cout<< p.substr(0,res);
      else
      cout<< v.substr(0,lcs[n-1]);
       cout<<ans2<< "\n";
       a.clear();
       s.clear();
      }

    }
    return 0;
}


#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define vll vector<ll>
#define pll pair<ll,ll>
#define mapll map<ll,ll>
#define endl "\n"
#define all(v) v.begin(),v.end()
using namespace std;
void swap(ll*,ll*);
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    ll testcase; cin>>testcase;
    while(testcase--)
    {
        string s; cin>>s; ll n=s.length();
        ll i; char special; bool flag=0;
        string temp; for( i=0;i<n;i++)
        {
            if(i>n-1-i) break;
            if(i==n-1-i)  { flag=1; special=s[i];}
            else if(s[i]==s[n-1-i]) temp+=s[i];
            else break;
        }
        if(i>n-1-i) { cout<<temp; reverse(all(temp)); if(flag) cout<<special; cout<<temp; cout<<endl; continue;}
        ll l=i; ll r=i;
        for(ll j=n-1-i;j>=i;j--)
        {
            if(l>j) break;
            if(s[j]==s[l]) { l++; r=max(r,j); }
            else{ if(l!=i) j++; l=i; r=i; }
        }
        ll l1=(r-i+1);
        l=n-1-i; r=n-1-i;
        for(ll j=i;j<=n-1-i;j++)
        {
            if(l<j) break;
            if(s[j]==s[l]) { l--; r=min(r,j);}
            else { if(l!=n-1-i) j--; l=n-1-i; r=n-1-i;}
        }
        ll l2=(n-1-i-r+1);
        cout<<temp;
        if(l1>=l2) { string ss=s.substr(i,l1); cout<<ss; reverse(all(temp)); cout<<temp;}
        else { string ss=s.substr(n-1-i-l2+1,l2); cout<<ss; reverse(all(temp)); cout<<temp;}
        cout<<endl;
    }
}
void swap(ll &a,ll &b)
{
    a=a^b; b=a^b; a=a^b;
}


#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define ld long double
#define mod 1000000007
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--)
    {
      int f=1;
      ll i,res,j;
      string a,temp,s,p="",ans1="",ans2="",v="";
      cin>>a;
      ll n1=a.size();
      for(i=0;i<((n1+1)/2);i++)
      {
          if(a[i]==a[n1-i-1])
          {
              ans1=ans1+a[i];
              if(i!=(n1/2))
              ans2=ans2+a[n1-i-1];
          }
          else
          {
              f=0;
              break;
          }
      }
      if(f)
      {
          if(n1==1)
            cout<<a<< "\n";
          else
          {
              ll l=n1/2;
          cout<<a.substr(0,l);
          cout<<a.substr(l,n1-l)<< "\n";
          }

     }
      else
      {
          reverse(ans2.begin(),ans2.end());
       cout<<ans1;
      s=a.substr(i,n1-2*i);
      //cout<<i<<" "<<s<< "\n";
      temp=s;
      reverse(s.begin(),s.end());
      p=temp+'?'+s;
      ll n=p.size();
      ll lcs[n],len=0; i=1;
      lcs[0]=0;
      //cout<<p<< "\n";
      while(i<n)
      {
          if(p[len]==p[i])
          {
              len++;
              lcs[i]=len;
              i++;
          }
          else
          {
              if(len==0)
              {
                  lcs[i]=0;
                  i++;
              }
              else
              {
                  len=lcs[len-1];
              }
          }
      }
      v=s+'?'+temp;
      res=lcs[n-1];

      i=1;
      len=0;

      while(i<n)
      {
          if(v[len]==v[i])
          {
              len++;
              lcs[i]=len;
              i++;
          }
          else
          {
              if(len==0)
              {
                  lcs[i]=0;
                  i++;
              }
              else
              {
                  len=lcs[len-1];
              }
          }
      }
      //cout<<p<< " "<<v<< "\n";
      if(res>lcs[n-1])
      cout<< p.substr(0,res);
      else
      cout<< v.substr(0,lcs[n-1]);
       cout<<ans2<< "\n";
       a.clear();
       s.clear();
      }

    }
    return 0;
}


#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define vll vector<ll>
#define pll pair<ll,ll>
#define mapll map<ll,ll>
#define endl "\n"
#define all(v) v.begin(),v.end()
using namespace std;
void swap(ll*,ll*);
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    ll testcase; cin>>testcase;
    while(testcase--)
    {
        string s; cin>>s; ll n=s.length();
        ll i; char special;
        string temp; for( i=0;i<n;i++)
        {
            if(i>n-1-i) break;
            if(i==n-1-i) special=s[i];
            else if(s[i]==s[n-1-i]) temp+=s[i];
            else break;
        }
        if(i>n-1-i) { cout<<temp; reverse(all(temp)); cout<<special; cout<<temp; cout<<endl; continue;}
        ll l=i; ll r=i;
        for(ll j=n-1-i;j>=i;j--)
        {
            if(l>j) break;
            if(s[j]==s[l]) { l++; r=max(r,j); }
            else{ if(l!=i) j++; l=i; r=i; }
        }
        ll l1=(r-i+1);
        l=n-1-i; r=n-1-i;
        for(ll j=i;j<=n-1-i;j++)
        {
            if(l<j) break;
            if(s[j]==s[l]) { l--; r=min(r,j);}
            else { if(l!=n-1-i) j--; l=n-1-i; r=n-1-i;}
        }
        ll l2=(n-1-i-r+1);
        cout<<temp;
        if(l1>=l2) { string ss=s.substr(i,l1); cout<<ss; reverse(all(temp)); cout<<temp;}
        else { string ss=s.substr(n-1-i-l2+1,l2); cout<<ss; reverse(all(temp)); cout<<temp;}
        cout<<endl;
    }
}
void swap(ll &a,ll &b)
{
    a=a^b; b=a^b; a=a^b;
}


#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define ld long double
#define mod 1000000007
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--)
    {
      int f=1;
      ll i,res,j;
      string a,temp,s,p="",ans1="",ans2="",v="";
      cin>>a;
      ll n1=a.size();
      for(i=0;i<((n1+1)/2);i++)
      {
          if(a[i]==a[n1-i-1])
          {
              ans1=ans1+a[i];
              if(i!=(n1/2))
              ans2=ans2+a[n1-i-1];
          }
          else
          {
              f=0;
              break;
          }
      }
      if(f)
      {
          if(n1==1)
            cout<<a<< "\n";
          else
          {
              ll l=n1/2;
          cout<<a.substr(0,l);
          cout<<a.substr(l,n1-l)<< "\n";
          }

     }
      else
      {
          reverse(ans2.begin(),ans2.end());
       cout<<ans1;
      s=a.substr(i,n1-2*i);
      //cout<<i<<" "<<s<< "\n";
      temp=s;
      reverse(s.begin(),s.end());
      p=temp+'?'+s;
      ll n=p.size();
      ll lcs[n],len=0; i=1;
      lcs[0]=0;
      //cout<<p<< "\n";
      while(i<n)
      {
          if(p[len]==p[i])
          {
              len++;
              lcs[i]=len;
              i++;
          }
          else
          {
              if(len==0)
              {
                  lcs[i]=0;
                  i++;
              }
              else
              {
                  len=lcs[len-1];
              }
          }
      }
      v=s+'?'+temp;
      res=lcs[n-1];
      n=v.size();
      i=1;
      len=0;
      lcs[0]=0;
      while(i<n)
      {
          if(v[len]==v[i])
          {
              len++;
              lcs[i]=len;
              i++;
          }
          else
          {
              if(len==0)
              {
                  lcs[i]=0;
                  i++;
              }
              else
              {
                  len=lcs[len-1];
              }
          }
      }
      //cout<<p<< " "<<v<< "\n";
      if(res>lcs[n-1])
      cout<< p.substr(0,res);
      else
      cout<< v.substr(0,lcs[n-1]);
       cout<<ans2<< "\n";
       a.clear();
       s.clear();
      }

    }
    return 0;
}


#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define vll vector<ll>
#define pll pair<ll,ll>
#define mapll map<ll,ll>
#define endl "\n"
#define all(v) v.begin(),v.end()
using namespace std;
void swap(ll*,ll*);
void fillLps(ll lps[],string s)
{
    ll n=s.length();
    ll i=1;
    ll len=lps[0];
    while(i<n)
    {
        if(s[len]==s[i]) { lps[i]=++len; i++;}
        else
        {
            if(len==0) {lps[i]=0; i++;}
            else len=lps[len-1];
        }
    }
}
ll longestPalindromicPrefix(string s)
{
    string temp=s+'?'; reverse(all(s)); temp+=s; s=temp;
    ll n=s.length();
    ll lps[n]={0};
    ll i=0;
    fillLps(lps,s);
    ll m=0; for(ll i=0;i<n;i++) m=max(m,lps[i]); return m;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    ll testcase; cin>>testcase;
    while(testcase--)
    {
        string s; cin>>s; ll n=s.length();
        ll i; char special; bool flag=0;
        string temp; for( i=0;i<n;i++)
        {
            if(i>n-1-i) break;
            if(i==n-1-i)  { flag=1; special=s[i];}
            else if(s[i]==s[n-1-i]) temp+=s[i];
            else break;
        }
        if(i>n-1-i) { if(temp.length()) {cout<<temp; reverse(all(temp));} if(flag) cout<<special; if(temp.length()) cout<<temp; cout<<endl; continue;}
        string str=s.substr(i,n-2*i);
        ll l1=longestPalindromicPrefix(str);
        reverse(all(str));
        ll l2=longestPalindromicPrefix(str);
        cout<<temp;
        if(l1>=l2) { string ss=s.substr(i,l1); cout<<ss; reverse(all(temp)); cout<<temp;}
        else { string ss=s.substr(n-1-i-l2+1,l2); cout<<ss; reverse(all(temp)); cout<<temp;}
        cout<<endl;
    }
}
void swap(ll &a,ll &b)
{
    a=a^b; b=a^b; a=a^b;
}


#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define vll vector<ll>
#define pll pair<ll,ll>
#define mapll map<ll,ll>
#define endl "\n"
#define all(v) v.begin(),v.end()
using namespace std;
void swap(ll*,ll*);
void fillLps(ll lps[],string s)
{
    ll n=s.length();
    ll i=1;
    ll len=lps[0];
    while(i<n)
    {
        if(s[len]==s[i]) { lps[i]=++len; i++;}
        else
        {
            if(len==0) {lps[i]=0; i++;}
            else len=lps[len-1];
        }
    }
}
ll longestPalindromicPrefix(string s)
{
    string temp=s+'?'; reverse(all(s)); temp+=s; s=temp;
    ll n=s.length();
    ll lps[n]={0};
    ll i=0;
    fillLps(lps,s);
    ll m=0; for(ll i=0;i<n;i++) m=max(m,lps[i]); return m;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    ll testcase; cin>>testcase;
    while(testcase--)
    {
        string s; cin>>s; ll n=s.length();
        ll i; char special; bool flag=0;
        string temp; for( i=0;i<n;i++)
        {
            if(i>n-1-i) break;
            if(i==n-1-i)  { flag=1; special=s[i];}
            else if(s[i]==s[n-1-i]) temp+=s[i];
            else break;
        }
        if(i>n-1-i) { cout<<temp; reverse(all(temp)); if(flag) cout<<special; cout<<temp; cout<<endl; continue;}
        string str=s.substr(i,n-2*i);
        ll l1=longestPalindromicPrefix(str);
        reverse(all(str));
        ll l2=longestPalindromicPrefix(str);
        cout<<temp;
        if(l1>=l2) { string ss=s.substr(i,l1); cout<<ss; reverse(all(temp)); cout<<temp;}
        else { string ss=s.substr(n-1-i-l2+1,l2); cout<<ss; reverse(all(temp)); cout<<temp;}
        cout<<endl;
    }
}
void swap(ll &a,ll &b)
{
    a=a^b; b=a^b; a=a^b;
}


#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define ld long double
#define mod 1000000007
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--)
    {
      int f=1;
      ll i,res,j;
      string a,temp,s,p="",ans1="",ans2="",v="";
      cin>>a;
      ll n1=a.size();
      for(i=0;i<((n1+1)/2);i++)
      {
          if(a[i]==a[n1-i-1])
          {
              ans1=ans1+a[i];
              if(i!=(n1/2))
              ans2=ans2+a[n1-i-1];
          }
          else
          {
              f=0;
              break;
          }
      }
      if(f)
      {
          if(n1==1)
            cout<<a<< "\n";
          else
          {
              ll l=n1/2;
          cout<<a.substr(0,l);
          cout<<a.substr(l,n1-l)<< "\n";
          }

     }
      else
      {
          reverse(ans2.begin(),ans2.end());
       cout<<ans1;
      s=a.substr(i,n1-2*i);
      //cout<<i<<" "<<s<< "\n";
      temp=s;
      reverse(s.begin(),s.end());
      p=temp+'?'+s;
      ll n=p.size();
      ll lcs[n],len=0,i=1;
      lcs[0]=0;
      //cout<<p<< "\n";
      while(i<n)
      {
          if(p[len]==p[i])
          {
              len++;
              lcs[i]=len;
              i++;
          }
          else
          {
              if(len==0)
              {
                  lcs[i]=0;
                  i++;
              }
              else
              {
                  len=lcs[len-1];
              }
          }
      }
      v=s+'?'+temp;
      res=lcs[n-1];
      n=v.size();
      i=1;
      len=0;
      lcs[0]=0;
      while(i<n)
      {
          if(v[len]==v[i])
          {
              len++;
              lcs[i]=len;
              i++;
          }
          else
          {
              if(len==0)
              {
                  lcs[i]=0;
                  i++;
              }
              else
              {
                  len=lcs[len-1];
              }
          }
      }
      //cout<<p<< " "<<v<< "\n";
      if(res>lcs[n-1])
      cout<< p.substr(0,res);
      else
      cout<< v.substr(0,lcs[n-1]);
       cout<<ans2<< "\n";
       a.clear();
       s.clear();
      }

    }
    return 0;
}


#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define vll vector<ll>
#define pll pair<ll,ll>
#define mapll map<ll,ll>
#define endl "\n"
#define all(v) v.begin(),v.end()
using namespace std;
string reverse(string s)
{
    reverse(all(s));return s;
}
bool isPalindrome(string s)
{
    ll n=s.length();
    if(n==0) return false; if(n==1) return true;
    string s1=s.substr(0,n/2);
    string s2=s.substr((n+1)/2);
    return (s1==reverse(s2));
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    ll t; cin>>t;
    while(t--)
    {
        string s; cin>>s;
        ll n=s.length(); string s1,s2; ll i;
        for( i=0;i<n-1-i;i++)
        {
            if(s[i]==s[n-1-i]) s1+=s[i];
            else break;
        }
        string temp;
        for(ll j=i;j<=n-1-i;j++)
        {
            temp+=s[j]; if(isPalindrome(temp)) s2=temp;
        }
        temp.clear(); string s3;
        for(ll j=n-1-i;j>=i;j--)
        {
            temp+=s[j]; if(isPalindrome(temp)) s3=temp;
        }
        if(s3.length()>s2.length()) s2=s3;
        cout<<s1; cout<<s2; cout<<reverse(s1)<<endl;
    }
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int prefix(const string &s)
{
            int pi[100001];
            pi[0]=0;
            for(int r=1;r<s.length();r++)
            {
                        int len=pi[r-1];
                        while(len>0&&s[r]!=s[len])
                        {
                                    len=pi[len-1];
                        }
                        if(s[len]==s[r])
                        {
                                    len++;
                        }
                        pi[r]=len;
            }
            return pi[s.length()-1];
}
int main()
{
            int n;
            cin>>n;
            while(n--)
            {
                        string s;
                        cin>>s;
                        int nl=s.length();
                        int j=nl-1;
                        int i=s.length();
                        for(int r=0;r<nl/2;r++)
                        {
                                    if(s[r]!=s[j])
                                    {
                                                i=r;
                                                break;
                                    }
                                    j--;
                        }
                        if(i==s.length())
                        {
                            cout<<s<<endl;
                            continue;
                        }
                        string pre=s.substr(0,i);
                        string suf=s.substr(j+1,i);
                        string revmid=s.substr(i,nl-(2*i));
                        string mid=revmid;
                        reverse(revmid.begin(),revmid.end());
                        int ans1=prefix(mid+"#"+revmid);
                        int ans2=prefix(revmid+"#"+mid);
                        if(ans1>ans2)
                        {
                                    cout<<pre<<s.substr(i,ans1)<<suf<<endl;
                        }
                        else
                        {
                                    cout<<pre<<s.substr(j-ans2+1,ans2)<<suf<<endl;

                        }

            }
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
string check(const string& t)
{
	c=0;
	string a=t;
	reverse(a.begin(),a.end());
	a=t+'$'+a;

	for(int i=1;i<(int)a.size();i++)
	{
		while(c and a[i]!=a[c])
			c=pref[c-1];
		if(a[c]==a[i])
			c++;
		pref[i]=c;
	}
	return c;
}
int main()
{
            int n;
            cin>>n;
            while(n--)
            {
                string s;
                cin>>s;
                int nl=s.length();
                int j=nl-1;
                int i=s.length();
                for(int r=0;r<nl/2;r++)
                {
	                if(s[r]!=s[j])
	                {
	                    i=r;
	                    break;
	                }
	                j--;
                }
                if(i==s.length())
                {
                    cout<<s<<endl;
                    continue;
                }
                string pre=s.substr(0,i);
                string suf=s.substr(j+1,i);
                string revmid=s.substr(i,nl-(2*i));
                string mid=revmid;
                reverse(revmid.begin(),revmid.end());
                int ans1=prefix(mid+"#"+revmid);
                int ans2=prefix(revmid+"#"+mid);
                if(ans1>ans2)
                {
                    cout<<pre<<s.substr(i,ans1)<<suf<<endl;
                }
                else
                {
                    cout<<pre<<s.substr(j-ans2+1,ans2)<<suf<<endl;

                }

            }
}


#include<bits/stdc++.h>
using namespace std;
#define ios ios_base::sync_with_stdio(0);cin.tie(0)
#define scn(n) scanf("%d",&n)
#define lscn(n) scanf("%lld",&n)
typedef long long ll;
#define pri(n) printf("%d\n",n)
#define lpri(n) printf("%lld\n",n);
#define rep(i,st,ed) for(int i=st;i<ed;i++)
#define F first
#define S second
#define pb(n) push_back(n)
const int N=1e5+5;
const ll M=1e9+7;

string s;

int main()
{
	ios;
	int T=1;
	// scn(T);
	cin>>T;
	while(T--)
	{
		// string s;
		cin>>s;

		reverse(ss.begin(),ss.end());
		if(ss==s)
		{
			cout<<s<<"\n";
			continue;
		}
		int sz=s.size();
		string pre="";
		int pos1=0,pos2=sz-1;

		while(pos1<pos2)
		{
			if(s[pos1]==s[pos2])
			{
				pre+=s[pos1];
				pos1++;
				pos2--;
			}
			else
				break;
		}
		int len=pre.size();

		string rem=s.substr(len,sz-2*len);

		// cout<<rem<<"\n";

		string suf=pre;
		reverse(suf.begin(),suf.end());

		for(int i=(int)rem.size();i>0;i--)
		{
			string w=rem.substr(0,i);
			string rev=w;
			// cout<<"checking  "<<w<<"\n";
			reverse(w.begin(),w.end());
			if(w==rev)
			{
				cout<<pre<<w<<suf<<"\n";
				break;
			}

			int st=(int)rem.size()-i;

			w=rem.substr(st,i);
			rev=w;

			// cout<<"checking  "<<w<<"\n";
			reverse(w.begin(),w.end());
			if(w==rev)
			{
				cout<<pre<<w<<suf<<"\n";
				break;
			}

		}
	}

}


#include<bits/stdc++.h>
using namespace std;
#define ios ios_base::sync_with_stdio(0);cin.tie(0)
#define scn(n) scanf("%d",&n)
#define lscn(n) scanf("%lld",&n)
typedef long long ll;
#define pri(n) printf("%d\n",n)
#define lpri(n) printf("%lld\n",n);
#define rep(i,st,ed) for(int i=st;i<ed;i++)
#define F first
#define S second
#define pb(n) push_back(n)
const int N=2e5+6;
const ll M=1e9+7;

int main()
{
	int T=1;
	scn(T);
	while(T--)
	{
		string s;
		cin>>s;

		string ss=s;

		reverse(ss.begin(),ss.end());
		if(ss==s)
		{
			cout<<s<<"\n";
			continue;
		}

		string pre="";
		int pos1=0,pos2=(int)s.size()-1;

		while(pos1<pos2)
		{
			if(s[pos1]==s[pos2])
			{
				pre+=s[pos1];
				pos1++;
				pos2--;
			}
			else
				break;
		}

		string rem=s.substr(pos1,pos2-pos1+1);

		// cout<<rem<<' ';
		string suf=pre;
		reverse(suf.begin(),suf.end());

		string ans=pre+s[pos1]+suf;
		for(int i=1;i<=(int)rem.size();i++)
		{
			string w=rem.substr(0,i);

			string rev=w;
			reverse(rev.begin(),rev.end());

			if(w==rev)
			{
				ans=pre+w+suf;
			}

			int st=(int)rem.size()-i;
			w=rem.substr(st,i);
			rev=w;
			reverse(rev.begin(),rev.end());

			if(w==rev)
			{
				ans=pre+w+suf;
			}

		}
		cout<<ans<<"\n";

	}

}


#include<bits/stdc++.h>
using namespace std;
#define ios ios_base::sync_with_stdio(0);cin.tie(0)
#define scn(n) scanf("%d",&n)
#define lscn(n) scanf("%lld",&n)
typedef long long ll;
#define pri(n) printf("%d\n",n)
#define lpri(n) printf("%lld\n",n);
#define rep(i,st,ed) for(int i=st;i<ed;i++)
#define F first
#define S second
#define pb(n) push_back(n)
const int N=1e6+5;
const ll M=1e9+7;

int pref[N],c;
string check(const string& t)
{
	c=0;
	string a=t;
	reverse(a.begin(),a.end());
	a=t+'$'+a;

	for(int i=1;i<(int)a.size();i++)
	{
		while(c and a[i]!=a[c])
			c=pref[c-1];
		if(a[c]==a[i])
			c++;
		pref[i]=c;
	}
	return t.substr(0,c);
}
int main()
{
	ios;
	int T;
	// scn(T);
	cin>>T;
	while(T--)
	{
		string s,add="";
		cin>>s;
		string r=s;
		reverse(r.begin(),r.end());
		if(s==r)
		{
			cout<<s<<"\n";
			continue;
		}
		int sz=s.size();
		int pos1=0,pos2=sz-1;

		while(pos1<pos2)
		{
			if(s[pos1]==s[pos2])
				pos1++,pos2--,add+=s[pos1-1];
			else break;
		}
		// string add=s.substr(0,pos1);
		// cout<<"add = "<<add<<"\n";
		string rem=s.substr(pos1,sz-2*(int)add.size());

		string s1=check(rem);
		reverse(rem.begin(),rem.end());
		string s2=check(rem);
		if((int)s1.size()<(int)s2.size())
			swap(s1,s2);

		cout<<add<<s1;
		reverse(add.begin(),add.end());
		cout<<add<<"\n";
	}
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int prefix(string s)
{
            static int pi[2000001]={0};
            pi[0]=0;
            for(int r=1;r<s.length();r++)
            {
                        int len=pi[r-1];
                        while(len>0&&s[r]!=s[len])
                        {
                                    len=pi[len-1];
                        }
                        if(s[len]==s[r])
                        {
                                    len++;
                        }
                        pi[r]=len;
            }
            return pi[s.length()-1];
}
int main()
{
            int n;
            cin>>n;
            while(n--)
            {
                        string s;
                        cin>>s;
                        int nl=s.length();
                        int j=nl-1;
                        int i=s.length();
                        for(int r=0;r<nl/2;r++)
                        {
                                    if(s[r]!=s[j])
                                    {
                                                i=r;
                                                break;
                                    }
                                    j--;
                        }
                        if(i==s.length())
                        {
                            cout<<s<<endl;
                            continue;
                        }
                        string pre=s.substr(0,i);
                        string suf=s.substr(j+1,i);
                        string revmid=s.substr(i,nl-(2*i));
                        string mid=revmid;
                        reverse(revmid.begin(),revmid.end());
                        int ans1=prefix(mid+"#"+revmid);
                        int ans2=prefix(revmid+"#"+mid);
                        if(ans1>ans2)
                        {
                                    cout<<pre<<s.substr(i,ans1)<<suf<<endl;
                        }
                        else
                        {
                                    cout<<pre<<s.substr(j-ans2+1,ans2)<<suf<<endl;

                        }

            }
}


#include<bits/stdc++.h>
using namespace std;
#define ios ios_base::sync_with_stdio(0);cin.tie(0)
#define scn(n) scanf("%d",&n)
#define lscn(n) scanf("%lld",&n)
typedef long long ll;
#define pri(n) printf("%d\n",n)
#define lpri(n) printf("%lld\n",n);
#define rep(i,st,ed) for(int i=st;i<ed;i++)
#define F first
#define S second
#define pb(n) push_back(n)
const int N=2e6+5;
const ll M=1e9+7;

int pref[N],c;
string check(const string& t)
{
	c=0;
	string a=t;
	reverse(a.begin(),a.end());
	a=t+'$'+a;

	for(int i=1;i<(int)a.size();i++)
	{
		while(c and a[i]!=a[c])
			c=pref[c-1];
		if(a[c]==a[i])
			c++;
		pref[i]=c;
	}
	return t.substr(0,c);
}
int main()
{
	ios;
	int T;
	// scn(T);
	cin>>T;
	while(T--)
	{
		string s,add="";
		cin>>s;
		string r=s;
		reverse(r.begin(),r.end());
		if(s==r)
		{
			cout<<s<<"\n";
			continue;
		}
		int sz=s.size();
		int pos1=0,pos2=sz-1;

		while(pos1<pos2)
		{
			if(s[pos1]==s[pos2])
				pos1++,pos2--,add+=s[pos1-1];
			else break;
		}
		// string add=s.substr(0,pos1);
		// cout<<"add = "<<add<<"\n";
		string rem=s.substr(pos1,sz-2*(int)add.size());

		string s1=check(rem);
		reverse(rem.begin(),rem.end());
		string s2=check(rem);
		if((int)s1.size()<(int)s2.size())
			swap(s1,s2);

		cout<<add<<s1;
		reverse(add.begin(),add.end());
		cout<<add<<"\n";
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, t, pos=-1;
string s;
bool us = 0;
int main() {
	cin >> n >> t;
	cin >> s;
	for (ll i = 0; i < s.size(); ++i){
		if (s[i]=='.'){
			us=1;
			continue;
		}
		if (!us){
			continue;
		}
		if (s[i] >= '5'){
			pos=i;
			break;
		}
	}
	if (pos==-1){
		cout << s;
		return 0;
	}
	for (ll i = pos-1; i >= 0&&t>0; --i, --pos, --t) {
		if (s[i]=='.'){
			ll ans=0;
			for (ll j = 0; s[j]!='.' && j< s.size(); ++j){
				ans+=s[j]-'0';
				ans*=10;
			}
			ans/=10;
			ans++;
			cout << ans;
			return 0;
		}
		s[i]++;
		if (s[i]=='5'){
			continue;
		}else{
			pos--;
			break;
		}
	}
	for (ll i = 0; i <= pos; ++i)
		cout << s[i];
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, t, pos=-1;
string s;
bool us = 0;
int main() {
	cin >> n >> t;
	cin >> s;
	for (ll i = 0; i < s.size(); ++i){
		if (s[i]=='.'){
			us=1;
			continue;
		}
		if (!us){
			continue;
		}
		if (s[i] >= '5'){
			pos=i;
			break;
		}
	}
	if (pos==-1){
		cout << s;
		return 0;
	}
	for (ll i = pos-1; i >= 0&&t>0; --i, --pos, --t) {
		if (s[i]=='.'){
			for (ll k=i-1; k >= 0; --k){
				if (s[k]=='9')
					s[k]='0';
					continue;
				s[k]++;
				break;
			}
			for (ll k = 0; k < i; ++k)
				cout << s[k];
			return 0;
		}
		s[i]++;
		if (s[i]=='5'){
			continue;
		}else{
			pos--;
			break;
		}
	}
	for (ll i = 0; i <= pos; ++i)
		cout << s[i];
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, t, pos=-1;
string s;
bool us = 0;
int main() {
	cin >> n >> t;
	cin >> s;
	for (ll i = 0; i < s.size(); ++i){
		if (s[i]=='.'){
			us=1;
			continue;
		}
		if (!us){
			continue;
		}
		if (s[i] >= '5'){
			pos=i;
			break;
		}
	}
	if (pos==-1){
		cout << s;
		return 0;
	}
	for (ll i = pos-1; i >= 0&&t>0; --i, --pos) {
		if (s[i]=='.'){
			ll ans=0;
			for (ll j = 0; s[j]!='.' && j< s.size(); ++j){
				ans+=s[j]-'0';
				ans*=10;
			}
			ans/=10;
			ans++;
			cout << ans;
			return 0;
		}
		s[i]++;
		t--;
		if (s[i]=='5'){
			continue;
		}
		break;
	}
	//cout << s << endl;
	for (ll i = 0; i < pos; ++i)
		cout << s[i];
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, t, pos=-1;
string s;
bool us = 0;
int main() {
	cin >> n >> t;
	cin >> s;
	for (ll i = 0; i < s.size(); ++i){
		if (s[i]=='.'){
			us=1;
			continue;
		}
		if (!us){
			continue;
		}
		if (s[i] >= '5'){
			pos=i;
			break;
		}
	}
	if (pos==-1){
		cout << s;
		return 0;
	}
	for (ll i = pos-1; i >= 0&&t>0; --i, --pos, --t) {
		if (s[i]=='.'){
			ll uss=0;
			for (ll k=i-1; k >= 0; --k){
				if (s[k]=='9'){
					s[k]='0';
					continue;
				}
				uss=1;
				s[k]++;
				break;
			}
			if (uss==0)
				cout << 1;
			for (ll k = 0; k < i; ++k)
				cout << s[k];
			return 0;
		}
		s[i]++;
		if (s[i]=='5'){
			continue;
		}else{
			pos--;
			break;
		}
	}
	for (ll i = 0; i <= pos; ++i)
		cout << s[i];
}


#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#include <deque>
#include <cstring>
#include <functional>
#include <climits>
#include <list>
#include <ctime>
#include <complex>

#define F1(x,y,z) for(int x=(y);x<(z);x++)
#define F2(x,y,z) for(int x=(y);x<=(z);x++)
#define F3(x,y,z) for(int x=(y);x>(z);x--)
#define F4(x,y,z) for(int x=(y);x>=(z);x--)
#define mp make_pair
#define pb push_back
#define LL long long
#define co complex<double>
#define fi first
#define se second

#define MAX 100005
#define AMAX 1025*1005
#define MOD 1000000007

#define f(c,d) ((1<<(c))*(d))

using namespace std;

int n,st,ch,t,at;
char x[MAX*2];

int main(){
	scanf("%d%d%s",&n,&t,x);
	reverse(x,x+n);
	F1(a,1,n){
		if(x[a]=='.')at=a;
		if(at)x[a]=x[a+1];
	}
	n--;
	F4(a,at-1,0)if(x[a]>'4'){
		ch=a+1;
		break;
	}
	if(ch){
		ch--;
		while(t--){
			while(ch<n&&x[ch]<'5')ch++;
			if(ch==n)break;
			if(ch>=at)break;
			st=ch+1;
			while(1){
				if(st==n){
					x[n++]='1';
					break;
				}else if(x[st]=='9')st++;
				else{
					x[st]++;
					break;
				}
			}
			ch=st;
		}
	}
	F4(a,n-1,min(at,st)){
		if(at==a+1)printf(".");
		if(a<st)x[a]='0';
		printf("%c",x[a]);
	}
	printf("\n");
	#ifdef LOCAL_PROJECT
	system("pause");
	#endif
	return 0;
}


#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#include <deque>
#include <cstring>
#include <functional>
#include <climits>
#include <list>
#include <ctime>
#include <complex>

#define F1(x,y,z) for(int x=(y);x<(z);x++)
#define F2(x,y,z) for(int x=(y);x<=(z);x++)
#define F3(x,y,z) for(int x=(y);x>(z);x--)
#define F4(x,y,z) for(int x=(y);x>=(z);x--)
#define mp make_pair
#define pb push_back
#define LL long long
#define co complex<double>
#define fi first
#define se second

#define MAX 100005
#define AMAX 1025*1005
#define MOD 1000000007

#define f(c,d) ((1<<(c))*(d))

using namespace std;

int n,st,ch,t,at;
char x[MAX];

int main(){
	scanf("%d%d%s",&n,&t,x);
	reverse(x,x+n);
	F1(a,1,n){
		if(x[a]=='.')at=a;
		if(at)x[a]=x[a+1];
	}
	n--;
	F4(a,at-1,0)if(x[a]>'4'){
		ch=a+1;
		break;
	}
	if(ch){
		ch--;
		while(t--){
			while(ch<n&&x[ch]<'5')ch++;
			if(ch==n)break;
			if(ch>=at)break;
			st=ch+1;
			while(1){
				if(st==n){
					x[n++]='1';
					break;
				}else if(x[st]=='9')st++;
				else{
					x[st]++;
					break;
				}
			}
			ch=st;
		}
	}
	F4(a,n-1,st){
		if(at==a+1)printf(".");
		printf("%c",x[a]);
	}
	printf("\n");
	#ifdef LOCAL_PROJECT
	system("pause");
	#endif
	return 0;
}


#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#include <deque>
#include <cstring>
#include <functional>
#include <climits>
#include <list>
#include <ctime>
#include <complex>

#define F1(x,y,z) for(int x=(y);x<(z);x++)
#define F2(x,y,z) for(int x=(y);x<=(z);x++)
#define F3(x,y,z) for(int x=(y);x>(z);x--)
#define F4(x,y,z) for(int x=(y);x>=(z);x--)
#define mp make_pair
#define pb push_back
#define LL long long
#define co complex<double>
#define fi first
#define se second

#define MAX 100005
#define AMAX 1025*1005
#define MOD 1000000007

#define f(c,d) ((1<<(c))*(d))

using namespace std;

int n,st,ch,t,at;
char x[MAX*2];

int main(){
	scanf("%d%d%s",&n,&t,x);
	reverse(x,x+n);
	F1(a,1,n){
		if(x[a]=='.')at=a;
		if(at)x[a]=x[a+1];
	}
	n--;
	F4(a,at-1,0)if(x[a]>'4'){
		ch=a+1;
		break;
	}
	if(ch){
		ch--;
		while(t--){
			while(ch<n&&x[ch]<'5')ch++;
			if(ch==n)break;
			if(ch>=at)break;
			st=ch+1;
			while(1){
				if(st==n){
					x[n++]='1';
					break;
				}else if(x[st]=='9')st++;
				else{
					x[st]++;
					break;
				}
			}
			ch=st;
		}
	}
	F4(a,n-1,st){
		if(at==a+1)printf(".");
		printf("%c",x[a]);
	}
	printf("\n");
	#ifdef LOCAL_PROJECT
	system("pause");
	#endif
	return 0;
}


#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#include <deque>
#include <cstring>
#include <functional>
#include <climits>
#include <list>
#include <ctime>
#include <complex>

#define F1(x,y,z) for(int x=(y);x<(z);x++)
#define F2(x,y,z) for(int x=(y);x<=(z);x++)
#define F3(x,y,z) for(int x=(y);x>(z);x--)
#define F4(x,y,z) for(int x=(y);x>=(z);x--)
#define mp make_pair
#define pb push_back
#define LL long long
#define co complex<double>
#define fi first
#define se second

#define MAX 100005
#define AMAX 1025*1005
#define MOD 1000000007

#define f(c,d) ((1<<(c))*(d))

using namespace std;

int n,st,ch,t,at;
char x[MAX];

int main(){
	scanf("%d%d%s",&n,&t,x);
	reverse(x,x+n);
	F1(a,1,n){
		if(x[a]=='.')at=a;
		if(at)x[a]=x[a+1];
	}
	n--;
	F4(a,n-1,0)if(x[a]>'4'){
		ch=a+1;
		break;
	}
	if(ch){
		ch--;
		while(t--){
			while(ch<n&&x[ch]<'5')ch++;
			if(ch==n)break;
			if(ch>=at)break;
			st=ch+1;
			while(1){
				if(st==n){
					x[n++]='1';
					break;
				}else if(x[st]=='9')st++;
				else{
					x[st]++;
					break;
				}
			}
			ch=st;
		}
	}
	F4(a,n-1,st){
		if(at==a+1)printf(".");
		printf("%c",x[a]);
	}
	printf("\n");
	#ifdef LOCAL_PROJECT
	system("pause");
	#endif
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define PI acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;

using namespace std;
typedef  long long ll;
typedef  unsigned long long  ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}

int main()
{
	char s[300000];
	int n,t,t1;
	int point;
//	freopen("test.txt","r",stdin);
    memset(s,0,sizeof(s));
	scanf("%d%d%s",&n,&t,s);
	t1=t;

	for(int i=n+1;i>=1;i--)
	{
		s[i]=s[i-1];
	}
	s[0]='0';
	for(int i=1;i<=n;i++)
		if(s[i]=='.')
			point = i;
	int flag = 0;

	for(int i=point+1;i<strlen(s);i++)
	{
		if(s[i]>='5')
		{
			flag = i;
			break;
		}
	}

	while(t--)
	{
		if(s[flag]<'5')
			break;
		s[flag]='\0';
		if(s[flag-1]=='.')
		{
			for(int i=point-1;i>=0;i--)
			{
				if(s[i]=='9')
					s[i]='0';
				else
				{
					s[i]+=1;
					break;
				}
			}
			break;
		}
		s[--flag] +=1;
	}

	for(int i=strlen(s)-1;i>=point;i--)
	{

		if(s[i]!='0'&&s[i]!='.')
			break;
		s[i]='\0';
	}

	if(s[0]=='1')
		printf("1");
	puts(s+1);

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define PI acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;

using namespace std;
typedef  long long ll;
typedef  unsigned long long  ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}

int main()
{
	char s[300000];
	int n,t;
	int point;
	double t1,t2;
//	freopen("test.txt","r",stdin);
    memset(s,0,sizeof(s));
	scanf("%d %d",&n,&t);
    getchar();
	gets(s);

	for(int i=n+1;i>=1;i--)
	{
		s[i]=s[i-1];
	}
	s[0]='0';
	for(int i=1;i<=n;i++)
		if(s[i]=='.')
			point = i;
	int flag = 0;
	for(int i=point+1;i<strlen(s);i++)
	{
		if(s[i]>='5')
		{
			flag = i;
			break;
		}
	}
	while(t-- && flag)
	{
		flag = strlen(s)-1;
		if(s[flag]<'5') break;
		s[flag]='\0';
		for(int i=flag-1;i>=0;i--)
		{
			if(i==point)
				continue;
			if(i==0)
			{
				s[i]='1';
				break;
			}

			if(s[i]=='9')
				s[i]='0';
			else
			{
				s[i]=s[i]+1;
				break;
			}

		}
		for(int i=strlen(s)-1;i>=point;i--)
		{
			if(s[i]!='0'&&s[i]!='.')
				break;
			if(s[i]=='.')
				flag=0;
			s[i]='\0';
		}
		//printf("flag = %d jingdu = %d\n",flag,jingdu);
	}

	for(int i=strlen(s)-1;i>=point;i--)
	{

		if(s[i]!='0'&&s[i]!='.')
			break;
		s[i]='\0';
	}

	if(s[0]=='1')
		printf("1");
	puts(s+1);

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define PI acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;

using namespace std;
typedef  long long ll;
typedef  unsigned long long  ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}

int main()
{
	char s[300000];
	int n,t,t1;
	int point;
//	freopen("test.txt","r",stdin);
    memset(s,0,sizeof(s));
	scanf("%d%d%s",&n,&t,s);
	t1=t;

	for(int i=n+1;i>=1;i--)
	{
		s[i]=s[i-1];
	}
	s[0]='0';
	for(int i=1;i<=n;i++)
		if(s[i]=='.')
			point = i;
	int flag = 0;

	for(int i=point+1;i<strlen(s);i++)
	{
		if(s[i]>='5')
		{
			flag = i;
			break;
		}
	}

	while(t--)
	{
		if(t1!=t+1)
			flag --;
		if(s[flag]<'5')
			break;
		s[flag]='\0';
		if(s[flag-1]=='.')
		{
			for(int i=point-1;i>=0;i--)
			{
				if(s[i]=='9')
					s[i]='0';
				else
				{
					s[i]+=1;
					break;
				}
			}
			break;
		}
		s[flag-1] +=1;
	}

	if(s[0]=='1')
		printf("1");
	puts(s+1);

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define PI acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;

using namespace std;
typedef  long long ll;
typedef  unsigned long long  ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}

int main()
{
	char s[300000];
	int n,t;
	int point;
	double t1,t2;
	//freopen("test.txt","r",stdin);
    memset(s,0,sizeof(s));
	scanf("%d %d",&n,&t);
    getchar();
	gets(s);

	t2 = atof(s);

	for(int i=0;i<n;i++)
		if(s[i]=='.')
			point = i;

	while(t--)
	{
		int flag = 0,jingdu;
		for(int i=point+1;i<strlen(s);i++)
		{
			if(s[i]>='5')
			{
				flag = i;
				jingdu = i - point;
				break;
			}
		}
		if(flag == 0)
			break;
		s[flag]='\0';
		t2 = atof(s);
		t2 += pow(0.1,jingdu-1);
		//printf("flag = %d jingdu = %d\n",flag,jingdu);

		gcvt(t2,jingdu+point-1,s);
	}

	printf("%g\n",t2);

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define PI acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;

using namespace std;
typedef  long long ll;
typedef  unsigned long long  ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}

int main()
{
	char s[300000];
	int n,t;
	int point;
	double t1,t2;
	freopen("test.txt","r",stdin);
    memset(s,0,sizeof(s));
	scanf("%d %d",&n,&t);
    getchar();
	gets(s);

	for(int i=0;i<n;i++)
		if(s[i]=='.')
			point = i;

	while(t--)
	{
		int flag = 0,jingdu;
		for(int i=point+1;i<strlen(s);i++)
		{
			if(s[i]>='5')
			{
				flag = i;
				jingdu = i - point;
				break;
			}
		}
		if(flag == 0)
			break;
		s[flag]='\0';
		t2 = atof(s);
		t2 += pow(0.1,jingdu-1);
		//printf("flag = %d jingdu = %d\n",flag,jingdu);

		gcvt(t2,jingdu+point-1,s);
	}

	printf("%g",t2);

    return 0;
}