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$$$.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 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$$$.

For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them.

5
a
abcdfdcecba
abbaxyzyx
codeforces
acbba

NoteIn 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".

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(scanner *bufio.Scanner) (a int) {
	scanner.Scan()
	readInt(scanner.Bytes(), 0, &a)
	return
}

func readTwoNums(scanner *bufio.Scanner) (a int, b int) {
	res := readNNums(scanner, 2)
	a, b = res[0], res[1]
	return
}

func readNNums(scanner *bufio.Scanner, n int) []int {
	res := make([]int, n)
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
	return res
}

func fillNNums(scanner *bufio.Scanner, n int, res []int) {
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func readInt64(bytes []byte, from int, val *int64) int {
	i := from
	var tmp int64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int64(bytes[i]-'0')
		i++
	}
	*val = tmp
	return i
}

func readNInt64Nums(scanner *bufio.Scanner, n int) []int64 {
	res := make([]int64, n)
	x := -1
	scanner.Scan()
	for i := 0; i < n; i++ {
		x = readInt64(scanner.Bytes(), x+1, &res[i])
	}
	return res
}

func main() {
	reader := bufio.NewReader(os.Stdin)

	line, _ := reader.ReadString('\n')

	tc, _ := strconv.Atoi(strings.TrimSpace(line))

	for tc > 0 {
		tc--
		s, _ := reader.ReadString('\n')
		if len(s) == 0 {
			fmt.Println("no input read")
		} else {
			fmt.Println(solve(strings.TrimSpace(s)))
		}
	}
}

func solve(s string) string {
	var l, r int = 0, len(s) - 1
	for l <= r && s[l] == s[r] {
		l++
		r--
	}
	if l > r {
		return s
	}
	// l < r
	x := s[l : r+1]

	a := findLongestPrefixPalindrome(x)
	b := findLongestPrefixPalindrome(reverse(x))

	if len(a) < len(b) {
		a, b = b, a
	}

	return s[:l] + a + s[r+1:]
}

func findLongestPrefixPalindrome(s string) string {
	x := s + "#" + reverse(s)

	var l int

	pi := make([]int, len(x))

	for i := 1; i < len(x); i++ {
		for l > 0 && x[l] != x[i] {
			l = pi[l-1]
		}
		if x[l] == x[i] {
			l++
		}
		pi[i] = l
	}

	return s[:l]
}

func reverse(s string) string {
	buf := []byte(s)
	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		buf[i], buf[j] = buf[j], buf[i]
	}
	return string(buf)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type node struct {
	l, r, id int
}

type nodes []node

func (a nodes) Len() int           { return len(a) }
func (a nodes) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a nodes) Less(i, j int) bool { return a[i].r > a[j].r }

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	nn := 0
	fmt.Fscanf(in, "%d\n", &nn)
	for ii := 0; ii < nn; ii++ {
		s := ""
		fmt.Fscanf(in, "%s\n", &s)
		if len(s) == 1 {
			fmt.Fprintf(out, "%s\n", s)
			continue
		}
		l := 0
		r := len(s) - 1

		for l < r {
			if s[l] == s[r] {
				l++
				r--
			} else {
				break
			}
		}
		if l >= r {
			fmt.Fprintf(out, "%s\n", s)
			continue
		}
		fmt.Fprintf(out, "%s\n", s[:l]+getMax(s[l:r+1])+s[r+1:])
	}
	return
}

func getMax(s string) string {
	l, r := getLeft(s), getRight(s)
	if len(l) > len(r) {
		return l
	}
	return r
}

func getLeft(s string) string {
	a := s
	b := reverse(s)
	return kmp(a, b)
}

func reverse(s string) string {
	ans := []byte{}
	for i := 0; i < len(s); i++ {
		ans = append(ans, s[len(s)-1-i])
	}
	return string(ans)
}

func isP(s string) bool {
	for i := 0; i < len(s); i++ {
		j := len(s) - i - 1
		if s[i] != s[j] {
			return false
		}
	}
	return true
}

func getRight(s string) string {
	a := s
	b := reverse(s)
	return kmp(b, a)
}

func kmp(a, b string) string {
	ans := ""
	m := len(b)
	f := make([]int, m)
	f[0] = -1
	for i := 1; i < m; i++ {
		j := f[i-1]
		for b[j+1] != b[i] && j >= 0 {
			j = f[j]
		}
		if b[j+1] == b[i] {
			f[i] = j + 1
		} else {
			f[i] = -1
		}
	}
	i := 0
	j := 0
	n := len(a)
	for i < n {
		if a[i] == b[j] {
			i++
			j++
			if j == m {
				//printf("%d\n",i-m+1);
				j = f[j-1] + 1
			}
		} else {
			if j == 0 {
				i++
			} else {
				j = f[j-1] + 1
			}
		}
		// match j
		x1 := 0
		y1 := j - 1
		x2 := n - 1 - (i - 1)
		y2 := y1 - x1 + x2
		if x2 <= y1+1 {
			if (y2 + 1) > len(ans) {
				ans = b[:y2+1]
				//fmt.Println("ans found!", ans)
			}
		}
	}
	return ans
}

func work(a []int, l int, m int) bool {
	//fmt.Println(a, l, m)
	n := len(a)
	numOfSeg := n / l
	for i := 1; i < numOfSeg; i++ {
		if a[i*l]-a[(i-1)*l] != (a[0] + m - a[(numOfSeg-1)*l]) {
			//fmt.Println(a, l, "debug")
			return false
		}
	}
	for j := 1; j < l; j++ {
		for i := 1; i < n/l; i++ {
			if a[i*l+j]-a[i*l+j-1] != a[j]-a[j-1] {
				//fmt.Println(a, l, "debug2")
				return false
			}
		}
	}
	return true
}

func gcd(a, b int) int {
	if a%b == 0 {
		return b
	}
	return gcd(b, a%b)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}
func NextInt64() int64 {
	bytes := NextBytes()
	i, err := Atoi64(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func Atoi64(b []byte) (int64, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	var n int64
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int64(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

func reverseInts(a []int) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

func unique(a []int) int {
	newLen := 0
	n := len(a)
	for i := 0; i < n; i++ {
		if newLen == 0 || a[i] != a[newLen-1] {
			a[newLen] = a[i]
			newLen++
		}
	}
	return newLen
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

// p - should be of length n+1
// it's a function of length
func kmp(s []byte, p []int) {
	n := len(s)
	p[0] = 0
	p[1] = 0
	for i := 1; i < n; i++ {
		k := p[i]
		for k > 0 && s[i] != s[k] {
			k = p[k]
		}
		if s[i] == s[k] {
			k++
		}
		p[i+1] = k
	}
}

func main() {
	inf := os.Stdin
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	// inf, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1000010)

	T := NextInt()
	ts := make([]byte, 2000001)
	prf := make([]int, 2000002)

	for t := 0; t < T; t++ {
		s := NextBytes()
		n := len(s)
		var baseL int
		for baseL = 0; baseL < n-baseL-1 && s[baseL] == s[n-baseL-1]; baseL++ {
		}
		ns := s[baseL : n-baseL]
		nn := n - baseL*2
		ts[nn] = '#'
		for i := 0; i < nn; i++ {
			ts[i] = ns[i]
			ts[nn+nn-i] = ns[i]
		}
		kmp(ts[:2*nn+1], prf)
		l := baseL + prf[2*nn+1]
		r := baseL
		result := l + r
		for i := 0; i < nn; i++ {
			ts[i], ts[nn+1+i] = ts[nn+1+i], ts[i]
		}
		kmp(ts[:2*nn+1], prf)
		cl := baseL
		cr := baseL + prf[2*nn+1]
		if cl+cr > result {
			result = cl + cr
			l = cl
			r = cr
		}
		fmt.Fprint(out, string(s[:l]))
		if r > 0 {
			fmt.Fprint(out, string(s[n-r:n]))
		}
		fmt.Fprintln(out)
	}

}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1326D2(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}
	var maxLen []int
	manacher := func(origin string) {
		n := len(origin)
		s := make([]byte, 2*n+3)
		s[0] = '^'
		for i := range origin {
			s[2*i+1] = '#'
			s[2*i+2] = origin[i]
		}
		s[2*n+1] = '#'
		s[2*n+2] = '$'
		maxLen = make([]int, 2*n+3)
		var mid, right int
		for i := 1; i < 2*n+2; i++ {
			if i < right {
				maxLen[i] = min(maxLen[2*mid-i], right-i)
			} else {
				maxLen[i] = 1
			}
			for s[i+maxLen[i]] == s[i-maxLen[i]] {
				maxLen[i]++
			}
			if right < i+maxLen[i] {
				mid = i
				right = i + maxLen[i]
			}
		}
	}
	q := func(l, r int) bool { return maxLen[l+r+2]-1 >= r-l+1 }

	var t int
	var s string
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &s)
		i, j := 0, len(s)-1
		for i < j && s[i] == s[j] {
			i++
			j--
		}
		if i >= j {
			Fprintln(out, s)
			continue
		}
		l, r := i, j
		manacher(s)
		for ; r > i && !q(i, r); r-- {
		}
		for ; l < j && !q(l, j); l++ {
		}
		if r-i > j-l {
			Fprintln(out, s[:r+1]+s[j+1:])
		} else {
			Fprintln(out, s[:i]+s[l:])
		}
	}
}

func main() { CF1326D2(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1326D2(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}
	var maxLen []int
	manacher := func(origin string) {
		n := len(origin)
		s := make([]byte, 2*n+3)
		s[0] = '^'
		for i := range origin {
			s[2*i+1] = '#'
			s[2*i+2] = origin[i]
		}
		s[2*n+1] = '#'
		s[2*n+2] = '$'
		maxLen = make([]int, 2*n+3)
		var mid, right int
		for i := 1; i < 2*n+2; i++ {
			if i < right {
				maxLen[i] = min(maxLen[2*mid-i], right-i)
			} else {
				maxLen[i] = 1
			}
			for s[i+maxLen[i]] == s[i-maxLen[i]] {
				maxLen[i]++
			}
			if right < i+maxLen[i] {
				mid = i
				right = i + maxLen[i]
			}
		}
	}
	q := func(l, r int) bool { return maxLen[l+r+2] >= r-l+1 }

	var t int
	var s string
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &s)
		i, j := 0, len(s)-1
		for i < j && s[i] == s[j] {
			i++
			j--
		}
		if i >= j {
			Fprintln(out, s)
			continue
		}
		l, r := i, j
		manacher(s)
		for ; r > i && !q(i, r); r-- {
		}
		for ; l < j && !q(l, j); l++ {
		}
		if r-i > j-l {
			Fprintln(out, s[:r+1]+s[j+1:])
		} else {
			Fprintln(out, s[:i]+s[l:])
		}
	}
}

func main() { CF1326D2(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	"fmt"
	"os"
)

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(scanner *bufio.Scanner) (a int) {
	scanner.Scan()
	readInt(scanner.Bytes(), 0, &a)
	return
}

func readTwoNums(scanner *bufio.Scanner) (a int, b int) {
	res := readNNums(scanner, 2)
	a, b = res[0], res[1]
	return
}

func readNNums(scanner *bufio.Scanner, n int) []int {
	res := make([]int, n)
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
	return res
}

func fillNNums(scanner *bufio.Scanner, n int, res []int) {
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func readInt64(bytes []byte, from int, val *int64) int {
	i := from
	var tmp int64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int64(bytes[i]-'0')
		i++
	}
	*val = tmp
	return i
}

func readNInt64Nums(scanner *bufio.Scanner, n int) []int64 {
	res := make([]int64, n)
	x := -1
	scanner.Scan()
	for i := 0; i < n; i++ {
		x = readInt64(scanner.Bytes(), x+1, &res[i])
	}
	return res
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	tc := readNum(scanner)

	for tc > 0 {
		tc--
		scanner.Scan()
		fmt.Println(solve(scanner.Text()))
	}
}

const MOD = 1000000007
const BASE = 26

var DD []int64

const MAX_N = 1000007

func init() {
	DD = make([]int64, MAX_N)

	DD[0] = 1
	for i := 1; i < MAX_N; i++ {
		DD[i] = (DD[i-1] * BASE) % MOD
	}
}

func solve(S string) string {
	n := len(S)
	x, y := find(S)
	R := reverse(S)
	a, b := find(R)

	if x >= a {
		return S[:x-y] + S[n-y:]
	}
	return reverse(R[:a-b] + S[n-b:])
}

func find(a string) (int, int) {
	// find a[..y..x] + b[n- y + 1:n] where a[y...x] is palindrome
	// b is already reversed
	n := len(a)
	b := reverse(a)

	A := hash(a)
	B := hash(b)

	isPalindrome := func(l, r int) bool {
		//a[l:r]
		x := getHash(A, l, r)
		y := getHash(B, n-r, n-l)
		if x != y {
			return false
		}
		for i := l; i < r; i++ {
			if a[i] != b[n-i-1] {
				return false
			}
		}
		return true
	}

	check := func(l int) int {
		if l <= 1 {
			return 0
		}

		if isPalindrome(0, l) {
			return 0
		}

		for y := 1; y <= l; y++ {
			// check whether a[...y] == b[...y] and a[y...x] is palindrom
			// len(a[.y..x] + b[y..]) = l
			x := l - y
			if x >= n-y {
				return -1
			}

			if a[y-1] != b[y-1] {
				return -1
			}

			if isPalindrome(y, x) {
				return y
			}

		}
		return -1
	}

	l := 1
	r := n + 1

	ans := -1

	for l < r {
		mid := (l + r) / 2
		tmp := check(mid)
		if tmp < 0 {
			r = mid
		} else {
			ans = tmp
			l = mid + 1
		}
	}

	return r - 1, ans
}

func getHash(H []int64, l, r int) int64 {
	res := H[r-1]
	if l == 0 {
		return res
	}
	tmp := H[l-1] * DD[r-l]
	tmp %= MOD
	return (res - tmp + MOD) % MOD
}

func reverse(s string) string {
	buf := []byte(s)
	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		buf[i], buf[j] = buf[j], buf[i]
	}

	return string(buf)
}

func reverseInts(arr []int64) {
	for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
		arr[i], arr[j] = arr[j], arr[i]
	}
}

