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.util.*;

public class D {

	static String rev(String x) {
		StringBuilder ans = new StringBuilder();
		for (int i = x.length() - 1; i >= 0; i--)
			ans.append(x.charAt(i));
		return ans.toString();
	}

	static String[] rev(String[] a) {
		String[] ans = new String[a.length];
		for (int i = 0; i < a.length; i++)
			ans[i] = rev(a[i]);
		return ans;
	}

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner();
		PrintWriter out = new PrintWriter(System.out);
		int tc = sc.nextInt();
		while (tc-- > 0) {
			char[] s = sc.next().toCharArray();
			int n = s.length;
			boolean[][] pal = new boolean[n][n];
			for (int len = 1; len <= n; len++) {
				for (int l = 0; l + len <= n; l++) {
					int r = l + len - 1;
					if (len <= 2)
						pal[l][r] = s[l] == s[r];
					else
						pal[l][r] = s[l] == s[r] && pal[l + 1][r - 1];
				}
			}
			if (pal[0][n - 1]) {
				out.println(s);
				continue;
			}

			int prefAns = -1, suffAns = -1, ans = 0;
			String[] suffixes = new String[n], prefixes = new String[n];
			for (int i = 0; i < n; i++) {
				StringBuilder sb = new StringBuilder();
				for (int j = i; j < n; j++)
					sb.append(s[j]);
				suffixes[i] = sb.toString();

			}
			for (int i = 0; i < n; i++) {
				StringBuilder sb = new StringBuilder();
				for (int j = 0; j <= i; j++)
					sb.append(s[j]);
				prefixes[i] = sb.toString();

			}
			String[] prefRev = rev(prefixes), sufRev = rev(suffixes);
			boolean[] eq = new boolean[n];
			for (int i = 0; i < n; i++)
				eq[i] = suffixes[i].equals(prefRev[n - i - 1]);
			for (int prefix = 0; prefix < n; prefix++) {
				// take complete prefix
				int sufIdx = n - (prefix + 1);
				if (sufIdx > prefix && eq[sufIdx]) {
					if (2 * (prefix + 1) > ans) {
						ans = 2 * (prefix + 1);
						prefAns = prefix;
						suffAns = sufIdx;
					}
				}
				for (int i = prefix; i > 0; i--) {

					if (!pal[i][prefix])
						continue;

					sufIdx = n - i;
					if (sufIdx > prefix && eq[sufIdx]) {
						if (prefix + 1 + n - sufIdx > ans) {
							ans = prefix + 1 + n - sufIdx;
							prefAns = prefix;
							suffAns = sufIdx;
						}

					}
				}

			}
			/////
			for (int suffIdx = 0; suffIdx < n; suffIdx++) {
				// take complete prefix
				int prefix = n - suffIdx - 1;
				if (suffIdx > prefix && eq[suffIdx]) {
					if (2 * (prefix + 1) > ans) {
						ans = 2 * (prefix + 1);
						prefAns = prefix;
						suffAns = suffIdx;
					}
				}
				for (int i = n - 1; i > suffIdx; i--) {

					if (!pal[suffIdx][i - 1])
						continue;

					int len = n - i;
					prefix = len - 1;

					if (suffIdx > prefix && eq[i]) {

						if (prefix + 1 + n - suffIdx > ans) {
							ans = prefix + 1 + n - suffIdx;
							prefAns = prefix;
							suffAns = suffIdx;
						}

					}
				}

			}
			for (int i = n - 1; i >= 0; i--) {
				if (pal[i][n - 1] && n - i > ans) {
					prefAns = -1;
					suffAns = i;
					ans = n - i;
				}
				if (pal[0][i] && i + 1 > ans) {
					prefAns = i;
					suffAns = -1;
					ans = i + 1;
				}
			}
			if (prefAns != -1)
				out.print(prefixes[prefAns]);
			if (suffAns != -1)
				out.print(suffixes[suffAns]);

			out.println();
		}
		out.close();

	}

	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

		Scanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		Scanner(String fileName) throws FileNotFoundException {
			br = new BufferedReader(new FileReader(fileName));
		}

		String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		String nextLine() throws IOException {
			return br.readLine();
		}

		int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		long nextLong() throws NumberFormatException, IOException {
			return Long.parseLong(next());
		}

		double nextDouble() throws NumberFormatException, IOException {
			return Double.parseDouble(next());
		}

		boolean ready() throws IOException {
			return br.ready();
		}

		int[] nxtArr(int n) throws IOException {
			int[] ans = new int[n];
			for (int i = 0; i < n; i++)
				ans[i] = nextInt();
			return ans;
		}

	}

	static void sort(int[] a) {
		shuffle(a);
		Arrays.sort(a);
	}

	static void shuffle(int[] a) {
		int n = a.length;
		Random rand = new Random();
		for (int i = 0; i < n; i++) {
			int tmpIdx = rand.nextInt(n);
			int tmp = a[i];
			a[i] = a[tmpIdx];
			a[tmpIdx] = tmp;
		}
	}

}