package main

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

func main() {
	var n int
	// var a int
	input := bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &n)

	ans := -19999999
	for i := 0; i < n; i++ {
		var a int
		fmt.Fscan(input, &a)
		if a < 0 {
			if ans < a {
				ans = a
			}
		} else {
			t := int(math.Floor(math.Sqrt(float64(a))))
			if t*t != a && ans < a {
				ans = a
			}
		}
	}
	fmt.Println(ans)
}

package main

import "fmt"

func main() {
	sq := make(map[int]bool)
	for i := 0; i < 1e4; i++ {
		sq[i*i] = true
	}
	var n int
	ma := int(-1e7)
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		var a int
		fmt.Scan(&a)
		if !sq[a] && ma < a {
			ma = a
		}
	}
	fmt.Println(ma)
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var n int32
	fmt.Scan(&n)
	a := make([]int32, n)
	for i := range a {
		fmt.Scan(&a[i])
	}

	f := false
	c := int32(math.MinInt32)
	for _, p := range a {
		if p == 0 || (f && p <= c) {
			continue
		}
		if p < 0 || !isPerfectSquare(p) {
			if p < c {
				continue
			}
			c = p
			f = true
		}
	}
	fmt.Println(c)
}

func isPerfectSquare(a int32) bool {
	s := int32(math.Sqrt(float64(a)))
	return (s * s) == a
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var n int
	fmt.Scanln(&n)
	result := -1000000
	for i := 0; i < n; i++ {
		var a int
		fmt.Scan(&a)
		if a >= 0 && int(math.Sqrt(float64(a)))*int(math.Sqrt(float64(a))) == a {
			continue
		}
		if a > result {
			result = a
		}

	}
	fmt.Println(result)
}

package main

import (
	"fmt"
	"math"
)

func sqrtSqr(a int) int {
	sqrtA := int(math.Sqrt(float64(a)))
	return sqrtA * sqrtA
}

func main() {
	var n int
	fmt.Scanln(&n)
	result := -1000000
	for i := 0; i < n; i++ {
		var a int
		fmt.Scan(&a)
		if a > result && sqrtSqr(a) != a {
			result = a
		}
	}
	fmt.Println(result)
}

package main

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

func main() {
	var n, x int
	maxNotPerfectSquare := -1000001
	stdin := bufio.NewReader(os.Stdin)
	fmt.Fscanf(stdin, "%d\n", &n)
	for i := 0; i < n; i++ {
		fmt.Fscan(stdin, &x)
		if !isPerfectSquare(x) && maxNotPerfectSquare < x {
			maxNotPerfectSquare = x
		}
	}
	fmt.Println(maxNotPerfectSquare)
}

func isPerfectSquare(x int) bool {
	if x < 0 {
		return false
	}
	num := math.Sqrt(float64(x))
	floorNum := math.Floor(num)
	return (num - floorNum) == 0
}

package main

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

const (
	max = 1000000
	min = -max
)

var sqrs map[int]struct{}

func init() {
	sqrs = map[int]struct{}{}
	for i := 0; i*i <= max; i++ {
		sqrs[i*i] = struct{}{}
	}
}

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

	var n int
	fmt.Fscan(r, &n)
	ans := min - 1
	for i := 0; i < n; i++ {
		var v int
		fmt.Fscan(r, &v)
		if _, ok := sqrs[v]; ok {
			continue
		}
		if v > ans {
			ans = v
		}
	}
	fmt.Println(ans)
}

package main

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

type Int64 []int64

func (a Int64) Len() int           { return len(a) }
func (a Int64) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a Int64) Less(i, j int) bool { return a[i] < a[j] }

func solve() {
	n := readInt()
	a := readInt64Array(n)
	sort.Sort(Int64(a))
	for i := n - 1; i >= 0; i-- {
		v := a[i]
		b := int64(math.Sqrt(float64(v)))
		if b*b != v {
			fmt.Println(v)
			return
		}
	}
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

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

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	scanner.Scan()
	a := strings.Split(scanner.Text(), " ")
	max := int(-10e6)
	for _, val := range a {
		i := 0
		intVal, _ := strconv.Atoi(val)
		for ; i*i < intVal; i++ {
		}
		if i*i != intVal {
			if max <= intVal {
				max = intVal
			}
		}
	}
	fmt.Println(max)
}

package main

import (
	"fmt"
	"math"
	//"time"
)

var (
	co int
	ar [1000]int
)

func main() {
	fmt.Scanf("%d \n", &co)
	for i := 0; i < co; i++ {
		fmt.Scanf("%d", &ar[i])
	}
	var res int = -10000000
	for i := 0; i < co; i++ {
		if int(math.Pow(float64(int(math.Sqrt(float64(ar[i])))), 2)) != ar[i] {
			if ar[i] > res {
				res = ar[i]
			}
		}
	}
	fmt.Println(res)
}

package main

import "fmt"
import "math"

func main() {
    var n int
    fmt.Scan(&n)

    var largest int = -9999999
    for i:=0; i < n; i++ {
        var tmp int
        fmt.Scan(&tmp)
        if tmp < 0 {
            if largest < tmp {
                largest = tmp
            }
            continue
        }
        sq := int(math.Floor(math.Sqrt(float64(tmp))))
        if sq * sq != tmp {
            if largest < tmp {
                largest = tmp
            }
        }
    }
    fmt.Print(largest)
}

package main

import (
    "fmt"
    "math"
)

func main() {
    var n int
    var a int
    var ans int = -1000001
    var x int
    fmt.Scan(&n)
    for i := 0; i < n;i ++ {
        fmt.Scan(&a);
        if a < 0 {
            if a > ans {
                ans = a
            }
        } else {
            x = int(math.Sqrt(float64(a)))
            if x * x != a {
                if a > ans {
                    ans = a
                }
            }
        }
    }
    fmt.Println(ans)
}

package main

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

var (
	SQ = make(map[int]bool)
)

func Init() {
	for i := 0; i * i <= 1000000; i++ {
		SQ[i * i] = true
	}
}

func Solve(io *FastIO) {
	N := io.NextInt()
	A := io.NextIntArray(N)

	best := -123456789
	for _, v := range A {
		if !SQ[v] {
			best = Max(best, v)
		}
	}
	io.Println(best)
}

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

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

func IsLineBreakChar(c byte) bool {
	switch c {
		case 0, '\n', '\r':
			return true
		default:
			return false
	}
}

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

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

func main() {
	sc := NewScanner()
	_ = sc.NextInt()
	A := sc.NextIntArray()

	mx := int(-1e7)
	for _, v := range A {

		if v < 0 {
			mx = max(mx, v)
		} else {
			ok := false
			for k := 0; k*k <= v; k++ {
				if k*k == v {
					ok = true
				}
			}
			if !ok {
				mx = max(mx, v)
			}
		}
	}
	fmt.Println(mx)

}

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

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

import (
	"fmt"
	"math"
)

func isSquare(a int) bool {
	if a < 0 {
		return false
	}
	var x = int(math.Sqrt(float64(a)))
	return x*x == a
}

func main() {
	var n int
	fmt.Scan(&n)
	var t int
	var m = math.MinInt32
	for i := 0; i < n; i++ {
		fmt.Scan(&t)
		if t > m && !isSquare(t) {
			m = t
		}
	}
	fmt.Println(m)
}

package main

import ("fmt";
		"math")

func check(a float64)bool{
	return math.Ceil(math.Sqrt(a)) * math.Ceil(math.Sqrt(a)) == a
}

func main(){
	var n, max int
	scan(&n)
	max = -1e8
	for i := 0; i < n; i++{
		var a int
		scan(&a)
		if max < a && !check(float64(a)){
			max = a
		}
	}
	sout(max)
}

func scan(in ... interface{}){
	for _,k := range in{
		fmt.Scan(k)
	}
}

func sout(obj ... interface{}){
	for _, k := range obj{
		fmt.Print(k, " ")
	}
	fmt.Println()
}

package main

import (
	"fmt"
)

func main() {
	n := 0
	fmt.Scan(&n)

	sq := make(map[int]bool)

	for i:=0; i < 1e4; i++ {
		sq[i*i] = true
	}

	mps := int(-1e7)
	for i := 0; i < n; i++ {
		var x int
		fmt.Scan(&x)

		if(!sq[x] && mps < x){
			mps = x
		}
	}
	fmt.Println(mps)
}

// cf914a project main.go
package main

import (
	"fmt"
	"math"
)

func main() {
	var n int
	fmt.Scan(&n)
	m := -10000000
	for i := 0; i < n; i++ {
		var t int
		fmt.Scan(&t)
		f := math.Sqrt(float64(t))
		if math.Ceil(f)-f != 0 {
			if t > m {
				m = t
			}
		}
	}
	if m == -10000000 {
		fmt.Println(-1)
	} else {
		fmt.Println(m)
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var n int
	fmt.Scanln(&n)
	result := -1000000
	for i := 0; i < n; i++ {
		var a int
		fmt.Scan(&a)
		if a > 0 && int(math.Sqrt(float64(a)))*int(math.Sqrt(float64(a))) == a {
			continue
		}
		if a > result {
			result = a
		}

	}
	fmt.Println(result)
}

package main

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

const (
	max = 1000000
	min = -max
)

var sqrs map[int]struct{}

func init() {
	sqrs = map[int]struct{}{}
	for i := 1; i*i <= max; i++ {
		sqrs[i*i] = struct{}{}
	}
}

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

	var n int
	fmt.Fscan(r, &n)
	ans := min - 1
	for i := 0; i < n; i++ {
		var v int
		fmt.Fscan(r, &v)
		if _, ok := sqrs[v]; ok {
			continue
		}
		if v > ans {
			ans = v
		}
	}
	fmt.Println(ans)
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	scanner.Scan()
	a := strings.Split(scanner.Text(), " ")
	max := int(-10e6)
	for _, val := range a {
		i := 1
		intVal, _ := strconv.Atoi(val)
		for ; i*i < intVal; i++ {
		}
		if i*i != intVal {
			if max <= intVal {
				max = intVal
			}
		}
	}
	fmt.Println(max)
}

package main

import (
	"fmt"
	"math"
	//"time"
)

var (
	co int
	ar [1000]int
)

func main() {
	fmt.Scanf("%d \n", &co)
	for i := 0; i < co; i++ {
		fmt.Scanf("%d \n", &ar[i])
	}
	var res int = -10000000
	for i := 0; i < co; i++ {
		if int(math.Pow(float64(int(math.Sqrt(float64(ar[i])))), 2)) != ar[i] {
			if ar[i] > res {
				res = ar[i]
			}
		}
	}
	fmt.Println(res)
}

package main

import (
	"fmt"
	"math"
	//"time"
)

var (
	co int
	ar [1000]int
)

