package main

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

func LL(n int) int { return n * 2 }
func RR(n int) int { return n*2 + 1 }

var sum, l, r, c, d []int
var f [310000]int

const MOD int = 1000000009

func InitDetail(a []int, ll, rr, n int) {
	l[n] = ll
	r[n] = rr
	c[n] = 0
	d[n] = 0
	if ll == rr {
		sum[n] = a[ll] % MOD
		return
	}

	var mid int = (ll + rr) / 2
	InitDetail(a, ll, mid, LL(n))
	InitDetail(a, mid+1, rr, RR(n))
	sum[n] = (sum[LL(n)] + sum[RR(n)]) % MOD
}

func Init(n int, a []int) {
	sum = make([]int, n*4)
	l = make([]int, n*4)
	r = make([]int, n*4)
	c = make([]int, n*4)
	d = make([]int, n*4)
	InitDetail(a, 1, n, 1)
}

func GetF(a, b, n int) int {
	if n == 1 {
		return a
	} else if n == 2 {
		return b
	} else {
		return int((int64(b)*int64(f[n-1]) + int64(a)*int64(f[n-2])) % int64(MOD))
	}
}

func GetSumF(a, b, n int) int {
	return ((GetF(a, b, n+2)-b)%MOD + MOD) % MOD
}

func Assign(a, b, length, n int) {
	// fmt.Println("Assign:", a, b, "for", l[n], r[n], "sum", GetSumF(a, b, length))
	c[n] = (c[n] + a) % MOD
	d[n] = (d[n] + b) % MOD
	sum[n] += GetSumF(a, b, length)
	sum[n] %= MOD
}

// define f(n) as fib sequeue
// define F[1] = a, F[2] = b, then F(n) = b*f(n-1) + a*f(n-2)
func Update(n int) {
	if c[n] == 0 && d[n] == 0 {
		return
	}
	var mid int = (l[n] + r[n]) / 2
	var lhalf = mid - l[n] + 1
	Assign(c[n], d[n], lhalf, LL(n))
	Assign(GetF(c[n], d[n], lhalf+1), GetF(c[n], d[n], lhalf+2), r[n]-l[n]+1-lhalf, RR(n))
	c[n] = 0
	d[n] = 0
}

func Query(ll, rr, n int) int {
	if ll == l[n] && rr == r[n] {
		return sum[n]
	}
	var mid int = (l[n] + r[n]) / 2
	var res int = 0
	Update(n)
	if rr <= mid {
		res = Query(ll, rr, LL(n))
	} else if ll > mid {
		res = Query(ll, rr, RR(n))
	} else {
		res = (Query(ll, mid, LL(n)) + Query(mid+1, rr, RR(n))) % MOD
	}
	sum[n] = (sum[LL(n)] + sum[RR(n)]) % MOD
	// fmt.Println("Query:", ll, rr, "->", res)
	return res
}

func Insert(ll, rr, n, prefix, seq_len int) {
	// fmt.Println("Interval:", l[n], r[n], "insert:", ll, rr, "seq:", prefix, seq_len)
	if l[n] == ll && r[n] == rr {
		Assign(f[prefix+1], f[prefix+2], seq_len-prefix, n)
		return
	}

	Update(n)
	var mid int = (l[n] + r[n]) / 2
	if rr <= mid {
		Insert(ll, rr, LL(n), prefix, seq_len)
	} else if mid < ll {
		Insert(ll, rr, RR(n), prefix, seq_len)
	} else {
		var l_len int = mid - ll + 1
		Insert(ll, mid, LL(n), prefix, l_len+prefix)
		Insert(mid+1, rr, RR(n), l_len+prefix, seq_len)
	}
	sum[n] = (sum[LL(n)] + sum[RR(n)]) % MOD
}

func main() {
	f[0] = 0
	f[1] = 1
	for i := 2; i < len(f); i++ {
		f[i] = (f[i-1] + f[i-2]) % MOD
	}

	reader := bufio.NewReader(os.Stdin)
	var n, m int
	fmt.Fscan(reader, &n, &m)

	var a []int = make([]int, n+1)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i+1])
	}

	Init(n, a)
	var cmd, li, ri int
	for i := 0; i < m; i++ {
		// fmt.Println(i, "Debug info")
		fmt.Fscan(reader, &cmd, &li, &ri)
		if cmd == 2 {
			fmt.Println(Query(li, ri, 1))
		} else {
			Insert(li, ri, 1, 0, ri-li+1)
		}
	}
}

package main

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

const (
	mod = 1000000009
	maxn = 4*300000+5
)

var (
	t [maxn]Segment
	fib [maxn]int64
)

type Segment struct {
	x0 int64
	x1 int64
	sum int64
}

func updateS(a, b int, x0, x1 int64) {
	x0 %= mod
	x1 %= mod

	t[a+b].x0 += x0
	t[a+b].x0 %= mod
	t[a+b].x1 += x1
	t[a+b].x1 %= mod

	t[a+b].sum += x0*fib[b-a] + x1*(fib[b-a+1]-1)
	t[a+b].sum %= mod
}

func Split(a, c, b int) {
	updateS(a, c, t[a+b].x0, t[a+b].x1)
	updateS(c, b,
		t[a+b].x0 * fib[c-a-1] + t[a+b].x1 * fib[c-a],
		t[a+b].x0 * fib[c-a] + t[a+b].x1 * fib[c-a+1])
	t[a+b].x0 = 0
	t[a+b].x1 = 0
	t[a+b].sum = 0
}

func Merge(a, c, b int) {
	t[a+b].sum = 0
	t[a+b].sum += t[a+c].sum
	t[a+b].sum += t[b+c].sum
	// if t[a+b].sum >= mod { t[a+b].sum -= mod }
	t[a+b].sum %= mod
}

func query(a, b, l, r int) int64 {
	if l <= a && b <= r {
		return t[a + b].sum
	}
	c := (a + b)/2
	Split(a, c, b)
	var res int64
	if l < c { res += query(a, c, l, r) }
	if c < r { res += query(c, b, l, r) }
	Merge(a, c, b)
	// if res >= mod { res -= mod }
	res %= mod
	return res
}
func update(a, b, l, r int, i int) {
	if l <= a && b <= r {
		updateS(a, b, fib[i], fib[i+1])
		return
	}
	c := (a + b)/2
	Split(a, c, b)
	if l < c { update(a, c, l, r, i) }
	if c < r { update(c, b, l, r, i+(c-a)) }
	Merge(a, c, b)
}
func fullMerge(a, b int) {
	if a + 1 == b {
		return
	}
	c := (a+b)/2
	fullMerge(a, c)
	fullMerge(c, b)
	Merge(a, c, b)
}
func print(a, b int, pref string) {
	fmt.Println(pref, t[a+b])
	if a+1 == b {
		return
	}
	c := (a+b)/2
	print(a,c,pref+"  ")
	print(c,b,pref+"  ")
}

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

	fib[1] = 1
	for i := 2; i < maxn; i++ {
		fib[i] = fib[i-1] + fib[i-2]
		// if fib[i] >= mod { fib[i] -= mod }
		fib[i] %= mod
	}

	var n, m int
	fmt.Fscan(in, &n, &m)

	b := 1
	for b < n {
		b *= 2
	}

	var x int64
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &x)
		t[i+i+1].sum = x
	}
	fullMerge(0, b)

	// print(0,b,"")

	var typ, l, r int
	for i := 0; i < m; i++ {
		fmt.Fscan(in, &typ, &l, &r)
		switch typ {
		case 1:
			update(0, b, l-1, r, 1-l+1)
		case 2:
			fmt.Fprintln(out, query(0, b, l-1, r))
		}
	}
}

package main

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

func LL(n int) int { return n * 2 }
func RR(n int) int { return n*2 + 1 }

var sum, l, r, c, d []int
var f, sumf [310000]int

const MOD int = 1000000009

func InitDetail(a []int, ll, rr, n int) {
	l[n] = ll
	r[n] = rr
	c[n] = 0
	d[n] = 0
	if ll == rr {
		sum[n] = a[ll] % MOD
		return
	}

	var mid int = (ll + rr) / 2
	InitDetail(a, ll, mid, LL(n))
	InitDetail(a, mid+1, rr, RR(n))
	sum[n] = (sum[LL(n)] + sum[RR(n)]) % MOD
}

func Init(n int, a []int) {
	sum = make([]int, n*4)
	l = make([]int, n*4)
	r = make([]int, n*4)
	c = make([]int, n*4)
	d = make([]int, n*4)
	InitDetail(a, 1, n, 1)
}

