#include <bits/stdc++.h>
using namespace std;
bool isPalin(string s) {
  int n = s.length();
  int hlf = n / 2;
  for (int i = 0; i < hlf; i++) {
    if (s[i] != s[n - 1 - i]) {
      return false;
    }
  }
  return true;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int n = s.length();
    if (isPalin(s)) {
      cout << s << endl;
      continue;
    }
    int maxlen = 0;
    string ans = "";
    for (int i = 0; i < n; i++) {
      string temp = s.substr(0, i + 1);
      if (temp.length() > maxlen && isPalin(temp)) {
        maxlen = temp.length();
        ans = temp;
      }
    }
    for (int i = 0; i < n; i++) {
      string temp = s.substr(i, n - i);
      if (temp.length() > maxlen && isPalin(temp)) {
        maxlen = temp.length();
        ans = temp;
      }
    }
    int hlf = n / 2;
    string pre = "";
    string suff = "";
    int cutforward = -1;
    int cutbackward = n;
    for (int i = 0; i < hlf; i++) {
      if (s[i] == s[n - 1 - i]) {
        pre += s[i];
        suff += s[n - 1 - i];
        cutforward = i;
        cutbackward = n - 1 - i;
      } else {
        break;
      }
    }
    string pretemp = pre;
    string sufftemp = suff;
    reverse(sufftemp.begin(), sufftemp.end());
    for (int j = cutforward + 1; j < cutbackward; j++) {
      pretemp += s[j];
      string temp = pretemp + sufftemp;
      if (temp.length() > maxlen && isPalin(temp)) {
        maxlen = temp.length();
        ans = temp;
      }
    }
    pretemp = pre;
    sufftemp = suff;
    for (int j = cutbackward - 1; j > cutforward; j--) {
      sufftemp += s[j];
      string newtemp = sufftemp;
      reverse(newtemp.begin(), newtemp.end());
      string temp = pretemp + newtemp;
      if (temp.length() > maxlen && isPalin(temp)) {
        maxlen = temp.length();
        ans = temp;
      }
    }
    cout << ans << endl;
  }
}