func main() {
	fmt.Scanf("%d", &co)
	for i := 0; i < co; i++ {
		fmt.Scanf("%d", &ar[i])
	}
	var res int = -10000000
	for i := 0; i < co; i++ {
		if int(math.Pow(float64(int(math.Sqrt(float64(ar[i])))), 2)) != ar[i] {
			if ar[i] > res {
				res = ar[i]
			}
		}
	}
	fmt.Println(res)
}

package main

import (
    "fmt"
    "math"
)

func main() {
    var n int
    var a int
    var ans int = 0
    var x int
    fmt.Scan(&n)
    for i := 0; i < n;i ++ {
        fmt.Scan(&a);
        x = int(math.Sqrt(float64(a)))
        if x * x != a {
            if a > ans {
                ans = a
            }
        }
    }
    fmt.Println(ans)
}

package main

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

func main() {
	sc := NewScanner()
	_ = sc.NextInt()
	A := sc.NextIntArray()

	mx := 0
	for _, v := range A {
		a := math.Sqrt(float64(v))
		if v < 0 || a*a != float64(v) {
			mx = max(mx, v)
		}
	}
	fmt.Println(mx)

}

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

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

func main() {
	sc := NewScanner()
	_ = sc.NextInt()
	A := sc.NextIntArray()

	mx := 0
	for _, v := range A {

		if v < 0 {
			mx = max(mx, v)
		} else {
			ok := false
			for k := 0; k*k <= v; k++ {
				if k*k == v {
					ok = true
				}
			}
			if !ok {
				mx = max(mx, v)
			}
		}
	}
	fmt.Println(mx)

}

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

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

func main() {
	sc := NewScanner()
	_ = sc.NextInt()
	A := sc.NextIntArray()

	mx := 0
	for _, v := range A {
		a := math.Sqrt(float64(v))
		if a*a != float64(v) {
			mx = max(mx, v)
		}
	}
	fmt.Println(mx)

}

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

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

import "fmt"
import (
	"math"
)

func main() {
	var n int
	// var a int
	//
	fmt.Scanf("%d", &n)
	ans := -19999999
	for i := 0; i < n; i++ {
		var a int
		fmt.Scanf("%d", &a)
		if a < 0 {
			if ans < a {
				ans = a
			}
		} else {
			t := int(math.Floor(math.Sqrt(float64(a))))
			if t*t != a && ans < a {
				ans = a
			}
		}
	}
	fmt.Println(ans)
}

package main

import "fmt"
import (
	"math"
)

func main() {
	var n int
	// var a int
	//
	fmt.Scanf("%d", &n)

	ans := -19999999
	for i := 0; i < n; i++ {
		var a int
		fmt.Scanf("%d", &n)
		if a < 0 {
			if ans < a {
				ans = a
			}
		} else {
			t := int(math.Floor(math.Sqrt(float64(a))))
			if t*t != a && ans < a {
				ans = a
			}
		}
	}
	fmt.Println(ans)
}

package main

import "fmt"
import (
	"math"
)

func main() {
	var n int
	// var a int
	//
	fmt.Scanf("%d", &n)
	fmt.Println(n)
	ans := -19999999
	for i := 0; i < n; i++ {
		var a int
		fmt.Scanf("%d", &a)
		if a < 0 {
			if ans < a {
				ans = a
			}
		} else {
			t := int(math.Floor(math.Sqrt(float64(a))))
			if t*t != a && ans < a {
				ans = a
			}
		}
	}
	fmt.Println(ans)
}

package main

import "fmt"
import (
	"math"
)

func main() {
	var n int
	// var a int
	//
	fmt.Scanf("%d", &n)
	ans := -19999999
	fmt.Println(n)
	for i := 0; i < n; i++ {
		var a int
		fmt.Scanf("%d", &a)
		fmt.Println(a)
		if a < 0 {
			if ans < a {
				ans = a
			}
		} else {
			t := int(math.Floor(math.Sqrt(float64(a))))
			if t*t != a && ans < a {
				ans = a
			}
		}
	}
	fmt.Println(ans)
}

package main

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

func main() {
	var n int
	// var a int
	input := bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &n)

	ans := -19999999
	for i := 0; i < n; i++ {
		var a int
		fmt.Fscan(input, &a)
		if a < 0 {
			if ans < a {
				ans = a
			}
		} else {
			t := int(math.Floor(math.Sqrt(float64(a))))
			if t*t == a && ans < a {
				ans = a
			}
		}
	}
	fmt.Println(ans)
}

package main

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

func main() {
	var n int
	// var a int
	input := bufio.NewReader(os.Stdin)
	fmt.Fscanf(input, "%d", &n)

	ans := -19999999
	for i := 0; i < n; i++ {
		var a int
		fmt.Fscanf(input, "%d", &a)
		if a < 0 {
			if ans < a {
				ans = a
			}
		} else {
			t := int(math.Floor(math.Sqrt(float64(a))))
			if t*t != a && ans < a {
				ans = a
			}
		}
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
	"math"
)

func isSquare(a int) bool {
	if a < 0 {
		return false
	}
	var x = int(math.Sqrt(float64(a)))
	return x*x == a
}

func main() {
	var n int
	fmt.Scanf("%d", &n)
	var t int
	var m = math.MinInt32
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &t)
		if t > m && !isSquare(t) {
			m = t
		}
	}
	fmt.Println(m)
}

package main

import ("fmt";
		"math")

func check(a float64)bool{
	return math.Sqrt(a) * math.Sqrt(a) == a
}

func main(){
	var n, max int
	scan(&n)
	max = -1e8
	for i := 0; i < n; i++{
		var a int
		scan(&a)
		if max < a && !check(float64(a)){
			max = a
		}
	}
	sout(max)
}

func scan(in ... interface{}){
	for _,k := range in{
		fmt.Scan(k)
	}
}

func sout(obj ... interface{}){
	for _, k := range obj{
		fmt.Print(k, " ")
	}
	fmt.Println()
}

package main

import (
	"fmt"
	"math"
)

func isPerfectSquare(number int32) bool {
	if number < 0 {
		return false
	}
	var i int32 = 0
	for ; i*i < number; i++ {
		;
	}
	return i*i == number
}

func min(nums ...int32) int32{
	var minv int32 = math.MaxInt32

	for _, v := range nums {
		if v < minv {
			minv = v
		}
	}
	return minv
}

func max(nums ...int32) int32{
	var maxv int32 = math.MinInt32

	for _, v := range nums {
		if v > maxv {
			maxv = v
		}
	}
	return maxv
}

func main() {
	var num int32
	_, _ = fmt.Scanf("%d", &num)

	arr := make([]int32, num)

	var i int32 = 0
	for ; i < num; i++ {
		_, _ = fmt.Scanf("%d", &arr[i])
	}

	var maxNotPerfect int32 = math.MinInt32
	for i = 0; i < num; i++ {

		if !isPerfectSquare(arr[i]) {
			maxNotPerfect = max(maxNotPerfect, arr[i])
		}
	}
	fmt.Printf("%v\n", maxNotPerfect)
}

// cf914a project main.go
package main

import (
	"fmt"
	"math"
)

func main() {
	var n int
	fmt.Scan(&n)
	m := -1
	for i := 0; i < n; i++ {
		t := -2
		fmt.Scan(&t)
		f := math.Sqrt(float64(t))
		if math.Ceil(f)-f != 0 {
			if t > m {
				m = t
			}
		}
	}
	fmt.Println(m)
}

// cf914a project main.go
package main

import (
	"fmt"
	"math"
)

func main() {
	var n int
	fmt.Scan(&n)
	var m int
	for i := 0; i < n; i++ {
		var t int
		fmt.Scan(&t)
		f := math.Sqrt(float64(t))
		if math.Ceil(f)-f != 0 {
			if t > m {
				m = t
			}
		}
	}
	fmt.Println(m)
}

// cf914a project main.go
package main

import (
	"fmt"
	"math"
)

func main() {
	var n int
	fmt.Scan(&n)
	var m int
	for i := 0; i < n; i++ {
		var t int
		fmt.Scan(&t)
		f := math.Sqrt(float64(t))
		if math.Ceil(f)-f != 0 {
			if t > m || t < 0 {
				m = t
			}
		}
	}
	if m == 0 {
		fmt.Println(-1)
	} else {
		fmt.Println(m)
	}
}

// cf914a project main.go
package main

import (
	"fmt"
	"math"
)

func main() {
	var n int
	fmt.Scan(&n)
	var m int
	for i := 0; i < n; i++ {
		var t int
		fmt.Scan(&t)
		f := math.Sqrt(float64(t))
		if math.Ceil(f)-f != 0 {
			if t > m {
				m = t
			}
		}
	}
	if m == 0 {
		fmt.Println(-1)
	} else {
		fmt.Println(m)
	}
}


package main

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

func solve() {
	readInt()
	readInt()
	n := []rune(readString())
	m := []rune(readString())
	best := 10000
	bestIndex := 0
	for i := 0; i < len(m); i++ {
		cnt := 0
		if i+len(n) <= len(m) {
			for j := 0; j < len(n); j++ {
				if m[i+j] != n[j] {
					cnt++
				}
			}
			if cnt < best {
				best = cnt
				bestIndex = i
			}
		}
	}
	fmt.Println(best)
	cnt := 0
	for j := 0; j < len(n); j++ {
		if m[bestIndex+j] != n[j] {
			if cnt > 0 {
				fmt.Printf(" ")
			}
			fmt.Printf("%d", j+1)
			cnt++
		}
	}
	fmt.Println()
}

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

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

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

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

package main

import (
	"fmt"
)

var s, t string
var ls, lt int

func diffNum(p int) int {
	sum := 0
	for i := 0; i < ls; {
		if s[i] != t[p] {
			sum++
		}
		i++
		p++
	}

	return sum
}

func main() {

	var temp, min, pos int

	fmt.Scanf("%d %d\n%s\n%s", &ls, &lt, &s, &t)

	//fmt.Scanf("%s", &s)
	//fmt.Scanf("%s", &t)

	min = ls + 1

	for i := 0; i <= lt-ls; i++ {
		temp = diffNum(i)
		if temp < min {
			min = temp
			pos = i
		}
	}

	fmt.Println(min)

	for i := 0; min > 0 && i < ls; {
		if s[i] != t[pos] {
			fmt.Printf("%d ", i+1)
			min--
		}

		pos++
		i++
	}
}

package main

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

func main() {
	sc := NewScanner()
	_ = sc.NextLine()
	S := sc.NextLine()

	T := sc.NextLine()

	var ansNum []int = nil

	for i := 0; i <= len(T)-len(S); i++ {
		a := make([]int, 0, len(S))
		for j := 0; j < len(S); j++ {
			if T[i+j] != S[j] {
				a = append(a, j+1)
			}
		}
		if ansNum == nil || len(ansNum) > len(a) {
			ansNum = a
		}
	}
	fmt.Println(len(ansNum))

	PrintI(ansNum...)
}

