Problem: This is the hard 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 ≤ 10^5), 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 10^6.

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.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/*
1
xxabacaba
 */
public class D2 {
	static PrintWriter out = new PrintWriter(System.out);

	static char c[];
	static int N;

	public static void main(String[] args) {
		FS in = new FS();
		
	
		
		int T = in.nextInt();
		for(int runs = 1; runs <= T; runs++) {
			c = in.next().toCharArray();
			N = c.length;
			
			int best = -1;
			int prefixLen = -1;
			
			// prefix is smaller
			Eertree et = new Eertree(N);
			int lsp[] = new int[N];
			for(int i = 0; i < N; i++) {
				et.addLetter(c[i]);
				lsp[i] = et.last;
			}
			
			// just take suffix
			best = et.len[et.last];
			prefixLen = 0;
			
			// take some non-empty prefix
			int curLen = 0;
			for(int i = 0; i < N; i++) {
				if(c[i] != c[N-i-1]) break;
				curLen += 2;
				if(curLen > N) break;
				
				int lenAllowed = N-curLen;
				int cur = lsp[N-i-2];
				
				if(et.len[cur] > lenAllowed) {
					for(int p = 20; p >= 0; p--) {
						int nextLen = et.len[et.lift[cur][p]];
						if(nextLen > lenAllowed) cur = et.lift[cur][p];
					}
					cur = et.lift[cur][0];
				}
				if(et.len[cur] <= lenAllowed) {
					if(curLen + et.len[cur] > best) {
						best = curLen + et.len[cur];
						prefixLen = i+1;
					}
				}
			}
			
			
			// Now do it backwards
			et = new Eertree(N);
			for(int i = N-1; i >= 0; i--) {
				et.addLetter(c[i]);
				lsp[i] = et.last;
			}
			if(et.len[et.last] > best) {
				best = et.len[et.last];
				prefixLen = best;
			}
			
			// take some non-empty suffix
			curLen = 0;
			for(int i = 0; i < N; i++) {
				if(c[i] != c[N-i-1]) break;
				curLen += 2;
				if(curLen > N) break;
				
				int lenAllowed = N-curLen;
				int cur = lsp[i+1];
				
				if(et.len[cur] > lenAllowed) {
					for(int p = 20; p >= 0; p--) {
						int nextLen = et.len[et.lift[cur][p]];
						if(nextLen > lenAllowed) cur = et.lift[cur][p];
					}
					cur = et.lift[cur][0];
				}
				if(et.len[cur] <= lenAllowed) {
					if(curLen + et.len[cur] > best) {
						best = curLen + et.len[cur];
						prefixLen = i+1+et.len[cur];
					}
				}
			}
			
			
			
			int sufLen = best-prefixLen;
			// get pali of mid
			for(int i = 0; i < prefixLen; i++) out.print(c[i]);
			for(int i = N-sufLen; i < N; i++) out.print(c[i]);
			out.println();
		}
	
		out.close();
	}

	static class Eertree {
		int[] s, len, link, slink, diff;
		int[][] to;
		int[][] lift;
		int sz, last, n;
		long pCnt;
		public Eertree(int nn) {
			s = new int[nn+2];
			len = new int[nn+2];
			link = new int[nn+2];
			to = new int[26][nn+2];
			slink = new int[nn+2];
			diff = new int[nn+2];
			lift = new int[nn+2][21];
			s[n++] = -1;
			link[0] = link[1] = 1;
			len[1] = -1;
			sz = 2;
		}
		int getLink(int v) {
			while(s[n-len[v]-2] != s[n-1]) v = link[v];
			return v;
		}
		void addLetter(char ch) {
			int c = ch-'a';
			s[n++] = c;
			last = getLink(last);
			if(to[c][last] == 0) {
				len[sz] = len[last]+2;
				link[sz] = to[c][getLink(link[last])];
				
				lift[sz][0] = link[sz];
				int cur = lift[sz][0];
				for(int i = 1; i < 21; i++) {
					lift[sz][i] = lift[cur][i-1];
					cur = lift[sz][i];
				}
				
				diff[sz] = len[sz]-len[link[sz]];
				if(diff[sz] == diff[link[sz]]) slink[sz] = slink[link[sz]];
				else slink[sz] = link[sz];
				to[c][last] = sz++;
			}
			last = to[c][last];
		}
	}

	
	static class FS{
		BufferedReader br;
		StringTokenizer st;
		public FS() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
		String next() {
			while(st == null || !st.hasMoreElements()) {
				try {st = new StringTokenizer(br.readLine());}
				catch(Exception e) { throw null;}
			}
			return st.nextToken();
		}
		int nextInt() { return Integer.parseInt(next());}
		double nextDouble() { return Double.parseDouble(next());}
		long nextLong() { return Long.parseLong(next());}
		int[] NIA(int n) {
			int r[] = new int[n];
			for(int i = 0; i < n; i++) r[i] = nextInt();
			return r;
		}
		long[] NLA(int n) {
			long r[] = new long[n];
			for(int i = 0; i < n; i++) r[i] = nextLong();
			return r;
		}
		char[][] grid(int r, int c){
			char res[][] = new char[r][c];
			for(int i = 0; i < r; i++) {
				char l[] = next().toCharArray();
				for(int j = 0; j < c; j++) {
					res[i][j] = l[j];
				}
			}
			return res;
		}
	}
	
}