#include <bits/stdc++.h>
using namespace std;
long t;
string s, t1, t2, s1, s2, ss, max1;
int main() {
  cin >> t;
  while (t--) {
    cin >> s;
    long l = 0, r = s.length() - 1, L, R;
    while (l < r && s[l] == s[r]) {
      l++;
      r--;
    }
    L = l, R = r;
    if (l > r)
      swap(l, r), l++;
    else if (l < r)
      r++;
    t1 = s.substr(0, l);
    t2 = s.substr(r);
    if (L < R) {
      ss = s.substr(L, R - L + 1);
      l = 0, r = ss.length() - 1;
      while (l <= r) {
        if (ss[l] == ss[r]) {
          s1 = ss.substr(l, r - l + 1);
          s2 = s1;
          reverse(s1.begin(), s1.end());
          if (s1 == s2) break;
        }
        r--;
      }
      max1 = t1 + s1 + t2;
      l = 0, r = ss.length() - 1;
      while (l <= r) {
        if (ss[l] == ss[r]) {
          s1 = ss.substr(l, r - l + 1);
          s2 = s1;
          reverse(s1.begin(), s1.end());
          if (s1 == s2) break;
        }
        l++;
      }
      string f = t1 + s1 + t2;
      if (f.length() > max1.length()) max1 = f;
    } else
      max1 = t1 + t2;
    cout << max1 << endl;
  }
}