func mapToString(arr []int) []string {
	ret := make([]string, len(arr))
	for i := 0; i < len(arr); i++ {
		ret[i] = strconv.Itoa(arr[i])
	}
	return ret
}

func PrintI(args ...int) {
	fmt.Println(strings.Join(mapToString(args), " "))
}

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

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

import (
    "fmt"
)

var (
    a,b int
    s,l string
)
func main(){
    fmt.Scanln(&a,&b)
    fmt.Scanln(&s)
    fmt.Scanln(&l)
    //fmt.Println(len(s))
    ansloc := make([]int, 0, len(s))
    for i:= 0 ; i < len(s) ; i++ {
        ansloc=append(ansloc,i+1)
    }
    for i := 0 ; i<len(l) ; i++ {
        if len(s) + i - 1 < len(l) {
            tmploc := make([]int, 0, len(s))
            for j := 0 ; j<len(s) ; j++ {
                if s[j] != l[i+j] {
                    tmploc=append(tmploc,j+1)
                }
            }
            if len(tmploc) < len(ansloc) { ansloc=tmploc }
        }
    }
    fmt.Println(len(ansloc))
    for _, loc := range ansloc {
        fmt.Printf("%d ",loc)
    }
}

package main

import (
	"fmt"
)

var s, t string

func diffNum(p int) int {
	sum := 0
	for i := 0; i < len(s); {
		if s[i] != t[p] {
			sum++
		}
		i++
		p++
	}

	return sum
}

func main() {

	var temp, min, pos int

	fmt.Scanf("%d %d\n%s\n%s", &min, &pos, &s, &t)

	//fmt.Scanf("%s", &s)
	//fmt.Scanf("%s", &t)

	min = len(s) + 1

	for i := 0; i <= len(t)-len(s); {
		temp = diffNum(i)
		if temp < min {
			min = temp
			pos = i
		}

		i++
	}

	fmt.Println(min)

	for i := 0; min > 0 && i < len(s); {
		if s[i] != t[pos] {
			fmt.Printf("%d ", i+1)
			min--
		}

		pos++
		i++
	}
}

package main

import (
	"fmt"
)

var s, t string

func diffNum(p int) int {
	sum := 0
	for i := 0; i < len(s); {
		if s[i] != t[p] {
			sum++
		}
		i++
		p++
	}

	return sum
}

func main() {

	var temp, min, pos int

	fmt.Scanf("%d %d", &min, &pos)

	fmt.Scanf("%s", &s)
	fmt.Scanf("%s", &t)

	min = len(s)

	for i := 0; i <= len(t)-len(s); i++ {
		temp = diffNum(i)
		if temp < min {
			min = temp
			pos = i
		}
	}

	fmt.Println(min, s, t)

	for i := 0; min > 0 && i < len(s); {
		if s[i] != t[pos] {
			fmt.Printf("%d ", i+1)
			min--
		}

		pos++
		i++
	}
}

package main

import (
	"fmt"
)

var s, t string

func diffNum(p int) int {
	sum := 0
	for i := 0; i < len(s); {
		if s[i] != t[p] {
			sum++
		}
		i++
		p++
	}

	return sum
}

func main() {

	var temp, min, pos int

	fmt.Scanf("%d %d", &min, &pos)
	fmt.Scanf("%s\n%s", &s, &t)

	min = len(s)

	for i := 0; i <= len(t)-len(s); i++ {
		temp = diffNum(i)
		if temp < min {
			min = temp
			pos = i
		}
	}

	fmt.Println(min)

	for i := 0; min > 0 && i < len(s); {
		if s[i] != t[pos] {
			fmt.Printf("%d ", i+1)
			min--
		}

		pos++
		i++
	}
}

package main

import (
	"fmt"
)

var s, t string

func diffNum(p int) int {
	sum := 0
	for i := 0; i < len(s); {
		if s[i] != t[p] {
			sum++
		}
		i++
		p++
	}

	return sum
}

func main() {

	var temp, min, pos int

	fmt.Scanf("%d %d", &min, &pos)

	fmt.Scanf("%s", &s)
	fmt.Scanf("%s", &t)

	min = len(s)

	for i := 0; i <= len(t)-len(s); i++ {
		temp = diffNum(i)
		if temp < min {
			min = temp
			pos = i
		}
	}

	fmt.Println(min)

	for i := 0; min > 0 && i < len(s); {
		if s[i] != t[pos] {
			fmt.Printf("%d ", i+1)
			min--
		}

		pos++
		i++
	}
}


package main

import (
	"fmt"
	"sort"
)

type exam struct {
	a int
	b int
}

type exams []exam

func (es exams) Len() int      { return len(es) }
func (es exams) Swap(i, j int) { es[i], es[j] = es[j], es[i] }
func (es exams) Less(i, j int) bool {
	if es[i].a == es[j].a {
		return es[i].b < es[j].b
	}
	return es[i].a < es[j].a
}

func main() {
	var n int
	fmt.Scan(&n)
	es := make(exams, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&es[i].a, &es[i].b)
	}
	sort.Sort(es)
	d := 0
	for i := 0; i < n; i++ {
		if es[i].b >= d {
			d = es[i].b
		} else {
			d = es[i].a
		}
	}
	fmt.Println(d)
}

package main

import (
	"fmt"
	"sort"
)

type Pair struct {
	x int
	y int
}

type PairList []Pair

func (p PairList) Len() int {
	return len(p)
}

func (p PairList) Less(i, j int) bool {
	return p[i].x < p[j].x || (p[i].x == p[j].x && p[i].y < p[j].y)
}

func (p PairList) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

func main() {
	var n int
	fmt.Scan(&n)
	pairs := make(PairList, n)

	for i := 0; i < n; i++ {
		fmt.Scan(&pairs[i].x, &pairs[i].y)
	}
	sort.Sort(pairs)

	minDay := pairs[0].y
	for _, v := range pairs {
		if v.y < minDay {
			minDay = v.x
		} else {
			minDay = v.y
		}
	}
	fmt.Println(minDay)
}

package main

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

type Pair struct {
	x int
	y int
}

type PairList []Pair

func (p PairList) Len() int {
	return len(p)
}

func (p PairList) Less(i, j int) bool {
	return p[i].x < p[j].x || (p[i].x == p[j].x && p[i].y < p[j].y)
}

func (p PairList) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

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

	var n int
	fmt.Scan(&n)
	pairs := make(PairList, n)

	for i := 0; i < n; i++ {
		for scanner.Scan() {
			if scanner.Text() != "" { break }
		}
		pairValues := strings.Split(scanner.Text(), " ")
		pairs[i].x, _ = strconv.Atoi(pairValues[0])
		pairs[i].y, _ = strconv.Atoi(pairValues[1])
	}
	sort.Sort(pairs)

	minDay := pairs[0].y
	for _, v := range pairs {
		if v.y < minDay {
			minDay = v.x
		} else {
			minDay = v.y
		}
	}
	fmt.Println(minDay)
}

package main

import (
	"bufio"
	"bytes"
	"errors"
	"io"
	"math/rand"
	"os"
	"strconv"
	"time"
	"sort"
	"unicode/utf8"
)

// ******************************** CODE STARTS ********************************

const (
)

var (
)

func solve() {
	n := readInt()
	array := make([]Tuple, n)
	for i := 0; i < n; i++ {
		array[i].a = readInt()
		array[i].b = readInt()
	}
	sort.Sort(TupleSlice(array))
//	logln(array)
	day := -1
	for i := 0; i < n; i++ {
		if array[i].b >= day {
			day = array[i].b
		} else {
			day = array[i].a
		}
	}
	printInt(day)
	printLine()
}

// ********************************* CODE ENDS *********************************

// IO.

var (
	scanner *Scanner
	writer  *bufio.Writer
)

func main() {
	input := os.Stdin
	output := os.Stdout
	//	input, _ := os.Open("")
	//	output, _ := os.Create("")

	defer input.Close()
	defer output.Close()

	scanner = NewScanner(input)
	scanner.Split(bufio.ScanWords)

	writer = bufio.NewWriter(output)
	defer writer.Flush()

	rand.Seed(int64(time.Now().Nanosecond()))
	solve()
}

// Read.

func readString() string {
	scanner.Scan()
	ans := scanner.Text()
	return ans
}

func readLine() string {
	scanner.Split(bufio.ScanLines)
	defer scanner.Split(bufio.ScanWords)
	return readString()
}

func readInt() int {
	ans, _ := strconv.ParseInt(readString(), 10, 0)
	return int(ans)
}

func readInt64() int64 {
	ans, _ := strconv.ParseInt(readString(), 10, 64)
	return ans
}

func readFloat64() float64 {
	ans, _ := strconv.ParseFloat(readString(), 64)
	return ans
}

// Write.

func printString(value string) {
	writer.WriteString(value)
}

func printLine() {
	printString("\n")
}

func printSpace() {
	printString(" ")
}

func printInt(value int) {
	printString(strconv.FormatInt(int64(value), 10))
}

func printInt64(value int64) {
	printString(strconv.FormatInt(value, 10))
}

func printFloat64(value float64) {
	printString(strconv.FormatFloat(value, 'f', 20, 64))
}

// Tuple

type Tuple struct {
	a, b int
}

func (p Tuple) Equals(key Key) bool {
	return p.a == key.(Tuple).a
}

func (p Tuple) Less(key Key) bool {
	return p.a < key.(Tuple).a
}

// Sorting

type TupleSlice []Tuple

func (p TupleSlice) Len() int {
	return len(p)
}

func (p TupleSlice) Less(j, i int) bool {
	if p[j].a == p[i].a {
		return p[j].b < p[i].b
	} else {
		return p[j].a < p[i].a
	}
}

func (p TupleSlice) Swap(j, i int) {
	p[j], p[i] = p[i], p[j]
}

// Min, max, abs.

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

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

func absInt(a int) int {
	if a > 0 {
		return a
	} else {
		return -a
	}
}

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

func minInt64(a, b int64) int64 {
	if a < b {
		return a
	} else {
		return b
	}
}

func absInt64(a int64) int64 {
	if a > 0 {
		return a
	} else {
		return -a
	}
}

func maxFloat64(a, b float64) float64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func minFloat64(a, b float64) float64 {
	if a < b {
		return a
	} else {
		return b
	}
}

func absFloat64(a float64) float64 {
	if a > 0 {
		return a
	} else {
		return -a
	}
}

// Set Key interface implementations.

type Int int

func (p Int) Equals(key Key) bool {
	return int(p) == int(key.(Int))
}

func (p Int) Less(key Key) bool {
	return int(p) < int(key.(Int))
}

// Set.

type Key interface {
	Equals(key Key) bool
	Less(key Key) bool
}

type node struct {
	key   Key
	left  *node
	right *node
}

func newNode(key Key) *node {
	return &node{key, nil, nil}
}

type SplayTree struct {
	root *node
	tmp  *node
	len  int
}

func NewSplayTree() *SplayTree {
	return &SplayTree{nil, newNode(nil), 0}
}

