#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
bool isV(char x) {
  if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u') {
    return 1;
  }
  return 0;
}
bool isP(long long n) {
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) return 0;
  }
  return 1;
}
void fio() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
void print(long long* a, long long n) {
  long long i;
  for (i = 0; i < (long long)n; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
}
void print2d(long long a[][1000], long long m, long long n) {
  long long i, j;
  for (i = 0; i < (long long)m; i++) {
    for (j = 0; j < (long long)n; j++) {
      cout << setw(2) << a[i][j];
    }
    cout << endl;
  }
}
long long prefix_function(string& s, long long val) {
  long long n = (long long)s.length();
  vector<long long> pi(n, 0);
  for (long long i = 1; i < n; i++) {
    long long j = pi[i - 1];
    while (j > 0 && s[i] != s[j]) j = pi[j - 1];
    if (s[i] == s[j]) j++;
    pi[i] = j;
  }
  return pi.back();
}
int32_t main() {
  fio();
  long long t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long st = 0, e = s.size() - 1;
    while (st < e && s[st] == s[e]) {
      st++, e--;
    }
    if (st >= e) {
      cout << s << endl;
      continue;
    } else {
      string x = s.substr(st, e - st + 1);
      string r = x;
      reverse(r.begin(), r.end());
      string z = x + '#' + r;
      long long m1 = prefix_function(z, x.size());
      z = r + '#' + x;
      long long m2 = prefix_function(z, x.size());
      if (m1 >= m2) {
        cout << s.substr(0, st + min(m1, (long long)x.size())) +
                    s.substr(e + 1, s.size() - 1 - e)
             << endl;
      } else {
        cout << s.substr(0, st) +
                    s.substr(e + 1 - min(m2, (long long)x.size()),
                             s.size() - 1 - e + min(m2, (long long)x.size()))
             << endl;
      }
    }
  }
}
