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 F {

	static int MOD = (int) 1e9 + 9, p = 31;
//	static int []MOD
	static long[] hash(char[] s) {
		int n = s.length;
		long[] ans = new long[n];
		long hash = 0;
		long p_pow = 1;
		for (int i = 0; i < n; i++) {
			hash = (hash + (s[i] - 'a' + 1) * p_pow) % MOD;
			p_pow = (p_pow * p) % MOD;
			ans[i] = hash;
		}
		return ans;
	}

	static int[][] manacher(char[] s) {
		int n = s.length, d1[] = new int[n], d2[] = new int[n];

		// Calculation of odd-length palindromes
		for (int i = 0, l = 0, r = -1; i < n; ++i) {
			int k = i > r ? 1 : Math.min(r - i + 1, d1[l + r - i]);
			while (i + k < n && i - k >= 0 && s[i + k] == s[i - k])
				++k;
			d1[i] = k--;
			if (i + k > r) {
				l = i - k;
				r = i + k;
			}
		}

		// Calculation of even-length palindromes
		// you may insert # between characters of s to get rid of the following part
		for (int i = 0, l = 0, r = -1; i < n; ++i) {
			int k = i > r ? 0 : Math.min(r - i + 1, d2[l + r - i + 1]);
			while (i + k < n && i - k - 1 >= 0 && s[i + k] == s[i - k - 1])
				++k;
			d2[i] = k--;
			if (i + k > r) {
				l = i - k - 1;
				r = i + k;
			}
		}

		return new int[][] { d1, d2 };
	}

	static char[] rev(char[] s) {
		return rev(new String(s)).toCharArray();
	}

	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 solve(char[] s) {
		int n = s.length;
		int[][] manacher = manacher(s);
		int[] d1 = manacher[0], d2 = manacher[1];
		ArrayList<Integer>[] odd = new ArrayList[n + 1], even = new ArrayList[n + 1];
		for (int i = 0; i <= n; i++) {
			odd[i] = new ArrayList();
			even[i] = new ArrayList<Integer>();
		}
		for (int i = 0; i < n; i++) {
			if (i - d1[i] >= 0)
				odd[i - d1[i]].add(i);
			if (i - d2[i] - 1 >= 0)
				even[i - d2[i] - 1].add(i);
		}
		long[] pow = new long[n + 1];
		pow[0] = 1;
		for (int i = 1; i <= n; i++)
			pow[i] = p * pow[i - 1] % MOD;
		long[] hash = hash(s), hashRev = hash(rev(s));

		int ans = 0, prefAns = -1, sufAns = -1;
		for (int i = 0; i < n; i++) {

			// check if prefix=suffix
			// if prefix palindrome ans=i+1;
			int sufIdx = n - i - 1;
			long suffixHash = hashRev[n - 1];
			long prefHash = hash[i];
			if (i != n - 1) {
				suffixHash -= hashRev[n - i - 2];
				if (suffixHash < 0)
					suffixHash += MOD;
				prefHash = prefHash * pow[n - i - 1] % MOD;

			}

			if (suffixHash == prefHash && i + 1 > ans) {
				ans = i + 1;
				prefAns = i;
				sufAns = -1;
			}
			if (sufIdx <= i)
				continue;
			if (hash[i] != hashRev[i])
				continue;
			int lo = 0, hi = odd[i].size() - 1;
			int best = i;
			while (lo <= hi) {
				int mid = lo + hi >> 1;
				int curr = odd[i].get(mid);
				if (curr + d1[curr] > sufIdx)
					hi = mid - 1;
				else {
					best = curr + d1[curr] - 1;
					lo = mid + 1;
				}
			}
			if (best + 1 + n - sufIdx > ans) {
				ans = best + 1 + n - sufIdx;
				prefAns = best;
				sufAns = sufIdx;
			}
			lo = 0;
			hi = even[i].size() - 1;
			best = i;
			while (lo <= hi) {
				int mid = lo + hi >> 1;
				int curr = even[i].get(mid);
				if (curr + d2[curr] > sufIdx)
					hi = mid - 1;
				else {
					best = curr + d2[curr] - 1;
					lo = mid + 1;
				}
			}
			if (best + 1 + n - sufIdx > ans) {
				ans = best + 1 + n - sufIdx;
				prefAns = best;
				sufAns = sufIdx;
			}

		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i <= prefAns; i++)
			sb.append(s[i]);
		for (int i = sufAns; i < n && sufAns != -1; i++)
			sb.append(s[i]);
		return sb.toString();
	}

	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();
			String ans1 = solve(s);
			String ans2 = solve(rev(s));
			if (ans1.length() > ans2.length())
				out.println(ans1);
			else
				out.println(ans2);
		}

		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;
		}
	}

}