func hash(s string) []int64 {
	n := len(s)
	arr := make([]int64, n)

	var sum int64

	for i := 0; i < n; i++ {
		sum = (sum*BASE + int64(s[i]-'a')) % MOD
		arr[i] = sum
	}

	return arr
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(scanner *bufio.Scanner) (a int) {
	scanner.Scan()
	readInt(scanner.Bytes(), 0, &a)
	return
}

func readTwoNums(scanner *bufio.Scanner) (a int, b int) {
	res := readNNums(scanner, 2)
	a, b = res[0], res[1]
	return
}

func readNNums(scanner *bufio.Scanner, n int) []int {
	res := make([]int, n)
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
	return res
}

func fillNNums(scanner *bufio.Scanner, n int, res []int) {
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func readInt64(bytes []byte, from int, val *int64) int {
	i := from
	var tmp int64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int64(bytes[i]-'0')
		i++
	}
	*val = tmp
	return i
}

func readNInt64Nums(scanner *bufio.Scanner, n int) []int64 {
	res := make([]int64, n)
	x := -1
	scanner.Scan()
	for i := 0; i < n; i++ {
		x = readInt64(scanner.Bytes(), x+1, &res[i])
	}
	return res
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	tc := readNum(scanner)

	for tc > 0 {
		tc--
		scanner.Scan()
		fmt.Println(solve(scanner.Text()))
	}
}

func solve(s string) string {
	var l, r int = 0, len(s) - 1
	for l <= r && s[l] == s[r] {
		l++
		r--
	}
	if l > r {
		return s
	}
	// l < r
	x := s[l : r+1]

	a := findLongestPrefixPalindrome(x)
	b := findLongestPrefixPalindrome(reverse(x))

	if len(a) < len(b) {
		a, b = b, a
	}

	return s[:l] + a + s[r+1:]
}

func findLongestPrefixPalindrome(s string) string {
	x := s + "#" + reverse(s)

	var l int

	pi := make([]int, len(x))

	for i := 1; i < len(x); i++ {
		for l > 0 && x[l] != x[i] {
			l = pi[l-1]
		}
		if x[l] == x[i] {
			l++
		}
		pi[i] = l
	}

	return s[:l]
}

func reverse(s string) string {
	buf := []byte(s)
	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		buf[i], buf[j] = buf[j], buf[i]
	}
	return string(buf)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(scanner *bufio.Scanner) (a int) {
	scanner.Scan()
	readInt(scanner.Bytes(), 0, &a)
	return
}

func readTwoNums(scanner *bufio.Scanner) (a int, b int) {
	res := readNNums(scanner, 2)
	a, b = res[0], res[1]
	return
}

func readNNums(scanner *bufio.Scanner, n int) []int {
	res := make([]int, n)
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
	return res
}

func fillNNums(scanner *bufio.Scanner, n int, res []int) {
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func readInt64(bytes []byte, from int, val *int64) int {
	i := from
	var tmp int64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int64(bytes[i]-'0')
		i++
	}
	*val = tmp
	return i
}

func readNInt64Nums(scanner *bufio.Scanner, n int) []int64 {
	res := make([]int64, n)
	x := -1
	scanner.Scan()
	for i := 0; i < n; i++ {
		x = readInt64(scanner.Bytes(), x+1, &res[i])
	}
	return res
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	tc := readNum(scanner)

	for tc > 0 {
		tc--
		scanner.Scan()
		fmt.Println(solve(scanner.Text()))
	}
}

const MOD = 10000000007
const BASE = 26

var DD []int64

const MAX_N = 1000007

func init() {
	DD = make([]int64, MAX_N)

	DD[0] = 1
	for i := 1; i < MAX_N; i++ {
		DD[i] = (DD[i-1] * BASE) % MOD
	}
}

func solve(S string) string {
	n := len(S)
	x, y := find(S)
	R := reverse(S)
	a, b := find(R)

	if x >= a {
		return S[:x-y] + S[n-y:]
	}
	return reverse(R[:a-b] + S[n-b:])
}

func find(a string) (int, int) {
	// find a[..y..x] + b[n- y + 1:n] where a[y...x] is palindrome
	// b is already reversed
	n := len(a)
	b := reverse(a)

	A := hash(a)
	B := hash(b)

	isPalindrome := func(l, r int) bool {
		//a[l:r]
		x := getHash(A, l, r)
		y := getHash(B, n-r, n-l)
		if x != y {
			return false
		}
		for i := l; i < r; i++ {
			if a[i] != b[n-i-1] {
				return false
			}
		}
		return true
	}

	check := func(l int) int {
		if l <= 1 {
			return 0
		}

		if isPalindrome(0, l) {
			return 0
		}

		for y := 1; y <= l; y++ {
			// check whether a[...y] == b[...y] and a[y...x] is palindrom
			// len(a[.y..x] + b[y..]) = l
			x := l - y
			if x >= n-y {
				return -1
			}

			if a[y-1] != b[y-1] {
				return -1
			}

			if isPalindrome(y, x) {
				return y
			}

		}
		return -1
	}

	l := 1
	r := n + 1

	ans := -1

	for l < r {
		mid := (l + r) / 2
		tmp := check(mid)
		if tmp < 0 {
			r = mid
		} else {
			ans = tmp
			l = mid + 1
		}
	}

	return r - 1, ans
}

func getHash(H []int64, l, r int) int64 {
	res := H[r-1]
	if l == 0 {
		return res
	}
	tmp := H[l-1] * DD[r-l]
	tmp %= MOD
	return (res - tmp + MOD) % MOD
}

func reverse(s string) string {
	buf := []byte(s)
	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		buf[i], buf[j] = buf[j], buf[i]
	}

	return string(buf)
}

func reverseInts(arr []int64) {
	for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
		arr[i], arr[j] = arr[j], arr[i]
	}
}

