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.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class D {
	
	public static void main(String[] args) throws Exception
	{
		new D().run();
	}
	
	public void run2()
	{
		System.out.println(bruteForce("codeforces"));
		while(true)
		{
			String gen = gen(5);
			String bruteForce = bruteForce(gen);
			String run = run(gen);
			if(!(new StringBuffer(run).reverse().toString().equals(run)) || run.length() != bruteForce.length())
			{
				System.out.println(gen+" "+run+" "+bruteForce);
			}
		}
	}
	
	public String gen(int N)
	{
		char[] s = "abcd".toCharArray();
		String ans = "";
		for(int i = 0;i<N;i++)
		{
			ans += s[(int)(Math.random() * s.length)];
		}
		return ans;
	}
	
	public String bruteForce(String s)
	{
		String best = "";
		for(int i = 0;i<s.length();i++)
		{
			for(int j = i+1;j<s.length();j++)
			{
				String a = s.substring(0, i);
				String b = s.substring(j, s.length());
				String c = a + b;
				if(new StringBuffer(c).reverse().toString().equals(c))
				{
					if(c.length() > best.length())
						best = c;
				}
			}
		}
		return best;
	}
	
	public String run(String s)
	{
		StringBuilder pre = new StringBuilder("");
		for(int j = 0;j<s.length()/2;j++)
		{
			if(s.charAt(j) == s.charAt(s.length() - 1 - j))
				pre.append(s.charAt(j));
			else
				break;
		}
		int len = pre.length();
		String sub = s.substring(len, s.length() - len);
		Manacher man = new Manacher(sub);
		int[] lens = man.p;
		int max = 0;
		boolean start = true;
		//System.out.println(sub);
		//System.out.println(Arrays.toString(lens));
		for(int j = 1;j<lens.length - 1;j++)
		{
			if(lens[j] == Math.min(j-1, lens.length - j - 2))
			{
				if(max < lens[j])
				{
					max = lens[j];
					if(lens.length - j - 2 < j-1)
					{
						start = false;
					}
				}
			}
		}
		String middle = sub.substring(0, max);
		if(!start)
			middle = sub.substring(sub.length() - max, sub.length());
		StringBuilder ans = new StringBuilder("");
		ans.append(pre);
		ans.append(middle);
		ans.append(new StringBuffer(pre).reverse().toString());
		return ans.toString();
	}
	
	public void run() throws Exception
	{
		BufferedReader file = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(file.readLine());
		for(int i = 0;i<N;i++)
		{
			String s = file.readLine();
			StringBuilder pre = new StringBuilder("");
			for(int j = 0;j<s.length()/2;j++)
			{
				//System.out.println(s.charAt(j) +" "+s.charAt(s.length() - 1 - j));
				if(s.charAt(j) == s.charAt(s.length() - 1 - j))
					pre.append(s.charAt(j));
				else
					break;
			}
			int len = pre.length();
			String sub = s.substring(len, s.length() - len);
			Manacher man = new Manacher(sub);
			int[] lens = man.p;
			int max = 0;
			boolean start = true;
			//System.out.println(sub);
			//System.out.println(Arrays.toString(lens));
			for(int j = 1;j<lens.length - 1;j++)
			{
				if(lens[j] == Math.min(j-1, lens.length - j - 2))
				{
					if(max < lens[j])
					{
						max = lens[j];
						if(lens.length - j - 2 < j-1)
						{
							start = false;
						}
					}
				}
			}
			String middle = sub.substring(0, max);
			if(!start)
				middle = sub.substring(sub.length() - max, sub.length());
			StringBuilder ans = new StringBuilder("");
			ans.append(pre);
			ans.append(middle);
			ans.append(new StringBuffer(pre).reverse().toString());
			System.out.println(ans);
		}
	}
	
	private class Manacher {
	    public int[]  p;  // p[i] = length of longest palindromic substring of t, centered at i
	    private String s;  // original string
	    private char[] t;  // transformed string

	    public Manacher(String s) {
	        this.s = s;
	        preprocess();
	        p = new int[t.length];

	        int center = 0, right = 0;
	        for (int i = 1; i < t.length-1; i++) {
	            int mirror = 2*center - i; 

	            if (right > i)
	                p[i] = Math.min(right - i, p[mirror]);
	 
	            // attempt to expand palindrome centered at i
	            while (t[i + (1 + p[i])] == t[i - (1 + p[i])])
	                p[i]++;
	 
	            // if palindrome centered at i expands past right,
	            // adjust center based on expanded palindrome.
	            if (i + p[i] > right) {
	                center = i;
	                right = i + p[i];
	            }
	        }

	    }

	    // Transform s into t.
	    // For example, if s = "abba", then t = "$#a#b#b#a#@"
	    // the # are interleaved to avoid even/odd-length palindromes uniformly
	    // $ and @ are prepended and appended to each end to avoid bounds checking
	    private void preprocess() {
	        t = new char[s.length()*2 + 3];
	        t[0] = '$';
	        t[s.length()*2 + 2] = '@';
	        for (int i = 0; i < s.length(); i++) {
	            t[2*i + 1] = '#';
	            t[2*i + 2] = s.charAt(i);
	        }
	        t[s.length()*2 + 1] = '#';
	    }
	 
	    // longest palindromic substring
	    public String longestPalindromicSubstring() {
	        int length = 0;   // length of longest palindromic substring
	        int center = 0;   // center of longest palindromic substring
	        for (int i = 1; i < p.length-1; i++) {
	            if (p[i] > length) {
	                length = p[i];
	                center = i;
	            }
	        }
	        return s.substring((center - 1 - length) / 2, (center - 1 + length) / 2);
	    }

	    // longest palindromic substring centered at index i/2
	    public String longestPalindromicSubstring(int i) {
	        int length = p[i + 2];
	        int center = i + 2;
	        return s.substring((center - 1 - length) / 2, (center - 1 + length) / 2);
	    }
	}
	
	
}