#include <bits/stdc++.h>
using namespace std;
vector<int> a;
void manachers(const string& s, int n) {
  int r = 0, p = 0;
  for (int i = 0; i < n; i++) {
    if (i <= r)
      a[i] = min(a[2 * p - i], r - i);
    else
      a[i] = 0;
    while (i - a[i] - 1 >= 0 && i + a[i] + 1 < n &&
           s[i - a[i] - 1] == s[i + a[i] + 1])
      a[i]++;
    if (r < i + a[i]) {
      r = i + a[i];
      p = i;
    }
  }
}
bool is_palindrome(const string& s) {
  int l = s.size();
  for (int i = 0; i < l / 2; i++)
    if (s[i] != s[l - i - 1]) return false;
  return true;
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  while (T--) {
    string s;
    cin >> s;
    int l = s.size(), idx = 0;
    for (int i = 0; i < l / 2; i++)
      if (s[i] != s[l - i - 1]) {
        idx = i;
        break;
      }
    string mid = s.substr(idx, l - 2 * idx);
    string temp;
    for (int i = 0; i < mid.size(); i++) {
      temp += mid[i];
      if (i != mid.size() - 1) temp += '#';
    }
    int m = temp.size();
    a.resize(m);
    manachers(temp, m);
    string prefix, suffix;
    for (int i = m - 1; i >= 0; i--)
      if (a[i] == i) {
        prefix = temp.substr(0, 2 * i + 1);
        break;
      }
    for (int i = 0; i < m; i++)
      if (a[i] == m - i - 1) {
        suffix = temp.substr(2 * i - m + 1);
        break;
      }
    string result = s.substr(0, idx);
    if (prefix.size() >= suffix.size())
      for (int i = 0; i < prefix.size(); i += 2) result += prefix[i];
    else
      for (int i = 0; i < suffix.size(); i += 2) result += suffix[i];
    result += s.substr(l - idx);
    cout << result << '\n';
  }
  return 0;
}
