Problem: This is the easy version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task.

You are given a string s, consisting of lowercase English letters. Find the longest string, t, which satisfies the following conditions: 

  * The length of t does not exceed the length of s. 
  * t is a palindrome. 
  * There exists two strings a and b (possibly empty), such that t = a + b ( "+" represents concatenation), and a is prefix of s while b is suffix of s. 

Input

The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 1000), the number of test cases. The next t lines each describe a test case.

Each test case is a non-empty string s, consisting of lowercase English letters.

It is guaranteed that the sum of lengths of strings over all test cases does not exceed 5000.

Output

For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them.

Example

Input


5
a
abcdfdcecba
abbaxyzyx
codeforces
acbba


Output


a
abcdfdcba
xyzyx
c
abba

Note

In the first test, the string s = "a" satisfies all conditions.

In the second test, the string "abcdfdcba" satisfies all conditions, because:

  * Its length is 9, which does not exceed the length of the string s, which equals 11. 
  * It is a palindrome. 
  * "abcdfdcba" = "abcdfdc" + "ba", and "abcdfdc" is a prefix of s while "ba" is a suffix of s. 



It can be proven that there does not exist a longer string which satisfies the conditions.

In the fourth test, the string "c" is correct, because "c" = "c" + "" and a or b can be empty. The other possible solution for this test is "s".

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
struct BigNum {
  vector<long long> value;
  void set(long long x) {
    value = *(new vector<long long>);
    value.push_back(x);
  }
  void duplicate(BigNum other) { value = other.value; }
  void add(BigNum other) {
    vector<long long> o = other.value;
    if (o.size() > value.size()) {
      vector<long long> t = value;
      value = o;
      o = t;
    }
    for (int i = 0; i < o.size(); i += 1) {
      value[i] += o[i];
      if (i < value.size() - 1 && value[i] >= (long long)1e9) {
        value[i + 1] += value[i] / (long long)1e9;
        value[i] = value[i] % (long long)1e9;
      }
    }
    if (value.back() >= (long long)1e9) {
      value.push_back(value.back() / (long long)1e9);
      value[value.size() - 2] = value[value.size() - 2] % (long long)1e9;
    }
  }
  void mul(long long mul) {
    for (int i = 0; i < value.size(); i += 1) {
      value[i] *= mul;
    }
    for (int i = 0; i < value.size() - 1; i += 1) {
      if (value[i] >= (long long)1e9) {
        value[i + 1] += value[i] / (long long)1e9;
        value[i] = value[i] % (long long)1e9;
      }
    }
    if (value.back() >= (long long)1e9) {
      value.push_back(value.back() / (long long)1e9);
      value[value.size() - 2] = value[value.size() - 2] % (long long)1e9;
    }
  }
  long double div(BigNum other1, BigNum other2) {
    int s = value.size();
    long double current = value.back();
    if (s >= 2) {
      current += ((long double)value[s - 2]) / (long long)1e9;
    }
    if (s >= 3) {
      current +=
          ((long double)value[s - 3]) / ((long long)1e9 * (long long)1e9);
    }
    int s1 = other1.value.size();
    int s2 = other2.value.size();
    long double other = 0;
    if (s1 + s2 - 1 == s) {
      other += other1.value.back() * other2.value.back();
      if (s1 >= 2) {
        other += ((long double)other1.value[s1 - 2] * other2.value.back()) /
                 (long long)1e9;
      }
      if (s2 >= 2) {
        other += ((long double)other2.value[s2 - 2] * other1.value.back()) /
                 (long long)1e9;
      }
      if (s1 >= 3) {
        other += ((long double)other1.value[s1 - 3] * other2.value.back()) /
                 ((long long)1e9 * (long long)1e9);
      }
      if (s2 >= 3) {
        other += ((long double)other2.value[s2 - 3] * other1.value.back()) /
                 ((long long)1e9 * (long long)1e9);
      }
    }
    if (s1 + s2 - 1 == s - 1) {
      other += other1.value.back() * other2.value.back();
      if (s1 >= 2) {
        other += ((long double)other1.value[s1 - 2] * other2.value.back()) /
                 (long long)1e9;
      }
      if (s2 >= 2) {
        other += ((long double)other2.value[s2 - 2] * other1.value.back()) /
                 (long long)1e9;
      }
      other /= (long long)1e9;
    }
    if (s1 + s2 - 1 == s - 2) {
      other += other1.value.back() * other2.value.back();
      other /= (long long)1e9 * (long long)1e9;
    }
    return other / current;
  }
  string stringify() {
    string out = to_string(value.back());
    for (int i = value.size() - 1 - 1; i > -1; i -= 1) {
      string cur = to_string(value[i] + (long long)1e9);
      out += cur.substr(1);
    }
    return out;
  }
};
long long gcd(long long a, long long b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
long long triangle(long long x) { return ((x + 1) * x) / 2; }
long long antitriangle(long long x) {
  long long low = 0, high = 1000000007;
  while (low < high) {
    long long mid = (low + high) / 2;
    if (triangle(mid) < x) {
      low = mid + 1;
    } else {
      high = mid;
    }
  }
  return low;
}
long long modInverse(long long a) {
  long long m = 1000000007;
  long long y = 0, x = 1;
  while (a > 1) {
    long long q = a / m;
    long long t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0) {
    x += 1000000007;
  }
  return x;
}
long long modInverse(long long a, long long b) {
  long long m = b;
  long long y = 0, x = 1;
  while (a > 1) {
    long long q = a / m;
    long long t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0) {
    x += b;
  }
  return x;
}
long long pow(long long a, long long b) {
  if (a <= 1) {
    return a;
  }
  if (b == 0) {
    return 1;
  }
  if (b % 2 == 0) {
    return pow((a * a) % 1000000007, b / 2) % 1000000007;
  }
  return (a * pow((a * a) % 1000000007, b / 2)) % 1000000007;
}
vector<long long> dev(vector<pair<long long, long long> > divisors) {
  if (divisors.size() == 0) {
    vector<long long> cur;
    cur.push_back(1);
    return cur;
  }
  long long x = divisors.back().first;
  long long n = divisors.back().second;
  divisors.pop_back();
  vector<long long> ans = dev(divisors);
  vector<long long> cur;
  long long xi = 1;
  for (int i = 0; i < n + 1; i += 1) {
    for (int j = 0; j < ans.size(); j += 1) {
      cur.push_back(ans[j] * xi);
    }
    xi *= x;
  }
  return cur;
}
bool isprime(long long a) {
  if (a == 1) {
    return false;
  }
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0) {
      return false;
    }
  }
  return true;
}
void add(vector<int>& x, vector<int>& y) {
  for (int i = 0; i < x.size(); i += 1) {
    x[i] += y[i];
  }
}
vector<long long> mul(vector<long long> x, long long a) {
  for (int i = 0; i < x.size(); i += 1) {
    x[i] = (x[i] * a) % 1000000007;
  }
  return x;
}
vector<long long> add(vector<long long> x, vector<long long>& y) {
  for (int i = 0; i < x.size(); i += 1) {
    x[i] = (x[i] + y[i]) % 1000000007;
  }
  return x;
}
int lca(vector<vector<int> >& f, vector<int>& depth, int a, int b) {
  if (depth[a] < depth[b]) {
    swap(a, b);
  }
  for (int k = 0; k < 22; k += 1) {
    if ((depth[a] - depth[b]) & (1 << k)) {
      a = f[k][a];
    }
  }
  if (a == b) {
    return a;
  }
  for (int k = 22 - 1; k > -1; k -= 1) {
    if (f[k][a] != f[k][b]) {
      a = f[k][a];
      b = f[k][b];
    }
  }
  return f[0][a];
}
struct pt {
  long long x, y;
  long long d() const { return x * x + y * y; }
};
inline pt operator-(const pt& a) { return {-a.x, -a.y}; }
inline pt operator+(const pt& a, const pt& b) { return {a.x + b.x, a.y + b.y}; }
inline pt operator-(const pt& a, const pt& b) { return {a.x - b.x, a.y - b.y}; }
inline long long operator*(const pt& a, const pt& b) {
  return a.x * b.y - a.y * b.x;
}
inline bool operator<(const pt& a, const pt& b) { return a * b < 0; }
bool llll(const pt& a, const pt& b) { return a * b > 0; }
inline bool operator<=(const pt& a, const pt& b) { return a * b <= 0; }
inline long long operator/(const pt& a, const pt& b) {
  return a.x * b.x + a.y * b.y;
}
inline bool operator==(const pt& a, const pt& b) {
  return a.x == b.x && a.y == b.y;
}
inline bool operator!=(const pt& a, const pt& b) {
  return a.x != b.x || a.y != b.y;
}
pt o = {0, 0};
bool comp(pt a, pt b) { return (a - o) * (b - o) < 0; }
bool decreasing(int a, int b) { return a > b; }
struct booleantree {
  booleantree* left = NULL;
  booleantree* right = NULL;
  void add(int val) {
    booleantree* cur = this;
    for (int i = 30 - 1; i > -1; i -= 1) {
      if ((val & (1 << i))) {
        if (cur->right == NULL) {
          cur->right = new booleantree();
        }
        cur = cur->right;
      } else {
        if (cur->left == NULL) {
          cur->left = new booleantree();
        }
        cur = cur->left;
      }
    }
  }
  int find(int val) {
    booleantree* cur = this;
    int ans = 0;
    for (int i = 30 - 1; i > -1; i -= 1) {
      if ((val & (1 << i))) {
        if (cur->left == NULL) {
          cur = cur->right;
        } else {
          cur = cur->left;
          ans += (1 << i);
        }
      } else {
        if (cur->right == NULL) {
          cur = cur->left;
        } else {
          cur = cur->right;
          ans += (1 << i);
        }
      }
    }
    return ans;
  }
};
vector<int> dummy;
long long funcc(vector<int> x, int k) {
  vector<int> l;
  for (int i = 0; i < x.size(); i += 1) {
    if (x[i]) {
      l.push_back(i);
    }
  }
  long long ans = 0;
  vector<int> a(k);
  while (a[0] < l.size()) {
    int all = 0;
    for (int i = 0; i < k; i += 1) {
      all |= l[a[i]];
    }
    if (all == (1 << k) - 1) {
      long long sum = 1;
      int num = 1;
      for (int i = 0; i < k; i += 1) {
        sum *= x[l[a[i]]];
        x[l[a[i]]]--;
        if (i > 0 && a[i] == a[i - 1]) {
          num++;
          sum /= num;
        } else {
          num = 1;
        }
      }
      ans += sum;
      for (int i = 0; i < k; i += 1) {
        x[l[a[i]]]++;
      }
    }
    int i = k - 1;
    while (i > 0 && a[i] == l.size() - 1) {
      i--;
    }
    a[i++]++;
    while (i < k) {
      a[i] = a[i - 1];
      i++;
    }
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long n;
  cin >> n;
  for (int i = 0; i < n; i += 1) {
    string line;
    cin >> line;
    int l = 0;
    int r = line.size() - 1;
    while (l < r) {
      if (line[l] == line[r]) {
        l++;
        r--;
      } else {
        break;
      }
    }
    string x = line.substr(l, r - l + 1);
    x.push_back('$');
    for (int i = r + 1 - 1; i > l - 1; i -= 1) {
      x.push_back(line[i]);
    }
    vector<int> a(x.size());
    int len = 0;
    for (int i = 1; i < x.size(); i += 1) {
      while (x[len] != x[i]) {
        if (len == 0) {
          break;
        }
        len = a[len - 1];
      }
      if (x[len] == x[i]) {
        len++;
      }
      a[i] = len;
    }
    int ll = l + a.back();
    x = "";
    for (int i = r + 1 - 1; i > l - 1; i -= 1) {
      x.push_back(line[i]);
    }
    x.push_back('$');
    x += line.substr(l, r - l + 1);
    len = 0;
    for (int i = 1; i < x.size(); i += 1) {
      while (x[len] != x[i]) {
        if (len == 0) {
          break;
        }
        len = a[len - 1];
      }
      if (x[len] == x[i]) {
        len++;
      }
      a[i] = len;
    }
    int rr = r - a.back();
    if (ll - l > r - rr) {
      cout << line.substr(0, ll) + line.substr(r + 1) << endl;
    } else {
      cout << line.substr(0, l) + line.substr(rr + 1) << endl;
    }
  }
  return 0;
}