func rotateLeft(x, p *node) {
	p.right = x.left
	x.left = p
}

func rotateRight(x, p *node) {
	p.left = x.right
	x.right = p
}

func (p *SplayTree) splay(x *node, key Key) *node {
	if x == nil {
		return nil
	}

	left := p.tmp
	right := p.tmp

	for {
		if key.Less(x.key) {
			y := x.left
			if y == nil {
				break
			}
			if key.Less(y.key) { // zig-zig
				rotateRight(y, x)
				x = y
				if x.left == nil {
					break
				}
			}
			// link right
			right.left = x
			right = x
			// move left
			x = x.left
		} else if x.key.Less(key) {
			y := x.right
			if y == nil {
				break
			}
			if y.key.Less(key) { // zig-zig
				rotateLeft(y, x)
				x = y
				if x.right == nil {
					break
				}
			}
			// link left
			left.right = x
			left = x
			// move right
			x = x.right
		} else {
			break
		}
	}

	left.right = x.left
	right.left = x.right
	x.left = p.tmp.right
	x.right = p.tmp.left

	return x
}

// left <= key < right
func (p *SplayTree) split(key Key) (left, right *node) {
	p.root = p.splay(p.root, key)
	if p.root.key.Equals(key) || p.root.key.Less(key) {
		right := p.root.right
		p.root.right = nil
		return p.root, right
	} else {
		left := p.root.left
		p.root.left = nil
		return left, p.root
	}
}

// keys from left tree must be less then keys from right tree
func (p *SplayTree) join(left, right *node) *node {
	if left == nil {
		return right
	} else if right == nil {
		return left
	}
	left = p.splay(left, right.key)
	left.right = right
	return left
}

type Set interface {
	Len() int
	Insert(key Key) error
	Find(key Key) bool
	Remove(key Key) error
}

func NewSet() Set {
	return Set(NewSplayTree())
}

func (p *SplayTree) Len() int {
	return p.len
}

func (p *SplayTree) Insert(key Key) error {
	if p.root == nil {
		p.root = newNode(key)
		p.len++
	} else {
		p.root = p.splay(p.root, key)
		if p.root.key.Equals(key) {
			return errors.New("Such key already exists")
		} else {
			left, right := p.split(key)
			p.root = newNode(key)
			p.root.left = left
			p.root.right = right
			p.len++
		}
	}
	return nil
}

func (p *SplayTree) Find(key Key) bool {
	if p.root == nil {
		return false
	}
	p.root = p.splay(p.root, key)
	return p.root.key.Equals(key)
}

func (p *SplayTree) Remove(key Key) error {
	p.root = p.splay(p.root, key)
	if p.root == nil || !p.root.key.Equals(key) {
		return errors.New("Such key doesn't exist")
	}
	p.root = p.join(p.split(key))
	return nil
}

// Copied scan.go
// Scanner provides a convenient interface for reading data such as
// a file of newline-delimited lines of text. Successive calls to
// the Scan method will step through the 'tokens' of a file, skipping
// the bytes between the tokens. The specification of a token is
// defined by a split function of type SplitFunc; the default split
// function breaks the input into lines with line termination stripped. Split
// functions are defined in this package for scanning a file into
// lines, bytes, UTF-8-encoded runes, and space-delimited words. The
// client may instead provide a custom split function.
//
// Scanning stops unrecoverably at EOF, the first I/O error, or a token too
// large to fit in the buffer. When a scan stops, the reader may have
// advanced arbitrarily far past the last token. Programs that need more
// control over error handling or large tokens, or must run sequential scans
// on a reader, should use bufio.Reader instead.
//
type Scanner struct {
	r            io.Reader // The reader provided by the client.
	split        SplitFunc // The function to split the tokens.
	maxTokenSize int       // Maximum size of a token; modified by tests.
	token        []byte    // Last token returned by split.
	buf          []byte    // Buffer used as argument to split.
	start        int       // First non-processed byte in buf.
	end          int       // End of data in buf.
	err          error     // Sticky error.
}

// SplitFunc is the signature of the split function used to tokenize the
// input. The arguments are an initial substring of the remaining unprocessed
// data and a flag, atEOF, that reports whether the Reader has no more data
// to give. The return values are the number of bytes to advance the input
// and the next token to return to the user, plus an error, if any. If the
// data does not yet hold a complete token, for instance if it has no newline
// while scanning lines, SplitFunc can return (0, nil, nil) to signal the
// Scanner to read more data into the slice and try again with a longer slice
// starting at the same point in the input.
//
// If the returned error is non-nil, scanning stops and the error
// is returned to the client.
//
// The function is never called with an empty data slice unless atEOF
// is true. If atEOF is true, however, data may be non-empty and,
// as always, holds unprocessed text.
type SplitFunc func(data []byte, atEOF bool) (advance int, token []byte, err error)

// Errors returned by Scanner.
var (
	ErrTooLong         = errors.New("bufio.Scanner: token too long")
	ErrNegativeAdvance = errors.New("bufio.Scanner: SplitFunc returns negative advance count")
	ErrAdvanceTooFar   = errors.New("bufio.Scanner: SplitFunc returns advance count beyond input")
)

const (
	// Maximum size used to buffer a token. The actual maximum token size
	// may be smaller as the buffer may need to include, for instance, a newline.
	MaxScanTokenSize = 1 << 20
)

// NewScanner returns a new Scanner to read from r.
// The split function defaults to ScanLines.
func NewScanner(r io.Reader) *Scanner {
	return &Scanner{
		r:            r,
		split:        ScanLines,
		maxTokenSize: MaxScanTokenSize,
		buf:          make([]byte, 4096), // Plausible starting size; needn't be large.
	}
}

// Err returns the first non-EOF error that was encountered by the Scanner.
func (s *Scanner) Err() error {
	if s.err == io.EOF {
		return nil
	}
	return s.err
}

// Bytes returns the most recent token generated by a call to Scan.
// The underlying array may point to data that will be overwritten
// by a subsequent call to Scan. It does no allocation.
func (s *Scanner) Bytes() []byte {
	return s.token
}

// Text returns the most recent token generated by a call to Scan
// as a newly allocated string holding its bytes.
func (s *Scanner) Text() string {
	return string(s.token)
}

// Scan advances the Scanner to the next token, which will then be
// available through the Bytes or Text method. It returns false when the
// scan stops, either by reaching the end of the input or an error.
// After Scan returns false, the Err method will return any error that
// occurred during scanning, except that if it was io.EOF, Err
// will return nil.
func (s *Scanner) Scan() bool {
	// Loop until we have a token.
	for {
		// See if we can get a token with what we already have.
		if s.end > s.start {
			advance, token, err := s.split(s.buf[s.start:s.end], s.err != nil)
			if err != nil {
				s.setErr(err)
				return false
			}
			if !s.advance(advance) {
				return false
			}
			s.token = token
			if token != nil {
				return true
			}
		}
		// We cannot generate a token with what we are holding.
		// If we've already hit EOF or an I/O error, we are done.
		if s.err != nil {
			// Shut it down.
			s.start = 0
			s.end = 0
			return false
		}
		// Must read more data.
		// First, shift data to beginning of buffer if there's lots of empty space
		// or space is neded.
		if s.start > 0 && (s.end == len(s.buf) || s.start > len(s.buf)/2) {
			copy(s.buf, s.buf[s.start:s.end])
			s.end -= s.start
			s.start = 0
		}
		// Is the buffer full? If so, resize.
		if s.end == len(s.buf) {
			if len(s.buf) >= s.maxTokenSize {
				s.setErr(ErrTooLong)
				return false
			}
			newSize := len(s.buf) * 2
			if newSize > s.maxTokenSize {
				newSize = s.maxTokenSize
			}
			newBuf := make([]byte, newSize)
			copy(newBuf, s.buf[s.start:s.end])
			s.buf = newBuf
			s.end -= s.start
			s.start = 0
			continue
		}
		// Finally we can read some input. Make sure we don't get stuck with
		// a misbehaving Reader. Officially we don't need to do this, but let's
		// be extra careful: Scanner is for safe, simple jobs.
		for loop := 0; ; {
			n, err := s.r.Read(s.buf[s.end:len(s.buf)])
			s.end += n
			if err != nil {
				s.setErr(err)
				break
			}
			if n > 0 {
				break
			}
			loop++
			if loop > 100 {
				s.setErr(io.ErrNoProgress)
				break
			}
		}
	}
}

// advance consumes n bytes of the buffer. It reports whether the advance was legal.
func (s *Scanner) advance(n int) bool {
	if n < 0 {
		s.setErr(ErrNegativeAdvance)
		return false
	}
	if n > s.end-s.start {
		s.setErr(ErrAdvanceTooFar)
		return false
	}
	s.start += n
	return true
}

// setErr records the first error encountered.
func (s *Scanner) setErr(err error) {
	if s.err == nil || s.err == io.EOF {
		s.err = err
	}
}

// Split sets the split function for the Scanner. If called, it must be
// called before Scan. The default split function is ScanLines.
func (s *Scanner) Split(split SplitFunc) {
	s.split = split
}

// Split functions

// ScanBytes is a split function for a Scanner that returns each byte as a token.
func ScanBytes(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	return 1, data[0:1], nil
}

var errorRune = []byte(string(utf8.RuneError))

// ScanRunes is a split function for a Scanner that returns each
// UTF-8-encoded rune as a token. The sequence of runes returned is
// equivalent to that from a range loop over the input as a string, which
// means that erroneous UTF-8 encodings translate to U+FFFD = "\xef\xbf\xbd".
// Because of the Scan interface, this makes it impossible for the client to
// distinguish correctly encoded replacement runes from encoding errors.
func ScanRunes(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}

	// Fast path 1: ASCII.
	if data[0] < utf8.RuneSelf {
		return 1, data[0:1], nil
	}

	// Fast path 2: Correct UTF-8 decode without error.
	_, width := utf8.DecodeRune(data)
	if width > 1 {
		// It's a valid encoding. Width cannot be one for a correctly encoded
		// non-ASCII rune.
		return width, data[0:width], nil
	}

	// We know it's an error: we have width==1 and implicitly r==utf8.RuneError.
	// Is the error because there wasn't a full rune to be decoded?
	// FullRune distinguishes correctly between erroneous and incomplete encodings.
	if !atEOF && !utf8.FullRune(data) {
		// Incomplete; get more bytes.
		return 0, nil, nil
	}

	// We have a real UTF-8 encoding error. Return a properly encoded error rune
	// but advance only one byte. This matches the behavior of a range loop over
	// an incorrectly encoded string.
	return 1, errorRune, nil
}

// dropCR drops a terminal \r from the data.
func dropCR(data []byte) []byte {
	if len(data) > 0 && data[len(data)-1] == '\r' {
		return data[0 : len(data)-1]
	}
	return data
}

