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".
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Rustam Musin (t.me/musin_acm)
 */
public class Main {
  public static void main(String[] args) {
    InputStream inputStream = System.in;
    OutputStream outputStream = System.out;
    InputReader in = new InputReader(inputStream);
    OutputWriter out = new OutputWriter(outputStream);
    D1PrefiksnoSuffiksniiPalindromUproshchennayaVersiya solver =
        new D1PrefiksnoSuffiksniiPalindromUproshchennayaVersiya();
    solver.solve(1, in, out);
    out.close();
  }

  static class D1PrefiksnoSuffiksniiPalindromUproshchennayaVersiya {
    public void solve(int testNumber, InputReader in, OutputWriter out) {
      int t = in.readInt();
      while (t-- > 0) {
        String ans = solve(in.next());
        out.printLine(ans);
      }
    }

    String solve(String s) {
      int len = 0;
      while (len < s.length() && s.charAt(len) == s.charAt(s.length() - len - 1)) {
        len++;
      }
      int ansLen = 0;
      for (IntIntPair p : manacher(s)) {
        int fromLeft = p.first;
        int fromRight = s.length() - p.second - 1;
        int m = Math.min(fromLeft, fromRight);
        if (m <= len) {
          int cur = p.second - p.first + 1 + m * 2;
          ansLen = Math.max(ansLen, cur);
        }
      }
      for (IntIntPair p : manacher(s)) {
        int fromLeft = p.first;
        int fromRight = s.length() - p.second - 1;
        int m = Math.min(fromLeft, fromRight);
        if (m <= len) {
          int cur = p.second - p.first + 1 + m * 2;
          if (cur == ansLen) {
            StringBuilder ans = new StringBuilder();
            for (int i = 0; i < m; i++) {
              ans.append(s.charAt(i));
            }
            for (int i = p.first; i <= p.second; i++) {
              ans.append(s.charAt(i));
            }
            for (int i = 0; i < m; i++) {
              ans.append(s.charAt(s.length() - m + i));
            }
            return ans.toString();
          }
        }
      }
      return null;
    }

    List<IntIntPair> manacher(String s) {
      List<IntIntPair> res = new ArrayList<>();
      int[] man = StringUtils.d1Manacher(s);
      for (int i = 0; i < man.length; i++) {
        res.add(IntIntPair.makePair(i - man[i] + 1, i + man[i] - 1));
      }
      man = StringUtils.d2Manacher(s);
      for (int i = 0; i < man.length; i++) {
        if (man[i] > 0) {
          res.add(IntIntPair.makePair(i - man[i], i + man[i] - 1));
        }
      }
      return res;
    }

  }

  static class InputReader {
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private InputReader.SpaceCharFilter filter;

    public InputReader(InputStream stream) {
      this.stream = stream;
    }

    public int read() {
      if (numChars == -1) {
        throw new InputMismatchException();
      }
      if (curChar >= numChars) {
        curChar = 0;
        try {
          numChars = stream.read(buf);
        } catch (IOException e) {
          throw new InputMismatchException();
        }
        if (numChars <= 0) {
          return -1;
        }
      }
      return buf[curChar++];
    }

    public int readInt() {
      int c = read();
      while (isSpaceChar(c)) {
        c = read();
      }
      int sgn = 1;
      if (c == '-') {
        sgn = -1;
        c = read();
      }
      int res = 0;
      do {
        if (c < '0' || c > '9') {
          throw new InputMismatchException();
        }
        res *= 10;
        res += c - '0';
        c = read();
      } while (!isSpaceChar(c));
      return res * sgn;
    }

    public String readString() {
      int c = read();
      while (isSpaceChar(c)) {
        c = read();
      }
      StringBuilder res = new StringBuilder();
      do {
        if (Character.isValidCodePoint(c)) {
          res.appendCodePoint(c);
        }
        c = read();
      } while (!isSpaceChar(c));
      return res.toString();
    }

    public boolean isSpaceChar(int c) {
      if (filter != null) {
        return filter.isSpaceChar(c);
      }
      return isWhitespace(c);
    }

    public static boolean isWhitespace(int c) {
      return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public String next() {
      return readString();
    }

    public interface SpaceCharFilter {
      public boolean isSpaceChar(int ch);

    }

  }

  static class IntIntPair implements Comparable<IntIntPair> {
    public final int first;
    public final int second;

    public static IntIntPair makePair(int first, int second) {
      return new IntIntPair(first, second);
    }

    public IntIntPair(int first, int second) {
      this.first = first;
      this.second = second;
    }

    public boolean equals(Object o) {
      if (this == o) {
        return true;
      }
      if (o == null || getClass() != o.getClass()) {
        return false;
      }

      IntIntPair pair = (IntIntPair) o;

      return first == pair.first && second == pair.second;
    }

    public int hashCode() {
      int result = first;
      result = 31 * result + second;
      return result;
    }

    public String toString() {
      return "(" + first + "," + second + ")";
    }

    public int compareTo(IntIntPair o) {
      int value = Integer.compare(first, o.first);
      if (value != 0) {
        return value;
      }
      return Integer.compare(second, o.second);
    }

  }

  static class OutputWriter {
    private final PrintWriter writer;

    public OutputWriter(OutputStream outputStream) {
      writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

    public OutputWriter(Writer writer) {
      this.writer = new PrintWriter(writer);
    }

    public void print(Object... objects) {
      for (int i = 0; i < objects.length; i++) {
        if (i != 0) {
          writer.print(' ');
        }
        writer.print(objects[i]);
      }
    }

    public void printLine(Object... objects) {
      print(objects);
      writer.println();
    }

    public void close() {
      writer.close();
    }

  }

  static class StringUtils {
    public static int[] d1Manacher(String s) {
      int n = s.length();
      int[] d1 = new int[n];
      int l = 0;
      int r = -1;
      for (int i = 0; i < n; ++i) {
        int k = (i > r ? 0 : Math.min(d1[l + r - i], r - i));
        while (i + k < n && i - k >= 0 && s.charAt(i + k) == s.charAt(i - k)) {
          ++k;
        }
        d1[i] = k--;
        if (i + k > r) {
          l = i - k;
          r = i + k;
        }
      }
      return d1;
    }

    public static int[] d2Manacher(String s) {
      int n = s.length();
      int[] d2 = new int[n];
      int l = 0;
      int r = -1;
      for (int i = 0; i < n; ++i) {
        int k = (i > r ? 0 : Math.min(d2[l + r - i + 1], r - i + 1)) + 1;
        while (i + k - 1 < n && i - k >= 0 && s.charAt(i + k - 1) == s.charAt(i - k)) {
          ++k;
        }
        d2[i] = --k;
        if (i + k - 1 > r) {
          l = i - k;
          r = i + k - 1;
        }
      }
      return d2;
    }

  }
}

