#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    string pre = "", suf = "", sspre = "", sssuf = "";
    string r = s;
    reverse(r.begin(), r.end());
    long long n = s.length();
    long long f = 0;
    while (f < n / 2) {
      if (s[f] == r[f]) {
        pre = pre + s[f];
        suf = s[f] + suf;
      } else
        break;
      f++;
    }
    for (int x = f; x <= n - 1 - f; x++) {
      long long findex = f;
      long long rindex = x;
      string temp = "";
      while (rindex >= f && s[rindex] == s[findex]) {
        temp += s[rindex];
        rindex--;
        findex++;
      }
      if (rindex == f - 1 && temp.length() > sspre.length()) {
        sspre = temp;
      }
    }
    for (int x = f; x <= n - 1 - f; x++) {
      long long findex = f;
      long long rindex = x;
      string temp = "";
      while (rindex >= f && r[rindex] == r[findex]) {
        temp += r[rindex];
        rindex--;
        findex++;
      }
      if (rindex == f - 1 && temp.length() > sssuf.length()) {
        sssuf = temp;
      }
    }
    if (s.length() == 1) {
      pre = s[0];
      suf = "";
      sspre = "";
      sssuf = "";
    }
    if (pre.length() == 0) {
      if (sssuf.length() > sspre.length()) {
        sspre = "";
      } else {
        sssuf = "";
      }
    } else if (s.length() - (pre.length() + suf.length()) != 1) {
      if (sssuf.length() == 1) {
        sssuf = "";
      } else if (sspre.length() == 1) {
        sspre = "";
      }
    } else {
      sssuf = "";
    }
    cout << pre;
    if (sspre.length() > sssuf.length()) {
      cout << sspre;
    } else {
      cout << sssuf;
    }
    cout << suf << endl;
  }
  return 0;
}