func GetF(a, b, n int) int {
	if n == 1 {
		return a
	} else if n == 2 {
		return b
	} else {
		return (b*f[n-1] + a*f[n-2]) % MOD
	}
}

func GetSumF(a, b, n int) int {
	return ((GetF(a, b, n+2)-b)%MOD + MOD) % MOD
}

func Assign(a, b, length, n int) {
	// fmt.Println("Assign:", a, b, "for", l[n], r[n])
	c[n] = (c[n] + a) % MOD
	d[n] = (d[n] + b) % MOD
	sum[n] += GetSumF(a, b, length)
}

// define f(n) as fib sequeue
// define F[1] = a, F[2] = b, then F(n) = b*f(n-1) + a*f(n-2)
func Update(n int) {
	if c[n] == 0 && d[n] == 0 {
		return
	}
	var mid int = (l[n] + r[n]) / 2
	var lhalf = mid - l[n] + 1
	Assign(c[n], d[n], lhalf, LL(n))
	Assign(GetF(c[n], d[n], lhalf+1), GetF(c[n], d[n], lhalf+2), r[n]-lhalf, RR(n))
	c[n] = 0
	d[n] = 0
}

func Query(ll, rr, n int) int {
	if ll == l[n] && rr == r[n] {
		return sum[n]
	}
	var mid int = (l[n] + r[n]) / 2
	var res int = 0
	Update(n)
	if rr <= mid {
		res = Query(ll, rr, LL(n))
	} else if ll > mid {
		res = Query(ll, rr, RR(n))
	} else {
		res = (Query(ll, mid, LL(n)) + Query(mid+1, rr, RR(n))) % MOD
	}
	// fmt.Println("Query:", ll, rr, "->", res)
	return res
}

func Insert(ll, rr, n, prefix, seq_len int) {
	// fmt.Println("Interval:", l[n], r[n], "insert:", ll, rr, "seq:", prefix, seq_len)
	if l[n] == ll && r[n] == rr {
		Assign(f[prefix+1], f[prefix+2], seq_len, n)
		return
	}

	Update(n)
	var mid int = (l[n] + r[n]) / 2
	if rr <= mid {
		Insert(ll, rr, LL(n), prefix, seq_len)
	} else if mid < ll {
		Insert(ll, rr, RR(n), prefix, seq_len)
	} else {
		var l_len int = mid - ll + 1
		Insert(ll, mid, LL(n), prefix, l_len+prefix)
		Insert(mid+1, rr, RR(n), l_len+prefix, seq_len)
	}
	sum[n] = sum[LL(n)] + sum[RR(n)]
}

func main() {
	sumf[0], f[0] = 0, 0
	sumf[1], f[1] = 1, 1
	for i := 2; i < len(f); i++ {
		f[i] = (f[i-1] + f[i-2]) % MOD
		sumf[i] = (sumf[i-1] + f[i]) % MOD
	}

	reader := bufio.NewReader(os.Stdin)
	var n, m int
	fmt.Fscan(reader, &n, &m)

	var a []int = make([]int, n+1)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i+1])
	}

	Init(n, a)
	var cmd, li, ri int
	for i := 0; i < m; i++ {
		// fmt.Println(i, "Debug info")
		fmt.Fscan(reader, &cmd, &li, &ri)
		if cmd == 2 {
			fmt.Println(Query(li, ri, 1))
		} else {
			Insert(li, ri, 1, 0, ri-li+1)
		}
	}
}

package main

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

const (
	mod = 1000000009
	maxn = 4*300000+5
)

var (
	t [maxn]Segment
	fib [maxn]int64
)

type Segment struct {
	x0 int64
	x1 int64
	sum int64
}

func updateS(a, b int, x0, x1 int64) {
	t[a+b].x0 += x0
	if t[a+b].x0 >= mod { t[a+b].x0 -= mod }
	t[a+b].x1 += x1
	if t[a+b].x1 >= mod { t[a+b].x1 -= mod }

	t[a+b].sum += x0*(fib[b-a]) + x1*(fib[b-a+1]-1)
	t[a+b].sum %= mod
}

func Split(a, c, b int) {
	updateS(a, c, t[a+b].x0, t[a+b].x1)
	updateS(c, b,
		t[a+b].x0 * fib[c-a-1] + t[a+b].x1 * fib[c-a],
		t[a+b].x0 * fib[c-a] + t[a+b].x1 * fib[c-a+1])
	t[a+b].x0 = 0
	t[a+b].x1 = 0
	t[a+b].sum = 0
}

func Merge(a, c, b int) {
	t[a+b].sum = 0
	t[a+b].sum += t[a+c].sum
	t[a+b].sum += t[b+c].sum
	if t[a+b].sum >= mod { t[a+b].sum -= mod }
}

func query(a, b, l, r int) int64 {
	if l <= a && b <= r {
		return t[a + b].sum
	}
	c := (a + b)/2
	Split(a, c, b)
	var res int64
	if l < c { res += query(a, c, l, r) }
	if c < r { res += query(c, b, l, r) }
	Merge(a, c, b)
	if res >= mod { res -= mod }
	return res
}
func update(a, b, l, r int, i int) {
	if l <= a && b <= r {
		updateS(a, b, fib[i], fib[i+1])
		return
	}
	c := (a + b)/2
	Split(a, c, b)
	if l < c { update(a, c, l, r, i) }
	if c < r { update(c, b, l, r, i+(c-a)) }
	Merge(a, c, b)
}
func fullMerge(a, b int) {
	if a + 1 == b {
		return
	}
	c := (a+b)/2
	fullMerge(a, c)
	fullMerge(c, b)
	Merge(a, c, b)
}
func print(a, b int, pref string) {
	fmt.Println(pref, t[a+b])
	if a+1 == b {
		return
	}
	c := (a+b)/2
	print(a,c,pref+"  ")
	print(c,b,pref+"  ")
}

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

	fib[1] = 1
	for i := 2; i < maxn; i++ {
		fib[i] = fib[i-1] + fib[i-2]
		if fib[i] >= mod { fib[i] -= mod }
	}

	var n, m int
	fmt.Fscan(in, &n, &m)

	b := 1
	for b < n {
		b *= 2
	}

	var x int64
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &x)
		t[i+i+1].sum = x
		// t[i+i+1].sum %= mod
		// TODO: Do we also need to update x0, x1?
		// No
	}
	fullMerge(0, b)

	// print(0,b,"")

	var typ, l, r int
	for i := 0; i < m; i++ {
		fmt.Fscan(in, &typ, &l, &r)
		switch typ {
		case 1:
			update(0, b, l-1, r, 1-l+1)
		case 2:
			fmt.Fprintln(out, query(0, b, l-1, r))
		}
	}
}

package main

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

const (
	mod = 1000000009
	maxn = 4*300000+5
)

var (
	t [maxn]Segment
	fib [maxn]int64
)

type Segment struct {
	x0 int64
	x1 int64
	sum int64
}

func updateS(a, b int, x0, x1 int64) {
	t[a+b].x0 += x0
	t[a+b].x0 %= mod
	t[a+b].x1 += x1
	t[a+b].x1 %= mod

	t[a+b].sum += x0*fib[b-a] + x1*(fib[b-a+1]-1)
	t[a+b].sum %= mod
}

func Split(a, c, b int) {
	updateS(a, c, t[a+b].x0, t[a+b].x1)
	updateS(c, b,
		t[a+b].x0 * fib[c-a-1] + t[a+b].x1 * fib[c-a],
		t[a+b].x0 * fib[c-a] + t[a+b].x1 * fib[c-a+1])
	t[a+b].x0 = 0
	t[a+b].x1 = 0
	t[a+b].sum = 0
}

func Merge(a, c, b int) {
	t[a+b].sum = 0
	t[a+b].sum += t[a+c].sum
	t[a+b].sum += t[b+c].sum
	// if t[a+b].sum >= mod { t[a+b].sum -= mod }
	t[a+b].sum %= mod
}

