#include <bits/stdc++.h>
using namespace std;
#define int long long
#define NAME "test"
#define endl '\n'
#define FOR(x, y, z) for(int x = y; x <= z; x++)
#define FORL(x, y, z) for(int x = y; x >= z; x--)
const int N = 5e5 + 5;
const int mod = 1e9 + 7;
const int base = 311;
int test;
string n;
int Size, lo;
int p1[N], p2[N];
int hash1[N], hash2[N];
int get1(int l, int r)
{
return ((hash1[r] - hash1[l - 1] * p1[r - l + 1]) % mod + mod) % mod;
}
int get2(int l, int r)
{
return ((hash2[l] - hash2[r + 1] * p2[r - l + 1]) % mod + mod) % mod;
}
void solve()
{
cin >> n;
Size = n.size();
n = " " + n;
p1[0] = 1;
FOR(i, 1, Size) p1[i] = (p1[i - 1] * base) % mod;
FORL(i, Size, 1) p2[i] = (p2[i + 1] * base) % mod;
FOR(i, 1, Size) hash1[i] = (hash1[i - 1] * base + n[i] - 'a' + 1) % mod;
FORL(i, Size, 1) hash2[i] = (hash2[i + 1] * base + n[i] - 'a' + 1) % mod;
FORL(i, Size, 1)
{
if (get1(i, Size) == get2(i, Size))
{
lo = i;
}
}
FOR(i, 1, Size) cout << n[i];
FORL(i, lo - 1, 1) cout << n[i];
cout << endl;
memset(hash1, 0, sizeof(hash1));
memset(hash2, 0, sizeof(hash2));
}
signed main()
{
if(fopen(NAME".inp", "r"))
{
freopen(NAME".inp","r",stdin);
freopen(NAME".out","w",stdout);
}
//test = 1;
cin >> test;
while (test--)
{
solve();
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgaW50IGxvbmcgbG9uZwojZGVmaW5lIE5BTUUgInRlc3QiCiNkZWZpbmUgZW5kbCAnXG4nCiNkZWZpbmUgRk9SKHgsIHksIHopIGZvcihpbnQgeCA9IHk7IHggPD0gejsgeCsrKQojZGVmaW5lIEZPUkwoeCwgeSwgeikgZm9yKGludCB4ID0geTsgeCA+PSB6OyB4LS0pCgpjb25zdCBpbnQgTiA9IDVlNSAr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