// ScanLines is a split function for a Scanner that returns each line of
// text, stripped of any trailing end-of-line marker. The returned line may
// be empty. The end-of-line marker is one optional carriage return followed
// by one mandatory newline. In regular expression notation, it is `\r?\n`.
// The last non-empty line of input will be returned even if it has no
// newline.
func ScanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	if i := bytes.IndexByte(data, '\n'); i >= 0 {
		// We have a full newline-terminated line.
		return i + 1, dropCR(data[0:i]), nil
	}
	// If we're at EOF, we have a final, non-terminated line. Return it.
	if atEOF {
		return len(data), dropCR(data), nil
	}
	// Request more data.
	return 0, nil, nil
}

// isSpace reports whether the character is a Unicode white space character.
// We avoid dependency on the unicode package, but check validity of the implementation
// in the tests.
func isSpace(r rune) bool {
	if r <= '\u00FF' {
		// Obvious ASCII ones: \t through \r plus space. Plus two Latin-1 oddballs.
		switch r {
		case ' ', '\t', '\n', '\v', '\f', '\r':
			return true
		case '\u0085', '\u00A0':
			return true
		}
		return false
	}
	// High-valued ones.
	if '\u2000' <= r && r <= '\u200a' {
		return true
	}
	switch r {
	case '\u1680', '\u180e', '\u2028', '\u2029', '\u202f', '\u205f', '\u3000':
		return true
	}
	return false
}

// ScanWords is a split function for a Scanner that returns each
// space-separated word of text, with surrounding spaces deleted. It will
// never return an empty string. The definition of space is set by
// unicode.IsSpace.
func ScanWords(data []byte, atEOF bool) (advance int, token []byte, err error) {
	// Skip leading spaces.
	start := 0
	for width := 0; start < len(data); start += width {
		var r rune
		r, width = utf8.DecodeRune(data[start:])
		if !isSpace(r) {
			break
		}
	}
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	// Scan until space, marking end of word.
	for width, i := 0, start; i < len(data); i += width {
		var r rune
		r, width = utf8.DecodeRune(data[i:])
		if isSpace(r) {
			return i + width, data[start:i], nil
		}
	}
	// If we're at EOF, we have a final, non-empty, non-terminated word. Return it.
	if atEOF && len(data) > start {
		return len(data), data[start:], nil
	}
	// Request more data.
	return 0, nil, nil
}

package main

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

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

type Byb []ExamDay

func (a Byb) Len() int 				{ return len(a) }
func (a Byb) Swap(i, j int) 		{ a[i], a[j] = a[j], a[i] }
func (a Byb) Less(i, j int) bool {
	if a[i].a == a[j].a {
		return a[i].b < a[j].b
	}

	return a[i].a < a[j].a
}

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

	n := nextInt()
	a := make([]ExamDay, n)

	for i := 0; i < n; i++ {
		a[i].a = nextInt()
		a[i].b = nextInt()
	}

	sort.Sort(Byb(a))
	day := 0
	// println(a)
	for i := 0; i < n; i ++ {
		if day <= a[i].b {
			day = a[i].b
		} else {
			day = a[i].a
		}
		// println(day)
	}

	println(day)
}

type ExamDay struct {
	a, b int
}

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

func nextInt() int {
	n, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return n
}

func nextFloat() float64 {
	n, err := strconv.ParseFloat(next(), 64)
	if err != nil {
		panic(err)
	}
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

package main

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

type branch struct {
    mx, mn int
}
//sort
type navalny []branch
func (a navalny) Len() int           { return len(a) }
func (a navalny) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a navalny) Less(i, j int) bool { return a[i].mx < a[j].mx || (a[i].mx == a[j].mx && a[i].mn < a[j].mn)}
//---

func main() {
    //io
    scanner := bufio.NewReader(os.Stdin)
    writer := bufio.NewWriter(os.Stdout)
    //---
    var n, cur int
    fmt.Fscan(scanner, &n)
    a := make([]branch, n)

    for i := 0; i < n; i++ {
        fmt.Fscan(scanner, &a[i].mx, &a[i].mn)
    }
    sort.Sort(navalny(a))

    for i := 0; i < n; i++ {
        if cur <= a[i].mn {
            cur = a[i].mn
        } else {
            cur = a[i].mx
        }
    }
    //fmt.Println(cur)
    //fmt.Println(a)
    writer.WriteString(strconv.Itoa(cur))
    writer.Flush()
}

package main

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

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

type pair struct {
	first  int
	second int
}

type pairs []pair

var n int

func main() {
	defer out.Flush()
	in.Split(bufio.ScanWords)

	read(&n)
	a := make([]pair, n)

	for i := 0; i < n; i++ {
		read(&a[i].first, &a[i].second)
	}

	sort.Sort(pairs(a))

	/*for i := 0; i < n; i++ {
		writeln(a[i].first, a[i].second)
	}*/

	ans := -1

	for i := 0; i < n; i++ {
		if ans <= a[i].second {
			ans = a[i].second
		} else {
			ans = a[i].first
		}
	}

	writeln(ans)
}

func (a pairs) Len() int {
	return len(a)
}

func (a pairs) Less(i, j int) bool {
	if a[i].first < a[j].first || (a[i].first == a[j].first && a[i].second < a[j].second) {
		return true
	} else {
		return false
	}
}

func (a pairs) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

func writeln(num ...int) {

	for _, value := range num {
		out.WriteString(strconv.Itoa(value))
		out.WriteString(" ")
	}
	out.WriteString("\n")
}

func read(num ...*int) {

	for _, value := range num {
		in.Scan()
		*value, _ = strconv.Atoi(in.Text())
	}
}

package main

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

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

type Byb []ExamDay

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

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

	n := nextInt()
	a := make([]ExamDay, n)

	for i := 0; i < n; i++ {
		a[i].a = nextInt()
		a[i].b = nextInt()
	}

	sort.Sort(Byb(a))
	day := 0
	// println(a)
	for i := 0; i < n; i ++ {
		if day <= a[i].b {
			day = a[i].b
		} else {
			day = a[i].a
		}
		// println(day)
	}

	println(day)
}

type ExamDay struct {
	a, b int
}

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

func nextInt() int {
	n, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return n
}

func nextFloat() float64 {
	n, err := strconv.ParseFloat(next(), 64)
	if err != nil {
		panic(err)
	}
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}


package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"
)

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	solve := func(Case int) {
		var n int
		Fscan(in, &n)

		ans := int64(0)

		type pair struct{ l, r int64 }
		c := make([]int64, n)
		for i := range c {
			Fscan(in, &c[i])
		}
		a := make([]pair, n)
		for i := range a {
			Fscan(in, &a[i].l)
		}
		for i := range a {
			Fscan(in, &a[i].r)
			if a[i].l > a[i].r {
				a[i].l, a[i].r = a[i].r, a[i].l
			}
		}

		s := c[n-1]
		for i := n - 2; i >= 0; i-- {
			p := a[i+1]
			if p.l == p.r {
				ans = max(ans, s+1)
				s = c[i]
			} else {
				ans = max(ans, s+p.r-p.l+1)
				s = max(c[i], s+c[i]-(p.r-p.l-1))
			}
		}

		Fprintln(out, ans)
	}

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

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}

func main() { run(os.Stdin, os.Stdout) }
func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

package main

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

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1e6), 1e6)
}

func printf(f string, a ...interface{}) {
	fmt.Fprintf(writer, f, a...)
}

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	scanner.Scan()
	return scanner.Text()
}

func sbytes() []byte {
	scanner.Scan()
	return scanner.Bytes()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		c := make([]int64, n)
		for i := range c {
			c[i] = sint64()
		}
		a := make([]int64, n)
		for i := range a {
			a[i] = sint64()
		}
		b := make([]int64, n)
		for i := range b {
			b[i] = sint64()
		}
		printf("%d\n", sol(n, c, a, b))
	}
}

func sol(n int64, c, a, b []int64) int64 {
	dp := make([]int64, n)
	for i := int64(1); i < n; i++ {
		top := a[i]
		btm := b[i]
		if btm < top { // enforce btm > top
			top, btm = btm, top
		}
		curr := c[i] + (btm - top + 1)
		if top != btm {
			curr = max(curr, c[i]+dp[i-1]-(btm-top-1))
		}
		dp[i] = curr
	}
	return max(dp...)
}

func max(a ...int64) int64 {
	ret := int64(0)
	for _, i := range a {
		if i > ret {
			ret = i
		}
	}
	return ret
}

package main

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

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

const maxN = 100005

var c [maxN]int64
var a [maxN]int64
var b [maxN]int64
var f [maxN]int64

func main() {
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		t--
		var n int
		fmt.Fscan(in, &n)
		for i := 1; i <= n; i++ {
			fmt.Fscan(in, &c[i])
		}
		for i := 1; i <= n; i++ {
			fmt.Fscan(in, &a[i])
		}
		for i := 1; i <= n; i++ {
			fmt.Fscan(in, &b[i])
		}

		var ans int64
		f[1] = -1e9
		for i := 2; i <= n; i++ {
			if a[i] > b[i] {
				a[i], b[i] = b[i], a[i]
			}
			if a[i] == b[i] {
				f[i] = 2
			} else {
				f1 := f[i-1] + c[i-1] - 1 - (b[i] - a[i]) + 2
				f2 := b[i] - a[i] + 2
				if f1 > f2 {
					f[i] = f1
				} else {
					f[i] = f2
				}
			}
			if f[i]+c[i]-1 > ans {
				ans = f[i] + c[i] - 1
			}
		}
		// for i := 1; i <= n; i++ {
		//     fmt.Printf("%d ", f[i])
		// }
		// fmt.Println()
		fmt.Fprintln(out, ans)
	}
	out.Flush()
}

package main

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

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

	tc := readNum(reader)

	var buf bytes.Buffer

	for tc > 0 {
		tc--
		n := readNum(reader)
		C := readNNums(reader, n)
		A := readNNums(reader, n)
		B := readNNums(reader, n)
		res := solve(n, C, A, B)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}

	fmt.Print(buf.String())
}

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] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

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

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

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

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

	return i
}

func solve(n int, C []int, A []int, B []int) int64 {
	var best int64
	open := int64(abs(B[1]-A[1])) + 2
	for i := 1; i < n; i++ {
		best = max(best, int64(C[i])-1+open)
		if i < n-1 {
			a, b := A[i+1], B[i+1]
			if a > b {
				a, b = b, a
			}
			if a == b {
				// have to close
				open = 0
			} else {
				open += int64(a - 1)
				open += int64(C[i] - b)
			}
			open = max(open, int64(b-a))
			open += 2
		}
	}
	return best
}

func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

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

package main