func query(a, b, l, r int) int64 {
	if l <= a && b <= r {
		return t[a + b].sum
	}
	c := (a + b)/2
	Split(a, c, b)
	var res int64
	if l < c { res += query(a, c, l, r) }
	if c < r { res += query(c, b, l, r) }
	Merge(a, c, b)
	// if res >= mod { res -= mod }
	res %= mod
	return res
}
func update(a, b, l, r int, i int) {
	if l <= a && b <= r {
		updateS(a, b, fib[i], fib[i+1])
		return
	}
	c := (a + b)/2
	Split(a, c, b)
	if l < c { update(a, c, l, r, i) }
	if c < r { update(c, b, l, r, i+(c-a)) }
	Merge(a, c, b)
}
func fullMerge(a, b int) {
	if a + 1 == b {
		return
	}
	c := (a+b)/2
	fullMerge(a, c)
	fullMerge(c, b)
	Merge(a, c, b)
}
func print(a, b int, pref string) {
	fmt.Println(pref, t[a+b])
	if a+1 == b {
		return
	}
	c := (a+b)/2
	print(a,c,pref+"  ")
	print(c,b,pref+"  ")
}

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

	fib[1] = 1
	for i := 2; i < maxn; i++ {
		fib[i] = fib[i-1] + fib[i-2]
		if fib[i] >= mod { fib[i] -= mod }
	}

	var n, m int
	fmt.Fscan(in, &n, &m)

	b := 1
	for b < n {
		b *= 2
	}

	var x int64
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &x)
		t[i+i+1].sum = x
	}
	fullMerge(0, b)

	// print(0,b,"")

	var typ, l, r int
	for i := 0; i < m; i++ {
		fmt.Fscan(in, &typ, &l, &r)
		switch typ {
		case 1:
			update(0, b, l-1, r, 1-l+1)
		case 2:
			fmt.Fprintln(out, query(0, b, l-1, r))
		}
	}
}

package main

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

const (
	mod = 1000000009
	maxn = 4*300000+5
)

var (
	t [maxn]Segment
	fib [maxn]int64
)

type Segment struct {
	x0 int64
	x1 int64
	sum int64
}

func updateS(a, b int, x0, x1 int64) {
	x0 %= mod
	x1 %= mod

	t[a+b].x0 += x0
	t[a+b].x0 %= mod
	t[a+b].x1 += x1
	t[a+b].x1 %= mod

	t[a+b].sum += x0*(fib[b-a]) + x1*(fib[b-a+1]-1)
	t[a+b].sum %= mod
}

func Split(a, c, b int) {
	updateS(a, c, t[a+b].x0, t[a+b].x1)
	updateS(c, b,
		t[a+b].x0 * fib[c-a-1] + t[a+b].x1 * fib[c-a],
		t[a+b].x0 * fib[c-a] + t[a+b].x1 * fib[c-a+1])
	t[a+b].x0 = 0
	t[a+b].x1 = 0
	t[a+b].sum = 0
}

func Merge(a, c, b int) {
	t[a+b].sum = 0
	t[a+b].sum += t[a+c].sum
	t[a+b].sum += t[b+c].sum
	t[a+b].sum %= mod
}

func query(a, b, l, r int) int64 {
	if l <= a && b <= r {
		return t[a + b].sum
	}
	c := (a + b)/2
	Split(a, c, b)
	var res int64
	if l < c { res += query(a, c, l, r) }
	if c < r { res += query(c, b, l, r) }
	Merge(a, c, b)
	return res
}
func update(a, b, l, r int, i int) {
	if l <= a && b <= r {
		updateS(a, b, fib[i], fib[i+1])
		return
	}
	c := (a + b)/2
	Split(a, c, b)
	if l < c { update(a, c, l, r, i) }
	if c < r { update(c, b, l, r, i+(c-a)) }
	Merge(a, c, b)
}
func fullMerge(a, b int) {
	if a + 1 == b {
		return
	}
	c := (a+b)/2
	fullMerge(a, c)
	fullMerge(c, b)
	Merge(a, c, b)
}
func print(a, b int, pref string) {
	fmt.Println(pref, t[a+b])
	if a+1 == b {
		return
	}
	c := (a+b)/2
	print(a,c,pref+"  ")
	print(c,b,pref+"  ")
}

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

	fib[1] = 1
	for i := 2; i < maxn; i++ {
		fib[i] = fib[i-1] + fib[i-2]
		fib[i] %= mod
	}

	var n, m int
	fmt.Fscan(in, &n, &m)

	b := 1
	for b < n {
		b *= 2
	}

	var x int64
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &x)
		t[i+i+1].sum = x
		// TODO: Do we also need to update x0, x1?
	}
	fullMerge(0, b)

	// print(0,b,"")

	var typ, l, r int
	for i := 0; i < m; i++ {
		fmt.Fscan(in, &typ, &l, &r)
		if typ == 1 {
			// fmt.Println("Update")
			update(0, b, l-1, r, 1-l+1)
			// print(0,b,"")
		} else {
			// fmt.Println("Query")
			fmt.Fprintln(out, query(0, b, l-1, r))
			// print(0,b,"")
		}
	}
}

package main

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

const (
	mod = 1000000009
	maxn = 4*300000+5
)

var (
	t [maxn]Segment
	fib [maxn]int64
)

type Segment struct {
	x0 int64
	x1 int64
	sum int64
}

func updateS(a, b int, x0, x1 int64) {
	t[a+b].x0 += x0
	t[a+b].x0 %= mod
	t[a+b].x1 += x1
	t[a+b].x1 %= mod

	t[a+b].sum += x0*fib[b-a] + x1*(fib[b-a+1]-1)
	t[a+b].sum %= mod
}

func Split(a, c, b int) {
	updateS(a, c, t[a+b].x0, t[a+b].x1)
	updateS(c, b,
		t[a+b].x0 * fib[c-a-1] + t[a+b].x1 * fib[c-a],
		t[a+b].x0 * fib[c-a] + t[a+b].x1 * fib[c-a+1])
	t[a+b].x0 = 0
	t[a+b].x1 = 0
	t[a+b].sum = 0
}

func Merge(a, c, b int) {
	t[a+b].sum = 0
	t[a+b].sum += t[a+c].sum
	t[a+b].sum += t[b+c].sum
	if t[a+b].sum >= mod { t[a+b].sum -= mod }
}

func query(a, b, l, r int) int64 {
	if l <= a && b <= r {
		return t[a + b].sum
	}
	c := (a + b)/2
	Split(a, c, b)
	var res int64
	if l < c { res += query(a, c, l, r) }
	if c < r { res += query(c, b, l, r) }
	Merge(a, c, b)
	// if res >= mod { res -= mod }
	res %= mod
	return res
}
func update(a, b, l, r int, i int) {
	if l <= a && b <= r {
		updateS(a, b, fib[i], fib[i+1])
		return
	}
	c := (a + b)/2
	Split(a, c, b)
	if l < c { update(a, c, l, r, i) }
	if c < r { update(c, b, l, r, i+(c-a)) }
	Merge(a, c, b)
}
func fullMerge(a, b int) {
	if a + 1 == b {
		return
	}
	c := (a+b)/2
	fullMerge(a, c)
	fullMerge(c, b)
	Merge(a, c, b)
}
func print(a, b int, pref string) {
	fmt.Println(pref, t[a+b])
	if a+1 == b {
		return
	}
	c := (a+b)/2
	print(a,c,pref+"  ")
	print(c,b,pref+"  ")
}

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

	fib[1] = 1
	for i := 2; i < maxn; i++ {
		fib[i] = fib[i-1] + fib[i-2]
		if fib[i] >= mod { fib[i] -= mod }
	}

	var n, m int
	fmt.Fscan(in, &n, &m)

	b := 1
	for b < n {
		b *= 2
	}

	var x int64
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &x)
		t[i+i+1].sum = x
	}
	fullMerge(0, b)

	// print(0,b,"")

	var typ, l, r int
	for i := 0; i < m; i++ {
		fmt.Fscan(in, &typ, &l, &r)
		switch typ {
		case 1:
			update(0, b, l-1, r, 1-l+1)
		case 2:
			fmt.Fprintln(out, query(0, b, l-1, r))
		}
	}
}

package main

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

const (
	mod = 1000000009
	maxn = 4*300000+5
)

var (
	t [maxn]Segment
	fib [maxn]int64
)

type Segment struct {
	x0 int64
	x1 int64
	sum int64
}

func updateS(a, b int, x0, x1 int64) {
	t[a+b].x0 += x0
	t[a+b].x0 %= mod
	t[a+b].x1 += x1
	t[a+b].x1 %= mod

	t[a+b].sum += x0*fib[b-a] + x1*(fib[b-a+1]-1)
	t[a+b].sum %= mod
}

