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.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Locale;
import java.util.StringTokenizer;

public class Solution implements Runnable {
	
	private PrintStream out;
	private BufferedReader in;
	private StringTokenizer st;
	
	public void solve() throws IOException {
		long time0 = System.currentTimeMillis();
		
		int t = nextInt();
		for (int test = 1; test <= t; test++) {
			String s = next();
			String answer = solve(s);
			out.println(answer);
		}
		
		System.err.println("time: " + (System.currentTimeMillis() - time0));
	}
	
	private String solve(String s) {
		int left = 0;
		int right = s.length();
		while (left < right && s.charAt(left) == s.charAt(right - 1)) {
			left++;
			right--;
		}
		if (left >= right) {
			return s;
		}
		String scut = s.substring(left, right);
		String sdup = duplicate(scut);
		SuffixArrayWithSLCP sa = new SuffixArrayWithSLCP(sdup);
		int leftLen = solveLeft(scut, sa);
		int rightLen = solveRight(scut, sa);
		if (leftLen >= rightLen) {
			return s.substring(0, left + leftLen) + s.substring(right);
		} else {
			return s.substring(0, left) + s.substring(right - rightLen);
		}
	}
	
	private int solveRight(String scut, SuffixArrayWithSLCP sa) {
		for (int answer = scut.length(); answer > 0; answer--) {
			int lcp = sa.suffixesLCP(scut.length() + 1, scut.length() - answer);
			if (lcp >= answer) {
				return answer;
			}
		}
		return 0;
	}

	private int solveLeft(String scut, SuffixArrayWithSLCP sa) {
		for (int answer = scut.length(); answer > 0; answer--) {
			int lcp = sa.suffixesLCP(0, scut.length() + 1 + (scut.length() - answer));
			if (lcp >= answer) {
				return answer;
			}
		}
		return 0;
	}

	private String duplicate(String s) {
		StringBuilder buf = new StringBuilder();
		buf.append(s);
		buf.append("#");
		for (int i = s.length() - 1; i >= 0; i--) {
			buf.append(s.charAt(i));
		}
		return buf.toString();
	}

	public class SegmentMin {
		private int[] pow;
		private int[] fln;
		private int[][] a;
		public SegmentMin(int[] v) {
			fln = new int[v.length + 1];
			fln[0] = -1;
			for (int i = 1; i < fln.length; i++) {
				fln[i] = fln[i / 2] + 1;
			}
			pow = new int[fln[v.length] + 1];
			pow[0] = 1;
			for (int i = 1; i < pow.length; i++) {
				pow[i] = pow[i - 1] * 2;
			}
			a = new int[pow.length][v.length];
			for (int i = 0; i < v.length; i++) {
				a[0][i] = v[i];
			}
			for (int p = 1, len = 2; p < pow.length; p += 1, len *= 2) {
				for (int i = 0; i + len <= v.length; i++) {
					a[p][i] = Math.min(a[p - 1][i], a[p - 1][i + len / 2]);
				}
			}
		}
		public int segmin(int from, int to) {
			int p = fln[to - from];
			return Math.min(a[p][from], a[p][to - pow[p]]);
		}
	}
	
	public class SuffixArrayWithSLCP extends SuffixArrayWithLCP {
		public SegmentMin seglcp;
		public SuffixArrayWithSLCP(String s) {
			super(s);
			seglcp = new SegmentMin(lcp);
			lcp = null;
		}
		public int suffixesLCP(int u1, int u2) {
			int i1 = u2i[u1];
			int i2 = u2i[u2];
			if (i1 < i2) {
				return seglcp.segmin(i1 + 1, i2 + 1);
			} else if (i1 > i2) {
				return seglcp.segmin(i2 + 1, i1 + 1);
			} else {
				return u2i.length - 1 - u1/* equals u2 */;
			}
		}
	}

