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:

import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.Map.Entry;
import java.util.Stack;


public class ROUGH{
	public static class FastReader {
		BufferedReader br;
		StringTokenizer root;
		
 
		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
 
		String next() {
			while (root == null || !root.hasMoreTokens()) {
				try {
					root = new StringTokenizer(br.readLine());
				} catch (Exception addd) {
					addd.printStackTrace();
				}
			}
			return root.nextToken();
		}
 
		int nextInt() {
			return Integer.parseInt(next());
		}
 
		double nextDouble() {
			return Double.parseDouble(next());
		}
 
		long nextLong() {
			return Long.parseLong(next());
		}
 
		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (Exception addd) {
				addd.printStackTrace();
			}
			return str;
		}
	}
	
	public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
	
	public static FastReader sc = new FastReader();
 
	static int mod = (int) (998244353);
	static List<Integer>[] edges ;
	//static long[] dp;
	
	final static int N = (int) (1e3 + 10) ;

	public static void main(String[] args) {
		  
		int t = sc.nextInt();
		while(t-->0) {
			String s = sc.next();
			List<Character> pre = new ArrayList<>();
			List<Character> suff = new ArrayList<>();
			int i=0,j=s.length()-1;
			while(i <= j) {
				if(s.charAt(i) == s.charAt(j)) {
					if(i == j) pre.add(s.charAt(i));
					else {
						pre.add(s.charAt(i));
						suff.add(s.charAt(j));
					}
					++i; --j;
				}else break;
			}
			
			if(i <= j) {
				String str = s.substring(i,j+1);
				int[] od = getod(str);
				int[] ev = getev(str);
				String[] tr = new String[4];
				if(od[0] > 0)
				tr[0] = str.substring(0,2*(od[0]-1)+1);
				else tr[0] = new String();
				
				if(od[1] > 0)
					tr[1] = str.substring(str.length()-(2*(od[1]-1)+1),str.length());
				else tr[1] = new String();
				
				if(ev[0] > 0)
					tr[2] = str.substring(0,2*ev[0]);
				else tr[2] = new String();
				
				if(ev[1] > 0)
					tr[3] = str.substring(str.length()-(2*ev[1]),str.length());
				else tr[3] = new String();
				
				StringBuilder ans = new StringBuilder();
				for(i=0;i<tr.length;++i) {
					if(ans.length() < tr[i].length()) ans = new StringBuilder(tr[i]);
				}
			for(i=0;i<ans.length();++i) pre.add(ans.charAt(i));
			}
		
		
		for(char x : pre) out.print(x);
		for(i=suff.size()-1;i>=0;--i) out.print(suff.get(i));
		
		out.println();
		}
	out.close();
}

	private static int[] getev(String s) {
		int n = s.length();
		int[] d2 = new int[n];
		for (int i = 0, l = 0, r = -1; i < n; i++) {
		    int k = (i > r) ? 0 : Math.min(d2[l + r - i + 1], r - i + 1);
		    while (0 <= i - k - 1 && i + k < n && s.charAt(i - k - 1) == s.charAt(i + k)) {
		        k++;
		    }
		    d2[i] = k--;
		    if (i + k > r) {
		        l = i - k - 1;
		        r = i + k ;
		    }
		}
		int pre = 0,suff = 0;
		for(int i=0;i<n;++i) {
			if(d2[i] == i) pre = Math.max(pre, d2[i]);
			if(d2[i]+i == n) suff = Math.max(suff, d2[i]);
		}
		return new int[] {pre,suff};
	}

	private static int[] getod(String s) {
		int n = s.length();
		int[] d1 = new int[n];
		for (int i = 0, l = 0, r = -1; i < n; i++) {
		    int k = (i > r) ? 1 : Math.min(d1[l + r - i], r - i + 1);
		    while (0 <= i - k && i + k < n && s.charAt(i-k) == s.charAt(i+k)) {
		        k++;
		    }
		    d1[i] = k--;
		    if (i + k > r) {
		        l = i - k;
		        r = i + k;
		    }
		}
		int pre = 0,suff=0;
		for(int i=0;i<n;++i) {
			if(d1[i] == (i+1)) pre = Math.max(pre, d1[i]);
			if(d1[i] + i == n) suff = Math.max(suff, d1[i]);
		}
		
		return new int[] {pre,suff};
	}

	private static boolean ok(String b) {
		return b.equals(new StringBuilder(b).reverse().toString());
	}

	
}