func hash(s string) []int64 {
	n := len(s)
	arr := make([]int64, n)

	var sum int64

	for i := 0; i < n; i++ {
		sum = (sum*BASE + int64(s[i]-'a')) % MOD
		arr[i] = sum
	}

	return arr
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(scanner *bufio.Scanner) (a int) {
	scanner.Scan()
	readInt(scanner.Bytes(), 0, &a)
	return
}

func readTwoNums(scanner *bufio.Scanner) (a int, b int) {
	res := readNNums(scanner, 2)
	a, b = res[0], res[1]
	return
}

func readNNums(scanner *bufio.Scanner, n int) []int {
	res := make([]int, n)
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
	return res
}

func fillNNums(scanner *bufio.Scanner, n int, res []int) {
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func readInt64(bytes []byte, from int, val *int64) int {
	i := from
	var tmp int64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int64(bytes[i]-'0')
		i++
	}
	*val = tmp
	return i
}

func readNInt64Nums(scanner *bufio.Scanner, n int) []int64 {
	res := make([]int64, n)
	x := -1
	scanner.Scan()
	for i := 0; i < n; i++ {
		x = readInt64(scanner.Bytes(), x+1, &res[i])
	}
	return res
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	tc := readNum(scanner)

	for tc > 0 {
		tc--
		scanner.Scan()
		fmt.Println(solve(scanner.Text()))
	}
}

func solve(s string) string {
	var l, r int = 0, len(s) - 1
	for l <= r && s[l] == s[r] {
		l++
		r--
	}
	if l >= r {
		return s
	}
	// l < r
	x := s[l : r+1]

	a := findLongestPrefixPalindrome(x)
	b := findLongestPrefixPalindrome(reverse(x))

	if len(a) < len(b) {
		a, b = b, a
	}

	return s[:l] + a + s[r+1:]
}

func findLongestPrefixPalindrome(s string) string {
	x := s + "#" + reverse(s)

	var l int

	pi := make([]int, len(x))

	for i := 1; i < len(x); i++ {
		for l > 0 && x[l] != x[i] {
			l = pi[l-1]
		}
		if x[l] == x[i] {
			l++
		}
		pi[i] = l
	}

	return s[:l]
}

func reverse(s string) string {
	buf := []byte(s)
	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		buf[i], buf[j] = buf[j], buf[i]
	}
	return string(buf)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(scanner *bufio.Scanner) (a int) {
	scanner.Scan()
	readInt(scanner.Bytes(), 0, &a)
	return
}

func readTwoNums(scanner *bufio.Scanner) (a int, b int) {
	res := readNNums(scanner, 2)
	a, b = res[0], res[1]
	return
}

func readNNums(scanner *bufio.Scanner, n int) []int {
	res := make([]int, n)
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
	return res
}

func fillNNums(scanner *bufio.Scanner, n int, res []int) {
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func readInt64(bytes []byte, from int, val *int64) int {
	i := from
	var tmp int64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int64(bytes[i]-'0')
		i++
	}
	*val = tmp
	return i
}

func readNInt64Nums(scanner *bufio.Scanner, n int) []int64 {
	res := make([]int64, n)
	x := -1
	scanner.Scan()
	for i := 0; i < n; i++ {
		x = readInt64(scanner.Bytes(), x+1, &res[i])
	}
	return res
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	tc := readNum(scanner)

	for tc > 0 {
		tc--
		scanner.Scan()
		fmt.Println(solve(scanner.Text()))
	}
}

func solve(s string) string {
	var l, r int = 0, len(s) - 1
	for l < r && s[l] == s[r] {
		l++
		r--
	}
	if l >= r {
		return s
	}
	// l < r
	x := s[l : r+1]

	a := findLongestPrefixPalindrome(x)
	b := findLongestPrefixPalindrome(reverse(x))

	if len(a) >= len(b) {
		return s[:l] + a + s[r+1:]
	}

	return s[:l] + b + s[r+1:]
}

func findLongestPrefixPalindrome(s string) string {
	x := s + "#" + reverse(s)

	var l int

	pi := make([]int, len(x))

	for i := 1; i < len(x); i++ {
		for l > 0 && x[l] != x[i] {
			l = pi[l-1]
		}
		if x[l] == x[i] {
			l++
		}
		pi[i] = l
	}

	return s[:l]
}

func reverse(s string) string {
	buf := []byte(s)
	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		buf[i], buf[j] = buf[j], buf[i]
	}
	return string(buf)
}

package main

import (
	"bufio"
	"fmt"
)

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(scanner *bufio.Scanner) (a int) {
	scanner.Scan()
	readInt(scanner.Bytes(), 0, &a)
	return
}

func readTwoNums(scanner *bufio.Scanner) (a int, b int) {
	res := readNNums(scanner, 2)
	a, b = res[0], res[1]
	return
}

func readNNums(scanner *bufio.Scanner, n int) []int {
	res := make([]int, n)
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
	return res
}

func fillNNums(scanner *bufio.Scanner, n int, res []int) {
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func readInt64(bytes []byte, from int, val *int64) int {
	i := from
	var tmp int64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int64(bytes[i]-'0')
		i++
	}
	*val = tmp
	return i
}

func readNInt64Nums(scanner *bufio.Scanner, n int) []int64 {
	res := make([]int64, n)
	x := -1
	scanner.Scan()
	for i := 0; i < n; i++ {
		x = readInt64(scanner.Bytes(), x+1, &res[i])
	}
	return res
}

func main() {

	// f := bufio.NewWriter(os.Stdout)

	// defer f.Flush()

	// scanner := bufio.NewScanner(os.Stdin)

	var tc int

	fmt.Scanf("%d", &tc)

	for tc > 0 {
		tc--
		var s string
		fmt.Scanf("%s", &s)
		fmt.Println(solve(s))
	}
}

func solve(s string) string {
	var l, r int = 0, len(s) - 1
	for l <= r && s[l] == s[r] {
		l++
		r--
	}
	if l > r {
		return s
	}
	// l < r
	x := s[l : r+1]

	a := findLongestPrefixPalindrome(x)
	b := findLongestPrefixPalindrome(reverse(x))

	if len(a) < len(b) {
		a, b = b, a
	}

	return s[:l] + a + s[r+1:]
}

func findLongestPrefixPalindrome(s string) string {
	x := s + "#" + reverse(s)

	var l int

	pi := make([]int, len(x))

	for i := 1; i < len(x); i++ {
		for l > 0 && x[l] != x[i] {
			l = pi[l-1]
		}
		if x[l] == x[i] {
			l++
		}
		pi[i] = l
	}

	return s[:l]
}

func reverse(s string) string {
	buf := []byte(s)
	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		buf[i], buf[j] = buf[j], buf[i]
	}
	return string(buf)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(scanner *bufio.Scanner) (a int) {
	scanner.Scan()
	readInt(scanner.Bytes(), 0, &a)
	return
}

func readTwoNums(scanner *bufio.Scanner) (a int, b int) {
	res := readNNums(scanner, 2)
	a, b = res[0], res[1]
	return
}

func readNNums(scanner *bufio.Scanner, n int) []int {
	res := make([]int, n)
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
	return res
}

func fillNNums(scanner *bufio.Scanner, n int, res []int) {
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func readInt64(bytes []byte, from int, val *int64) int {
	i := from
	var tmp int64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int64(bytes[i]-'0')
		i++
	}
	*val = tmp
	return i
}

func readNInt64Nums(scanner *bufio.Scanner, n int) []int64 {
	res := make([]int64, n)
	x := -1
	scanner.Scan()
	for i := 0; i < n; i++ {
		x = readInt64(scanner.Bytes(), x+1, &res[i])
	}
	return res
}

func main() {

	f := bufio.NewWriter(os.Stdout)

	defer f.Flush()

	// scanner := bufio.NewScanner(os.Stdin)

	var tc int

	fmt.Scanf("%d", &tc)

	for tc > 0 {
		tc--
		var s string
		fmt.Scanf("%s", &s)
		f.WriteString(solve(s))
		f.WriteByte('\n')
	}
}

func solve(s string) string {
	var l, r int = 0, len(s) - 1
	for l <= r && s[l] == s[r] {
		l++
		r--
	}
	if l > r {
		return s
	}
	// l < r
	x := s[l : r+1]

	a := findLongestPrefixPalindrome(x)
	b := findLongestPrefixPalindrome(reverse(x))

	if len(a) < len(b) {
		a, b = b, a
	}

	return s[:l] + a + s[r+1:]
}

func findLongestPrefixPalindrome(s string) string {
	x := s + "#" + reverse(s)

	var l int

	pi := make([]int, len(x))

	for i := 1; i < len(x); i++ {
		for l > 0 && x[l] != x[i] {
			l = pi[l-1]
		}
		if x[l] == x[i] {
			l++
		}
		pi[i] = l
	}

	return s[:l]
}

func reverse(s string) string {
	buf := []byte(s)
	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		buf[i], buf[j] = buf[j], buf[i]
	}
	return string(buf)
}

package main

import (
	"bufio"
	"os"
)

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(scanner *bufio.Scanner) (a int) {
	scanner.Scan()
	readInt(scanner.Bytes(), 0, &a)
	return
}

func readTwoNums(scanner *bufio.Scanner) (a int, b int) {
	res := readNNums(scanner, 2)
	a, b = res[0], res[1]
	return
}

func readNNums(scanner *bufio.Scanner, n int) []int {
	res := make([]int, n)
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
	return res
}

func fillNNums(scanner *bufio.Scanner, n int, res []int) {
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func readInt64(bytes []byte, from int, val *int64) int {
	i := from
	var tmp int64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int64(bytes[i]-'0')
		i++
	}
	*val = tmp
	return i
}

func readNInt64Nums(scanner *bufio.Scanner, n int) []int64 {
	res := make([]int64, n)
	x := -1
	scanner.Scan()
	for i := 0; i < n; i++ {
		x = readInt64(scanner.Bytes(), x+1, &res[i])
	}
	return res
}

func main() {

	f := bufio.NewWriter(os.Stdout)

	defer f.Flush()

	scanner := bufio.NewScanner(os.Stdin)

	tc := readNum(scanner)

	for tc > 0 {
		tc--
		scanner.Scan()
		s := scanner.Text()
		if len(s) == 0 {
			f.WriteString("no input read")
		} else {
			f.WriteString(solve(s))
			f.WriteByte('\n')
		}
	}
}

func solve(s string) string {
	var l, r int = 0, len(s) - 1
	for l <= r && s[l] == s[r] {
		l++
		r--
	}
	if l > r {
		return s
	}
	// l < r
	x := s[l : r+1]

	a := findLongestPrefixPalindrome(x)
	b := findLongestPrefixPalindrome(reverse(x))

	if len(a) < len(b) {
		a, b = b, a
	}

	return s[:l] + a + s[r+1:]
}

func findLongestPrefixPalindrome(s string) string {
	x := s + "#" + reverse(s)

	var l int

	pi := make([]int, len(x))

	for i := 1; i < len(x); i++ {
		for l > 0 && x[l] != x[i] {
			l = pi[l-1]
		}
		if x[l] == x[i] {
			l++
		}
		pi[i] = l
	}

	return s[:l]
}

func reverse(s string) string {
	buf := []byte(s)
	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		buf[i], buf[j] = buf[j], buf[i]
	}
	return string(buf)
}

package main

import (
	"bufio"
	"os"
)

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(scanner *bufio.Scanner) (a int) {
	scanner.Scan()
	readInt(scanner.Bytes(), 0, &a)
	return
}

func readTwoNums(scanner *bufio.Scanner) (a int, b int) {
	res := readNNums(scanner, 2)
	a, b = res[0], res[1]
	return
}

func readNNums(scanner *bufio.Scanner, n int) []int {
	res := make([]int, n)
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
	return res
}

func fillNNums(scanner *bufio.Scanner, n int, res []int) {
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func readInt64(bytes []byte, from int, val *int64) int {
	i := from
	var tmp int64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int64(bytes[i]-'0')
		i++
	}
	*val = tmp
	return i
}

func readNInt64Nums(scanner *bufio.Scanner, n int) []int64 {
	res := make([]int64, n)
	x := -1
	scanner.Scan()
	for i := 0; i < n; i++ {
		x = readInt64(scanner.Bytes(), x+1, &res[i])
	}
	return res
}

func main() {

	f := bufio.NewWriter(os.Stdout)

	defer f.Flush()

	scanner := bufio.NewScanner(os.Stdin)

	tc := readNum(scanner)

	for tc > 0 {
		tc--
		scanner.Scan()
		f.WriteString(solve(scanner.Text()))
		f.WriteByte('\n')
	}
}

func solve(s string) string {
	var l, r int = 0, len(s) - 1
	for l <= r && s[l] == s[r] {
		l++
		r--
	}
	if l > r {
		return s
	}
	// l < r
	x := s[l : r+1]

	a := findLongestPrefixPalindrome(x)
	b := findLongestPrefixPalindrome(reverse(x))

	if len(a) < len(b) {
		a, b = b, a
	}

	return s[:l] + a + s[r+1:]
}