import (
	"bufio"
	"fmt"
	"os"
)
func max(x, y int64) int64{
	if x>y{
		return x
	}
	return y
}
func abs(x int)int{
	if x<0{
		return -x
	}
	return x
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var t int
	fmt.Fscan(in,&t)
	for u:=1;u<=t;u++{
		var n int
		fmt.Fscan(in,&n)
		c := make([]int,n+5)
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&c[i])
		}
		a := make([]int,n+5)
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&a[i])
		}
		b := make([]int,n+5)
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&b[i])
		}
		var ans,cur int64
		ans = 0
		cur = 0
		for i:=2;i<=n;i++{
			anow := a[i]
			bnow := b[i]
			if anow>bnow{
				temp := anow
				anow=bnow
				bnow=temp
			}
			if i==2{
				cur = int64(bnow-anow)+2+int64(c[i]-1)
				ans = max(ans,cur)
				continue
			}
			if bnow==anow{
				cur = 2+int64(c[i]-1)
			}else{
				t := cur
				t -= int64(abs(a[i]-b[i]))
				t += 2+int64(c[i]-1)
				cur = max(int64(abs(a[i]-b[i]))+2+int64(c[i]-1),t)
			}
			ans = max(ans,cur)
		}
		fmt.Fprintln(out,ans)
	}
}

package main

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

func solve(n int64, c, a, b []int64) {
	var (
		ans      int64
		opt      int64
		inD      = make([]int64, n)
		outD     = make([]int64, n)
		prevOutD int64
	)

	inD[n-1] = -1
	outD[n-1] = -1
	for i := int64(0); i < n-1; i++ {
		mx, mn := IntMax(a[i+1], b[i+1]), IntMin(a[i+1], b[i+1])
		inD[i] = mx - mn
		outD[i] = (mn - 1) + (c[i] - mx)
	}

	// ans[i] = c[i] - 1 + 2i + psOutterDist[i-1] + max[j] --> -2j + innerDist[j] - psOutterDist[j]
	prevOutD = outD[0]
	opt = -prevOutD + inD[0]
	for i := int64(1); i < n; i++ {
		if a[i] == b[i] {
			ans = IntMax(ans, c[i]+1)
			prevOutD = outD[i]
			opt = IntMax(-2*(i-1), -prevOutD-2*i+inD[i])
			continue
		}

		res := (c[i] - 1) + prevOutD + 2*i + opt
		ans = IntMax(ans, res)

		prevOutD += outD[i]
		opt = IntMax(opt, -prevOutD-2*i+inD[i])
	}

	printf("%v\n", ans)
}

func main() {
	defer writer.Flush()
	//RedirectInputToFile(reader, "input.txt")
	t := 1
	scan(&t)

	for T := 0; T < t; T++ {
		var (
			n int64
		)
		scan(&n)

		var (
			c = make([]int64, n)
			a = make([]int64, n)
			b = make([]int64, n)
		)
		for i := range c {
			scan(&c[i])
		}
		for i := range a {
			scan(&a[i])
		}
		for i := range b {
			scan(&b[i])
		}

		solve(n, c, a, b)
	}
}

// IntMax returns maximum of received int numbers
func IntMax(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand < num {
			cand = num
		}
	}
	return cand
}

func IntMin(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand > num {
			cand = num
		}
	}
	return cand
}

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func scan(a ...interface{})             { fmt.Fscan(reader, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

package main

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

func solve(n int64, c, a, b []int64) {
	var (
		ans      int64
		opt      int64
		inD      = make([]int64, n)
		outD     = make([]int64, n)
		prevOutD int64
	)

	inD[n-1] = -1
	outD[n-1] = -1
	for i := int64(0); i < n-1; i++ {
		mx, mn := IntMax(a[i+1], b[i+1]), IntMin(a[i+1], b[i+1])
		inD[i] = mx - mn
		outD[i] = (mn - 1) + (c[i] - mx)
	}

	// ans[i] = c[i] - 1 + 2i + psOutterDist[i-1] + max[j] --> -2j + innerDist[j] - psOutterDist[j]
	prevOutD = outD[0]
	opt = -prevOutD + inD[0]
	for i := int64(1); i < n; i++ {
		if a[i] == b[i] {
			ans = IntMax(ans, c[i]+1)
			prevOutD = outD[i]
			opt = IntMax(-2*(i-1), -prevOutD-2*i+inD[i])
			continue
		}

		res := (c[i] - 1) + prevOutD + 2*i + opt
		ans = IntMax(ans, res)

		prevOutD += outD[i]
		opt = IntMax(opt, -prevOutD-2*i+inD[i])
	}

	printf("%v\n", ans)
}

func main() {
	defer writer.Flush()
	//RedirectInputToFile(reader, "input.txt")
	t := 1
	scan(&t)

	for T := 0; T < t; T++ {
		var (
			n int64
		)
		scan(&n)

		var (
			c = make([]int64, n)
			a = make([]int64, n)
			b = make([]int64, n)
		)
		for i := range c {
			scan(&c[i])
		}
		for i := range a {
			scan(&a[i])
		}
		for i := range b {
			scan(&b[i])
		}

		solve(n, c, a, b)
	}
}

// ------------------------------------------------------------------------------------------------

// UniqueInts takes a sorted slice of ints, transforms it to contain only unique numbers, and returns this slice
// E.g. v = []{3, 3, 7, 8, 8, 8, 9} -> v = []{3, 7, 8, 9}
func UniqueInts(v []int) []int {
	prev := int(0)
	if len(v) > 0 && v[0] == prev {
		prev = -1
	}

	i := 0
	for j := range v {
		if v[j] != prev {
			prev = v[j]
			v[i], v[j] = v[j], v[i]
			i++
		}
	}

	return v[:i]
}

// IntMax returns maximum of received int numbers
func IntMax(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand < num {
			cand = num
		}
	}
	return cand
}

// IntMin returns minimum of received int numbers
func IntMin(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand > num {
			cand = num
		}
	}
	return cand
}

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func scan(a ...interface{})             { fmt.Fscan(reader, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func RedirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

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

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
}

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

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

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n)
		c := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &c[i])
		}
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		b := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &b[i])
		}

		var oldLen, newLen int64
		curLen := int64(-10000000000)
		maxLen := curLen + int64(c[1]-1)
		for i := 1; i < n; i++ {
			if a[i] == b[i] {
				maxLen = max64(curLen+int64(c[i-1]-1), maxLen)
				curLen = 2
			} else {
				oldLen = curLen + int64(c[i-1]-max(a[i], b[i])) + int64(min(a[i], b[i])-1) + 2
				newLen = int64(max(a[i], b[i])-min(a[i], b[i])) + 2
				curLen = max64(oldLen, newLen)
			}
			maxLen = max64(maxLen, curLen+int64(c[i]-1))
			//fmt.Println(curLen, maxLen, i)
		}
		fmt.Println(maxLen)
	}
}

package main

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

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

	tc := readNum(reader)

	var buf bytes.Buffer

	for tc > 0 {
		tc--
		n := readNum(reader)
		C := readNNums(reader, n)
		A := readNNums(reader, n)
		B := readNNums(reader, n)
		res := solve(n, C, A, B)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}

	fmt.Print(buf.String())
}

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] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

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

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

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

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

	return i
}

func solve(n int, C []int, A []int, B []int) int64 {
	var best int64 = int64(C[0])
	open := int64(abs(B[1]-A[1])) + 2
	for i := 1; i < n; i++ {
		best = max(best, int64(C[i])-1+open)
		if i < n-1 {
			a, b := A[i+1], B[i+1]
			if a > b {
				a, b = b, a
			}
			if a == b {
				// have to close
				open = 0
			} else {
				open += int64(a - 1)
				open += int64(C[i] - b)
			}
			open += 2
		}
	}
	return best
}

func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

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

package main

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

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

	tc := readNum(reader)

	var buf bytes.Buffer

	for tc > 0 {
		tc--
		n := readNum(reader)
		C := readNNums(reader, n)
		A := readNNums(reader, n)
		B := readNNums(reader, n)
		res := solve(n, C, A, B)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}

	fmt.Print(buf.String())
}

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] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

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

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

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

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

	return i
}

func solve(n int, C []int, A []int, B []int) int64 {
	var best int64
	open := int64(abs(B[1]-A[1])) + 2
	for i := 1; i < n; i++ {
		best = max(best, int64(C[i])-1+open)
		if i < n-1 {
			a, b := A[i+1], B[i+1]
			if a > b {
				a, b = b, a
			}
			if a == b {
				// have to close
				open = 0
			} else {
				open += int64(a - 1)
				open += int64(C[i] - b)
			}
			open += 2
		}
	}
	return best
}

func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

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

package main

import (
	"bufio"
	"fmt"
	"os"
)
func max(x, y int) int{
	if x>y{
		return x
	}
	return y
}
func abs(x int)int{
	if x<0{
		return -x
	}
	return x
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var t int
	fmt.Fscan(in,&t)
	for u:=1;u<=t;u++{
		var n int
		fmt.Fscan(in,&n)
		c := make([]int,n+5)
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&c[i])
		}
		a := make([]int,n+5)
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&a[i])
		}
		b := make([]int,n+5)
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&b[i])
		}
		ans := 0
		cur := 0
		for i:=2;i<=n;i++{
			anow := a[i]
			bnow := b[i]
			if anow>bnow{
				temp := anow
				anow=bnow
				bnow=temp
			}
			if i==2{
				cur = (bnow-anow)+2+(c[i]-1)
				ans = max(ans,cur)
				continue
			}
			if bnow==anow{
				cur = 2+(c[i]-1)
			}else{
				t := cur
				t -= abs(a[i]-b[i])
				t += 2+(c[i]-1)
				cur = max(abs(a[i]-b[i])+2+(c[i]-1),t)
			}
			ans = max(ans,cur)
		}
		fmt.Fprintln(out,ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)
func max(x, y int) int{
	if x>y{
		return x
	}
	return y
}
func abs(x int)int{
	if x<0{
		return -x
	}
	return x
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var t int
	fmt.Fscan(in,&t)
	for u:=1;u<=t;u++{
		var n int
		fmt.Fscan(in,&n)
		c := make([]int,n+5)
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&c[i])
		}
		a := make([]int,n+5)
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&a[i])
		}
		b := make([]int,n+5)
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&b[i])
		}
		ans := 0
		cur := 0
		for i:=2;i<=n;i++{
			anow := a[i]
			bnow := b[i]
			if anow>bnow{
				temp := anow
				anow=bnow
				bnow=temp
			}
			if i==2{
				cur = (bnow-anow)+2+(c[i]-1)

				ans = max(ans,cur)
				continue
			}
			if bnow==anow{
				cur = 2+(c[i]-1)
			}else{

				cur -= abs(a[i]-b[i])

				cur += 2+(c[i]-1)
			}
			ans = max(ans,cur)
		}
		fmt.Fprintln(out,ans)
	}
}