func Split(a, c, b int) {
	updateS(a, c, t[a+b].x0, t[a+b].x1)
	updateS(c, b,
		t[a+b].x0 * fib[c-a-1] + t[a+b].x1 * fib[c-a],
		t[a+b].x0 * fib[c-a] + t[a+b].x1 * fib[c-a+1])
	t[a+b].x0 = 0
	t[a+b].x1 = 0
	t[a+b].sum = 0
}

func Merge(a, c, b int) {
	t[a+b].sum = 0
	t[a+b].sum += t[a+c].sum
	t[a+b].sum += t[b+c].sum
	if t[a+b].sum >= mod { t[a+b].sum -= mod }
}

func query(a, b, l, r int) int64 {
	if l <= a && b <= r {
		return t[a + b].sum
	}
	c := (a + b)/2
	Split(a, c, b)
	var res int64
	if l < c { res += query(a, c, l, r) }
	if c < r { res += query(c, b, l, r) }
	Merge(a, c, b)
	if res >= mod { res -= mod }
	return res
}
func update(a, b, l, r int, i int) {
	if l <= a && b <= r {
		updateS(a, b, fib[i], fib[i+1])
		return
	}
	c := (a + b)/2
	Split(a, c, b)
	if l < c { update(a, c, l, r, i) }
	if c < r { update(c, b, l, r, i+(c-a)) }
	Merge(a, c, b)
}
func fullMerge(a, b int) {
	if a + 1 == b {
		return
	}
	c := (a+b)/2
	fullMerge(a, c)
	fullMerge(c, b)
	Merge(a, c, b)
}
func print(a, b int, pref string) {
	fmt.Println(pref, t[a+b])
	if a+1 == b {
		return
	}
	c := (a+b)/2
	print(a,c,pref+"  ")
	print(c,b,pref+"  ")
}

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

	fib[1] = 1
	for i := 2; i < maxn; i++ {
		fib[i] = fib[i-1] + fib[i-2]
		if fib[i] >= mod { fib[i] -= mod }
	}

	var n, m int
	fmt.Fscan(in, &n, &m)

	b := 1
	for b < n {
		b *= 2
	}

	var x int64
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &x)
		t[i+i+1].sum = x
	}
	fullMerge(0, b)

	// print(0,b,"")

	var typ, l, r int
	for i := 0; i < m; i++ {
		fmt.Fscan(in, &typ, &l, &r)
		switch typ {
		case 1:
			update(0, b, l-1, r, 1-l+1)
		case 2:
			fmt.Fprintln(out, query(0, b, l-1, r))
		}
	}
}

package main

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

const (
	mod = 1000000009
	maxn = 4*300000+5
)

var (
	t [maxn]Segment
	fib [maxn]int64
)

type Segment struct {
	x0 int64
	x1 int64
	sum int64
}

func updateS(a, b int, x0, x1 int64) {
	t[a+b].x0 += x0
	t[a+b].x0 %= mod
	t[a+b].x1 += x1
	t[a+b].x1 %= mod

	t[a+b].sum += x0*fib[b-a] + x1*(fib[b-a+1]-1)
	t[a+b].sum %= mod
}

func Split(a, c, b int) {
	updateS(a, c, t[a+b].x0, t[a+b].x1)
	updateS(c, b,
		t[a+b].x0 * fib[c-a-1] + t[a+b].x1 * fib[c-a],
		t[a+b].x0 * fib[c-a] + t[a+b].x1 * fib[c-a+1])
	t[a+b].x0 = 0
	t[a+b].x1 = 0
	t[a+b].sum = 0
}

func Merge(a, c, b int) {
	t[a+b].sum = 0
	t[a+b].sum += t[a+c].sum
	t[a+b].sum += t[b+c].sum
	// if t[a+b].sum >= mod { t[a+b].sum -= mod }
	t[a+b].sum %= mod
}

func query(a, b, l, r int) int64 {
	if l <= a && b <= r {
		return t[a + b].sum
	}
	c := (a + b)/2
	Split(a, c, b)
	var res int64
	if l < c { res += query(a, c, l, r) }
	if c < r { res += query(c, b, l, r) }
	Merge(a, c, b)
	// if res >= mod { res -= mod }
	res %= mod
	return res
}
func update(a, b, l, r int, i int) {
	if l <= a && b <= r {
		updateS(a, b, fib[i], fib[i+1])
		return
	}
	c := (a + b)/2
	Split(a, c, b)
	if l < c { update(a, c, l, r, i) }
	if c < r { update(c, b, l, r, i+(c-a)) }
	Merge(a, c, b)
}
func fullMerge(a, b int) {
	if a + 1 == b {
		return
	}
	c := (a+b)/2
	fullMerge(a, c)
	fullMerge(c, b)
	Merge(a, c, b)
}
func print(a, b int, pref string) {
	fmt.Println(pref, t[a+b])
	if a+1 == b {
		return
	}
	c := (a+b)/2
	print(a,c,pref+"  ")
	print(c,b,pref+"  ")
}

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

	fib[1] = 1
	for i := 2; i < maxn; i++ {
		fib[i] = fib[i-1] + fib[i-2]
		// if fib[i] >= mod { fib[i] -= mod }
		fib[i] %= mod
	}

	var n, m int
	fmt.Fscan(in, &n, &m)

	b := 1
	for b < n {
		b *= 2
	}

	var x int64
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &x)
		t[i+i+1].sum = x
	}
	fullMerge(0, b)

	// print(0,b,"")

	var typ, l, r int
	for i := 0; i < m; i++ {
		fmt.Fscan(in, &typ, &l, &r)
		switch typ {
		case 1:
			update(0, b, l-1, r, 1-l+1)
		case 2:
			fmt.Fprintln(out, query(0, b, l-1, r))
		}
	}
}

package main

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

func LL(n int) int { return n * 2 }
func RR(n int) int { return n*2 + 1 }

var sum, l, r, length, prev []int
var f, sumf [310000]int

const MOD int = 1000000009

func InitDetail(a []int, ll, rr, n int) {
	l[n] = ll
	r[n] = rr
	length[n] = 0
	prev[n] = -1
	sum[n] = 0
	if ll == rr {
		sum[n] = a[ll]
		return
	}

	var mid int = (ll + rr) / 2
	InitDetail(a, ll, mid, LL(n))
	InitDetail(a, mid+1, rr, RR(n))
	sum[n] = (sum[LL(n)] + sum[RR(n)]) % MOD
}

func Init(n int, a []int) {
	sum = make([]int, n*4)
	l = make([]int, n*4)
	r = make([]int, n*4)
	length = make([]int, n*4)
	prev = make([]int, n*4)
	InitDetail(a, 1, n, 1)
}

func Assign(n, prefix, seq_len int) {
	prev[n] = prefix
	length[n] = seq_len
	// fmt.Println(l[n], r[n], "+", sumf[seq_len]-sumf[prefix])
	sum[n] += sumf[seq_len] - sumf[prefix]
	sum[n] = (sum[n] + MOD) % MOD
}

func Update(n int) {
	if prev[n] == -1 || l[n] == r[n] {
		return
	}
	var mid int = (l[n] + r[n]) / 2
	Update(LL(n))
	Update(RR(n))
	Assign(LL(n), prev[n], prev[n]+mid-l[n]+1)
	Assign(RR(n), prev[n]+mid-l[n]+1, length[n])
	prev[n] = -1
}

func Query(ll, rr, n int) int {
	if ll == l[n] && rr == r[n] {
		return sum[n]
	}
	var mid int = (l[n] + r[n]) / 2
	var res int = 0
	Update(n)
	if rr <= mid {
		res = Query(ll, rr, LL(n))
	} else if ll > mid {
		res = Query(ll, rr, RR(n))
	} else {
		res = (Query(ll, mid, LL(n)) + Query(mid+1, rr, RR(n))) % MOD
	}
	// fmt.Println("Query:", ll, rr, "->", res)
	return res
}

func Insert(ll, rr, n, prefix, seq_len int) {
	// fmt.Println("Interval:", l[n], r[n], "insert:", ll, rr, "seq:", prefix, seq_len)
	if l[n] == ll && r[n] == rr {
		Update(n)
		Assign(n, prefix, seq_len)
		return
	}
	sum[n] += sumf[seq_len] - sumf[prefix]
	sum[n] = (sum[n] + MOD) % MOD
	Update(n)
	var mid int = (l[n] + r[n]) / 2
	if rr <= mid {
		Insert(ll, rr, LL(n), prefix, seq_len)
	} else if mid < ll {
		Insert(ll, rr, RR(n), prefix, seq_len)
	} else {
		var l_len int = mid - ll + 1
		Insert(ll, mid, LL(n), prefix, l_len+prefix)
		Insert(mid+1, rr, RR(n), l_len, seq_len)
	}
}

