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 JAVA):

Solution:
//package codeforces;
import java.util.*;
import java.io.*;

public class esehi {
  static FastReader scan;
  static PrintWriter pw;
  static int count = 0;

  public static void main(String[] args) {

    new Thread(null, null, "Thread 1", 1 << 27) {
      public void run() {
        try {
          setup();
          solve();
          closeResources();
        } catch (Exception e) {
          e.printStackTrace();
          System.exit(1);
        }
      }
    }.start();
  }

  private static void solve() throws IOException{
    //scan.read();
    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();
    sc.nextLine();
    while (t-- > 0) {
      String da = sc.nextLine();
      //StringBuilder data = new StringBuilder(scan.readLine());
      StringBuilder data = new StringBuilder(da);
      //pl(data.toString());
      int len = data.length();
      int l = 0;
      for (int i = 0, j=len-1; i < j; i++, j--  ) {
        if(data.charAt(i) == data.charAt(j))l++;
        else break;
      }
      StringBuilder rem = new StringBuilder(data.substring(l, len-l));
      int prefix = getlps(rem + "?" + rem.reverse());
      int suffix = getlps(rem + "?" + rem.reverse());
      if(prefix > suffix) {
        //pl(data.substring(0, l) + rem.substring(0, prefix) + data.substring(len-l));
        System.out.println(data.substring(0, l) + rem.substring(0, prefix) + data.substring(len-l));
      }
      else {
        rem = rem.reverse();
        //pl(data.substring(0, l) + rem.substring(0, suffix) + data.substring(len - l));
        System.out.println(data.substring(0, l) + rem.substring(0, suffix) + data.substring(len - l));
      }
    }
  }

  private static int getlps(String s) {
    int []pi = new int[s.length()];
    for (int i = 1; i < s.length(); i++) {
      int j = pi[i-1];
      while (j > 0 && s.charAt(i) != s.charAt(j)) {
        j = pi[j-1];
      }
      pi[i] = j + (s.charAt(i) == s.charAt(j) ? 1 : 0);
    }
    return pi[s.length()-1];
  }

  static String ns() throws IOException {
    return scan.readLine();
  }


  static int ni() throws IOException {
    return scan.nextInt();
  }

  static long nl() throws IOException {
    return scan.nextLong();
  }

  static double nd() throws IOException {
    return scan.nextDouble();
  }

  static long[] nla(int n) throws IOException {
    long []data = new long[n];
    for (int i = 0; i < n; i++) {
      data[i] = nl();
    }
    return data;
  }

  static int[] nia(int n) throws IOException {
    int []data = new int[n];
    for (int i = 0; i < n; i++) {
      data[i] = ni();
    }
    return data;
  }

  static void pl() {
    pw.println();
  }

  static void p(Object o) {
    pw.print(o + " ");
  }

  static void pl(Object o) {
    pw.println(o);
  }

  static void psb(StringBuilder sb) {
    pw.print(sb);
  }

  static void pa(Object arr[]) {
    for (Object o : arr) {
      p(o);
    }
    pl();
  }

  static void pa(int arr[]) {
    for (int o : arr) {
      p(o);
    }
    pl();
  }

  static void pa(long arr[]) {
    for (long o : arr) {
      p(o);
    }
    pl();
  }

  static void pa(double arr[]) {
    for (double o : arr) {
      p(o);
    }
    pl();
  }

  static void pa(char arr[]) {
    for (char o : arr) {
      p(o);
    }
    pl();
  }

  static void pa(List list) {
    for (Object o : list) {
      p(o);
    }
    pl();
  }

  static void pa(Object[][] arr) {
    for (int i = 0; i < arr.length; ++i) {
      for (Object o : arr[i]) {
        p(o);
      }
      pl();
    }
  }

  static void pa(int[][] arr) {
    for (int i = 0; i < arr.length; ++i) {
      for (int o : arr[i]) {
        p(o);
      }
      pl();
    }
  }

  static void pa(long[][] arr) {
    for (int i = 0; i < arr.length; ++i) {
      for (long o : arr[i]) {
        p(o);
      }
      pl();
    }
  }

  static void pa( char[][] arr) {
    for (int i = 0; i < arr.length; ++i) {
      for (char o : arr[i]) {
        p(o);
      }
      pl();
    }
  }

  static void pa(double[][] arr) {
    for (int i = 0; i < arr.length; ++i) {
      for (double o : arr[i]) {
        p(o);
      }
      pl();
    }
  }

  static class FastReader {
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public FastReader() {
      din = new DataInputStream(System.in);
      buffer = new byte[BUFFER_SIZE];
      bufferPointer = bytesRead = 0;
    }

    public FastReader(String file_name) throws IOException {
      din = new DataInputStream(new FileInputStream(file_name));
      buffer = new byte[BUFFER_SIZE];
      bufferPointer = bytesRead = 0;
    }

    public String readLine() throws IOException {
      byte[] buf = new byte[1000009];  //Line length
      int cnt = 0, c;
      while ((c = read()) != -1) {
        if (c == '\n') {
          break;
        }
        buf[cnt++] = (byte) c;
      }
      return new String(buf, 0, cnt);
    }

    public int nextInt() throws IOException {
      int ret = 0;
      byte c = read();
      while (c <= ' ') {
        c = read();
      }
      boolean neg = (c == '-');
      if (neg) {
        c = read();
      }
      do {
        ret = ret * 10 + c - '0';
      } while ((c = read()) >= '0' && c <= '9');
      if (neg) {
        return -ret;
      }
      return ret;
    }

    public long nextLong() throws IOException {
      long ret = 0;
      byte c = read();
      while (c <= ' ') {
        c = read();
      }
      boolean neg = (c == '-');
      if (neg) {
        c = read();
      }
      do {
        ret = ret * 10 + c - '0';
      } while ((c = read()) >= '0' && c <= '9');
      if (neg) {
        return -ret;
      }
      return ret;
    }

    public double nextDouble() throws IOException {
      double ret = 0, div = 1;
      byte c = read();
      while (c <= ' ') {
        c = read();
      }
      boolean neg = (c == '-');
      if (neg) {
        c = read();
      }
      do {
        ret = ret * 10 + c - '0';
      } while ((c = read()) >= '0' && c <= '9');
      if (c == '.') {
        while ((c = read()) >= '0' && c <= '9') {
          ret += (c - '0') / (div *= 10);
        }
      }
      if (neg) {
        return -ret;
      }
      return ret;
    }

    private void fillBuffer() throws IOException {
      bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
      if (bytesRead == -1) {
        buffer[0] = -1;
      }
    }

    private byte read() throws IOException {
      if (bufferPointer == bytesRead) {
        fillBuffer();
      }
      return buffer[bufferPointer++];
    }

    public void close() throws IOException {
      if (din == null) {
        return;
      }
      din.close();
    }
  }

  private static void setup() {
    scan = new FastReader();
    pw = new PrintWriter(System.out, true); // set auto flush to true if you want to directly flush to the output stream.
  }

  private static void closeResources() throws IOException {
    pw.flush();
    pw.close();
    scan.close();
  }

}