#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
string solve_palindrome(const string &s) {
  string a = s;
  reverse(a.begin(), a.end());
  a = s + "#" + a;
  int pref[a.size() + 3];
  pref[0] = 0;
  int c = 0;
  for (int i = 1; i < (int)a.size(); i++) {
    while (c != 0 && a[c] != a[i]) c = pref[c - 1];
    if (a[c] == a[i]) c++;
    pref[i] = c;
  }
  return s.substr(0, c);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long i, tc, cs = 1, n, m, k, cnt = 0, j, q;
  string s, f, l, st, pt;
  cin >> tc;
  while (tc--) {
    cin >> s;
    long long len = s.size();
    if (len == 1) {
      cout << s << endl;
      continue;
    }
    for (i = 0, j = len - 1; i < len / 2; i++, j--) {
      if (s[i] == s[j])
        continue;
      else
        break;
    }
    f = s.substr(0, i);
    l = f;
    reverse(l.begin(), l.end());
    s = s.substr(i, len - f.size() * 2);
    st = solve_palindrome(s);
    reverse(s.begin(), s.end());
    pt = solve_palindrome(s);
    if (st.size() > pt.size()) swap(st, pt);
    cout << f << pt << l << endl;
  }
  return 0;
}
