#include <bits/stdc++.h>
using namespace std;
int longpre(string y) {
  string s = y + "?";
  reverse(y.begin(), y.end());
  s += y;
  int n = s.length();
  int lps[n];
  for (int i = 0; i < n; i++) lps[i] = 0;
  int i = 0;
  int j = 1;
  while (j < n) {
    if (s[i] == s[j]) {
      lps[j] = i + 1;
      i++;
      j++;
    } else {
      if (i == 0) {
        lps[j] = 0;
        j++;
      } else {
        i = lps[i - 1];
      }
    }
  }
  return lps[n - 1];
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int k = 0;
    int i = 0;
    int n = s.length();
    int j = n - 1;
    while (i < j) {
      if (s[i] == s[j])
        k++;
      else
        break;
      i++;
      j--;
    }
    string t = "";
    if (k > 0) {
      t += s.substr(0, k);
    }
    string mid = s.substr(k, n - 2 * k);
    if (mid.length() > 0) {
      int prefix = longpre(mid);
      reverse(mid.begin(), mid.end());
      int suffix = longpre(mid);
      if (prefix > suffix) {
        reverse(mid.begin(), mid.end());
        t += mid.substr(0, prefix);
      } else {
        t += mid.substr(0, suffix);
      }
    }
    t += s.substr(n - k, k);
    cout << t << endl;
  }
}
