#include <bits/stdc++.h>
using namespace std;
int t;
string a, b, s, c, k, ans, anw;
bool is(string s) {
  for (int i = 0; i <= s.size() / 2; ++i) {
    if (s[i] != s[s.size() - 1 - i]) {
      return false;
    }
  }
  return true;
}
int main() {
  cin >> t;
  while (t--) {
    cin >> s;
    int l = 0, r = (int)s.size() - 1;
    a = b = c = k = ans = anw = "";
    while (l <= r) {
      if (s[l] == s[r] && l != r) {
        a += s[l];
        b += s[r];
      } else if (s[l] == s[r] && l == r) {
        a += s[l];
      } else {
        break;
      }
      ++l;
      --r;
    }
    reverse(b.begin(), b.end());
    int m = l;
    while (l < r) {
      c += s[l];
      if (is(c)) {
        ans = c;
      }
      ++l;
    }
    l = m;
    while (l < r) {
      k += s[r];
      if (is(k)) {
        anw = k;
      }
      --r;
    }
    if (ans.size() > anw.size()) {
      cout << a + ans + b << endl;
    } else {
      cout << a + anw + b << endl;
    }
  }
  return 0;
}
