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;
char local = 'O';
template <typename T>
void read(T &a) {
  cin >> a;
}
template <typename T, typename T1>
void read(T &a, T1 &b) {
  cin >> a >> b;
}
template <typename T, typename T1, typename T2>
void read(T &a, T1 &b, T2 &c) {
  cin >> a >> b >> c;
}
template <typename T, typename T1>
void read(pair<T, T1> &p) {
  cin >> p.first >> p.second;
}
template <typename T>
void read(T arr[], long long int n) {
  for (long long int i = 0; i < n; i++) {
    read(arr[i]);
  }
}
template <typename T>
void read(vector<T> &arr) {
  for (long long int i = 0; i < arr.size(); i++) {
    read(arr[i]);
  }
}
long long int modularExponentiation(long long int x, long long int n,
                                    long long int M) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return modularExponentiation((x * x) % M, n / 2, M);
  return (x * modularExponentiation((x * x) % M, (n - 1) / 2, M)) % M;
}
long long int binaryExponentiation(long long int x, long long int n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return binaryExponentiation(x * x, n / 2);
  else
    return x * binaryExponentiation(x * x, (n - 1) / 2);
}
long long int GCD(long long int A, long long int B) {
  if (B == 0)
    return A;
  else
    return GCD(B, A % B);
}
long long int extended_GCD(long long int a, long long int b, long long int &x,
                           long long int &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  long long int x1, y1;
  long long int gcd = extended_GCD(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return gcd;
}
long long int modinv(long long int a, long long int mod) {
  long long int x, y;
  extended_GCD(a, mod, x, y);
  if (x < 0) x += mod;
  return x;
}
long long int modPrimeInverse(long long int A, long long int M) {
  return modularExponentiation(A, M - 2, M);
}
void sieve(long long int N) {
  bool isPrime[N + 1];
  for (long long int i = 0; i <= N; ++i) {
    isPrime[i] = true;
  }
  isPrime[0] = false;
  isPrime[1] = false;
  for (long long int i = 2; i * i <= N; ++i) {
    if (isPrime[i] == true) {
      for (long long int j = i * i; j <= N; j += i) isPrime[j] = false;
    }
  }
}
void factorSieve(long long int n) {
  long long int minPrime[n + 1];
  memset(minPrime, 0, sizeof(minPrime));
  for (long long int i = 2; i * i <= n; ++i) {
    if (minPrime[i] == 0) {
      for (long long int j = i * i; j <= n; j += i) {
        if (minPrime[j] == 0) {
          minPrime[j] = i;
        }
      }
    }
  }
  for (long long int i = 2; i <= n; ++i) {
    if (minPrime[i] == 0) {
      minPrime[i] = i;
    }
  }
}
vector<long long int> factorize(long long int n, long long int minPrime[]) {
  vector<long long int> res;
  while (n != 1) {
    res.push_back(minPrime[n]);
    n /= minPrime[n];
  }
  return res;
}
void merge(long long int A[], long long int start, long long int mid,
           long long int end) {
  long long int p = start, q = mid + 1;
  long long int Arr[end - start + 1], k = 0;
  for (long long int i = start; i <= end; i++) {
    if (p > mid)
      Arr[k++] = A[q++];
    else if (q > end)
      Arr[k++] = A[p++];
    else if (A[p] < A[q])
      Arr[k++] = A[p++];
    else
      Arr[k++] = A[q++];
  }
  for (long long int p = 0; p < k; p++) {
    A[start++] = Arr[p];
  }
}
void merge_sort(long long int A[], long long int start, long long int end) {
  if (start < end) {
    long long int mid = (start + end) / 2;
    merge_sort(A, start, mid);
    merge_sort(A, mid + 1, end);
    merge(A, start, mid, end);
  }
}
long long int binarySearch(long long int a[], long long int low,
                           long long int high, long long int key) {
  while (low <= high) {
    long long int mid = (low + high) / 2;
    if (a[mid] < key) {
      low = mid + 1;
    } else if (a[mid] > key) {
      high = mid - 1;
    } else {
      return mid;
    }
  }
  return -1;
}
bool sortbysec(const pair<long long int, long long int> &a,
               const pair<long long int, long long int> &b) {
  if (a.first == b.first) {
    a.second > b.second;
  }
  return (a.first < b.first);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    if (local == 'L') {
      cout << "t"
           << " " << t << "\n";
    }
    string s;
    cin >> s;
    int n = s.size();
    int i = 0;
    int j = n - 1;
    while (i < j && s[i] == s[j]) {
      i++;
      j--;
    }
    if (local == 'L') {
      cout << "i"
           << " " << i << " "
           << "j"
           << " " << j << "\n";
    }
    if (i >= j) {
      cout << s << "\n";
      continue;
    }
    i--;
    j++;
    long long int i1 = i + 1;
    long long int j1 = j - 1;
    vector<pair<char, long long int>> si;
    char c = ' ';
    int co = 0;
    for (int k = i + 1; k <= j - 1; k++) {
      if (c == s[k]) {
        co++;
      } else {
        if (co > 0) si.push_back({c, co});
        c = s[k];
        co = 1;
      }
    }
    if (co > 0) si.push_back({c, co});
    int e = si.size();
    for (int k = e - 1; k >= 0; k--) {
      int f = 0;
      if (si[k].first == si[0].first && si[k].second >= si[0].second) {
        if (local == 'L') {
          cout << "k"
               << " " << k << " "
               << "f"
               << " " << f << "\n";
        }
        if (k == 0)
          f += si[0].second;
        else {
          f += 2 * si[0].second;
        }
        int g = 1;
        int h = k - 1;
        while (g <= h && si[g].first == si[h].first &&
               si[g].second == si[h].second) {
          if (g == h)
            f += si[g].second;
          else {
            f += 2 * si[g].second;
          }
          g++;
          h--;
        }
        if (local == 'L') {
          cout << "g"
               << " " << g << " "
               << "h"
               << " " << h << " "
               << "f"
               << " " << f << "\n";
        }
        if (g >= h) {
          i1 = i + f;
          break;
        }
      }
    }
    for (int k = 0; k < e; k++) {
      int f = 0;
      if (si[k].first == si[e - 1].first && si[k].second >= si[e - 1].second) {
        if (local == 'L') {
          cout << "k"
               << " " << k << " "
               << "f"
               << " " << f << "\n";
        }
        if (k == e - 1)
          f += si[e - 1].second;
        else {
          f += 2 * si[e - 1].second;
        }
        int g = k + 1;
        int h = e - 2;
        while (g <= h && si[g].first == si[h].first &&
               si[g].second == si[h].second) {
          if (g == h)
            f += si[g].second;
          else {
            f += 2 * si[g].second;
          }
          g++;
          h--;
        }
        if (local == 'L') {
          cout << "g"
               << " " << g << " "
               << "h"
               << " " << h << " "
               << "f"
               << " " << f << "\n";
        }
        if (g >= h) {
          j1 = j - f;
          break;
        }
      }
    }
    if (local == 'L') {
      cout << "i1"
           << " " << i1 << " "
           << "j1"
           << " " << j1 << "\n";
    }
    if (i1 - i > j - j1) {
      cout << s.substr(0, i1 + 1) + s.substr(j, n - j);
    } else {
      if (local == 'L') {
        cout << "2"
             << " " << 2 << "\n";
      }
      cout << s.substr(0, i + 1) + s.substr(j1, n - j1);
    }
    cout << "\n";
  }
}