package main

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

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
}

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

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n)
		c := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &c[i])
		}
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		b := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &b[i])
		}

		curLen := int64(max(a[1], b[1])-min(a[1], b[1])) + 2
		maxLen := curLen + int64(c[1]-1)
		for i := 2; i < n; i++ {
			curLen += int64(c[i-1]-max(a[i], b[i])) + int64(min(a[i], b[i])-1) + 2
			if a[i] == b[i] {
				if curLen-2 > maxLen {
					maxLen = curLen - 2
				}
				curLen = 2
			}
			if curLen+int64(c[i]-1) > maxLen {
				maxLen = curLen + int64(c[i]-1)
			}
		}
		curLen += int64(c[n-1] - 1)
		if curLen > maxLen {
			maxLen = curLen
		}
		fmt.Println(maxLen)
	}
}

package main

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

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
}

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

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n)
		c := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &c[i])
		}
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		b := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &b[i])
		}

		curLen := int64(max(a[1], b[1])-min(a[1], b[1])) + 2
		maxLen := curLen + int64(c[1]-1)
		for i := 2; i < n; i++ {
			curLen += int64(c[i-1]-max(a[i], b[i])) + int64(min(a[i], b[i])-1) + 2
			if a[i] == b[i] {
				curLen -= 2
				if curLen > maxLen {
					maxLen = curLen
				}
				curLen = 2
			} else {
				if curLen-2+int64(c[i]-1) > maxLen {
					maxLen = curLen - 2 + int64(c[i]-1)
				}
			}
		}
		curLen += int64(c[n-1] - 1)
		if curLen > maxLen {
			maxLen = curLen
		}
		fmt.Println(maxLen)
	}
}

package main

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

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1e6), 1e6)
}

func printf(f string, a ...interface{}) {
	fmt.Fprintf(writer, f, a...)
}

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	scanner.Scan()
	return scanner.Text()
}

func sbytes() []byte {
	scanner.Scan()
	return scanner.Bytes()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		c := make([]int64, n)
		for i := range c {
			c[i] = sint64()
		}
		a := make([]int64, n)
		for i := range a {
			a[i] = sint64()
		}
		b := make([]int64, n)
		for i := range b {
			b[i] = sint64()
		}
		printf("%d\n", sol(n, c, a, b))
	}
}

func sol(n int64, c, a, b []int64) int64 {
	dp := make([]int64, n)
	for i := int64(1); i < n; i++ {
		top := a[i]
		btm := b[i]
		curr := c[i] + (btm - top + 1)
		if top != btm {
			curr = max(curr, c[i]+dp[i-1]-(btm-top-1))
		}
		dp[i] = curr
	}
	return max(dp...)
}

func max(a ...int64) int64 {
	ret := int64(0)
	for _, i := range a {
		if i > ret {
			ret = i
		}
	}
	return ret
}


package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

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

func main() {
	defer writer.Flush()
	////////////////////
	c := 0
	scan(&c)
	for c != 0 {
		solve()
		c--
	}
}

func solve() {
	n, k := 0, 0
	max := 0
	scan(&n, &k)
	m := make([]int, n)
	for i := 0; i < n; i++ {
		scan(&m[i])
		if max < m[i] {
			max = m[i]
		}
	}
	if k == 0 {
		print(n, "\n")
		return
	}
	sort.Ints(m)
	mex := -1
	for i := 0; i < n; i++ {
		if m[i] != i {
			mex = i
			break
		}
	}
	if mex == -1 {
		print(n+k, "\n")
		return
	}
	t := (max + mex + 1) / 2
	for _, v := range m {
		if v == t {
			print(n, "\n")
			return
		}
		if v > t {
			break
		}
	}
	print(n+1, "\n")
}

package main

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

func getNextString(scanner *bufio.Scanner) string {
	if !scanner.Scan() {
		panic("scan failed")
	}
	return scanner.Text()
}
func atoi(s string) int                             { x, _ := strconv.Atoi(s); return x }
func getNextInt(scanner *bufio.Scanner) int         { return atoi(getNextString(scanner)) }
func atoi64(s string) int64                         { x, _ := strconv.ParseInt(s, 10, 64); return x }
func getNextInt64(scanner *bufio.Scanner) int64     { return atoi64(getNextString(scanner)) }
func atof64(s string) float64                       { x, _ := strconv.ParseFloat(s, 64); return x }
func getNextFloat64(scanner *bufio.Scanner) float64 { return atof64(getNextString(scanner)) }
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	t := getNextInt(scanner)
	for t > 0 {
		t--
		func() {
			n := getNextInt(scanner)
			k := getNextInt(scanner)
			aa := make([]int64, n)
			exists := map[int64]bool{}
			for i := 0; i < n; i++ {
				aa[i] = getNextInt64(scanner)
				exists[aa[i]] = true
			}
			l := len(exists)
			if k == 0 {
				fmt.Fprintln(writer, l)
				return
			}
			sort.Slice(aa, func(i, j int) bool {
				return aa[i] < aa[j]
			})
			var ex, ax int64
			for i := 0; i < n+1 && exists[ex]; i++ {
				ex++
			}
			ax = aa[n-1]
			if ex > ax {
				fmt.Fprintln(writer, k+l)
				return
			}
			if exists[(ex+ax+1)/2] {
				fmt.Fprintln(writer, l)
				return
			}
			fmt.Fprintln(writer, len(exists)+1)
		}()
	}
}

package main

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

type i64 int64

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, args ...interface{}) { fmt.Fprintf(writer, f, args...) }
func scanf(f string, args ...interface{})  { fmt.Fscanf(reader, f, args...) }

func main() {
	var t int
	scanf("%d\n", &t)
	for t > 0 {
		var n, k, i i64
		scanf("%d %d\n", &n, &k)
		arr := make([]i64, n)
		b := i64(math.MinInt64)
		mp := make(map[i64]bool)
		for i = 0; i < n; i++ {
			if i == n-1 {
				scanf("%d\n", &arr[i])
			} else {
				scanf("%d ", &arr[i])
			}
			mp[arr[i]] = true
			b = max(b, arr[i])
		}
		if k == 0 {
			fmt.Println(n)
		} else {
			a := i64(0) //mex
			for i = 0; i <= n; i++ {
				if mp[i] == false {
					a = i
					break
				}
			}
			if a > b {
				fmt.Println(n + k)
			} else {
				ans := len(arr)
				val := (a + b + 1) / 2
				if mp[val] == false {
					ans++
				}
				fmt.Println(ans)
			}
		}
		t--
	}
}

package main

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

func main() {
	var tcs int
	in := bufio.NewReader(os.Stdin)
	_, _ = fmt.Fscan(in, &tcs)

	for tc := 0; tc < tcs; tc++ {
		var n, k int
		_, _ = fmt.Fscan(in, &n, &k)

		hole := false

		mp := make(map[int]bool)
		for i := 0; i < n; i++ {
			var temp int
			_, _ = fmt.Fscan(in, &temp)

			if temp >= n {
				hole = true
			}

			mp[temp] = true
		}

		if k == 0 {
			fmt.Println(len(mp))
			continue
		}

		if hole {
			high := max(mp)
			mid := mex(mp)

			mp[(high+mid+1)/2] = true

			fmt.Println(len(mp))
			continue
		} else {
			fmt.Println(n + k)
		}

	}
}

func max(mp map[int]bool) int {
	ans := -1

	for k := range mp {
		if ans < k {
			ans = k
		}
	}

	return ans
}

func mex(mp map[int]bool) int {
	for i := 0; i <= len(mp); i++ {
		if _, ok := mp[i]; !ok {
			return i
		}
	}

	return -1
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n, k := 0, 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n, &k)
		o, mex, max, l := map[int]int{}, -1, -1, 0
		for j := 0; j < n; j++ {
			p := 0
			fmt.Fscan(f, &p)
			if p > max {
				max = p
			}
			o[p]++
		}
		for {
			_, ok := o[l]
			if !ok {
				mex = l
				break
			}
			l++
		}
		if n == len(o) && max == n-1 || k == 0 {
			fmt.Fprintln(w, n+k)
		} else {
			_, ok := o[(mex+max+1)>>1]
			if !ok {
				fmt.Fprintln(w, len(o)+1)
			} else {
				fmt.Fprintln(w, len(o))
			}
		}
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

var T int
func main() {
    in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	for Fscan(in, &T); T > 0; T-- {
	    var n, k int
	    set := make(map[int]bool)
	    Fscan(in, &n)
	    Fscan(in, &k)
	    var s []int
	    for i := 0; i < n; i++ {
	        var x int
	        Fscan(in, &x)
	        s = append(s, x)
	        set[x] = true
	    }
	    if k == 0 {
	        Fprintln(out, n)
	        continue
	    }
	    sort.Slice(s, func(i, j int) bool {
		    return s[i] < s[j]
	    })
	    var mex, max = 0, 0
	    for _, value := range(s) {
	        if value > max {
	            max = value
	        }
	    }
	    if s[0] > 0 {
	        mex = 0
	    } else {
	        for i := 1; i < n; i++ {
	            if s[i] > s[i - 1] + 1 {
	                mex = s[i - 1] + 1
	                break
	            }
	        }
	        if mex == 0 {
	            mex = s[n - 1] + 1
	        }
	    }
	    var ans int = n
	    if (mex == max + 1)  {
	        ans += k
	    } else {
	        m := (mex + max + 1) / 2
	        if set[m] == false {
	            ans++
	        }
	    }
	    Fprintln(out, ans)
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		var n, k int
		fmt.Fscan(in, &n, &k)
		isInS := make(map[int]bool, n)
		set := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &set[i])
			isInS[set[i]] = true
		}
		sort.Sort(sort.IntSlice(set))
		mex := Mex(set)
		max := set[len(set)-1]
		var res int64
		if mex == max {
			res = int64(n) + int64(k)
		} else {
			avg := int(math.Ceil((float64(mex) + float64(max)) / 2))
			if !isInS[avg] && k > 0 {
				res = int64(n) + 1
			} else {
				res = int64(n)
			}
		}
		fmt.Printf("%d\n", res)
		t--
	}
}

func Mex(set []int) int {
	for i := 0; i < len(set); i++ {
		if i != set[i] {
			return i
		}
	}
	return set[len(set)-1]
}

package main

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

var in = bufio.NewReader(os.Stdin)

func main() {
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n, k, mex, max int
		m := make(map[int]bool)
		fmt.Fscan(in, &n, &k)
		for i := 0; i < n; i++ {
			var tp int
			fmt.Fscan(in, &tp)
			m[tp] = true
			if tp > max {
				max = tp
			}
		}
		if max == n-1 {
			fmt.Println(n + k)
			continue
		}
		for i := 0; i <= 100_000; i++ {
			if m[i] == false {
				mex = i
				break
			}
		}
		// log.Printf("mex: %#+v\n", mex)
		// log.Printf("max: %#+v\n", max)
		tp := (mex + max + 1) / 2
		// log.Printf("tp: %#+v\n", tp)
		if !m[tp] && k > 0 {
			fmt.Println(n + 1)
		} else {
			fmt.Println(n)
		}
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1496B(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T, n, k, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		mx := 0
		s := map[int]bool{}
		for i := 0; i < n; i++ {
			Fscan(in, &v)
			if v > mx {
				mx = v
			}
			s[v] = true
		}
		mex := 0
		for ; s[mex]; mex++ {
		}
		if mex > mx {
			Fprintln(out, len(s)+k)
		} else if k > 0 && !s[(mex+mx+1)/2] {
			Fprintln(out, len(s)+1)
		} else {
			Fprintln(out, len(s))
		}
	}
}

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