func main() {
	sumf[0], f[0] = 0, 0
	sumf[1], f[1] = 1, 1
	for i := 2; i < len(f); i++ {
		f[i] = (f[i-1] + f[i-2]) % MOD
		sumf[i] = (sumf[i-1] + f[i]) % MOD
	}

	reader := bufio.NewReader(os.Stdin)
	var n, m int
	fmt.Fscan(reader, &n, &m)

	var a []int = make([]int, n+1)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i+1])
	}

	Init(n, a)
	var cmd, li, ri int
	for i := 0; i < m; i++ {
		fmt.Fscan(reader, &cmd, &li, &ri)
		if cmd == 2 {
			fmt.Println(Query(li, ri, 1))
		} else {
			Insert(li, ri, 1, 0, ri-li+1)
		}
	}
}

package main

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

func LL(n int) int { return n * 2 }
func RR(n int) int { return n*2 + 1 }

var sum, l, r, c, d []int
var f [310000]int

const MOD int = 1000000009

func InitDetail(a []int, ll, rr, n int) {
	l[n] = ll
	r[n] = rr
	c[n] = 0
	d[n] = 0
	if ll == rr {
		sum[n] = a[ll] % MOD
		return
	}

	var mid int = (ll + rr) / 2
	InitDetail(a, ll, mid, LL(n))
	InitDetail(a, mid+1, rr, RR(n))
	sum[n] = (sum[LL(n)] + sum[RR(n)]) % MOD
}

func Init(n int, a []int) {
	sum = make([]int, n*4)
	l = make([]int, n*4)
	r = make([]int, n*4)
	c = make([]int, n*4)
	d = make([]int, n*4)
	InitDetail(a, 1, n, 1)
}

func GetF(a, b, n int) int {
	if n == 1 {
		return a
	} else if n == 2 {
		return b
	} else {
		return (b*f[n-1] + a*f[n-2]) % MOD
	}
}

func GetSumF(a, b, n int) int {
	return ((GetF(a, b, n+2)-b)%MOD + MOD) % MOD
}

func Assign(a, b, length, n int) {
	// fmt.Println("Assign:", a, b, "for", l[n], r[n], "sum", GetSumF(a, b, length))
	c[n] = (c[n] + a) % MOD
	d[n] = (d[n] + b) % MOD
	sum[n] += GetSumF(a, b, length)
}

// define f(n) as fib sequeue
// define F[1] = a, F[2] = b, then F(n) = b*f(n-1) + a*f(n-2)
func Update(n int) {
	if c[n] == 0 && d[n] == 0 {
		return
	}
	var mid int = (l[n] + r[n]) / 2
	var lhalf = mid - l[n] + 1
	Assign(c[n], d[n], lhalf, LL(n))
	Assign(GetF(c[n], d[n], lhalf+1), GetF(c[n], d[n], lhalf+2), r[n]-l[n]+1-lhalf, RR(n))
	c[n] = 0
	d[n] = 0
}

func Query(ll, rr, n int) int {
	if ll == l[n] && rr == r[n] {
		return sum[n]
	}
	var mid int = (l[n] + r[n]) / 2
	var res int = 0
	Update(n)
	if rr <= mid {
		res = Query(ll, rr, LL(n))
	} else if ll > mid {
		res = Query(ll, rr, RR(n))
	} else {
		res = (Query(ll, mid, LL(n)) + Query(mid+1, rr, RR(n))) % MOD
	}
	sum[n] = sum[LL(n)] + sum[RR(n)]
	// fmt.Println("Query:", ll, rr, "->", res)
	return res
}

func Insert(ll, rr, n, prefix, seq_len int) {
	// fmt.Println("Interval:", l[n], r[n], "insert:", ll, rr, "seq:", prefix, seq_len)
	if l[n] == ll && r[n] == rr {
		Assign(f[prefix+1], f[prefix+2], seq_len-prefix, n)
		return
	}

	Update(n)
	var mid int = (l[n] + r[n]) / 2
	if rr <= mid {
		Insert(ll, rr, LL(n), prefix, seq_len)
	} else if mid < ll {
		Insert(ll, rr, RR(n), prefix, seq_len)
	} else {
		var l_len int = mid - ll + 1
		Insert(ll, mid, LL(n), prefix, l_len+prefix)
		Insert(mid+1, rr, RR(n), l_len+prefix, seq_len)
	}
	sum[n] = sum[LL(n)] + sum[RR(n)]
}

func main() {
	f[0] = 0
	f[1] = 1
	for i := 2; i < len(f); i++ {
		f[i] = (f[i-1] + f[i-2]) % MOD
	}

	reader := bufio.NewReader(os.Stdin)
	var n, m int
	fmt.Fscan(reader, &n, &m)

	var a []int = make([]int, n+1)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i+1])
	}

	Init(n, a)
	var cmd, li, ri int
	for i := 0; i < m; i++ {
		// fmt.Println(i, "Debug info")
		fmt.Fscan(reader, &cmd, &li, &ri)
		if cmd == 2 {
			fmt.Println(Query(li, ri, 1))
		} else {
			Insert(li, ri, 1, 0, ri-li+1)
		}
	}
}

package main

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

func LL(n int) int { return n * 2 }
func RR(n int) int { return n*2 + 1 }

var sum, l, r, length, prev []int
var f, sumf [310000]int

const MOD int = 1000000009

func InitDetail(a []int, ll, rr, n int) {
	l[n] = ll
	r[n] = rr
	length[n] = 0
	prev[n] = -1
	sum[n] = 0
	if ll == rr {
		sum[n] = a[ll] % MOD
		return
	}

	var mid int = (ll + rr) / 2
	InitDetail(a, ll, mid, LL(n))
	InitDetail(a, mid+1, rr, RR(n))
	sum[n] = (sum[LL(n)] + sum[RR(n)]) % MOD
}

func Init(n int, a []int) {
	sum = make([]int, n*4)
	l = make([]int, n*4)
	r = make([]int, n*4)
	length = make([]int, n*4)
	prev = make([]int, n*4)
	InitDetail(a, 1, n, 1)
}

func Assign(n, prefix, seq_len int) {
	prev[n] = prefix
	length[n] = seq_len
	// fmt.Println(l[n], r[n], "+", sumf[seq_len]-sumf[prefix])
	sum[n] += ((sumf[seq_len]-sumf[prefix])%MOD + MOD) % MOD
	sum[n] = (sum[n] + MOD) % MOD
}

func Update(n int) {
	if prev[n] == -1 || l[n] == r[n] {
		return
	}
	var mid int = (l[n] + r[n]) / 2
	Update(LL(n))
	Update(RR(n))
	Assign(LL(n), prev[n], prev[n]+mid-l[n]+1)
	Assign(RR(n), prev[n]+mid-l[n]+1, length[n])
	prev[n] = -1
}

func Query(ll, rr, n int) int {
	if ll == l[n] && rr == r[n] {
		return sum[n]
	}
	var mid int = (l[n] + r[n]) / 2
	var res int = 0
	Update(n)
	if rr <= mid {
		res = Query(ll, rr, LL(n))
	} else if ll > mid {
		res = Query(ll, rr, RR(n))
	} else {
		res = (Query(ll, mid, LL(n)) + Query(mid+1, rr, RR(n))) % MOD
	}
	// fmt.Println("Query:", ll, rr, "->", res)
	return res
}

func Insert(ll, rr, n, prefix, seq_len int) {
	// fmt.Println("Interval:", l[n], r[n], "insert:", ll, rr, "seq:", prefix, seq_len)
	if l[n] == ll && r[n] == rr {
		Update(n)
		Assign(n, prefix, seq_len)
		return
	}
	sum[n] += ((sumf[seq_len]-sumf[prefix])%MOD + MOD) % MOD
	sum[n] = (sum[n] + MOD) % MOD
	Update(n)
	var mid int = (l[n] + r[n]) / 2
	if rr <= mid {
		Insert(ll, rr, LL(n), prefix, seq_len)
	} else if mid < ll {
		Insert(ll, rr, RR(n), prefix, seq_len)
	} else {
		var l_len int = mid - ll + 1
		Insert(ll, mid, LL(n), prefix, l_len+prefix)
		Insert(mid+1, rr, RR(n), l_len, seq_len)
	}
}