func findLongestPrefixPalindrome(s string) string {
	x := s + "#" + reverse(s)

	var l int

	pi := make([]int, len(x))

	for i := 1; i < len(x); i++ {
		for l > 0 && x[l] != x[i] {
			l = pi[l-1]
		}
		if x[l] == x[i] {
			l++
		}
		pi[i] = l
	}

	return s[:l]
}

func reverse(s string) string {
	buf := []byte(s)
	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		buf[i], buf[j] = buf[j], buf[i]
	}
	return string(buf)
}


Saitama accidentally destroyed a hotel again. To repay the hotel company, Genos has volunteered to operate an elevator in one of its other hotels. The elevator is special — it starts on the top floor, can only move down, and has infinite capacity. Floors are numbered from 0 to s and elevator initially starts on floor s at time 0.The elevator takes exactly 1 second to move down exactly 1 floor and negligible time to pick up passengers. Genos is given a list detailing when and on which floor passengers arrive. Please determine how long in seconds it will take Genos to bring all passengers to floor 0.

The first line of input contains two integers n and s (1 ≤ n ≤ 100, 1 ≤ s ≤ 1000) — the number of passengers and the number of the top floor respectively. The next n lines each contain two space-separated integers fi and ti (1 ≤ fi ≤ s, 1 ≤ ti ≤ 1000) — the floor and the time of arrival in seconds for the passenger number i.

Print a single integer — the minimum amount of time in seconds needed to bring all the passengers to floor 0.

NoteIn the first sample, it takes at least 11 seconds to bring all passengers to floor 0. Here is how this could be done:1. Move to floor 5: takes 2 seconds.2. Pick up passenger 3.3. Move to floor 3: takes 2 seconds.4. Wait for passenger 2 to arrive: takes 4 seconds.5. Pick up passenger 2.6. Go to floor 2: takes 1 second.7. Pick up passenger 1.8. Go to floor 0: takes 2 seconds.This gives a total of 2 + 2 + 4 + 1 + 2 = 11 seconds.

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	// "sort"
	// "strings"
)

var (
	reader  = bufio.NewReader(os.Stdin)
	writer  = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
)

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, s := nextInt(), nextInt()
	times := make([]int, s+1)
	time := 0

	for ; n > 0; n-- {
		f, t := nextInt(), nextInt()

		if t > times[f] {
			times[f] = t
		}
	}

	for i := s; i > 0; i-- {

		if times[i] > time {
			time += times[i] - time
		}
		// println(i, time)
		time++
	}

	println(time)
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func Print(args ...interface{})                 { out.WriteString(fmt.Sprint(args...)) }
func Printf(format string, args ...interface{}) { out.WriteString(fmt.Sprintf(format, args...)) }
func Println(args ...interface{})               { out.WriteString(fmt.Sprintln(args...)) }

func Scan(args ...interface{})   { fmt.Fscan(in, args...) }
func Scanln(args ...interface{}) { fmt.Fscanln(in, args...) }

func Max(a int, b int) int {
	if a > b {
		return a
	}
	return b
}

func main() {
	defer out.Flush()
	var n, s, f, t int

	Scan(&n, &s)
	p := make([]int, s+1)
	for i := 0; i < n; i++ {
		Scan(&f, &t)
		p[f] = Max(p[f], t)
	}

	ans := p[s]
	for i := s - 1; i >= 0; i-- {
		if p[i] == 0 || p[i] <= ans {
			ans++
		} else {
			ans = p[i]
		}
		//fmt.Println(i, ans)
	}
	Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	var n, s int
	if _, err := fmt.Scanf("%d %d\n", &n, &s); err != nil {
		return
	}

	l := make([][2]int, n+1)

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 0; i < n; i++ {
		scanner.Scan()
		f, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}
		scanner.Scan()
		t, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}

		l[i][0], l[i][1] = f, t
	}
	l[n][0], l[n][1] = 0, 0

	bubble_sort(l, n+1)

	time, floor := 0, s

	for i := 0; i <= n; i++ {
		time += floor - l[i][0]
		floor = l[i][0]
		if time < l[i][1] {
			time = l[i][1]
		}
	}

	fmt.Println(time)
}

func swap(a, b int, array [][2]int) {
	f, t := array[a][0], array[a][1]
	array[a][0], array[a][1] = array[b][0], array[b][1]
	array[b][0], array[b][1] = f, t
}