package main

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

// var m []int
// err = json.Unmarshal([]byte(r.FormValue("example")), &m)

// for i := 0; i < len(m); i++ {
// 	fmt.Println(m[i])
// }

func MIN(x, y int) int {
	if x > y {
		return y
	} else {
		return x
	}
}

func MAX(x, y int) int {
	if x > y {
		return x
	} else {
		return y
	}
}

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}

}

// sort.Slice(arr, func(i, j int) bool {
// 	return arr[i] > arr[j]
// })

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)
	for o := 0; o < t; o++ {

		var n, k int
		fmt.Fscan(input, &n)
		fmt.Fscan(input, &k)

		ARRAY := make([]int64, n)

		for i := 0; i < n; i++ {
			fmt.Fscan(input, &ARRAY[i])
		}

		sort.Slice(ARRAY, func(i, j int) bool {
			return ARRAY[i] < ARRAY[j]
		})

		if k == 0 {
			fmt.Println(n)
		} else {
			if ARRAY[n-1] == int64(n-1) {
				fmt.Println(n + k)
			} else {
				var indd int64
				for i := 0; i < n; i++ {
					if ARRAY[i] > int64(i) {
						indd = int64(i)
						break
					}
				}
				m := ARRAY[n-1]
				nnn := int64((m + indd + 1) / 2)
				g := 0
				for j := 0; j < n; j++ {
					if ARRAY[j] == nnn {
						g = 1
					}
					if ARRAY[j] > nnn {
						break
					}
				}
				if g == 1 {
					fmt.Println(n)
				} else {
					fmt.Println(n + 1)

				}

			}
		}

	}
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

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

func main() {
	defer writer.Flush()
	////////////////////
	c := 0
	scan(&c)
	for c != 0 {
		solve()
		c--
	}
}

func solve() {
	n, k := 0, 0
	max := 0
	scan(&n, &k)
	m := make([]int, n)
	for i := 0; i < n; i++ {
		scan(&m[i])
		if max < m[i] {
			max = m[i]
		}
	}
	if k == 0 {
		print(n, "\n")
		return
	}
	sort.Ints(m)
	mex := -1
	for i := 0; i < n; i++ {
		if m[i] != i {
			mex = i
			break
		}
	}
	if mex == -1 {
		print(n+k, "\n")
		return
	}
	t := (max + mex + 1) / 2
	if t == n {
		print(n+1, "\n")
	} else {
		print(n, "\n")
	}
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

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

func main() {
	defer writer.Flush()
	////////////////////
	c := 0
	scan(&c)
	for c != 0 {
		solve()
		c--
	}
}

func solve() {
	n, k := 0, 0
	max := 0
	scan(&n, &k)
	m := make(map[int]int)
	for i := 0; i < n; i++ {
		t := 0
		scan(&t)
		if max < t {
			max = t
		}
		m[t]++

	}
	mex := make([]int, 1e5)
	f := 0
	o := 0
	for i := 0; i < 1e5; i++ {
		if f == k {
			break
		}
		if m[i] == 0 {
			mex[f] = i
			f++
		}
	}
	for i := 0; i < k; i++ {
		t := (max + mex[o] + 1) / 2
		m[t]++

		if t != mex[o] && t <= max {
			break
		}
		if t == mex[o] {
			o++
		}
		if t > max {
			max = t
		}
	}
	print(len(m), "\n")
}

package main

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

func getNextString(scanner *bufio.Scanner) string {
	if !scanner.Scan() {
		panic("scan failed")
	}
	return scanner.Text()
}
func atoi(s string) int                             { x, _ := strconv.Atoi(s); return x }
func getNextInt(scanner *bufio.Scanner) int         { return atoi(getNextString(scanner)) }
func atoi64(s string) int64                         { x, _ := strconv.ParseInt(s, 10, 64); return x }
func getNextInt64(scanner *bufio.Scanner) int64     { return atoi64(getNextString(scanner)) }
func atof64(s string) float64                       { x, _ := strconv.ParseFloat(s, 64); return x }
func getNextFloat64(scanner *bufio.Scanner) float64 { return atof64(getNextString(scanner)) }
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	t := getNextInt(scanner)
	for t > 0 {
		t--
		func() {
			n := getNextInt(scanner)
			k := getNextInt(scanner)
			aa := make([]int64, n)
			exists := map[int64]bool{}
			for i := 0; i < n; i++ {
				aa[i] = getNextInt64(scanner)
				exists[aa[i]] = true
			}
			l := len(exists)
			if k == 0 {
				fmt.Fprintln(writer, l)
				return
			}
			sort.Slice(aa, func(i, j int) bool {
				return aa[i] < aa[j]
			})
			var ex, ax int64
			for i := 0; i < n+1 && exists[ex]; i++ {
				ex++
			}
			ax = aa[n-1]
			if ex > ax {
				fmt.Fprintln(writer, k+l)
				return
			}
			if exists[(ex+ax)/2] {
				fmt.Fprintln(writer, l)
				return
			}
			if ex+1 == ax && !exists[ex] {
				fmt.Fprintln(writer, l+k)
				return
			}
			fmt.Fprintln(writer, len(exists)+1)
		}()
	}
}

package main

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

func getNextString(scanner *bufio.Scanner) string {
	if !scanner.Scan() {
		panic("scan failed")
	}
	return scanner.Text()
}
func atoi(s string) int                             { x, _ := strconv.Atoi(s); return x }
func getNextInt(scanner *bufio.Scanner) int         { return atoi(getNextString(scanner)) }
func atoi64(s string) int64                         { x, _ := strconv.ParseInt(s, 10, 64); return x }
func getNextInt64(scanner *bufio.Scanner) int64     { return atoi64(getNextString(scanner)) }
func atof64(s string) float64                       { x, _ := strconv.ParseFloat(s, 64); return x }
func getNextFloat64(scanner *bufio.Scanner) float64 { return atof64(getNextString(scanner)) }
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	t := getNextInt(scanner)
	for t > 0 {
		t--
		func() {
			n := getNextInt(scanner)
			k := getNextInt(scanner)
			aa := make([]int64, n)
			exists := map[int64]bool{}
			for i := 0; i < n; i++ {
				aa[i] = getNextInt64(scanner)
				exists[aa[i]] = true
			}
			sort.Slice(aa, func(i, j int) bool {
				return aa[i] < aa[j]
			})
			var ex, ax int64
			for i := 0; i < n+1 && exists[ex]; i++ {
				ex++
			}
			ax = aa[n-1]
			if ex > ax {
				fmt.Fprintln(writer, k+len(exists))
				return
			}
			if exists[(ex+ax)/2] || k == 0 {
				fmt.Fprintln(writer, len(exists))
				return
			}
			fmt.Fprintln(writer, len(exists)+1)
		}()
	}
}

package main

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

func getNextString(scanner *bufio.Scanner) string {
	if !scanner.Scan() {
		panic("scan failed")
	}
	return scanner.Text()
}
func atoi(s string) int                             { x, _ := strconv.Atoi(s); return x }
func getNextInt(scanner *bufio.Scanner) int         { return atoi(getNextString(scanner)) }
func atoi64(s string) int64                         { x, _ := strconv.ParseInt(s, 10, 64); return x }
func getNextInt64(scanner *bufio.Scanner) int64     { return atoi64(getNextString(scanner)) }
func atof64(s string) float64                       { x, _ := strconv.ParseFloat(s, 64); return x }
func getNextFloat64(scanner *bufio.Scanner) float64 { return atof64(getNextString(scanner)) }
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	t := getNextInt(scanner)
	for t > 0 {
		t--
		func() {
			n := getNextInt(scanner)
			k := getNextInt(scanner)
			aa := make([]int64, n)
			exists := map[int64]bool{}
			for i := 0; i < n; i++ {
				aa[i] = getNextInt64(scanner)
				exists[aa[i]] = true
			}
			sort.Slice(aa, func(i, j int) bool {
				return aa[i] < aa[j]
			})
			var ex, ax int64
			for i := 0; i < n+1 && exists[ex]; i++ {
				ex++
			}
			ax = aa[n-1]
			if ex > ax {
				fmt.Fprintln(writer, k+n)
				return
			}
			if exists[(ex+ax)/2] || k == 0 {
				fmt.Fprintln(writer, n)
				return
			}
			fmt.Fprintln(writer, n+1)
		}()
	}
}

package main

import (
	"fmt"
	"math"
)

type i64 int64

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

func main() {
	var t int
	fmt.Scan(&t)
	for t > 0 {
		var n, k, i i64
		fmt.Scan(&n, &k)
		arr := make([]i64, n)
		b := i64(math.MinInt64)
		mp := make(map[i64]bool)
		for i = 0; i < n; i++ {
			fmt.Scan(&arr[i])
			mp[arr[i]] = true
			b = max(b, arr[i])
		}
		if k == 0 {
			fmt.Println(n)
		} else {
			a := i64(0) //mex
			for i = 0; i <= n; i++ {
				if mp[i] == false {
					a = i
					break
				}
			}
			if a > b {
				fmt.Println(n + k)
			} else {
				ans := len(arr)
				val := (a + b) / 2
				if mp[val] == false {
					ans++
				}
				fmt.Println(ans)
			}
		}
		t--
	}
}

package main

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

func main() {
	var tcs int
	in := bufio.NewReader(os.Stdin)
	_, _ = fmt.Fscan(in, &tcs)

	for tc := 0; tc < tcs; tc++ {
		var n, k int
		_, _ = fmt.Fscan(in, &n, &k)

		hole := false

		mp := make(map[int]bool)
		for i := 0; i < n; i++ {
			var temp int
			_, _ = fmt.Fscan(in, &temp)

			if temp >= n {
				hole = true
			}

			mp[temp] = true
		}

		if k == 0 {
			fmt.Println(len(mp))
			break
		}

		if hole {
			high := max(mp)
			mid := mex(mp)

			mp[(high+mid+1)/2] = true

			fmt.Println(len(mp))
			break
		} else {
			fmt.Println(n + k)
		}

	}
}

func max(mp map[int]bool) int {
	ans := -1

	for k := range mp {
		if ans < k {
			ans = k
		}
	}

	return ans
}

func mex(mp map[int]bool) int {
	for i := 0; i <= len(mp); i++ {
		if _, ok := mp[i]; !ok {
			return i
		}
	}

	return -1
}