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.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;


import java.util.Random;
import java.util.TreeSet;

public final class CF_GlobalRound_7_D
{


	static boolean verb=true;
	static void log(Object X){if (verb) System.err.println(X);}
	static void log(Object[] X){if (verb) {for (Object U:X) System.err.print(U+" ");System.err.println("");}}
	static void log(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");System.err.println("");}}
	static void log(int[] X,int L){if (verb) {for (int i=0;i<L;i++) System.err.print(X[i]+" ");System.err.println("");}}
	static void log(long[] X){if (verb) {for (long U:X) System.err.print(U+" ");System.err.println("");}}

	static void logWln(Object X){if (verb) System.err.print(X);}
	static void info(Object o){	System.out.println(o);}
	static void output(Object o){outputWln(""+o+"\n");	}
	static void outputFlush(Object o){try {out.write(""+ o+"\n");out.flush();} catch (Exception e) {}}
	static void outputWln(Object o){try {out.write(""+ o);} catch (Exception e) {}}



	// Global vars
	static BufferedWriter out;
	static InputReader reader;



	static void test() {
		log("testing");

		Random r=new Random();
		int NTESTS=100000;
		int NMAX=10;
		for (int t=0;t<NTESTS;t++) {
		}

		log("done");

	}


	static int pgcd(int a,int b) {
		if (a<b)
			return pgcd(b,a);
		while (b>0) {
			int c=b;
			b=a%b;
			a=c;
		}
		return a;

	}

	static long pgcd(long a,long b) {
		if (a<b)
			return pgcd(b,a);
		while (b>0) {
			long c=b;
			b=a%b;
			a=c;
		}
		return a;

	}

	static class Composite implements Comparable<Composite>{
		int v;
		int idx;

		public int compareTo(Composite X) {
			if (v!=X.v)
				return X.v-v;
			return idx-X.idx;
		}

		public Composite(int v, int idx) {
			this.v = v;
			this.idx = idx;
		}





	}





	static String solveBourrin(String s) {
		int L=s.length();
		int i=0;
		int j=L-1;
		while (i+1<=j-1 && s.charAt(i)==s.charAt(j)){
			i++;
			j--;
			
		}
		if (i==j)
			return s;
		
		if (j==i+1 && s.charAt(i)==s.charAt(j))
			return s;
	
		
		
		//log("first step: i:"+i+" j:"+i+" "+s.substring(0,i)+" # "+s.substring(j+1,L));
		// now need to find border left or border right
		// fisrt border left
		int tu=i;
		for (int u=i+1;u<=j;u++) {
			if (s.charAt(u)==s.charAt(i)) {
				int e=i;
				int f=u;
				while (e<=f && s.charAt(e)==s.charAt(f)) {
					e++;
					f--;
				}
				if (e>=f) {
					tu=u;
				}
				
			}
		}
		//log("best tu:"+tu);
		
		int tv=j;
		for (int v=j-1;v>=i;v--) {
			if (s.charAt(v)==s.charAt(j)) {
				int e=v;
				int f=j;
				while (e<=f && s.charAt(e)==s.charAt(f)) {
					e++;
					f--;
				}
				if (e>=f) {
					tv=v;
				}
				
			}
			
		}
		if (j-tv>tu-i) 
			return s.substring(0,i)+s.substring(tv,L);
			
		else
			return s.substring(0,tu+1)+s.substring(j+1,L);
		
		
	}


	static void process() throws Exception {

		//log("hello");

		out = new BufferedWriter(new OutputStreamWriter(System.out));
		reader = new InputReader(System.in);


		// test


		int T=reader.readInt();
		for (int t=0;t<T;t++) {
			String s=reader.readString();
			int L=s.length();
			String ans="";
			if (L==1)
				ans=s;
			else {
			ans=solveBourrin(s);
			
			}
			output(ans);


		}

		try {
			out.close();
		} catch (Exception Ex) {
		}

	}





	public static void main(String[] args) throws Exception {
		process();

	}

	static final class InputReader {
		private final InputStream stream;
		private final byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public InputReader(InputStream stream) {
			this.stream = stream;
		}

		private int read() throws IOException {
			if (curChar >= numChars) {
				curChar = 0;
				numChars = stream.read(buf);
				if (numChars <= 0) {
					return -1;
				}
			}
			return buf[curChar++];
		}

		public final String readString() throws IOException {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			StringBuilder res = new StringBuilder();
			do {
				res.append((char) c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public final String readString(int L) throws IOException {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			StringBuilder res = new StringBuilder(L);
			do {
				res.append((char) c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public final int readInt() throws IOException {
			int c = read();
			boolean neg = false;
			while (isSpaceChar(c)) {
				c = read();
			}
			char d = (char) c;
			// log("d:"+d);
			if (d == '-') {
				neg = true;
				c = read();
			}
			int res = 0;
			do {
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			// log("res:"+res);
			if (neg)
				return -res;
			return res;

		}

		public final long readLong() throws IOException {
			int c = read();
			boolean neg = false;
			while (isSpaceChar(c)) {
				c = read();
			}
			char d = (char) c;
			// log("d:"+d);
			if (d == '-') {
				neg = true;
				c = read();
			}
			long res = 0;
			do {
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			// log("res:"+res);
			if (neg)
				return -res;
			return res;

		}

		private boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}
	}

}
/*

5
1 4 5 2 3

 */