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 implements Runnable {
	boolean judge = false;
	FastReader scn;
	PrintWriter out;
	String INPUT = "";

	void solve() {
		int t = scn.nextInt();
		for (int ii = 1; ii <= t; ii++) {
			char[] arr = scn.next().toCharArray();
			int n = arr.length;

			int[] even = palinEven(arr);
			int[] odd = palinOdd(arr);

			int lenP = 0, lenS = 0;
			for (int i = 0; i < n; i++) {
				if (i - odd[i] == 0) {
					lenP = Math.max(lenP, 1 + 2 * odd[i]);
				}
				if (i + odd[i] == n - 1) {
					lenS = Math.max(lenS, 1 + 2 * odd[i]);
				}
			}

			for (int i = 0; i < n - 1; i++) {
				if (i + 1 - even[i] == 0) {
					lenP = Math.max(lenP, 2 * even[i]);
				}
				if (i + even[i] == n - 1) {
					lenS = Math.max(lenS, 2 * even[i]);
				}
			}

			char[] ans = new char[n];
			Arrays.fill(ans, '.');
			int p = 0, q = n - 1;

			int len = 0;
			while (p <= q) {
				if (arr[p] == arr[q] && p != q) {
					ans[p] = arr[p];
					ans[q] = arr[q];
					p++;
					q--;
					len += 2;
				} else if (p == q) {
					ans[p] = arr[p];
					len++;
					p++;
					q--;
				} else {
					int x = 0, y = 0;
					for (int i = p; i < q; i++) {
						if (i - odd[i] <= p && i + odd[i] <= q) {
							x = Math.max(x, 1 + 2 * Math.min(i - p, q - i));
						}
						if (i + odd[i] >= q && i - odd[i] >= p) {
							y = Math.max(y, 1 + 2 * Math.min(i - p, q - i));
						}
					}
					for (int i = p; i < Math.min(q, n - 1); i++) {
						if (i + 1 - even[i] <= p && i + even[i] <= q) {
							x = Math.max(x, 2 * Math.min(i - p + 1, q - i));
						}
						if (i + even[i] >= q && i + 1 - even[i] >= p) {
							y = Math.max(y, 2 * Math.min(i - p + 1, q - i));
						}
					}

					if (x > y) {
						for (int i = p; i < p + x; i++) {
							ans[i] = arr[i];
							len++;
						}
					} else {
						for (int i = q, j = 0; j < y; i--, j++) {
							ans[i] = arr[i];
							len++;
						}
					}
					break;
				}
			}

			if (len > lenS && len > lenP) {
				for (char c : ans) {
					if (c != '.') {
						out.print(c);
					}
				}
			} else if (lenP > lenS) {
				for (int i = 0; i < lenP; i++) {
					out.print(arr[i]);
				}
			} else {
				for (int j = n - 1, i = 0; i < lenS; i++, j--) {
					out.print(arr[j]);
				}
			}
			out.println();
		}
	}

	int[] palinEven(char[] s) {
		int n = s.length;
		int[] a = new int[n - 1];
		int l = -1, r = -1;
		// i-a[i]+1 .. i+a[i]
		for (int i = 0; i < n - 1; ++i) {
			if (i < r) {
				a[i] = Math.min(r - i, a[l + r - i - 1]);
			}
			while (i - a[i] >= 0 && i + a[i] + 1 < n && s[i - a[i]] == s[i + a[i] + 1]) {
				a[i]++;
			}
			if (i + a[i] > r) {
				l = i - a[i] + 1;
				r = i + a[i];
			}
		}
		return a;
	}

	// assumes center to at (i)
	int[] palinOdd(char[] s) {
		int n = s.length;
		int[] a = new int[n];
		int l = -1, r = -1;
		// i-a[i] .. i+a[i]
		for (int i = 0; i < n; ++i) {
			if (i < r) {
				a[i] = Math.min(r - i, a[l + r - i]);
			}
			while (i - a[i] - 1 >= 0 && i + a[i] + 1 < n && s[i - a[i] - 1] == s[i + a[i] + 1]) {
				a[i]++;
			}
			if (i + a[i] > r) {
				l = i - a[i];
				r = i + a[i];
			}
		}
		return a;
	}

	public void run() {
		long time = System.currentTimeMillis();
		boolean oj = System.getProperty("ONLINE_JUDGE") != null || judge;
		out = new PrintWriter(System.out);
		scn = new FastReader(oj);
		solve();
		out.flush();
		if (!oj) {
			System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" }));
		}
	}

	public static void main(String[] args) {
		new Thread(null, new D(), "Main", 1 << 28).start();
	}

	class FastReader {
		InputStream is;

		public FastReader(boolean onlineJudge) {
			is = onlineJudge ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		}

		byte[] inbuf = new byte[1024];
		public int lenbuf = 0, ptrbuf = 0;

		int readByte() {
			if (lenbuf == -1)
				throw new InputMismatchException();
			if (ptrbuf >= lenbuf) {
				ptrbuf = 0;
				try {
					lenbuf = is.read(inbuf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (lenbuf <= 0)
					return -1;
			}
			return inbuf[ptrbuf++];
		}

		boolean isSpaceChar(int c) {
			return !(c >= 33 && c <= 126);
		}

		int skip() {
			int b;
			while ((b = readByte()) != -1 && isSpaceChar(b))
				;
			return b;
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		char nextChar() {
			return (char) skip();
		}

		String next() {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		String nextLine() {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			while ((!isSpaceChar(b) || b == ' ')) { // when nextLine, (isSpaceChar(b) && b != ' ')
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		char[] next(int n) {
			char[] buf = new char[n];
			int b = skip(), p = 0;
			while (p < n && !(isSpaceChar(b))) {
				buf[p++] = (char) b;
				b = readByte();
			}
			return n == p ? buf : Arrays.copyOf(buf, p);
		}

		int nextInt() {
			int num = 0, b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		long nextLong() {
			long num = 0;
			int b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		char[][] nextMatrix(int n, int m) {
			char[][] map = new char[n][];
			for (int i = 0; i < n; i++)
				map[i] = next(m);
			return map;
		}

		int[] nextIntArray(int n) {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = nextInt();
			return a;
		}

		long[] nextLongArray(int n) {
			long[] a = new long[n];
			for (int i = 0; i < n; i++)
				a[i] = nextLong();
			return a;
		}

		int[][] next2DInt(int n, int m) {
			int[][] arr = new int[n][];
			for (int i = 0; i < n; i++) {
				arr[i] = nextIntArray(m);
			}
			return arr;
		}

		long[][] next2DLong(int n, int m) {
			long[][] arr = new long[n][];
			for (int i = 0; i < n; i++) {
				arr[i] = nextLongArray(m);
			}
			return arr;
		}

		int[] shuffle(int[] arr) {
			Random r = new Random();
			for (int i = 1, j; i < arr.length; i++) {
				j = r.nextInt(i);
				int c = arr[i];
				arr[i] = arr[j];
				arr[j] = c;
			}
			return arr;
		}

		long[] shuffle(long[] arr) {
			Random r = new Random();
			for (int i = 1, j; i < arr.length; i++) {
				j = r.nextInt(i);
				long c = arr[i];
				arr[i] = arr[j];
				arr[j] = c;
			}
			return arr;
		}

		int[] uniq(int[] arr) {
			arr = scn.shuffle(arr);
			Arrays.sort(arr);
			int[] rv = new int[arr.length];
			int pos = 0;
			rv[pos++] = arr[0];
			for (int i = 1; i < arr.length; i++) {
				if (arr[i] != arr[i - 1]) {
					rv[pos++] = arr[i];
				}
			}
			return Arrays.copyOf(rv, pos);
		}

		long[] uniq(long[] arr) {
			arr = scn.shuffle(arr);
			Arrays.sort(arr);
			long[] rv = new long[arr.length];
			int pos = 0;
			rv[pos++] = arr[0];
			for (int i = 1; i < arr.length; i++) {
				if (arr[i] != arr[i - 1]) {
					rv[pos++] = arr[i];
				}
			}
			return Arrays.copyOf(rv, pos);
		}

		int[] reverse(int[] arr) {
			int l = 0, r = arr.length - 1;
			while (l < r) {
				arr[l] = arr[l] ^ arr[r];
				arr[r] = arr[l] ^ arr[r];
				arr[l] = arr[l] ^ arr[r];
				l++;
				r--;
			}
			return arr;
		}

		long[] reverse(long[] arr) {
			int l = 0, r = arr.length - 1;
			while (l < r) {
				arr[l] = arr[l] ^ arr[r];
				arr[r] = arr[l] ^ arr[r];
				arr[l] = arr[l] ^ arr[r];
				l++;
				r--;
			}
			return arr;
		}

		int[] compress(int[] arr) {
			int n = arr.length;
			int[] rv = Arrays.copyOf(arr, n);
			rv = uniq(rv);
			for (int i = 0; i < n; i++) {
				arr[i] = Arrays.binarySearch(rv, arr[i]);
			}
			return arr;
		}

		long[] compress(long[] arr) {
			int n = arr.length;
			long[] rv = Arrays.copyOf(arr, n);
			rv = uniq(rv);
			for (int i = 0; i < n; i++) {
				arr[i] = Arrays.binarySearch(rv, arr[i]);
			}
			return arr;
		}
	}
}