#include <bits/stdc++.h>
using namespace std;
vector<int> v1;
int n, k;
vector<int> v;
vector<int> loc;
int hashh(string s) {
  int n = s.size();
  int pos = -1;
  long long B = 29, mod = 1000000207, POW = 1, hash1 = 0, hash2 = 0;
  for (int i = 0; i < n; i++) {
    hash1 = (hash1 * B + s[i] - 'a' + 1) % mod;
    hash2 = (hash2 + (s[i] - 'a' + 1) * POW) % mod;
    if (hash1 == hash2) pos = i;
    POW = POW * B % mod;
  }
  return pos;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int idx1 = 0, idx2 = s.size() - 1;
    while (idx1 < idx2 && s[idx1] == s[idx2]) {
      idx1++;
      idx2--;
    }
    string x;
    for (int i = idx1; i <= idx2; i++) x.push_back(s[i]);
    string imsi = x;
    int x1 = hashh(x);
    reverse(imsi.begin(), imsi.end());
    int x2 = hashh(imsi);
    if (x1 > x2) {
      for (int i = 0; i < idx1; i++) printf("%c", s[i]);
      for (int i = 0; i <= x1; i++) printf("%c", x[i]);
      for (int i = idx2 + 1; i < s.size(); i++) printf("%c", s[i]);
    } else {
      for (int i = 0; i < idx1; i++) printf("%c", s[i]);
      for (int i = 0; i <= x2; i++) printf("%c", imsi[i]);
      for (int i = idx2 + 1; i < s.size(); i++) printf("%c", s[i]);
    }
    cout << '\n';
  }
}