func main() {
	sumf[0], f[0] = 0, 0
	sumf[1], f[1] = 1, 1
	for i := 2; i < len(f); i++ {
		f[i] = (f[i-1] + f[i-2]) % MOD
		sumf[i] = (sumf[i-1] + f[i]) % MOD
	}

	reader := bufio.NewReader(os.Stdin)
	var n, m int
	fmt.Fscan(reader, &n, &m)

	var a []int = make([]int, n+1)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i+1])
	}

	Init(n, a)
	var cmd, li, ri int
	for i := 0; i < m; i++ {
		fmt.Fscan(reader, &cmd, &li, &ri)
		if cmd == 2 {
			fmt.Println(Query(li, ri, 1))
		} else {
			Insert(li, ri, 1, 0, ri-li+1)
		}
	}
}

package main

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

func LL(n int) int { return n * 2 }
func RR(n int) int { return n*2 + 1 }

var sum, l, r, length, prev []int
var f, sumf [310000]int

const MOD int = 1000000009

func InitDetail(a []int, ll, rr, n int) {
	l[n] = ll
	r[n] = rr
	length[n] = 0
	prev[n] = -1
	sum[n] = 0
	if ll == rr {
		sum[n] = a[ll] % MOD
		return
	}

	var mid int = (ll + rr) / 2
	InitDetail(a, ll, mid, LL(n))
	InitDetail(a, mid+1, rr, RR(n))
	sum[n] = (sum[LL(n)] + sum[RR(n)]) % MOD
}

func Init(n int, a []int) {
	sum = make([]int, n*4)
	l = make([]int, n*4)
	r = make([]int, n*4)
	length = make([]int, n*4)
	prev = make([]int, n*4)
	InitDetail(a, 1, n, 1)
}

func Assign(n, prefix, seq_len int) {
	prev[n] = prefix
	length[n] = seq_len
	// fmt.Println(l[n], r[n], "+", sumf[seq_len]-sumf[prefix])
	sum[n] += ((sumf[seq_len]-sumf[prefix])%MOD + MOD) % MOD
	sum[n] = (sum[n] + MOD) % MOD
}

func Update(n int) {
	if prev[n] == -1 || l[n] == r[n] {
		return
	}
	var mid int = (l[n] + r[n]) / 2
	Update(LL(n))
	Update(RR(n))
	Assign(LL(n), prev[n], prev[n]+mid-l[n]+1)
	Assign(RR(n), prev[n]+mid-l[n]+1, length[n])
	prev[n] = -1
}

func Query(ll, rr, n int) int {
	if ll == l[n] && rr == r[n] {
		return sum[n]
	}
	var mid int = (l[n] + r[n]) / 2
	var res int = 0
	Update(n)
	if rr <= mid {
		res = Query(ll, rr, LL(n))
	} else if ll > mid {
		res = Query(ll, rr, RR(n))
	} else {
		res = (Query(ll, mid, LL(n)) + Query(mid+1, rr, RR(n))) % MOD
	}
	// fmt.Println("Query:", ll, rr, "->", res)
	return res
}

func Insert(ll, rr, n, prefix, seq_len int) {
	// fmt.Println("Interval:", l[n], r[n], "insert:", ll, rr, "seq:", prefix, seq_len)
	if l[n] == ll && r[n] == rr {
		Update(n)
		Assign(n, prefix, seq_len)
		return
	}
	sum[n] += ((sumf[seq_len]-sumf[prefix])%MOD + MOD) % MOD
	sum[n] = (sum[n] + MOD) % MOD
	Update(n)
	var mid int = (l[n] + r[n]) / 2
	if rr <= mid {
		Insert(ll, rr, LL(n), prefix, seq_len)
	} else if mid < ll {
		Insert(ll, rr, RR(n), prefix, seq_len)
	} else {
		var l_len int = mid - ll + 1
		Insert(ll, mid, LL(n), prefix, l_len+prefix)
		Insert(mid+1, rr, RR(n), l_len+prefix, seq_len)
	}
}

func main() {
	sumf[0], f[0] = 0, 0
	sumf[1], f[1] = 1, 1
	for i := 2; i < len(f); i++ {
		f[i] = (f[i-1] + f[i-2]) % MOD
		sumf[i] = (sumf[i-1] + f[i]) % MOD
	}

	reader := bufio.NewReader(os.Stdin)
	var n, m int
	fmt.Fscan(reader, &n, &m)

	var a []int = make([]int, n+1)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i+1])
	}

	Init(n, a)
	var cmd, li, ri int
	for i := 0; i < m; i++ {
		// fmt.Println(i, "Debug info")
		fmt.Fscan(reader, &cmd, &li, &ri)
		if cmd == 2 {
			fmt.Println(Query(li, ri, 1))
		} else {
			Insert(li, ri, 1, 0, ri-li+1)
		}
	}
}

package main

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

func LL(n int) int { return n * 2 }
func RR(n int) int { return n*2 + 1 }

var sum, l, r, length, prev []int
var f, sumf [310000]int

const MOD int = 1000000009

func InitDetail(a []int, ll, rr, n int) {
	l[n] = ll
	r[n] = rr
	length[n] = 0
	prev[n] = -1
	sum[n] = 0
	if ll == rr {
		sum[n] = a[ll]
		return
	}

	var mid int = (ll + rr) / 2
	InitDetail(a, ll, mid, LL(n))
	InitDetail(a, mid+1, rr, RR(n))
	sum[n] = (sum[LL(n)] + sum[RR(n)]) % MOD
}

func Init(n int, a []int) {
	sum = make([]int, n*4)
	l = make([]int, n*4)
	r = make([]int, n*4)
	length = make([]int, n*4)
	prev = make([]int, n*4)
	InitDetail(a, 1, n, 1)
}

func Assign(n, prefix, seq_len int) {
	prev[n] = prefix
	length[n] = seq_len
	// fmt.Println(l[n], r[n], "+", sumf[seq_len]-sumf[prefix])
	sum[n] += sumf[seq_len] - sumf[prefix]
}

func Update(n int) {
	if prev[n] == -1 || l[n] == r[n] {
		return
	}
	var mid int = (l[n] + r[n]) / 2
	Update(LL(n))
	Update(RR(n))
	Assign(LL(n), prev[n], prev[n]+mid-l[n]+1)
	Assign(RR(n), prev[n]+mid-l[n]+1, length[n])
	prev[n] = -1
}

func Query(ll, rr, n int) int {
	if ll == l[n] && rr == r[n] {
		return sum[n]
	}
	var mid = (l[n] + r[n]) / 2
	var res = 0
	Update(n)
	if rr <= mid {
		res = Query(ll, rr, LL(n))
	} else if ll > mid {
		res = Query(ll, rr, RR(n))
	} else {
		res = (Query(ll, mid, LL(n)) + Query(mid+1, rr, RR(n))) % MOD
	}
	// fmt.Println("Query:", ll, rr, "->", res)
	return res
}

func Insert(ll, rr, n, prefix, seq_len int) {
	// fmt.Println("Interval:", l[n], r[n], "insert:", ll, rr, "seq:", prefix, seq_len)
	if l[n] == ll && r[n] == rr {
		Update(n)
		Assign(n, prefix, seq_len)
		return
	}
	sum[n] += sumf[seq_len] - sumf[prefix]
	Update(n)
	var mid int = (l[n] + r[n]) / 2
	if rr <= mid {
		Insert(ll, rr, LL(n), prefix, seq_len)
	} else if mid < ll {
		Insert(ll, rr, RR(n), prefix, seq_len)
	} else {
		var l_len int = mid - ll + 1
		Insert(ll, mid, LL(n), prefix, l_len)
		Insert(mid+1, rr, RR(n), l_len, seq_len)
	}
}

func main() {
	sumf[0], f[0] = 0, 0
	sumf[1], f[1] = 1, 1
	for i := 2; i < len(f); i++ {
		f[i] = (f[i-1] + f[i-2]) % MOD
		sumf[i] = sumf[i-1] + f[i]
	}

	reader := bufio.NewReader(os.Stdin)
	var n, m int
	fmt.Fscan(reader, &n, &m)

	var a []int = make([]int, n+1)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i+1])
	}

	Init(n, a)
	var cmd, li, ri int
	for i := 0; i < m; i++ {
		fmt.Fscan(reader, &cmd, &li, &ri)
		if cmd == 2 {
			fmt.Println(Query(li, ri, 1))
		} else {
			Insert(li, ri, 1, 0, ri-li+1)
		}
	}
}

package main

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

func LL(n int) int { return n * 2 }
func RR(n int) int { return n*2 + 1 }

var sum, l, r, c, d []int
var f [310000]int

