#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
int t;
string s;
bool is_pal(string x) {
  string temp = x;
  reverse(x.begin(), x.end());
  return x == temp;
}
string edge_pal(const string& a) {
  string ans = "";
  for (int i = 1; i <= a.size(); i++) {
    string temp_a = a.substr(0, i);
    string temp_b = a.substr(a.size() - i, i);
    if (temp_a.size() > ans.size() && is_pal(temp_a)) ans = temp_a;
    if (temp_b.size() > ans.size() && is_pal(temp_b)) ans = temp_b;
  }
  return ans;
}
void solve() {
  if (is_pal(s)) {
    cout << s << '\n';
    return;
  }
  int max_pre_len = s.size() / 2;
  string pre, mid, suf;
  int i = 0;
  while (i <= max_pre_len && s[i] == s[s.size() - i - 1]) {
    i++;
  };
  pre = s.substr(0, i);
  mid = edge_pal(s.substr(i, s.size() - i * 2));
  suf = pre;
  reverse(suf.begin(), suf.end());
  cout << pre << mid << suf << '\n';
}
int main() {
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> s;
    solve();
  }
  return 0;
}