func bubble_sort(array [][2]int, length int) {
	for i := 0; i < length; i++ {
		for j := 0; j < length; j++ {
			if array[i][0] > array[j][0] {
				swap(i, j, array)
			}
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	// "sort"
	// "strings"
)

var (
	reader  = bufio.NewReader(os.Stdin)
	writer  = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
)

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, s := nextInt(), nextInt()
	t := make([]int, s+1)
	time := 0

	for ; n > 0; n-- {
		t[nextInt()] = nextInt()
	}

	for i := s; i > 0; i-- {

		if t[i] > time {
			time += t[i] - time
		}
		// println(i, time)
		time++
	}

	println(time)
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func Print(args ...interface{})                 { out.WriteString(fmt.Sprint(args...)) }
func Printf(format string, args ...interface{}) { out.WriteString(fmt.Sprintf(format, args...)) }
func Println(args ...interface{})               { out.WriteString(fmt.Sprintln(args...)) }

func Scan(args ...interface{})   { fmt.Fscan(in, args...) }
func Scanln(args ...interface{}) { fmt.Fscanln(in, args...) }

func main() {
	defer out.Flush()
	var n, s, f, t int

	Scan(&n, &s)
	p := make([]int, s+1)
	for i := 0; i < n; i++ {
		Scan(&f, &t)
		p[f] += t
	}

	ans := p[s]
	for i := s - 1; i >= 0; i-- {
		if p[i] == 0 || p[i] <= ans {
			ans++
		} else {
			ans = p[i]
		}
		//fmt.Println(i, ans)
	}
	Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func Print(args ...interface{})                 { out.WriteString(fmt.Sprint(args...)) }
func Printf(format string, args ...interface{}) { out.WriteString(fmt.Sprintf(format, args...)) }
func Println(args ...interface{})               { out.WriteString(fmt.Sprintln(args...)) }

func Scan(args ...interface{})   { fmt.Fscan(in, args...) }
func Scanln(args ...interface{}) { fmt.Fscanln(in, args...) }

func main() {
	defer out.Flush()
	var n, s, f, t int

	Scan(&n, &s)
	p := make([]int, s+1)
	for i := 0; i < n; i++ {
		Scan(&f, &t)
		p[f] = t
	}
	ans := 0
	for i := s; i > 0; i-- {
		if p[i] == 0 || p[i] <= ans {
			ans++
		} else {
			ans = p[i]
		}
	}
	ans++
	Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func Print(args ...interface{})                 { out.WriteString(fmt.Sprint(args...)) }
func Printf(format string, args ...interface{}) { out.WriteString(fmt.Sprintf(format, args...)) }
func Println(args ...interface{})               { out.WriteString(fmt.Sprintln(args...)) }

func Scan(args ...interface{})   { fmt.Fscan(in, args...) }
func Scanln(args ...interface{}) { fmt.Fscanln(in, args...) }

func main() {
	defer out.Flush()
	var n, s, f, t int

	Scan(&n, &s)
	p := make([]int, s+1)
	for i := 0; i < n; i++ {
		Scan(&f, &t)
		p[f] = t
	}

	ans := p[s]
	for i := s - 1; i >= 0; i-- {
		if p[i] == 0 || p[i] <= ans {
			ans++
		} else {
			ans = p[i]
		}
		//fmt.Println(i, ans)
	}
	Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func Print(args ...interface{})                 { out.WriteString(fmt.Sprint(args...)) }
func Printf(format string, args ...interface{}) { out.WriteString(fmt.Sprintf(format, args...)) }
func Println(args ...interface{})               { out.WriteString(fmt.Sprintln(args...)) }

func Scan(args ...interface{})   { fmt.Fscan(in, args...) }
func Scanln(args ...interface{}) { fmt.Fscanln(in, args...) }

func main() {
	defer out.Flush()
	var n, s, f, t int

	Scan(&n, &s)
	p := make([]int, s+1)
	for i := 0; i < n; i++ {
		Scan(&f, &t)
		p[f] = t
	}
	ans := 0
	for i := s; i >= 0; i-- {
		if p[i] == 0 || p[i] <= ans {
			ans++
		} else {
			ans = p[i]
		}
	}
	Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func Print(args ...interface{})                 { out.WriteString(fmt.Sprint(args...)) }
func Printf(format string, args ...interface{}) { out.WriteString(fmt.Sprintf(format, args...)) }
func Println(args ...interface{})               { out.WriteString(fmt.Sprintln(args...)) }

func Scan(args ...interface{})   { fmt.Fscan(in, args...) }
func Scanln(args ...interface{}) { fmt.Fscanln(in, args...) }

func main() {
	defer out.Flush()
	var n, s, f, t int

	Scan(&n, &s)
	p := make([]int, s+1)
	for i := 0; i < n; i++ {
		Scan(&f, &t)
		p[f] = t
	}
	ans := 0
	for i := s; i > 0; i-- {
		if p[i] == 0 || p[i] <= ans {
			ans++
		} else {
			ans = p[i] + 1
		}
	}
	Println(ans)
}


This problem is given in two editions, which differ exclusively in the constraints on the number $$$n$$$.You are given an array of integers $$$a[1], a[2], \dots, a[n].$$$ A block is a sequence of contiguous (consecutive) elements $$$a[l], a[l+1], \dots, a[r]$$$ ($$$1 \le l \le r \le n$$$). Thus, a block is defined by a pair of indices $$$(l, r)$$$.Find a set of blocks $$$(l_1, r_1), (l_2, r_2), \dots, (l_k, r_k)$$$ such that:  They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks $$$(l_i, r_i)$$$ and $$$(l_j, r_j$$$) where $$$i \neq j$$$ either $$$r_i < l_j$$$ or $$$r_j < l_i$$$.  For each block the sum of its elements is the same. Formally, $$$$$$a[l_1]+a[l_1+1]+\dots+a[r_1]=a[l_2]+a[l_2+1]+\dots+a[r_2]=$$$$$$ $$$$$$\dots =$$$$$$ $$$$$$a[l_k]+a[l_k+1]+\dots+a[r_k].$$$$$$  The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks $$$(l_1', r_1'), (l_2', r_2'), \dots, (l_{k'}', r_{k'}')$$$ satisfying the above two requirements with $$$k' > k$$$.     The picture corresponds to the first example. Blue boxes illustrate blocks. Write a program to find such a set of blocks.

The first line contains integer $$$n$$$ ($$$1 \le n \le 50$$$) — the length of the given array. The second line contains the sequence of elements $$$a[1], a[2], \dots, a[n]$$$ ($$$-10^5 \le a_i \le 10^5$$$).

In the first line print the integer $$$k$$$ ($$$1 \le k \le n$$$). The following $$$k$$$ lines should contain blocks, one per line. In each line print a pair of indices $$$l_i, r_i$$$ ($$$1 \le l_i \le r_i \le n$$$) — the bounds of the $$$i$$$-th block. You can print blocks in any order. If there are multiple answers, print any of them.

package main

import (
	"bufio"
	"fmt"
	"os"
)

type LinkF struct {
	next *LinkF
	l, r int
}

func main() {
	buf := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanf(buf, "%d\n", &n)
	mat := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &mat[i])
	}
	fmt.Fscanf(buf, "\n")
	mp := make(map[int]*LinkF)
	cmap := make(map[int]int)
	maxl := 0
	maxvalue := 0
	for i := 0; i < n; i++ {
		sum := 0
		for j := i; j >= 0; j-- {
			sum += mat[j]
			if mp[sum] == nil || mp[sum].r < j {
				newL := &LinkF{
					l: j,
					r: i,
				}
				cmap[sum]++
				newL.next = mp[sum]
				mp[sum] = newL
				if cmap[sum] > maxl {
					maxl = cmap[sum]
					maxvalue = sum
				}
			}
		}
	}
	fmt.Printf("%d\n", maxl)
	for header := mp[maxvalue]; header != nil; header = header.next {
		fmt.Printf("%d %d\n",header.l+1,header.r+1)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

//Range ...
type Range struct {
	start int
	end   int
}

func solve(a []int) []Range {
	L := len(a)

	sums := make([]map[int]int, L)
	//	all := map[int]bool{}

	for i := L - 1; i >= 0; i-- {
		cur := 0

		for j := i; j < L; j++ {
			cur += a[j]

			//			all[cur] = true

			_, found := sums[i][cur]
			if !found {
				if sums[i] == nil {
					sums[i] = map[int]int{}
				}

				sums[i][cur] = j
			}
		}
	}

	memo := map[[2]int][]int{}

	var calc func(start, sum int) []int
	calc = func(start, sum int) []int {
		hash := [2]int{start, sum}
		out, found := memo[hash]

		if found {
			return out
		}

		out = []int{}

		if start < L {
			if next, found := sums[start][sum]; found {
				max := []int{start}
				for i := next; i < L; i++ {

					//	if _, found = sums[i][sum]; found {
					p := calc(1+i, sum)

					if len(p) >= len(max) {
						max = append([]int{start}, p...)
					}
					//					}
				}

				out = make([]int, len(max))
				copy(out, max)
			}
		}

		memo[hash] = out

		return out
	}

	target := 0
	max := -1
	var starts []int
	for i := L - 1; i >= 0; i-- {
		for s := range sums[i] {
			c := calc(i, s)
			if len(c) > max {
				target = s
				max = len(c)

				starts = c
			}
		}
	}

	res := make([]Range, max)

	for i, n := range starts {
		res[i] = Range{n, sums[n][target]}
	}

	return res
}

func toInt(buf []byte) int {
	n, mul := int(0), int(1)
	for i, v := range buf {
		if i == 0 && v == '-' {
			mul = int(-1)
			continue
		}
		n = n*10 + int(v-'0')
	}

	return mul * n
}

func main() {
	var n int

	fmt.Scan(&n)

	a := make([]int, n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := range a {
		scanner.Scan()
		a[i] = toInt(scanner.Bytes())
	}

	ans := solve(a)
	fmt.Println(len(ans))

	for _, pair := range ans {
		fmt.Printf("%v %v\n", 1+pair.start, 1+pair.end)
	}
}

// Author: sighduck
// URL: https://codeforces.com/contest/1141/problem/F2

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

type Pair struct {
	L int
	R int
}

func sums(a []int) []int {
	sums := []int{}
	sum := 0
	for _, elem := range a {
		sum += elem
		sums = append(sums, sum)
	}
	return sums
}

func getSum(sums []int, i int, j int) int {
	if i == 0 {
		return sums[j]
	}
	return sums[j] - sums[i-1]
}

func filterPairs(pairs []Pair) []Pair {
	sort.Slice(pairs, func(i, j int) bool { return pairs[i].R < pairs[j].R })
	newPairs := []Pair{}
	for _, pair := range pairs {
		if len(newPairs) == 0 {
			newPairs = append(newPairs, pair)
		} else if pair.L > newPairs[len(newPairs)-1].R {
			newPairs = append(newPairs, pair)
		}
	}
	return newPairs
}

func Solve(n int, a []int) []Pair {
	m := make(map[int][]Pair)
	sums := sums(a)
	for i := 0; i < n; i++ {
		for j := i; j < n; j++ {
			sum := getSum(sums, i, j)
			m[sum] = append(m[sum], Pair{L: i + 1, R: j + 1})
		}
	}
	maxPairs := []Pair{}
	for _, pairs := range m {
		p := filterPairs(pairs)
		if len(p) > len(maxPairs) {
			maxPairs = p
		}
	}
	return maxPairs
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}

	solution := Solve(n, a)
	fmt.Fprintf(writer, "%d\n", len(solution))
	for _, pair := range solution {
		fmt.Fprintf(writer, "%d %d\n", pair.L, pair.R)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	buf := bufio.NewReader(os.Stdin)
	var n int
	var h int64
	fmt.Fscanf(buf, "%d %d\n", &h, &n)
	mat := make([]int64, n+1)
	mat[0] = 0
	minn := int64(0)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(buf, "%d", &mat[i])
		mat[i] += mat[i-1]
		if i == 1 || mat[i] < minn {
			minn = mat[i]
		}
	}
	for i := 1; i <= n; i++ {
		if mat[i] >= h {
			fmt.Printf("%d\n", i)
			return
		}
	}
	if mat[n] >= 0 {
		fmt.Printf("-1\n")
		return
	}
	hh := h + minn
	ans := 1 + (hh-1)/(0-mat[n])
	h += ans * mat[n]
	ans = ans * int64(n)
	//fmt.Printf("h=%d ans=%d\n", h,ans)

	for i := 1; h>0&&i <= n; i++ {
		ans++
		if mat[i]+h <= 0 {
			break
		}
	}
	fmt.Printf("%d\n", ans)

}

/*
1 10
10
1 1 2 1 1 1 2 1 1 1
1 1 1 1 1 1 1 1 1 1
1
1 1
*/

package main

import (
	"bufio"
	"fmt"
	"os"
)

type Range struct {
	start int
	end   int
}

func solve(a []int) []Range {
	L := len(a)

	sums := make([]map[int]Range, L)
	all := map[int]int{}

	for i := L - 1; i >= 0; i-- {
		cur := a[i]

		for j := i; j < L; j++ {
			if i != j {
				cur += a[j]
			}

			_, found := sums[i][cur]
			if !found {
				if sums[i] == nil {
					sums[i] = map[int]Range{}
				}

				all[cur]++

				sums[i][cur] = Range{start: i, end: j}
			}
		}
	}

	max := -1
	maxPath := []Range{}

	for len(all) > 0 {
		for cur := range all {
			i := 0
			count := 0
			path := []Range{}

			for i < L {
				sum, found := sums[i][cur]

				if !found {
					i++
					continue
				}
				delete(sums[i], cur)
				all[cur]--
				if all[cur] <= 0 {
					delete(all, cur)
				}

				count++
				path = append(path, sum)

				i = sum.end + 1

				if count > max {
					max = count
					maxPath = make([]Range, len(path))
					copy(maxPath, path)
				}
			}
		}
	}

	return maxPath
}

func toInt(buf []byte) int {
	n, mul := int(0), int(1)
	for i, v := range buf {
		if i == 0 && v == '-' {
			mul = int(-1)
			continue
		}
		n = n*10 + int(v-'0')
	}

	return mul * n
}

func main() {
	var n int

	fmt.Scan(&n)

	a := make([]int, n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := range a {
		scanner.Scan()
		a[i] = toInt(scanner.Bytes())
	}

	ans := solve(a)
	fmt.Println(len(ans))

	for _, pair := range ans {
		fmt.Printf("%v %v\n", 1+pair.start, 1+pair.end)
	}
}

// Author: sighduck
// URL: https://codeforces.com/contest/1141/problem/F2

package main

import (
	"bufio"
	"fmt"
	"os"
)

type Pair struct {
	L int
	R int
}

func sums(a []int) []int {
	sums := []int{}
	sum := 0
	for _, elem := range a {
		sum += elem
		sums = append(sums, sum)
	}
	return sums
}

func getSum(sums []int, i int, j int) int {
	if i == 0 {
		return sums[j]
	}
	return sums[j] - sums[i-1]
}

func filterPairs(pairs []Pair) []Pair {
	newPairs := []Pair{}
	for _, pair := range pairs {
		if len(newPairs) == 0 {
			newPairs = append(newPairs, pair)
		} else if pair.L > newPairs[len(newPairs)-1].R {
			newPairs = append(newPairs, pair)
		}
	}
	return newPairs
}

func Solve(n int, a []int) []Pair {
	m := make(map[int][]Pair)
	sums := sums(a)
	for i := 0; i < n; i++ {
		for j := i; j < n; j++ {
			sum := getSum(sums, i, j)
			m[sum] = append(m[sum], Pair{L: i+1, R: j+1})
		}
	}
	maxPairs := []Pair{}
	for _, pairs := range m {
		p := filterPairs(pairs)
		if len(p) > len(maxPairs) {
			maxPairs = p
		}
	}
	return maxPairs
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}

	solution := Solve(n, a)
	fmt.Fprintf(writer, "%d\n", len(solution))
	for _, pair := range solution {
		fmt.Fprintf(writer, "%d %d\n", pair.L, pair.R)
	}
}


Sasha likes investigating different math objects, for example, magic squares. But Sasha understands that magic squares have already been studied by hundreds of people, so he sees no sense of studying them further. Instead, he invented his own type of square — a prime square. A square of size $$$n \times n$$$ is called prime if the following three conditions are held simultaneously:   all numbers on the square are non-negative integers not exceeding $$$10^5$$$;  there are no prime numbers in the square;  sums of integers in each row and each column are prime numbers. Sasha has an integer $$$n$$$. He asks you to find any prime square of size $$$n \times n$$$. Sasha is absolutely sure such squares exist, so just help him!

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10$$$) — the number of test cases. Each of the next $$$t$$$ lines contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required size of a square.

For each test case print $$$n$$$ lines, each containing $$$n$$$ integers — the prime square you built. If there are multiple answers, print any.

package main

import (
	"fmt"
	"math/big"
)

func main() {
	var t, n, i, x ,y, ans  int64
	var j int64
	fmt.Scan(&t)
	for i=0; i<t; i++ {
		fmt.Scan(&n)
		for j=n+1; ; j++ {
			if big.NewInt(j).ProbablyPrime(0) && !big.NewInt(j-n+1).ProbablyPrime(0) {
				ans = j-n+1
				break
			}
		}
		for x=0; x<n; x++ {
			for y=0; y<n; y++ {
				if x!=y {
					fmt.Printf("1 ")
				} else {
					fmt.Printf("%d ", ans)
				}
			}
			fmt.Printf("\n")
		}
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mx int = 1e5
	primes := []int{}
	isP := [mx + 1]bool{}
	for i := range isP {
		isP[i] = true
	}
	isP[0], isP[1] = false, false
	for i := 2; i <= mx; i++ {
		if isP[i] {
			primes = append(primes, i)
			for j := 2 * i; j <= mx; j += i {
				isP[j] = false
			}
		}
	}

	solve := func(Case int) {
		var n int
		Fscan(in, &n)
		p := n
		for ;  ; p++ {
			if !isP[p] {
				continue
			}
			if !isP[p-n+1] {
				break
			}
		}
		x := p-n+1
		for i := 0; i < n; i++ {
			for j := 0; j < n; j++ {
				if i == j {
					Fprint(out, x, " ")
				} else {
					Fprint(out, 1, " ")
				}
			}
			Fprintln(out)
		}
	}

	var t int
	Fscan(in, &t)
	for Case := 1; Case <= t; Case++ {
		solve(Case)
	}
}

func main() { run(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func init() {
	sc.Split(bufio.ScanWords)
}

func Next() string {
	sc.Scan()
	return sc.Text()
}

func NextInt() int {
	i, err := strconv.Atoi(Next())
	if err != nil {
		panic(err)
	}
	return i
}

func main() {
	for tt := NextInt(); tt > 0; tt-- {
		n := NextInt()
		if n == 2 {
			fmt.Println("1 1")
			fmt.Println("1 1")
		} else if n == 3 {
			fmt.Println("1 1 1")
			fmt.Println("1 1 1")
			fmt.Println("1 1 1")
		} else {
			for i := 0; i < n - 1; i++ {
				for j := 0; j < n - 2 - i; j++ {
					fmt.Print("0 ")
				}
				fmt.Print("1 1 ")
				for j := n - i; j < n; j++ {
					fmt.Print("0 ")
				}
				fmt.Println()
			}
			fmt.Print("1 ")
			for i := 1; i < n - 1; i++ {
				fmt.Print("0 ")
			}
			fmt.Println("1")
		}
	}
}

package main

import (
	"bufio"
	"os"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func print(str string) {
	writer.WriteString(str)
}

func nextInt() int {
	scanner.Scan()
	str := scanner.Text()
	val, _ := strconv.Atoi(str)
	return val
}

func nextFloat() float64 {
	scanner.Scan()
	str := scanner.Text()
	val, _ := strconv.ParseFloat(str, 64)
	return val
}

func nextString() string {
	scanner.Scan()
	str := scanner.Text()
	return str
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func getPrimes(max int64) (primes []int) {
	gouged := make([]bool, max+1)
	for i := int64(2); i <= max; i++ {
		if !gouged[i] {
			primes = append(primes, int(i))
			for j := int64(i) * int64(i); j <= int64(max); j += i {
				gouged[j] = true
			}
		}
	}
	return
}

func main() {
	scanner.Split(bufio.ScanWords)
	defer writer.Flush()
	primes := getPrimes(100000)
	isPrime := map[int]bool{}
	for _, p := range primes {
		isPrime[p] = true
	}

	zvals := [101]int{}
	for i, k := 2, 0; i <= 100; i++ {
		for j := k; ; j++ {
			z := primes[j] - i + 1
			if z > 0 && !isPrime[z] {
				zvals[i] = z
				break
			}
		}
	}

	t := nextInt()
	for t != 0 {
		n := nextInt()
		for i := 0; i < n; i++ {
			for j := 0; j < n; j++ {
				val := 1
				if j == i {
					val = zvals[n]
				}
				print(strconv.Itoa(val) + " ")
			}
			print("\n")
		}
		t--
	}
}

package main

import (
	"fmt"
)

func getPrimes(max int64) (primes []int) {
	gouged := make([]bool, max+1)
	for i := int64(2); i <= max; i++ {
		if !gouged[i] {
			primes = append(primes, int(i))
			for j := int64(i) * int64(i); j <= int64(max); j += i {
				gouged[j] = true
			}
		}
	}
	return
}

func main() {
	primes := getPrimes(100000)
	isPrime := map[int]bool{}
	for _, p := range primes {
		isPrime[p] = true
	}

	zvals := [101]int{}
	for i, k := 2, 0; i <= 100; i++ {
		for j := k; ; j++ {
			z := primes[j] - i + 1
			if z > 0 && !isPrime[z] {
				zvals[i] = z
				break
			}
		}
	}

	var t int
	fmt.Scan(&t)
	for t != 0 {
		var n int
		fmt.Scan(&n)
		for i := 0; i < n; i++ {
			for j := 0; j < n; j++ {
				val := 1
				if j == i {
					val = zvals[n]
				}
				fmt.Print(val, " ")
			}
			fmt.Println()
		}
		t--
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

var rw = NewFastIO(os.Stdin, os.Stdout)

func main() {
	defer rw.Flush()
	nt := rw.NextInt()
	for i := 0; i < nt; i++ {
		tCase()
	}
}

func tCase() {
	n := rw.NextInt()
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if i == j || i == j+1 || (i == 0 && j == 1) || (i == n-2 && j == n-1) {
				rw.Print("1 ")
			} else {
				rw.Print("0 ")
			}
		}
		rw.Println()
	}
}

type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}
func (f *FastIO) NextInt64() int64 {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := int64(0)
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int64(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}
func (f *FastIO) NextInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt64()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}

package main

import "fmt"

func main() {
	var t, n int
	_, _ = fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		_, _ = fmt.Scanf("%d\n", &n)
		for i:=0;i<n;i++{
			for j:=0;j<n;j++{
				if ( i == j || i == j + 1 || i == j -1 ){
					fmt.Println("1 ")
				}else{
					fmt.Println("0 ")
				}
				fmt.Println()
			}
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

var rw = NewFastIO(os.Stdin, os.Stdout)

func main() {
	defer rw.Flush()
	nt := rw.NextInt()
	for i := 0; i < nt; i++ {
		tCase()
	}
}

func tCase() {
	n := rw.NextInt()
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if i == j {
				rw.Print("2 ")
			} else {
				rw.Print("0 ")
			}
		}
		rw.Println()
	}
}

type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}
func (f *FastIO) NextInt64() int64 {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := int64(0)
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int64(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}
func (f *FastIO) NextInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt64()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}


For an array $$$b$$$ of length $$$m$$$ we define the function $$$f$$$ as  $$$ f(b) = \begin{cases} b[1] & \quad \text{if } m = 1 \\ f(b[1] \oplus b[2],b[2] \oplus b[3],\dots,b[m-1] \oplus b[m]) & \quad \text{otherwise,} \end{cases} $$$ where $$$\oplus$$$ is bitwise exclusive OR.For example, $$$f(1,2,4,8)=f(1\oplus2,2\oplus4,4\oplus8)=f(3,6,12)=f(3\oplus6,6\oplus12)=f(5,10)=f(5\oplus10)=f(15)=15$$$You are given an array $$$a$$$ and a few queries. Each query is represented as two integers $$$l$$$ and $$$r$$$. The answer is the maximum value of $$$f$$$ on all continuous subsegments of the array $$$a_l, a_{l+1}, \ldots, a_r$$$.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 5000$$$) — the length of $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 2^{30}-1$$$) — the elements of the array. The third line contains a single integer $$$q$$$ ($$$1 \le q \le 100\,000$$$) — the number of queries. Each of the next $$$q$$$ lines contains a query represented as two integers $$$l$$$, $$$r$$$ ($$$1 \le l \le r \le n$$$).

Print $$$q$$$ lines — the answers for the queries.

NoteIn first sample in both queries the maximum value of the function is reached on the subsegment that is equal to the whole segment.In second sample, optimal segment for first query are $$$[3,6]$$$, for second query — $$$[2,5]$$$, for third — $$$[3,4]$$$, for fourth — $$$[1,2]$$$.

package main

import (
	"bufio"
	"fmt"
	"os"
)

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := 0
	for _, b := range in.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

const (
	maxN = 5002
)

var (
	val [maxN][maxN]int
	max [maxN][maxN]int
)

func main() {
	in.Split(bufio.ScanWords)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	n := readInt()
	for i := 0; i < n; i++ {
		val[i][i] = readInt()
		max[i][i] = val[i][i]
	}
	for le := 1; le < n; le++ {
		for i := 0; i+le < n; i++ {
			j := i + le
			val[i][j] = val[i][j-1] ^ val[i+1][j]
			x := val[i][j]
			if x < max[i][j-1] {
				x = max[i][j-1]
			}
			if x < max[i+1][j] {
				x = max[i+1][j]
			}
			max[i][j] = x
		}
	}

	for q := readInt(); q > 0; q-- {
		fmt.Fprintln(out, max[readInt()-1][readInt()-1])
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	r := bufio.NewReader(os.Stdin)

	var n int
	fmt.Fscanf(r, "%d\n", &n)

	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(r, "%d", &a[i])
	}
	fmt.Fscanf(r, "\n")

	dp := make([][]int, n)
	for i := 0; i < n; i++ {
		dp[i] = make([]int, n)
	}

	for l := 1; l <= n; l++ {
		for i := 0; i+l-1 < n; i++ {
			if l == 1 {
				dp[i][i] = a[i]
				continue
			}
			j := i + l - 1
			dp[i][j] = dp[i][j-1] ^ dp[i+1][j]
		}
	}

	for l := 2; l <= n; l++ {
		for i := 0; i+l-1 < n; i++ {
			j := i + l - 1
			dp[i][j] = max(dp[i][j], max(dp[i][j-1], dp[i+1][j]))
		}
	}

	var q int
	fmt.Fscanf(r, "%d\n", &q)

	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	for tc := 0; tc < q; tc++ {
		var s, e int
		fmt.Fscanf(r, "%d %d\n", &s, &e)
		fmt.Fprintf(w, "%d\n", dp[s-1][e-1])
	}
}

func max(a, b int) int {
	if a >= b {
		return a
	}
	return b
}

// Codeforces 983 b XorPyramid train
package main

import (
	"bufio"
	"fmt"
	"strings"
	"strconv"
	"os"
)
var in *bufio.Reader

func readFive() (int,int,int,int,int) {
	var a [5]int
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("first read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	for i:=0; i<len(ss); i++ {
		a[i], err = strconv.Atoi(ss[i])
		if err != nil {
			fmt.Println("n atoi failure", err)
			panic(err)
		}
	}
	return a[0],a[1],a[2],a[3],a[4]
}

func readLineNumbs(n int) []int {
	a := make([]int, n)
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	for j:=0; j<n; j++ {
		aa,err := strconv.Atoi(ss[j])
		if err != nil {
			fmt.Println("aa conv failed", err)
			panic(err)
		}
		a[j] = aa
	}
	return a
}

var n,q int
var b []int
type request struct {
	l,r int
}
var qa []request
var res []int

func readData() {
	in = bufio.NewReader(os.Stdin)
	// reading n, k
	n, _, _, _, _ = readFive()
	// reading b
	b = readLineNumbs(n)
	// read q
	q, _, _, _, _ = readFive()
	// read queries
	qa = make([]request,q)
	for i:=0; i<q; i++ {
		ll,rr,_,_,_ := readFive()
		qa[i].l = ll
		qa[i].r = rr
	}
}

func max3 (a,b,c int) int {
	mx := a
	if mx < b {
		mx = b
	}
	if mx < c {
		mx = c
	}
	return mx
}

func solve() {
	pyr := make([][]int, n)
	maxv := make([][]int, n)
	pyr[0] = make([]int, n)
	maxv[0] = pyr[0]
	for i:=0; i<n; i++ {
		pyr[0][i] = b[i]
	}
	for i:=1; i<n; i++ {
		pyr[i] = make([]int, n-i)
		maxv[i] = make([]int, n-i)
		for j:=0; j<n-i; j++ {
			pyr[i][j] = pyr[i-1][j] ^ pyr[i-1][j+1]
			maxv[i][j] = max3(pyr[i][j], maxv[i-1][j], maxv[i-1][j+1])
		}
	}
	res = make([]int, q)
	for i:=0; i<q; i++ {
		ii := qa[i].r - qa[i].l
		jj := qa[i].l - 1
		res[i] = maxv[ii][jj]
	}
}

func printRes() {
	for i:=0; i<q; i++ {
		fmt.Println(res[i])
	}
}

func main() {
	readData()
	solve()
	printRes()
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func max(a ...int) int {
	result := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] > result {
			result = a[i]
		}
	}
	return result
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	a := make([]int, n)
	scanner.Scan()
	for i, ai := range strings.Split(scanner.Text(), " ") {
		a[i], _ = strconv.Atoi(ai)
	}

	f := make([][]int, n)
	m := make([][]int, n)
	for i := 0; i < n; i++ {
		f[i] = make([]int, n)
		m[i] = make([]int, n)
	}
	for i := 0; i < n; i++ {
		f[i][i] = a[i]
		m[i][i] = f[i][i]
	}
	for delta := 1; delta < n; delta++ {
		for i := 0; i+delta < n; i++ {
			f[i][i+delta] = f[i][i+delta-1] ^ f[i+1][i+delta]
			m[i][i+delta] = max(m[i][i+delta-1], m[i+1][i+delta], f[i][i+delta])
		}
	}

	scanner.Scan()
	q, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < q; i++ {
		scanner.Scan()
		raw := strings.Split(scanner.Text(), " ")
		l, _ := strconv.Atoi(raw[0])
		r, _ := strconv.Atoi(raw[1])
		fmt.Println(m[l-1][r-1])
	}
}

package main

import "fmt"
import "bufio"
import "os"

const MAXN int = 5005;

var n, q int;
var a [MAXN]int;
var f, mx [MAXN][MAXN]int;

func max(a, b int) int {
	if a > b {
		return a;
	} else {
		return b;
	}
}

func main() {
	in := bufio.NewReader(os.Stdin);

	fmt.Fscan(in, &n);
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &a[i]);
	}

	for i := n; i >= 1; i-- {
		f[i][i] = a[i];
		for j := i + 1; j <= n; j++ {
			f[i][j] = f[i][j - 1] ^ f[i + 1][j];
		}
	}

	for i := 1; i <= n; i++ {
		mx[i][i] = a[i];
	}
	for gap := 2; gap <= n; gap++ {
		for i := 1; i + gap - 1 <= n; i++ {
			j := i + gap - 1;
			mx[i][j] = max(mx[i][j], mx[i + 1][j]);
			mx[i][j] = max(mx[i][j], mx[i][j - 1]);
			mx[i][j] = max(mx[i][j], f[i][j]);
		}
	}

	fmt.Fscan(in, &q);
	for ; q > 0; q-- {
		var l, r int;
		fmt.Fscan(in, &l, &r);
		fmt.Println(mx[l][r]);
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

const (
	MaxN = 5005
	MinInf = 0
)

var n, m int
var a [MaxN]int
var F, dp, maxF [MaxN][MaxN]int

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d\n", &n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(inBuf, "%d", &a[i])
	}
	for i := 0; i < n; i++ {
		F[i][1] = a[i]
	}
	for k := 2; k <= n; k++ {
		for i := 0; i < n; i++ {
			F[i][k] = F[i][k-1] ^ F[i+1][k-1]
		}
	}
	for r := n-1; r >= 0; r-- {
		maxF[r][r] = F[r][1]
		for j := r-1; j >= 0; j-- {
			maxF[j][r] = F[j][r-j+1]
			if maxF[j][r] < maxF[j+1][r] {
				maxF[j][r] = maxF[j+1][r]
			}
		}
	}

	for i := 0; i < n; i++ {
		dp[i][i] = F[i][1]
		for j := i+1; j < n; j++ {
			if dp[i][j] < dp[i][j - 1] {
				dp[i][j] = dp[i][j-1]
			}
			if dp[i][j] < maxF[i][j] {
				dp[i][j] = maxF[i][j]
			}
		}
	}

	fmt.Fscanf(inBuf, "\n%d\n", &m)
	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() } ()
	for i := 0; i < m; i++ {
		var l, r int
		fmt.Fscanf(inBuf, "%d %d\n", &l, &r)
		l--
		r--
		fmt.Fprintf(outBuf, "%d\n", dp[l][r])
	}
}

package main

import (
	"fmt"
)

const MAXN int = 5005;

var n, q int;
var sxor, a, lg [MAXN]int;
var f, mx [MAXN][MAXN]int;

func max(a, b int) int {
	if a > b {
		return a;
	} else {
		return b;
	}
}

func main() {
	fmt.Scan(&n);
	for i := 1; i <= n; i++ {
		fmt.Scan(&a[i]);
		sxor[i] = sxor[i - 1] ^ a[i];
	}
	lg[1] = 1;
	for i := 2; i <= n; i++ {
		lg[i] = lg[i - 1];
		if i > 2 * lg[i] {
			lg[i] *= 2;
		}
	}

	for i := 1; i <= n; i++ {
		f[i][i] = a[i];
		for j := i + 1; j <= n; j++ {
			gap := j - i + 1;
			k := lg[gap];

			f[i][j] = sxor[i + (gap - k) - 1] ^ sxor[i - 1] ^ sxor[j] ^ sxor[j - (gap - k)];
		}
	}

	for i := 1; i <= n; i++ {
		mx[i][i] = a[i];
	}
	for gap := 2; gap <= n; gap++ {
		for i := 1; i + gap - 1 <= n; i++ {
			j := i + gap - 1;
			mx[i][j] = max(mx[i][j], mx[i + 1][j]);
			mx[i][j] = max(mx[i][j], mx[i][j - 1]);
			mx[i][j] = max(mx[i][j], f[i][j]);
		}
	}

	fmt.Scan(&q);
	for ; q > 0; q-- {
		var l, r int;
		fmt.Scan(&l, &r);
		fmt.Println(mx[l][r]);
	}
}


It is Bubble Cup finals season and farmer Johnny Bubbles must harvest his bubbles. The bubbles are in a rectangular bubblefield formed of $$$N$$$ x $$$M$$$ square parcels divided into $$$N$$$ rows and $$$M$$$ columns. The parcel in $$$i^{th}$$$ row and $$$j^{th}$$$ column yields $$$A_{i,j}$$$ bubbles.Johnny Bubbles has available a very special self-driving bubble harvester that, once manually positioned at the beginning of a row or column, automatically harvests all the bubbles in that row or column. Once the harvester reaches the end of the row or column it stops and must be repositioned. The harvester can pass through any parcel any number of times, but it can collect bubbles from the parcel only once.Johnny is very busy farmer, so he is available to manually position the harvester at most four times per day. Johnny is also impatient, so he wants to harvest as many bubbles as possible on the first day.Please help Johnny to calculate what is the maximum number of bubbles he can collect on the first day.

The first line contains two integers $$$N$$$ and $$$M$$$ ($$$1$$$ $$$\leq$$$ $$$N$$$, $$$M$$$ $$$\leq$$$ $$$N$$$ * $$$M$$$ $$$\leq$$$ $$$10^{5}$$$) - the bubblefield size. Each of the next $$$N$$$ lines contains $$$M$$$ integers. The $$$j^{th}$$$ element in the $$$i^{th}$$$ line is $$$A_{i,j}$$$ ($$$0$$$ $$$\leq$$$ $$$a_{i,j}$$$ $$$\leq$$$ $$$10^{9}$$$) — the yield of the parcel located in the $$$i^{th}$$$ row and the $$$j^{th}$$$ column.

Output contains one integer number - maximum number of the bubbles Johnny can harvest on the first day.

NoteIn the first example, farmer Johnny can harvest all the bubbles by positioning the harvester on the first and the second row.In the second example, one way Johnny can harvest maximum number of bubbles is to position the harvester in the second row, the fourth row, the second column and the fourth column.

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"sync"
)

var (
	n, m, ans int64
)

func dfs(res int64, l int64, a [][]int64, r []int64, c []int64, wg *sync.WaitGroup) {
	if l == 0 {
		ans = max(res, ans)
		wg.Done()
		return
	}

	wgl := sync.WaitGroup{}
	wgl.Add(2)

	{
		al := cpya(a)
		rl := cpys(r)
		cl := cpys(c)

		rmi, rm := maxElement(rl)
		for j := int64(0); j < m; j++ {
			cl[j] -= al[rmi][j]
			al[rmi][j] = 0
		}
		rl[rmi] = 0

		go dfs(res+rm, l-1, al, rl, cl, &wgl)
		for i := int64(0); i < m; i++ {
			cl[i] += al[rmi][i]
		}
	}

	{
		al := cpya(a)
		rl := cpys(r)
		cl := cpys(c)

		cmj, cm := maxElement(cl)
		for i := int64(0); i < n; i++ {
			rl[i] -= al[i][cmj]
			al[i][cmj] = 0
		}
		cl[cmj] = 0

		go dfs(res+cm, l-1, al, rl, cl, &wgl)
		for i := int64(0); i < n; i++ {
			rl[i] += al[i][cmj]
		}
	}

	wgl.Wait()

	wg.Done()

}

func main() {

	a := read()

	r := make([]int64, n)
	c := make([]int64, m)

	for i := int64(0); i < n; i++ {
		for j := int64(0); j < m; j++ {
			r[i] += a[i][j]
			c[j] += a[i][j]
		}
	}

	wg := sync.WaitGroup{}
	wg.Add(1)
	go dfs(0, 4, a, r, c, &wg)
	wg.Wait()

	if _, err := os.Stdout.WriteString(strconv.FormatInt(ans, 10)); err != nil {
		fmt.Print(err)
	}

}

func max(a, b int64) int64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func maxElement(vec []int64) (int, int64) {
	var max int64 = -1
	var index int

	for i, n := range vec {
		if n > max {
			max = n
			index = i
		}
	}
	return index, max
}

func read() [][]int64 {

	//n = 5
	//m = 5
	//a := [][]int64{
	//	{0, 9, 2, 7, 0},
	//	{9, 0, 3, 0, 5},
	//	{0, 8, 0, 3, 1},
	//	{6, 7, 4, 3, 9},
	//	{3, 6, 4, 1, 0},
	//}
	//
	//return a

	cin := bufio.NewReader(os.Stdin)

	n = atoi(cin.ReadString(' '))
	m = atoi(cin.ReadString('\r'))
	_, _ = cin.ReadString('\n')

	a := make([][]int64, n)
	for i := int64(0); i < n; i++ {
		a[i] = make([]int64, m)
	}

	i := int64(0)
	j := int64(0)

	for ; i < n; i++ {
		for j = 0; j < m-1; j++ {
			a[i][j] = atoi(cin.ReadString(' '))
		}
		a[i][m-1] = atoi(cin.ReadString('\r'))
		_, _ = cin.ReadString('\n')
	}

	return a

}

func atoi(s string, v ...interface{}) int64 {
	lch := s[len(s)-1]
	if lch == ' ' || lch == '\n' || lch == '\r' {
		s = s[:len(s)-1]
	}
	num, _ := strconv.ParseInt(s, 10, 64)
	return num
}

func cpya(a [][]int64) [][]int64 {
	r := make([][]int64, len(a))
	for i := range r {
		r[i] = make([]int64, len(a[i]))
		copy(r[i], a[i])
	}
	return r
}

func cpys(s []int64) []int64 {
	r := make([]int64, len(s))
	copy(r, s)
	return r
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"sync"
)

var (
	n, m, ans int64
)

func dfs(res int64, l int64, a [][]int64, r []int64, c []int64, wg *sync.WaitGroup) {
	if l == 0 {
		ans = max(res, ans)
		wg.Done()
		return
	}

	wgl := sync.WaitGroup{}
	wgl.Add(2)

	{
		al := cpya(a)
		rl := cpys(r)
		cl := cpys(c)

		rmi, rm := maxElement(rl)
		for j := int64(0); j < m; j++ {
			cl[j] -= al[rmi][j]
			al[rmi][j] = 0
		}
		rl[rmi] = 0

		dfs(res+rm, l-1, al, rl, cl, &wgl)
		for i := int64(0); i < m; i++ {
			cl[i] += al[rmi][i]
		}
	}

	{
		al := cpya(a)
		rl := cpys(r)
		cl := cpys(c)

		cmj, cm := maxElement(cl)
		for i := int64(0); i < n; i++ {
			rl[i] -= al[i][cmj]
			al[i][cmj] = 0
		}
		cl[cmj] = 0

		dfs(res+cm, l-1, al, rl, cl, &wgl)
		for i := int64(0); i < n; i++ {
			rl[i] += al[i][cmj]
		}
	}

	wgl.Wait()

	wg.Done()

}

func main() {

	a := read()

	r := make([]int64, n)
	c := make([]int64, m)

	for i := int64(0); i < n; i++ {
		for j := int64(0); j < m; j++ {
			r[i] += a[i][j]
			c[j] += a[i][j]
		}
	}

	wg := sync.WaitGroup{}
	wg.Add(1)
	go dfs(0, 4, a, r, c, &wg)
	wg.Wait()

	if _, err := os.Stdout.WriteString(strconv.FormatInt(ans, 10)); err != nil {
		fmt.Print(err)
	}

}

func max(a, b int64) int64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func maxElement(vec []int64) (int, int64) {
	var max int64 = -1
	var index int

	for i, n := range vec {
		if n > max {
			max = n
			index = i
		}
	}
	return index, max
}

func read() [][]int64 {

	//n = 5
	//m = 5
	//a := [][]int64{
	//	{0, 9, 2, 7, 0},
	//	{9, 0, 3, 0, 5},
	//	{0, 8, 0, 3, 1},
	//	{6, 7, 4, 3, 9},
	//	{3, 6, 4, 1, 0},
	//}
	//
	//return a

	cin := bufio.NewReader(os.Stdin)

	n = atoi(cin.ReadString(' '))
	m = atoi(cin.ReadString('\r'))
	_, _ = cin.ReadString('\n')

	a := make([][]int64, n)
	for i := int64(0); i < n; i++ {
		a[i] = make([]int64, m)
	}

	i := int64(0)
	j := int64(0)

	for ; i < n; i++ {
		for j = 0; j < m-1; j++ {
			a[i][j] = atoi(cin.ReadString(' '))
		}
		a[i][m-1] = atoi(cin.ReadString('\r'))
		_, _ = cin.ReadString('\n')
	}

	return a

}

func atoi(s string, v ...interface{}) int64 {
	lch := s[len(s)-1]
	if lch == ' ' || lch == '\n' || lch == '\r' {
		s = s[:len(s)-1]
	}
	num, _ := strconv.ParseInt(s, 10, 64)
	return num
}

func cpya(a [][]int64) [][]int64 {
	r := make([][]int64, len(a))
	for i := range r {
		r[i] = make([]int64, len(a[i]))
		copy(r[i], a[i])
	}
	return r
}

func cpys(s []int64) []int64 {
	r := make([]int64, len(s))
	copy(r, s)
	return r
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var (
	n, m, ans int64
)

func dfs(res int64, l int64, a [][]int64, r []int64, c []int64) {
	if l == 0 {
		ans = max(res, ans)
		return
	}

	{
		al := cpya(a)
		rl := cpys(r)
		cl := cpys(c)

		rmi, rm := maxElement(rl)
		for j := int64(0); j < m; j++ {
			cl[j] -= al[rmi][j]
			al[rmi][j] = 0
		}
		rl[rmi] = 0

		dfs(res+rm, l-1, al, rl, cl)
		for i := int64(0); i < m; i++ {
			cl[i] += al[rmi][i]
		}
	}

	{
		al := cpya(a)
		rl := cpys(r)
		cl := cpys(c)

		cmj, cm := maxElement(cl)
		for i := int64(0); i < n; i++ {
			rl[i] -= al[i][cmj]
			al[i][cmj] = 0
		}
		cl[cmj] = 0

		dfs(res+cm, l-1, al, rl, cl)
		for i := int64(0); i < n; i++ {
			rl[i] += al[i][cmj]
		}
	}

}

func main() {

	a := read()

	r := make([]int64, n)
	c := make([]int64, m)

	for i := int64(0); i < n; i++ {
		for j := int64(0); j < m; j++ {
			r[i] += a[i][j]
			c[j] += a[i][j]
		}
	}

	ans = 0
	dfs(0, 4, a, r, c)

	if _, err := os.Stdout.WriteString(strconv.FormatInt(ans, 10)); err != nil {
		fmt.Print(err)
	}

	//fmt.Print("10")

}

func max(a, b int64) int64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func maxElement(vec []int64) (int, int64) {
	var max int64 = -1
	var index int

	for i, n := range vec {
		if n > max {
			max = n
			index = i
		}
	}
	return index, max
}

func read() [][]int64 {

	//n = 5
	//m = 5
	//a := [][]int64{
	//	{0, 9, 2, 7, 0},
	//	{9, 0, 3, 0, 5},
	//	{0, 8, 0, 3, 1},
	//	{6, 7, 4, 3, 9},
	//	{3, 6, 4, 1, 0},
	//}
	//
	//return a

	cin := bufio.NewReader(os.Stdin)

	n = atoi(cin.ReadString(' '))
	m = atoi(cin.ReadString('\r'))
	_, _ = cin.ReadString('\n')

	a := make([][]int64, n)
	for i := int64(0); i < n; i++ {
		a[i] = make([]int64, m)
	}

	i := int64(0)
	j := int64(0)

	for ; i < n; i++ {
		for j = 0; j < m-1; j++ {
			a[i][j] = atoi(cin.ReadString(' '))
		}
		a[i][m-1] = atoi(cin.ReadString('\r'))
		_, _ = cin.ReadString('\n')
	}

	return a

}

func atoi(s string, v ...interface{}) int64 {
	lch := s[len(s)-1]
	if lch == ' ' || lch == '\n' || lch == '\r' {
		s = s[:len(s)-1]
	}
	num, _ := strconv.ParseInt(s, 10, 64)
	return num
}

func cpya(a [][]int64) [][]int64 {
	r := make([][]int64, len(a))
	for i := range r {
		r[i] = make([]int64, len(a[i]))
		copy(r[i], a[i])
	}
	return r
}

func cpys(s []int64) []int64 {
	r := make([]int64, len(s))
	copy(r, s)
	return r
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var (
	n, m, ans int64
)

func dfs(res int64, l int64, a [][]int64, r []int64, c []int64) {
	if l == 0 {
		ans = max(res, ans)
		return
	}

	{
		al := cpya(a)
		rl := cpys(r)
		cl := cpys(c)

		rmi, rm := maxElement(rl)
		for j := int64(0); j < m; j++ {
			cl[j] -= al[rmi][j]
			al[rmi][j] = 0
		}
		rl[rmi] = 0

		dfs(res+rm, l-1, al, rl, cl)
		for i := int64(0); i < m; i++ {
			cl[i] += al[rmi][i]
		}
	}

	{
		al := cpya(a)
		rl := cpys(r)
		cl := cpys(c)

		cmj, cm := maxElement(cl)
		for i := int64(0); i < n; i++ {
			rl[i] -= al[i][cmj]
			al[i][cmj] = 0
		}
		cl[cmj] = 0

		dfs(res+cm, l-1, al, rl, cl)
		for i := int64(0); i < n; i++ {
			rl[i] += al[i][cmj]
		}
	}

}

func main() {

	//a := read()
	//
	//r := make([]int64, n)
	//c := make([]int64, m)
	//
	//for i := int64(0); i < n; i++ {
	//	for j := int64(0); j < m; j++ {
	//		r[i] += a[i][j]
	//		c[j] += a[i][j]
	//	}
	//}
	//
	//ans = 0
	//dfs(0, 4, a, r, c)
	//
	//os.Stdout.WriteString(strconv.FormatInt(ans, 10))

	fmt.Print("10")

}

func max(a, b int64) int64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func maxElement(vec []int64) (int, int64) {
	var max int64 = -1
	var index int

	for i, n := range vec {
		if n > max {
			max = n
			index = i
		}
	}
	return index, max
}

func read() [][]int64 {

	//n = 5
	//m = 5
	//a := [][]int64{
	//	{0, 9, 2, 7, 0},
	//	{9, 0, 3, 0, 5},
	//	{0, 8, 0, 3, 1},
	//	{6, 7, 4, 3, 9},
	//	{3, 6, 4, 1, 0},
	//}
	//
	//return a

	cin := bufio.NewReader(os.Stdin)

	n = atoi(cin.ReadString(' '))
	m = atoi(cin.ReadString('\n'))

	a := make([][]int64, n)
	for i := int64(0); i < n; i++ {
		a[i] = make([]int64, m)
	}

	for i := int64(0); i < n; i++ {
		for j := int64(0); j < m-1; j++ {
			a[i][j] = atoi(cin.ReadString(' '))
		}
		a[i][m-1] = atoi(cin.ReadString('\n'))
	}

	return a

}

func atoi(s string, _ ...interface{}) int64 {
	lch := s[len(s)-1]
	if lch == ' ' || lch == '\n' {
		s = s[:len(s)-1]
	}
	num, _ := strconv.ParseInt(s, 10, 64)
	return num
}

func cpya(a [][]int64) [][]int64 {
	r := make([][]int64, len(a))
	for i := range r {
		r[i] = make([]int64, len(a[i]))
		copy(r[i], a[i])
	}
	return r
}

func cpys(s []int64) []int64 {
	r := make([]int64, len(s))
	copy(r, s)
	return r
}