const MOD int = 1000000009

func InitDetail(a []int, ll, rr, n int) {
	l[n] = ll
	r[n] = rr
	c[n] = 0
	d[n] = 0
	if ll == rr {
		sum[n] = a[ll] % MOD
		return
	}

	var mid int = (ll + rr) / 2
	InitDetail(a, ll, mid, LL(n))
	InitDetail(a, mid+1, rr, RR(n))
	sum[n] = (sum[LL(n)] + sum[RR(n)]) % MOD
}

func Init(n int, a []int) {
	sum = make([]int, n*4)
	l = make([]int, n*4)
	r = make([]int, n*4)
	c = make([]int, n*4)
	d = make([]int, n*4)
	InitDetail(a, 1, n, 1)
}

func GetF(a, b, n int) int {
	if n == 1 {
		return a
	} else if n == 2 {
		return b
	} else {
		return int((int64(b)*int64(f[n-1]) + int64(a)*int64(f[n-2])) % int64(MOD))
	}
}

func GetSumF(a, b, n int) int {
	return ((GetF(a, b, n+2)-b)%MOD + MOD) % MOD
}

func Assign(a, b, length, n int) {
	// fmt.Println("Assign:", a, b, "for", l[n], r[n], "sum", GetSumF(a, b, length))
	c[n] = (c[n] + a) % MOD
	d[n] = (d[n] + b) % MOD
	sum[n] += GetSumF(a, b, length)
}

// define f(n) as fib sequeue
// define F[1] = a, F[2] = b, then F(n) = b*f(n-1) + a*f(n-2)
func Update(n int) {
	if c[n] == 0 && d[n] == 0 {
		return
	}
	var mid int = (l[n] + r[n]) / 2
	var lhalf = mid - l[n] + 1
	Assign(c[n], d[n], lhalf, LL(n))
	Assign(GetF(c[n], d[n], lhalf+1), GetF(c[n], d[n], lhalf+2), r[n]-l[n]+1-lhalf, RR(n))
	c[n] = 0
	d[n] = 0
}

func Query(ll, rr, n int) int {
	if ll == l[n] && rr == r[n] {
		return sum[n]
	}
	var mid int = (l[n] + r[n]) / 2
	var res int = 0
	Update(n)
	if rr <= mid {
		res = Query(ll, rr, LL(n))
	} else if ll > mid {
		res = Query(ll, rr, RR(n))
	} else {
		res = (Query(ll, mid, LL(n)) + Query(mid+1, rr, RR(n))) % MOD
	}
	sum[n] = (sum[LL(n)] + sum[RR(n)]) % MOD
	// fmt.Println("Query:", ll, rr, "->", res)
	return res
}

func Insert(ll, rr, n, prefix, seq_len int) {
	// fmt.Println("Interval:", l[n], r[n], "insert:", ll, rr, "seq:", prefix, seq_len)
	if l[n] == ll && r[n] == rr {
		Assign(f[prefix+1], f[prefix+2], seq_len-prefix, n)
		return
	}

	Update(n)
	var mid int = (l[n] + r[n]) / 2
	if rr <= mid {
		Insert(ll, rr, LL(n), prefix, seq_len)
	} else if mid < ll {
		Insert(ll, rr, RR(n), prefix, seq_len)
	} else {
		var l_len int = mid - ll + 1
		Insert(ll, mid, LL(n), prefix, l_len+prefix)
		Insert(mid+1, rr, RR(n), l_len+prefix, seq_len)
	}
	sum[n] = (sum[LL(n)] + sum[RR(n)]) % MOD
}

func main() {
	f[0] = 0
	f[1] = 1
	for i := 2; i < len(f); i++ {
		f[i] = (f[i-1] + f[i-2]) % MOD
	}

	reader := bufio.NewReader(os.Stdin)
	var n, m int
	fmt.Fscan(reader, &n, &m)

	var a []int = make([]int, n+1)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i+1])
	}

	Init(n, a)
	var cmd, li, ri int
	for i := 0; i < m; i++ {
		// fmt.Println(i, "Debug info")
		fmt.Fscan(reader, &cmd, &li, &ri)
		if cmd == 2 {
			fmt.Println(Query(li, ri, 1))
		} else {
			Insert(li, ri, 1, 0, ri-li+1)
		}
	}
}

package main

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

func LL(n int) int { return n * 2 }
func RR(n int) int { return n*2 + 1 }

var sum, l, r, c, d []int
var f [310000]int

const MOD int = 1000000009

func InitDetail(a []int, ll, rr, n int) {
	l[n] = ll
	r[n] = rr
	c[n] = 0
	d[n] = 0
	if ll == rr {
		sum[n] = a[ll] % MOD
		return
	}

	var mid int = (ll + rr) / 2
	InitDetail(a, ll, mid, LL(n))
	InitDetail(a, mid+1, rr, RR(n))
	sum[n] = (sum[LL(n)] + sum[RR(n)]) % MOD
}

func Init(n int, a []int) {
	sum = make([]int, n*4)
	l = make([]int, n*4)
	r = make([]int, n*4)
	c = make([]int, n*4)
	d = make([]int, n*4)
	InitDetail(a, 1, n, 1)
}

func GetF(a, b, n int) int {
	if n == 1 {
		return a
	} else if n == 2 {
		return b
	} else {
		return int((int64(b)*int64(f[n-1]) + int64(a)*int64(f[n-2])) % int64(MOD))
	}
}

func GetSumF(a, b, n int) int {
	return ((GetF(a, b, n+2)-b)%MOD + MOD) % MOD
}

func Assign(a, b, length, n int) {
	// fmt.Println("Assign:", a, b, "for", l[n], r[n], "sum", GetSumF(a, b, length))
	c[n] = (c[n] + a) % MOD
	d[n] = (d[n] + b) % MOD
	sum[n] += GetSumF(a, b, length)
}

// define f(n) as fib sequeue
// define F[1] = a, F[2] = b, then F(n) = b*f(n-1) + a*f(n-2)
func Update(n int) {
	if c[n] == 0 && d[n] == 0 {
		return
	}
	var mid int = (l[n] + r[n]) / 2
	var lhalf = mid - l[n] + 1
	Assign(c[n], d[n], lhalf, LL(n))
	Assign(GetF(c[n], d[n], lhalf+1), GetF(c[n], d[n], lhalf+2), r[n]-l[n]+1-lhalf, RR(n))
	c[n] = 0
	d[n] = 0
}

func Query(ll, rr, n int) int {
	if ll == l[n] && rr == r[n] {
		return sum[n]
	}
	var mid int = (l[n] + r[n]) / 2
	var res int = 0
	Update(n)
	if rr <= mid {
		res = Query(ll, rr, LL(n))
	} else if ll > mid {
		res = Query(ll, rr, RR(n))
	} else {
		res = (Query(ll, mid, LL(n)) + Query(mid+1, rr, RR(n))) % MOD
	}
	sum[n] = sum[LL(n)] + sum[RR(n)]
	// fmt.Println("Query:", ll, rr, "->", res)
	return res
}

func Insert(ll, rr, n, prefix, seq_len int) {
	// fmt.Println("Interval:", l[n], r[n], "insert:", ll, rr, "seq:", prefix, seq_len)
	if l[n] == ll && r[n] == rr {
		Assign(f[prefix+1], f[prefix+2], seq_len-prefix, n)
		return
	}

	Update(n)
	var mid int = (l[n] + r[n]) / 2
	if rr <= mid {
		Insert(ll, rr, LL(n), prefix, seq_len)
	} else if mid < ll {
		Insert(ll, rr, RR(n), prefix, seq_len)
	} else {
		var l_len int = mid - ll + 1
		Insert(ll, mid, LL(n), prefix, l_len+prefix)
		Insert(mid+1, rr, RR(n), l_len+prefix, seq_len)
	}
	sum[n] = sum[LL(n)] + sum[RR(n)]
}

func main() {
	f[0] = 0
	f[1] = 1
	for i := 2; i < len(f); i++ {
		f[i] = (f[i-1] + f[i-2]) % MOD
	}

	reader := bufio.NewReader(os.Stdin)
	var n, m int
	fmt.Fscan(reader, &n, &m)

	var a []int = make([]int, n+1)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i+1])
	}

	Init(n, a)
	var cmd, li, ri int
	for i := 0; i < m; i++ {
		// fmt.Println(i, "Debug info")
		fmt.Fscan(reader, &cmd, &li, &ri)
		if cmd == 2 {
			fmt.Println(Query(li, ri, 1))
		} else {
			Insert(li, ri, 1, 0, ri-li+1)
		}
	}
}

