#include <bits/stdc++.h>
using namespace std;
string palindrone(string t) {
  vector<long long> pre(t.size(), 0);
  long long j = 0;
  for (long long i = 1; i < t.size(); i++) {
    while (j > 0 && t[i] != t[j]) j = pre[j - 1];
    if (t[i] == t[j]) j++;
    pre[i] = j;
  }
  return t.substr(0, j);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    string s, w = "", ss = "", v = "";
    cin >> s;
    long long n = s.size();
    long long p = 0, q = n - 1, k;
    while (p < q) {
      if (s[p] != s[q]) break;
      p++, q--;
    }
    for (long long i = p; i <= q; i++) w += s[i];
    v = string(w.rbegin(), w.rend());
    w += "#" + string(w.rbegin(), w.rend());
    v += "#" + string(v.rbegin(), v.rend());
    w = palindrone(w);
    v = palindrone(v);
    for (long long i = 0; i < p; i++) ss += s[i];
    cout << ss << (w.size() > v.size() ? w : v)
         << string(ss.rbegin(), ss.rend()) << "\n";
  }
}
