#include <bits/stdc++.h>
using namespace std;
bool dp[10000 + 5][10000 + 5];
void solve(string& s, long long n) {
  long long mx = 1;
  long long st = 0;
  for (long long i = 0; i < n + 2; ++i) {
    for (long long j = 0; j < n + 2; ++j) dp[i][j] = 1;
  }
  for (long long d = 1; d < n; ++d) {
    for (long long i = 0; i < n - d; ++i) {
      long long j = i + d;
      if (s[i] == s[j] && dp[i + 1][j - 1]) {
        if (j - i + 1 > mx) {
          st = i;
          mx = j - i + 1;
        }
        dp[i][j] = 1;
      } else
        dp[i][j] = 0;
    }
  }
  cout << s.substr(st, mx);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long tt;
  cin >> tt;
  while (tt--) {
    string s;
    cin >> s;
    solve(s, s.size());
    cout << '\n';
  }
}