package main

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

func LL(n int) int { return n * 2 }
func RR(n int) int { return n*2 + 1 }

var sum, l, r, length, prev []int
var f, sumf [310000]int

const MOD int = 1000000009

func InitDetail(a []int, ll, rr, n int) {
	l[n] = ll
	r[n] = rr
	length[n] = 0
	prev[n] = -1
	sum[n] = 0
	if ll == rr {
		sum[n] = a[ll]
		return
	}

	var mid int = (ll + rr) / 2
	InitDetail(a, ll, mid, LL(n))
	InitDetail(a, mid+1, rr, RR(n))
	sum[n] = (sum[LL(n)] + sum[RR(n)]) % MOD
}

func Init(n int, a []int) {
	sum = make([]int, n*4)
	l = make([]int, n*4)
	r = make([]int, n*4)
	length = make([]int, n*4)
	prev = make([]int, n*4)
	InitDetail(a, 1, n, 1)
}

func Assign(n, prefix, seq_len int) {
	prev[n] = prefix
	length[n] = seq_len
	// fmt.Println(l[n], r[n], "+", sumf[seq_len]-sumf[prefix])
	sum[n] += sumf[seq_len] - sumf[prefix]
	sum[n] = (sum[n] + MOD) % MOD
}

func Update(n int) {
	if prev[n] == -1 || l[n] == r[n] {
		return
	}
	var mid int = (l[n] + r[n]) / 2
	Update(LL(n))
	Update(RR(n))
	Assign(LL(n), prev[n], prev[n]+mid-l[n]+1)
	Assign(RR(n), prev[n]+mid-l[n]+1, length[n])
	prev[n] = -1
}

func Query(ll, rr, n int) int {
	if ll == l[n] && rr == r[n] {
		return sum[n]
	}
	var mid int = (l[n] + r[n]) / 2
	var res int = 0
	Update(n)
	if rr <= mid {
		res = Query(ll, rr, LL(n))
	} else if ll > mid {
		res = Query(ll, rr, RR(n))
	} else {
		res = (Query(ll, mid, LL(n)) + Query(mid+1, rr, RR(n))) % MOD
	}
	// fmt.Println("Query:", ll, rr, "->", res)
	return res
}

func Insert(ll, rr, n, prefix, seq_len int) {
	// fmt.Println("Interval:", l[n], r[n], "insert:", ll, rr, "seq:", prefix, seq_len)
	if l[n] == ll && r[n] == rr {
		Update(n)
		Assign(n, prefix, seq_len)
		return
	}
	sum[n] += sumf[seq_len] - sumf[prefix]
	sum[n] = (sum[n] + MOD) % MOD
	Update(n)
	var mid int = (l[n] + r[n]) / 2
	if rr <= mid {
		Insert(ll, rr, LL(n), prefix, seq_len)
	} else if mid < ll {
		Insert(ll, rr, RR(n), prefix, seq_len)
	} else {
		var l_len int = mid - ll + 1
		Insert(ll, mid, LL(n), prefix, l_len+prefix)
		Insert(mid+1, rr, RR(n), l_len, seq_len)
	}
}

func main() {
	sumf[0], f[0] = 0, 0
	sumf[1], f[1] = 1, 1
	for i := 2; i < len(f); i++ {
		f[i] = (f[i-1] + f[i-2]) % MOD
		sumf[i] = sumf[i-1] + f[i]
	}

	reader := bufio.NewReader(os.Stdin)
	var n, m int
	fmt.Fscan(reader, &n, &m)

	var a []int = make([]int, n+1)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i+1])
	}

	Init(n, a)
	var cmd, li, ri int
	for i := 0; i < m; i++ {
		fmt.Fscan(reader, &cmd, &li, &ri)
		if cmd == 2 {
			fmt.Println(Query(li, ri, 1))
		} else {
			Insert(li, ri, 1, 0, ri-li+1)
		}
	}
}

package main

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

func LL(n int) int { return n * 2 }
func RR(n int) int { return n*2 + 1 }

var sum, l, r, c, d []int
var f, sumf [310000]int

const MOD int = 1000000009

func InitDetail(a []int, ll, rr, n int) {
	l[n] = ll
	r[n] = rr
	c[n] = 0
	d[n] = 0
	if ll == rr {
		sum[n] = a[ll] % MOD
		return
	}

	var mid int = (ll + rr) / 2
	InitDetail(a, ll, mid, LL(n))
	InitDetail(a, mid+1, rr, RR(n))
	sum[n] = (sum[LL(n)] + sum[RR(n)]) % MOD
}

func Init(n int, a []int) {
	sum = make([]int, n*4)
	l = make([]int, n*4)
	r = make([]int, n*4)
	c = make([]int, n*4)
	d = make([]int, n*4)
	InitDetail(a, 1, n, 1)
}

func GetF(a, b, n int) int {
	if n == 1 {
		return a
	} else if n == 2 {
		return b
	} else {
		return (b*f[n-1] + a*f[n-2]) % MOD
	}
}

func GetSumF(a, b, n int) int {
	return GetF(a, b, n+2) - b
}

func Assign(a, b, length, n int) {
	// fmt.Println("Upddate:", a, b, "for", l[n], r[n])
	c[n] = (c[n] + a) % MOD
	d[n] += (d[n] + b) % MOD
	sum[n] += GetSumF(a, b, length)
}

// define f(n) as fib sequeue
// define F[1] = a, F[2] = b, then F(n) = b*f(n-1) + a*f(n-2)
func Update(n int) {
	if c[n] == 0 && d[n] == 0 {
		return
	}
	var mid int = (l[n] + r[n]) / 2
	var lhalf = mid - l[n] + 1
	Assign(c[n], d[n], lhalf, LL(n))
	Assign(GetF(c[n], d[n], lhalf+1), GetF(c[n], d[n], lhalf+2), r[n]-lhalf, RR(n))
	c[n] = 0
	d[n] = 0
}

func Query(ll, rr, n int) int {
	if ll == l[n] && rr == r[n] {
		return sum[n]
	}
	var mid int = (l[n] + r[n]) / 2
	var res int = 0
	Update(n)
	if rr <= mid {
		res = Query(ll, rr, LL(n))
	} else if ll > mid {
		res = Query(ll, rr, RR(n))
	} else {
		res = (Query(ll, mid, LL(n)) + Query(mid+1, rr, RR(n))) % MOD
	}
	// fmt.Println("Query:", ll, rr, "->", res)
	return res
}

func Insert(ll, rr, n, prefix, seq_len int) {
	// fmt.Println("Interval:", l[n], r[n], "insert:", ll, rr, "seq:", prefix, seq_len)
	sum[n] += ((sumf[seq_len]-sumf[prefix])%MOD + MOD) % MOD
	sum[n] %= MOD
	if l[n] == ll && r[n] == rr {
		c[n] += f[prefix+1]
		d[n] += f[prefix+2]
		return
	}

	Update(n)
	var mid int = (l[n] + r[n]) / 2
	if rr <= mid {
		Insert(ll, rr, LL(n), prefix, seq_len)
	} else if mid < ll {
		Insert(ll, rr, RR(n), prefix, seq_len)
	} else {
		var l_len int = mid - ll + 1
		Insert(ll, mid, LL(n), prefix, l_len+prefix)
		Insert(mid+1, rr, RR(n), l_len+prefix, seq_len)
	}
}

func main() {
	sumf[0], f[0] = 0, 0
	sumf[1], f[1] = 1, 1
	for i := 2; i < len(f); i++ {
		f[i] = (f[i-1] + f[i-2]) % MOD
		sumf[i] = (sumf[i-1] + f[i]) % MOD
	}

	reader := bufio.NewReader(os.Stdin)
	var n, m int
	fmt.Fscan(reader, &n, &m)

	var a []int = make([]int, n+1)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i+1])
	}

	Init(n, a)
	var cmd, li, ri int
	for i := 0; i < m; i++ {
		// fmt.Println(i, "Debug info")
		fmt.Fscan(reader, &cmd, &li, &ri)
		if cmd == 2 {
			fmt.Println(Query(li, ri, 1))
		} else {
			Insert(li, ri, 1, 0, ri-li+1)
		}
	}
}


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] >= 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"
	"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"
	"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"
	"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"
	"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)
}


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"
	"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"
)

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"
	// "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)
}


// 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"
)

//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)
	}
}

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"
)

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)
	}
}