#include <bits/stdc++.h>
const int maxn = 5e5 + 10;
using namespace std;
int main() {
  string s;
  long long n;
  cin >> n;
  while (n--) {
    cin >> s;
    int b = 0, e = s.size() - 1;
    while (b < e && s[b] == s[e]) b++, e--;
    if (b >= e)
      cout << s << "\n";
    else {
      string s1, s2, cmax;
      for (int i = b; i <= e; i++) {
        s1 += s[i];
        s2 = s1;
        reverse(s2.begin(), s2.end());
        if (s1 == s2) cmax = s1;
      }
      string s3, s4, cmax2;
      for (int i = e; i >= b; i--) {
        s3 += s[i];
        s4 = s3;
        reverse(s4.begin(), s4.end());
        if (s3 == s4) cmax2 = s3;
      }
      if (cmax.size() > cmax2.size())
        cout << s.substr(0, b) << cmax << s.substr(e + 1) << endl;
      else
        cout << s.substr(0, b) << cmax2 << s.substr(e + 1) << endl;
    }
  }
}