	public class SuffixArrayWithLCP extends SuffixArray {
		public int[] lcp;
		public SuffixArrayWithLCP(String s) {
			super(s);
			lcp = new int[u2i.length];
			lcp[0] = Integer.MIN_VALUE;
			int k = 0;
			for (int u = 0; u < u2i.length - 1; u++) {
				int v = i2u[u2i[u] - 1];
				while (u + k < s.length() && v + k < s.length() && 
						s.charAt(u + k) == s.charAt(v + k)) {
					k++;
				}
				lcp[u2i[u]] = k;
				k = Math.max(0, k - 1);
			}
		}
	}

	public class SuffixArray {
		private static final int MAXCHAR = 255;
		public int[] i2u;
		public int[] u2i;
		public SuffixArray(String s) {
			u2i = new int[s.length() + 1];
			for (int u = 0; u < s.length(); u++) {
				u2i[u] = s.charAt(u);
			}
			u2i[s.length()] = 0;
			
			i2u = new int[u2i.length];
			for (int i = 0; i < i2u.length; i++) {
				i2u[i] = i;
			}
			
			int[] val_buf = new int[u2i.length];
			int[] cnt_buf = new int[Math.max(MAXCHAR, i2u.length/* equals u2i.length */)];
			
			for (int k = 1; true; k *= 2) {
				count_sort(val_buf, cnt_buf);
				assign_classes(val_buf, k / 2);
				if (u2i[i2u[i2u.length - 1]] == i2u.length - 1) {
					break;
				}
				for (int i = 0; i < i2u.length; i++) {
					i2u[i] = (i2u[i] - k + u2i.length) % u2i.length;
				}
			}
		}
		private void assign_classes(int[] u2i_buf, int k) {
			u2i_buf[i2u[0]] = 0;
			for (int i = 1; i < i2u.length; i++) {
				if (u2i[i2u[i]] == u2i[i2u[i - 1]] && u2i[(i2u[i] + k) % u2i.length] == u2i[(i2u[i - 1] + k) % u2i.length]) {
					u2i_buf[i2u[i]] = u2i_buf[i2u[i - 1]];
				} else {
					u2i_buf[i2u[i]] = u2i_buf[i2u[i - 1]] + 1;
				}
			}
			flush_buf(u2i, u2i_buf);
		}
		private void count_sort(int[] i2u_buf, int[] cnt_buf) {
			Arrays.fill(cnt_buf, 0);
			for (int u = 0; u < u2i.length; u++) {
				cnt_buf[u2i[u]]++;
			}
			int cnt_sum = 0;
			for (int c = 0; c < cnt_buf.length; c++) {
				cnt_sum = cnt_sum + cnt_buf[c];
				cnt_buf[c] = cnt_sum - cnt_buf[c];
			}
			for (int i = 0; i < i2u.length; i++) {
				i2u_buf[cnt_buf[u2i[i2u[i]]]++] = i2u[i];
			}
			flush_buf(i2u, i2u_buf);
		}
		private void flush_buf(int[] a, int[] buf) {
			for (int i = 0; i < a.length; i++) {
				a[i] = buf[i];
			}
		}
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}
	
	public long nextLong() throws IOException {
		return Long.parseLong(next());
	}
	
	public int nextInt() throws IOException {
		return Integer.parseInt(next());
	}
	
	public String next() throws IOException {
		while (!st.hasMoreTokens()) {
			String line = in.readLine();
			if (line == null) {
				return null;
			}
			st = new StringTokenizer(line);
		}
		return st.nextToken();
	}
	
	@Override
	public void run() {
		try {
			solve();
			out.close();
		} catch (Throwable e) {
			throw new RuntimeException(e);
		}
	}
	
	public Solution(String name) throws IOException {
		Locale.setDefault(Locale.US);
		if (name == null) {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintStream(new BufferedOutputStream(System.out));
		} else {
			in = new BufferedReader(new InputStreamReader(new FileInputStream(name + ".in")));
			out = new PrintStream(new BufferedOutputStream(new FileOutputStream(name + ".out")));
		}
		st = new StringTokenizer("");
	}
	
	public static void main(String[] args) throws IOException {
		new Thread(new Solution(null)).start();
	}
}