package main

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

func main() {
	var (
		t int
	)

	reader := bufio.NewReader(os.Stdin)
	fmt.Scanf("%d\n", &t)

	for t > 0 {
		t--

		text, _ := reader.ReadString('\n')
		ninps := strings.Fields(text)
		l1, _ := strconv.Atoi(ninps[0])
                r1, _ := strconv.Atoi(ninps[1])
		l2, _ := strconv.Atoi(ninps[2])
		r2, _ := strconv.Atoi(ninps[3])

		if l1 >= l2 && l1 <= r2 {
			fmt.Println(l1)
		} else if l2 >= l1 && l2 <= r1 {
			fmt.Println(l2)
		} else {
			fmt.Println(l1+l2)
		}
	}
}

package main

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

func main_edu_128_A() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		var l1, l2, r1, r2 int
		Fscan(in, &l1, &r1, &l2, &r2)
		if l1 > l2 {
			l1, r1, l2, r2 = l2, r2, l1, r1
		}
		if l1 == l2 {
			Fprintln(out, l1)
			return
		}
		if r1 < l2 {
			Fprintln(out, l1+l2)
			return
		}
		Fprintln(out, l2)

	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() {
	main_edu_128_A()
}

package main

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

var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)
func slove() {
	var l1, r1, l2, r2 int
	fmt.Fscan(r, &l1, &r1)
	fmt.Fscan(r, &l2, &r2)
	ans := 100
	if l1 >= l2 && l1 <= r2 {
		ans = min(ans, l1)
	}
	if l2 >= l1 && l2 <= r1 {
		ans = min(ans, l2)
	}
	ans = min(l1 + l2, ans)
	fmt.Fprintln(w, ans)
}

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

func main() {
	defer w.Flush()
	var n int
	fmt.Fscan(r, &n)
	for i:=1;i<=n;i++ {
		slove()
	}
}

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var a,b,c,d int
		fmt.Fscan(in,&a,&b,&c,&d)

		if c >= a && c <= b {
			fmt.Fprintln(out,c)
		} else if a >=c && a <= d{
			fmt.Fprintln(out,a)
		} else if b >= c && b <= d {
			fmt.Fprintln(out,c)
		} else if d >= a && d <= b {
			fmt.Fprintln(out,a)
		} else {
			fmt.Fprintln(out,c+a)
		}
	}
}

package main

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

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var l1, r1, l2, r2 int
		fmt.Fscan(in, &l1, &r1, &l2, &r2)

		if l2 <= r1 && l2 >= l1 || l1 >= l2 && l1 <= r2 {
			fmt.Fprintln(out, max(l1, l2))
		} else {
			fmt.Fprintln(out, l1+l2)
		}
	}
}

package main

//we need to know where to start execution of the code
//print("hello")
import (
	"fmt"
)

// start execu from main zy c++
func main() {

	var t, l1, l2, r1, r2 int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&l1, &r1, &l2, &r2)
		if (l2 >= l1 && l2 <= r1) || (l1 >= l2 && l1 <= r2) {
			if l1 >= l2 {
				fmt.Printf("%v\n", l1)
			} else {
				fmt.Printf("%v\n", l2)
			}
		} else {
			fmt.Printf("%v\n", l1+l2)
		}
	}

}

package main

import ("fmt"
"math")

func main() {

	var t, l1, l2, r1, r2 int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&l1,&r1,&l2,&r2)
		if (l2>=l1 && l2<=r1) || (l1>=l2 && l1<=r2) {
			fmt.Println(math.Max(float64(l1),float64(l2)))
		}else {
			fmt.Printf("%v\n",l1+l2)
		}
	}

}

package main

import (
	"fmt"
)

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

func main() {
	var t int
	fmt.Scanf("%d\n",&t)
	for ; t > 0; t-- {
		var l1, l2, r1, r2 int
		fmt.Scanf("%d %d %d %d\n", &l1, &r1, &l2, &r2)
		if l1 >= l2 && l1 <= r2 {
			fmt.Println(l1)
		} else if l2 >= l1 && l2 <= r1 {
			fmt.Println(l2)
		} else {
			fmt.Println(min(l1, r1)+min(l2, r2))
		}
	}
}

package main

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

var (
	n int
)

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

	Fscan(in, &n)
	for ; n > 0; n-- {
		var l1, r1, l2, r2 int
		Fscan(in, &l1, &r1, &l2, &r2)
		if l2 >= l1 && l2 <= r1 {
			Fprintln(out, l2)
		} else if l1 >= l2 && l1 <= r2 {
			Fprintln(out, l1)
		} else {
			Fprintln(out, l1 + l2)
		}
	}

}

func min(a, b int) int { if a > b { return b }; return a }
func max(a, b int) int { if a < b { return b }; return a }
func abs(v int) int { if v > 0 { return v }; return -v }
func gcd(a, b int) int { if b > 0 { return gcd(b, a % b) }; return a }
func lcm(a, b int) int { return a * b / gcd(a, b) }

package main

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

func acc(op func(int64, int64) int64, c ...int64) int64 {
	res := c[0]
	for _, i := range c {
		res = op(res, i)
	}
	return res
}

func max(c ...int64) int64 {
	return acc(func(a int64, b int64) int64 {
		if a < b {
			return b
		}
		return a
	}, c...)
}

func min(c ...int64) int64 {
	return acc(func(a int64, b int64) int64 {
		if a > b {
			return b
		}
		return a
	}, c...)
}

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

func solve(inout io.ReadWriter) {
	scan := func(inp ...interface{}) {
		fmt.Fscan(inout, inp...)
	}
	printf := func(format string, outp ...interface{}) {
		fmt.Fprintf(inout, format, outp...)
	}

	var l1, r1, l2, r2 int
	scan(&l1, &r1, &l2, &r2)

	if r2 < l1 || r1 < l2 {
		printf("%d\n", l1+l2)
	} else {
		printf("%d\n", max(int64(l1), int64(l2)))
	}

}

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

	var T int = 1
	fmt.Fscan(inout, &T)
	for ; T > 0; T-- {
		solve(inout)
	}
}

package main

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

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

	var l1, r1, l2, r2 int
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &l1)
		fmt.Fscan(in, &r1)
		fmt.Fscan(in, &l2)
		fmt.Fscan(in, &r2)

		if l1 <= r2 && l2 <= r1 {
			fmt.Println(max(l1, l2))
		} else {
			fmt.Println(l1 + l2)
		}
	}
}

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

package main

import (
	"fmt"
	"math"
)
func main() {
	TC := new(int)
	fmt.Scan(TC)
	for (*TC) > 0 {
		var l1, l2, r1, r2 int
		fmt.Scan(&l1)
		fmt.Scan(&r1)
		fmt.Scan(&l2)
		fmt.Scan(&r2)
//		fmt.Println(l1, l2, r1, r2)
		if (l1 <= l2 && l2 <= r1) || (l1 >= l2 && l1 <= r2) {
			result := math.Max(float64(l1), float64(l2))
			fmt.Println(result)
			*TC -= 1
			continue
		}
		fmt.Println(l1 + l2)
		*TC -= 1
	}
}

package main

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

type CF1680A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1680A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1680A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1680A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1680A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1680A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1680A
Date: 20/05/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1680/A
Problem: CF1680A
**/
func (in *CF1680A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		l1, r1, l2, r2 := in.NextInt(), in.NextInt(), in.NextInt(), in.NextInt()

		if r1 < l2  || r2 < l1{
			fmt.Println(l1 + l2)

		} else {
			fmt.Println(int(math.Max(float64(l1), float64(l2))))
		}

	}
}

func NewCF1680A(r *bufio.Reader) *CF1680A {
	return &CF1680A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1680A(bufio.NewReader(os.Stdin)).Run()
}

package main

import "fmt"

func solve(lmin, rmin, lmax, rmax int) int {
	if rmin < lmax || rmax < lmin {
		return lmin + lmax
	}
	if lmin > lmax {
		return lmin
	}
	return lmax
}

func main() {
	var test int
	fmt.Scanln(&test)

	for test > 0 {
		test--
		var lmin, rmin, lmax, rmax int
		fmt.Scanln(&lmin, &rmin, &lmax, &rmax)
		fmt.Println(solve(lmin, rmin, lmax, rmax))
	}
}

package main

import (
	"fmt"
)

func max(a uint, b uint) uint {
	if a > b {
		return a
	} else {
		return b
	}
}
func main() {
	var t uint
	fmt.Scanln(&t)
	for t > 0 {
		var l1 uint
		var l2 uint
		var r1 uint
		var r2 uint
		fmt.Scanln(&l1, &r1, &l2, &r2)
		if l2 <= r1 && l1 <= r2 {
			fmt.Println(max(l1, l2))
		} else {
			fmt.Println(l1 + l2)
		}
		t = t - 1
	}

}

package main

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

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

func a1680(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T int
	var l int
	var r int
	var a int
	var b int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &l)
		Fscan(in, &r)
		Fscan(in, &a)
		Fscan(in, &b)

		ans := 0
		if l <= a {
			if a <= r {
				ans = a
			} else {
				ans = l + a
			}
		} else {
			if b < l {
				ans = a + l
			} else {
				ans = l
			}
		}

		Fprintln(out, ans)
	}
}

package main

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

type Int int64

const (
	INF = 3e5 + 10
	L0  = Int(0)
	L1  = Int(1)
)

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

	var T int
	Fscan(in, &T)

	for tt := 0; tt < T; tt++ {
		var l1, l2, r1, r2 int
		Fscan(in, &l1, &r1, &l2, &r2)

		if l1 > l2 {
			l1, r1, l2, r2 = l2, r2, l1, r1
		}

		if l2 <= r1 {
			Fprintln(out, l2)
		} else {
			Fprintln(out, l1+l2)
		}

	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var a,b,c,d int
		fmt.Fscan(in,&a,&b,&c,&d)

		if c >= a && c <= b {
			fmt.Fprintln(out,c)
		} else if d >= a && d <= b {
			fmt.Fprintln(out,a)
		} else if b >= c && b <= d {
			fmt.Fprintln(out,c)
		} else if a >=c && a <= d{
			fmt.Fprintln(out,a)
		} else {
			fmt.Fprintln(out,c+a)
		}
	}
}

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var a,b,c,d int
		fmt.Fscan(in,&a,&b,&c,&d)
		if c >= a && c <= b {
			fmt.Fprintln(out,c)
		} else if d >= a && d <= b {
			fmt.Fprintln(out,d)
		} else if b >= c && b <= d {
			fmt.Fprintln(out,b)
		} else if a >= c && a <= d{
			fmt.Fprintln(out,a)
		} else {
			fmt.Fprintln(out,c+a)
		}
	}
}

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var a,b,c,d int
		fmt.Fscan(in,&a,&b,&c,&d)
		if c >= a && c <= b {
			fmt.Fprintln(out,c)
		} else if d >= a && d <= b {
			fmt.Fprintln(out,d)
		} else if b >= c && b <= d {
			fmt.Fprintln(out,b)
		} else {
			fmt.Fprintln(out,c+a)
		}
	}
}

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var a,b,c,d int
		fmt.Fscan(in,&a,&b,&c,&d)
		if c >= a && c <= b {
			fmt.Fprintln(out,c)
		} else if d >= a && d <= b {
			fmt.Fprintln(out,d)
		} else {
			fmt.Fprintln(out,c+a)
		}
	}
}

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var a,b,c,d int
		fmt.Fscan(in,&a,&b,&c,&d)
		if c >= a && c <= b {
			fmt.Fprintln(out,c)
		} else {
			fmt.Fprintln(out,c+a)
		}
	}
}

package main

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

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var l1, r1, l2, r2 int
		fmt.Fscan(in, &l1, &r1, &l2, &r2)

		if l2 >= l1 && l2 <= r1 || r1 >= l2 && r1 <= r2 {
			fmt.Fprintln(out, min(l2, r1))
		} else {
			fmt.Fprintln(out, l1+l2)
		}
	}
}

package main

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var l1, r1, l2, r2 int
		fmt.Fscan(in, &l1, &r1, &l2, &r2)

		if l2 >= l1 && l2 <= r1 {
			fmt.Fprintln(out, l2)
		} else {
			fmt.Fprintln(out, l1+l2)
		}
	}
}

package main

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var l1, r1, l2, r2 int
		fmt.Fscan(in, &l1, &r1, &l2, &r2)

		if l2 >= l1 && l2 <= r1 {
			fmt.Fprintln(out, l2)
		} else {
			fmt.Fprintln(out, r1+l2)
		}
	}
}

package main

//we need to know where to start execution of the code
//print("hello")
import (
	"fmt"
)

// start execu from main zy c++
func main() {

	var t, l1, l2, r1, r2 int8
	fmt.Scan(&t)
	for i := 0; i < int(t); i++ {
		fmt.Scan(&l1, &r1, &l2, &r2)
		if (l2 >= l1 && l2 <= r1) || (l1 >= l2 && l1 <= r2) {
			if l1 >= l2 {
				fmt.Printf("%v\n", l1)
			} else {
				fmt.Printf("%v\n", l2)
			}
		} else {
			fmt.Printf("%v\n", l1+l2)
		}
	}

}

package main

import (
	"fmt"
)

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

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

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for ; t > 0; t-- {
		var l1, l2, r1, r2 int
		fmt.Scanf("%d %d %d %d\n", &l1, &r1, &l2, &r2)
		l := max(l1, r1)
		r := min(l2, r2)
		if l < r || l1 > r2 {
			fmt.Println(min(l1, r1)+r)
		} else {
			fmt.Println(r)
		}
	}
}

package main

import (
	"fmt"
)

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

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

func main() {
	var t int
	fmt.Scan(&t)
	for ; t > 0; t-- {
		var l1, l2, r1, r2 int
		fmt.Scanf("%d %d %d %d\n", &l1, &r1, &l2, &r2)
		l := max(l1, r1)
		r := min(l2, r2)
		if l < r || l1 > r2 {
			fmt.Println(min(l1, r1)+r)
		} else {
			fmt.Println(r)
		}
	}
}

package main

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

var (
	n int
)

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

	Fscan(in, &n)
	for ; n > 0; n-- {
		var l1, r1, l2, r2 int
		Fscan(in, &l1, &r1, &l2, &r2)
		if l2 >= l1 && l2 <= r1 {
			Fprintln(out, l2)
		} else if l1 < l2 && r2 < r1 {
			Fprintln(out, min(l1 + l2, r1))
		} else {
			Fprintln(out, l2 + l1)
		}
	}

}

func min(a, b int) int { if a > b { return b }; return a }
func max(a, b int) int { if a < b { return b }; return a }
func abs(v int) int { if v > 0 { return v }; return -v }
func gcd(a, b int) int { if b > 0 { return gcd(b, a % b) }; return a }
func lcm(a, b int) int { return a * b / gcd(a, b) }

package main

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

var (
	n int
)

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

	Fscan(in, &n)
	for ; n > 0; n-- {
		var l1, r1, l2, r2 int
		Fscan(in, &l1, &r1, &l2, &r2)
		// if l2 >= l1 && l2 <= r1 {
		// 	Fprintln(out, l2)
		// } else if l2 > r1 {
		// 	Fprintln(out, l2 + l1)
		// } else if l2 < l1 {
		// 	Fprintln(out, l2 + l1)
		// }
		if l2 >= l1 && l2 <= r1 {
			Fprintln(out, l2)
		} else {
			Fprintln(out, l2 + l1)
		}
	}

}

func min(a, b int) int { if a > b { return b }; return a }
func max(a, b int) int { if a < b { return b }; return a }
func abs(v int) int { if v > 0 { return v }; return -v }
func gcd(a, b int) int { if b > 0 { return gcd(b, a % b) }; return a }
func lcm(a, b int) int { return a * b / gcd(a, b) }

package main

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

func acc(op func(int64, int64) int64, c ...int64) int64 {
	res := c[0]
	for _, i := range c {
		res = op(res, i)
	}
	return res
}

func max(c ...int64) int64 {
	return acc(func(a int64, b int64) int64 {
		if a < b {
			return b
		}
		return a
	}, c...)
}

func min(c ...int64) int64 {
	return acc(func(a int64, b int64) int64 {
		if a > b {
			return b
		}
		return a
	}, c...)
}

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

func solve(inout io.ReadWriter) {
	scan := func(inp ...interface{}) {
		fmt.Fscan(inout, inp...)
	}
	printf := func(format string, outp ...interface{}) {
		fmt.Fprintf(inout, format, outp...)
	}

	var l1, r1, l2, r2 int
	scan(l1, r1, l2, r2)

	if r2 < l1 || r1 < l2 {
		printf("%d\n", l1+l2)
	} else {
		printf("%d\n", max(int64(l1), int64(l2)))
	}

}

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

	var T int = 1
	fmt.Fscan(inout, &T)
	for ; T > 0; T-- {
		solve(inout)
	}
}


package main

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

func scanInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	a := 0
	for _, num := range scanner.Bytes() {
		a = a*10 + int(num-'0')
	}
	return a
}
func check(u, v int) bool {
	if u > v {
		return false
	}
	cnt := 0
	for i := 0; i < 30; i++ {
		cnt += u & 1
		cnt -= v & 1
		if cnt < 0 {
			return false
		}
		u >>= 1
		v >>= 1
	}
	return true
}
func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1024*1024*128), 1024*1024*128)

	q := scanInt(scanner)
	fmt.Scanf("%d\n", &q)
	for ; q > 0; q-- {
		u, v := scanInt(scanner), scanInt(scanner)
		if check(u, v) {
			fmt.Printf("YES\n")
		} else {
			fmt.Printf("NO\n")
		}
	}
}

package main

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

const maxlog = 30

func min(v ...int) int {
	res := v[0]
	for _, x := range v {
		if x < res {
			res = x
		}
	}
	return res
}

func next1(num, from int) int {
	for i := from; i >= 0; i-- {
		if num&(1<<i) != 0 {
			return i
		}
	}
	return -1
}

/*
	01111001101
	10011110100
*/

func ok(from, to int) bool {
	if from > to {
		return false
	}

	i, j := maxlog-1, maxlog-1
	for {
		j = next1(to, j)
		if j == -1 {
			break
		}

		i = next1(from, i)
		for i > j {
			i--
			i = next1(from, i)
		}

		if i == -1 {
			return false
		}

		j--
		i--
	}

	return true
}

func main() {
	defer writer.Flush()
	//redirectInputToFile(reader, "input.txt")

	T := 1
	// scan(&T)
	for t := 0; t < T; t++ {
		var q int
		scan(&q)
		for i := 0; i < q; i++ {
			var u, v int
			scan(&u, &v)
			if ok(u, v) {
				printf("YES\n")
			} else {
				printf("NO\n")
			}
		}
	}
}

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

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

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

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

func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sint64s(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = sint64()
	}
	return a
}

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

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		printf("%s\n", sol(sint64(), sint64()))
	}
}

func sol(u, v int64) string {
	ub := bits(u)
	vb := bits(v)
	if len(vb) > len(ub) {
		return "NO"
	}
	if u > v {
		return "NO"
	}
	for i := range vb {
		if ub[i] > vb[i] {
			return "NO"
		}
	}
	return "YES"
}

func bits(x int64) []int64 {
	ret := []int64{}
	for i := int64(0); i < 100; i++ {
		if 1<<i > x {
			break
		}
		if (1<<i)&x != 0 {
			ret = append(ret, i)
		}
	}
	return ret
}

package main

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

func scanInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	a := 0
	for _, num := range scanner.Bytes() {
		a = a*10 + int(num-'0')
	}
	return a
}
func check(u, v int) bool {
	if u > v {
		return false
	}
	cnt := 0
	for i := 0; i < 30; i++ {
		cnt += u & 1
		cnt -= v & 1
		if cnt < 0 {
			return false
		}
		u >>= 1
		v >>= 1
	}
	return true
}
func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1024*1024*4), 1024*1024*4)

	q := scanInt(scanner)
	fmt.Scanf("%d\n", &q)
	for ; q > 0; q-- {
		u, v := scanInt(scanner), scanInt(scanner)
		if check(u, v) {
			fmt.Printf("YES\n")
		} else {
			fmt.Printf("NO\n")
		}
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1491D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var q, v, w int
o:
	for Fscan(in, &q); q > 0; q-- {
		Fscan(in, &v, &w)
		if v > w {
			Fprintln(out, "NO")
			continue
		}
		for i, c := 0, 0; i < 30; i++ {
			c += v>>i&1 - w>>i&1
			if c < 0 {
				Fprintln(out, "NO")
				continue o
			}
		}
		Fprintln(out, "YES")
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var q, v, w int
o:
	for Fscan(in, &q); q > 0; q-- {
		Fscan(in, &v, &w)
		if v > w {
			Fprintln(out, "NO")
			continue
		}
		for c := 0; w > 0; w >>= 1 {
			c += v&1 - w&1
			if c < 0 {
				Fprintln(out, "NO")
				continue o
			}
			v >>= 1
		}
		Fprintln(out, "YES")
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var q, v, w int
o:
	for Fscan(in, &q); q > 0; q-- {
		Fscan(in, &v, &w)
		if v > w {
			Fprintln(out, "NO")
			continue
		}
		for i, c := 0, 0; i < 30; i++ {
			c += v>>i&1 - w>>i&1
			if c < 0 {
				Fprintln(out, "NO")
				continue o
			}
		}
		Fprintln(out, "YES")
	}
}

package main

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

func scanInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	a := 0
	for _, num := range scanner.Bytes() {
		a = a*10 + int(num-'0')
	}
	return a
}
func check(u, v int) bool {
	if u > v {
		return false
	}
	cnt := 0
	for i := 0; i < 30; i++ {
		cnt += u & 1
		cnt -= v & 1
		if cnt < 0 {
			return false
		}
		u >>= 1
		v >>= 1
	}
	return true
}
func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	q := scanInt(scanner)
	fmt.Scanf("%d\n", &q)
	for ; q > 0; q-- {
		u, v := scanInt(scanner), scanInt(scanner)
		if check(u, v) {
			fmt.Printf("YES\n")
		} else {
			fmt.Printf("NO\n")
		}
	}
}

package main

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

func scanInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	a := 0
	for _, num := range scanner.Bytes() {
		a = a*10 + int(num-'0')
	}
	return a
}
func check(u, v int) bool {
	if u > v {
		return false
	}
	cnt := 0
	for i := 0; i < 30; i++ {
		cnt += u & 1
		cnt -= v & 1
		if cnt < 0 {
			return false
		}
		u >>= 1
		v >>= 1
	}
	return true
}
func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1024*1024), 1024*1024)

	q := scanInt(scanner)
	fmt.Scanf("%d\n", &q)
	for ; q > 0; q-- {
		u, v := scanInt(scanner), scanInt(scanner)
		if check(u, v) {
			fmt.Printf("YES\n")
		} else {
			fmt.Printf("NO\n")
		}
	}
}

package main

import (
	"fmt"
)

func check(u, v int) bool {
	if u > v {
		return false
	}
	cnt := 0
	for i := 0; i < 30; i++ {
		cnt += u & 1
		cnt -= v & 1
		if cnt < 0 {
			return false
		}
		u >>= 1
		v >>= 1
	}
	return true
}
func main() {
	var q, u, v int
	fmt.Scanf("%d", &q)
	for ; q > 0; q-- {
		fmt.Scanf("%d %d", &u, &v)
		if check(u, v) {
			fmt.Printf("YES\n")
		} else {
			fmt.Printf("NO\n")
		}
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

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

	var q int
	var v, w uint
	Fscan(in, &q)
	for i := 1; i <= q; i++ {
		Fscan(in, &v, &w)
		if q > 10 && i != 1589 {
			continue
		}
		if q > 10 {
			Fprintln(out, v, w)
		}
		if v <= w && bits.OnesCount(v) >= bits.OnesCount(w) {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

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

	var q, v, w uint
	for Fscan(in, &q); q > 0; q-- {
		if Fscan(in, &v, &w); v <= w && bits.OnesCount(v) >= bits.OnesCount(w) {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

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

package main

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

func solveD(queries [][2]int) {
	ok := func(a, b int) bool {
		i := 29
		for j := 29; j >= 0; j-- {
			if b&(1<<j) == 0 {
				continue
			}

			for ; i >= 0 && a&(1<<i) == 0; i-- {
			}

			if i == -1 || i > j {
				return false
			}
			i--
		}
		return true
	}

	for _, q := range queries {
		u, v := q[0], q[1]
		if ok(u, v) {
			printf("YES\n")
		} else {
			printf("NO\n")
		}
	}
}

func main() {
	defer writer.Flush()
	//redirectInputToFile(reader, "input.txt")

	T := 1
	// scan(&T)
	for t := 0; t < T; t++ {
		var q int
		scan(&q)
		queries := make([][2]int, q)
		for i := range queries {
			for j := 0; j < 2; j++ {
				scan(&queries[i][j])
			}
		}
		solveD(queries)
	}
}

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

// github.com/EndlessCheng/codeforces-go
func CF1323C(_r io.Reader, _w io.Writer) {
	var n, c, ans int
	var s []byte
	Fscan(bufio.NewReader(_r), &n, &s)
	for _, b := range s {
		if b == '(' {
			c++
		}
	}
	if 2*c != n {
		Fprint(_w, -1)
		return
	}

	c = 0
	for i := 0; i < n; i++ {
		if s[i] == '(' {
			c++
			continue
		}
		c--
		if c >= 0 {
			continue
		}
		st := i
		for i++; i < n; i++ {
			if s[i] == '(' {
				c++
				if c == 0 {
					break
				}
			} else {
				c--
			}
		}
		ans += i - st + 1
	}
	Fprint(_w, ans)
}

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

/*
URL:
https://codeforces.com/problemset/problem/1322/A
*/

package main

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

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const (
	// General purpose
	MOD          = 1000000000 + 7
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	ReadString = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

var (
	n int
	S []rune
)

func main() {
	n = ReadInt()
	S = ReadRuneSlice()

	pref := make([]int, n+1)
	for i := 0; i < n; i++ {
		if S[i] == '(' {
			pref[i+1] = pref[i] + 1
		} else {
			pref[i+1] = pref[i] - 1
		}
	}
	PrintfDebug("%v\n", pref)

	if pref[len(pref)-1] != 0 {
		fmt.Println(-1)
		return
	}

	ans := 0
	var start int
	var flag bool
	for i := 1; i <= n; i++ {
		if pref[i] < 0 && !flag {
			start = i
			flag = true
			continue
		}

		if pref[i] == 0 && flag {
			ans += i - start + 1
			flag = false
		}
	}

	fmt.Println(ans)
}

/*******************************************************************/

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintfDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintfDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

package main

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

var reader io.Reader
var scanner *bufio.Scanner

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

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

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

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

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

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

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

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

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

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

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

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

	lastPos := -1
	var result int
	var bal int
	for i := 0; i < n; i++ {
		if s[i] == '(' {
			bal++
		} else if s[i] == ')' {
			bal--
		}

		if bal == 0 && lastPos != -1 {
			result += i - lastPos + 1
			lastPos = -1
		}
		if bal < 0 && lastPos == -1 {
			lastPos = i
		}
	}
	if bal != 0 {
		fmt.Fprintln(out, -1)
		return
	}
	fmt.Fprintln(out, result)
}

package main

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

func main() {
	var n int64
	fmt.Scanf("%d\n", &n)
	reader := bufio.NewReader(os.Stdin)
	str := readLine(reader)
	if n%2 != 0 {
		fmt.Println(-1)
		return
	}
	left := 0
	wstart := int64(0)
	flag := true
	result := int64(0)
	for i := int64(0); i < n; i++ {
		if str[i] == ')' {
			left--
			if left == 0 && !flag {
				flag = true
				result += i - wstart + 1
				wstart = 0
			} else if left < 0 && flag {
				flag = false
				wstart = i
			}
		}
		if str[i] == '(' {
			left++
			if left == 0 && !flag {
				flag = true
				result += i-wstart+1
				wstart = 0
			}
		}
	}
	if left != 0 {
		fmt.Println(-1)
		return
	}
	fmt.Println(result)
}

func readLine(reader *bufio.Reader) []byte {
	result := make([]byte, 0)
	for {
		s, isPrefix, err := reader.ReadLine()
		if err != nil {
			fmt.Printf("error=%+v\n", err)
			break
		}
		result = append(result, s...)
		if !isPrefix {
			// fmt.Println(string(result))
			break
		}
	}
	return result
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    capacity := 5000 * 1024
    scanner.Buffer(make([]byte, capacity), capacity)

    scanner.Scan()
    line := scanner.Text()
    n, _ := strconv.Atoi(line)

    scanner.Scan()
    line = scanner.Text()
    a := make([]int8, n)
    for i := 0; i < n; i++ {
        if line[i] == '(' {
            a[i] = 1
        } else if line[i] == ')' {
            a[i] = -1
        } else {
            panic("invalid char")
        }
    }
    //~ fmt.Println(a)

    sum := make([]int, n)
    sum[0] = int(a[0])
    for i := 1; i < n; i++ {
        sum[i] = sum[i - 1] + int(a[i])
    }

    //~ fmt.Println(sum)

    if sum[n - 1] != 0 {
        fmt.Println(-1)
        return
    }

    zeroes := find_zeroes(sum)
    //~ fmt.Println(zeroes)

    last_zero := -1
    count := 0
    for _, zero := range zeroes {
        if sum[zero - 1] < 0 {
            count += zero - last_zero
        }
        last_zero = zero
    }

    fmt.Println(count)
}

func find_zeroes(a []int) []int {
    z := make([]int, 0)
    for i, v := range a {
        if v == 0 {
            z = append(z, i)
        }
    }
    return z
}

package main

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

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

	var n, cntL, ans int
	var s []byte
	Fscan(in, &n, &s)
	for _, b := range s {
		if b == '(' {
			cntL++
		}
	}
	if 2*cntL != n {
		Fprint(out, -1)
		return
	}

	cntL = 0
	for i := 0; i < n; i++ {
		if s[i] == '(' {
			cntL++
			continue
		}
		cntL--
		if cntL >= 0 {
			continue
		}
		st := i
		for i++; i < n; i++ {
			if s[i] == '(' {
				cntL++
				if cntL == 0 {
					break
				}
			} else {
				cntL--
			}
		}
		ans += i - st + 1
	}
	Fprint(out, ans)
}

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

package main

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

func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n int
	var s []byte
	Fscan(in, &n, &s)
	if n%2 == 1 {
		Fprintln(out, -1)
		return
	}
	cntl, cntr := 0, 0
	for _, b := range s {
		if b == '(' {
			cntl++
		} else {
			cntr++
		}
	}
	if cntl != n/2 {
		Fprintln(out, -1)
		return
	}

	ans := 0
	cntl, cntr = 0, 0
	okl := 0
	for i := 0; i < n; i++ {
		b := s[i]

		if b == '(' {
			//cntl++
			okl++
		} else {
			//cntr++
			okl--
			if okl < 0 {
				j := i+1
				for ; j < n; j++ {
					b := s[j]
					if b == '(' {
						okl++
						if okl == 0 {
							break
						}
					} else {
						okl--
					}
				}
				ans += j-i+1
				i=j
			}
		}

	}
	Fprintln(out, ans)
}

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

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
)

func main() {
	buf, _ := ioutil.ReadAll(os.Stdin)
	buf = bytes.TrimSpace(buf)

	parts := bytes.Split(buf, []byte("\n"))
	if len(parts) != 2 {
		panic("!!!")
	}

	str := bytes.TrimSpace(parts[1])

	curr := 0

	ans := 0

	for _, c := range str {
		if c == '(' {
			curr++
			if curr <= 0 {
				ans++
			}
		} else if c == ')' {
			curr--
			if curr < 0 {
				ans++
			}
		} else {
			panic(string([]byte{c}))
		}
	}

	if curr != 0 {
		fmt.Println(-1)
	} else {
		fmt.Println(ans)
	}
}


package main

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

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

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

var n int
var m [][]int
var values [][]int
var ans = 0

func pick(i, j int) {
	// printf("pick %d,%d\n", i, j)
	ans ^= values[i][j]
	if i+1 < n {
		m[i+1][j] ^= 1
	}
	if i > 0 {
		m[i-1][j] ^= 1
	}
	if j+1 < n {
		m[i][j+1] ^= 1
	}
	if j > 0 {
		m[i][j-1] ^= 1
	}
	// printf("ans = %d\n", ans)
}

func printMatrix() {
	printf("Matrix: \n")
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			printf("%d", m[i][j])
		}
		printf("\n")
	}
	printf("-------------------\n")
}

func solveCase() {
	scan(&n)
	m = make([][]int, n)
	values = make([][]int, n)
	ans = 0
	for i := 0; i < n; i++ {
		m[i] = make([]int, n)
		values[i] = make([]int, n)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			scan(&values[i][j])
		}
	}
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if m[i][j] == 0 {
				if j+1 < n {
					if i > 0 && m[i-1][j+1] == 1 {
						pick(i+1, j)
					} else {
						pick(i, j+1)
					}
					// printMatrix()
				} else if i+1 < n {
					pick(i+1, j)
					// printMatrix()
				}
			}
		}
	}
	printf("%d\n", ans)
}

func main() {
	defer out.Flush()

	var ntc int
	scan(&ntc)
	for t := 0; t < ntc; t++ {
		solveCase()
	}
}

package main

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

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

	t := readInt(in)
	b := strings.Builder{}
	for i := 0; i < t; i++ {
		str := solve(in)
		b.WriteString(str + "\n")
	}
	fmt.Println(b.String())
}

func solve(in *bufio.Reader) string {
	n := readInt(in)
	grid := make([][]int, n)
	visited := make([][]int, n)

	for i := 0; i < n; i++ {
		row := readArrInt(in)
		visitedRow := make([]int, n)
		grid[i] = row
		visited[i] = visitedRow
	}

	res := 0
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			xor := grid[i][j]
			if checkAdj(visited, i, j, n) {
				res = res ^ xor
			}
		}
	}

	return strconv.Itoa(res)
}

func printArr(visited [][]int, n int) {
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			fmt.Printf("%d ", visited[i][j])
		}
		fmt.Println()
	}
}

type coord struct {
	i int
	j int
}

func changeVisited(arr [][]int, coords []coord) {
	for _, c := range coords {
		arr[c.i][c.j] = 1
	}
}

func checkAdj(arr [][]int, i, j, n int) bool {
	toChange := []coord{}
	left := coord{i, j - 1}
	right := coord{i, j + 1}
	up := coord{i - 1, j}
	down := coord{i + 1, j}
	if left.inBorder(n) {
		if arr[left.i][left.j] != 0 {
			return false
		}
		toChange = append(toChange, left)
	}
	if right.inBorder(n) {
		if arr[right.i][right.j] != 0 {
			return false
		}
		toChange = append(toChange, right)
	}
	if up.inBorder(n) {
		if arr[up.i][up.j] != 0 {
			return false
		}
		toChange = append(toChange, up)
	}
	if down.inBorder(n) {
		if arr[down.i][down.j] != 0 {
			return false
		}
		toChange = append(toChange, down)
	}
	changeVisited(arr, toChange)
	return true
}

func (c *coord) inBorder(n int) bool {
	return c.j < n && c.j >= 0 && c.i < n && c.i >= 0
}

func readInt(in *bufio.Reader) int {
nStr, _ := in.ReadString('\n')
nStr = strings.ReplaceAll(nStr, "\r", "")
nStr = strings.ReplaceAll(nStr, "\n", "")
n, _ := strconv.Atoi(nStr)
return n
}

func readLineNumbs(in *bufio.Reader) []string {
line, _ := in.ReadString('\n')
line = strings.ReplaceAll(line, "\r", "")
line = strings.ReplaceAll(line, "\n", "")
numbs := strings.Split(line, " ")
return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}


package main

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

func main() {
	defer out.Flush()

	var t int
	for scan(&t); t > 0; t-- {
		var n int
		scan(&n)
		a := make([]int, n)
		b := make([]int, n)
		aPairs := make(map[int][]int)
		bPairs := make(map[int][]int)
		for i := range a {
			scan(&a[i])
		}
		for i := range b {
			scan(&b[i])
		}
		for i := range a {
			aPairs[a[i]] = append(aPairs[a[i]], a[n-1-i])
		}
		for i := range b {
			bPairs[b[i]] = append(bPairs[b[i]], b[n-1-i])
		}
		bad := false
		for key, aValue := range aPairs {
			if bValue, ok := bPairs[key]; ok {
				sort.Ints(aValue)
				sort.Ints(bValue)
				if len(aValue) != len(bValue) {
					bad = true
				} else {
					for i, a := range aValue {
						if a != bValue[i] {
							bad = true
							break
						}
					}
				}
			} else {
				bad = true
			}
			if bad {
				break
			}
		}
		if bad {
			println("No")
		} else {
			println("Yes")
		}
	}
}

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

func scan(x ...interface{}) (n int, err error) {
	return fmt.Fscan(in, x...)
}
func println(x ...interface{}) (n int, err error) {
	return fmt.Fprintln(out, x...)
}

package main

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

type node struct {
	diff int
	time int
}

type ByTime []node

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		a, b := make([]int, n), make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
		}
		fmt.Fscanf(in, "\n")
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &b[i])
		}
		fmt.Fscanf(in, "\n")
		if n%2 == 1 {
			if a[n/2] != b[n/2] {
				fmt.Fprintf(out, "No\n")
				continue
			}
		}
		left := 0
		right := 0
		used := make([]bool, n)
		if n%2 == 0 {
			left = n/2 - 1
			right = left + 1
		} else {
			left = n/2 - 1
			right = left + 2
		}
		ok := true
		for left >= 0 {
			found := false
			for i := 0; i < n/2; i++ {
				x := i
				y := n - 1 - x
				if !used[i] {
					if a[x] == b[left] && a[y] == b[right] || a[x] == b[right] && a[y] == b[left] {
						used[i] = true
						found = true
						break
					}
				}
			}
			if !found {
				ok = false
				break
			}
			left--
			right++
		}
		if ok {
			fmt.Fprintf(out, "Yes\n")
		} else {
			fmt.Fprintf(out, "No\n")
		}
	}
	return
}

package main

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

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

	buff := make([]byte, 2 * 1024 * 1024)

	scanner.Buffer(buff, 1024 * 1024)

	scanner.Scan()

	t := scanner.Text()

	testCases, err := strconv.Atoi(t)

	if err != nil {
		panic(err)
	}

	for j := 0; j < int(testCases); j ++ {

		scanner.Scan()

		N := scanner.Text()

		n, _  := strconv.Atoi(N)

		var a []int
		var b []int

		scanner.Scan()

		aas := scanner.Text()

		ps := strings.Fields(aas)

		scanner.Scan()

		bbs := scanner.Text()

		bps := strings.Fields(bbs)

		for i:= 0; i < n; i++ {
			v1, _ := strconv.Atoi(ps[i])

			a = append(a, v1)

			v2, _ := strconv.Atoi(bps[i])

			b = append(b, v2)
		}

		fmt.Println(solve3(a, b))
	}
}

func solve3(a,b []int) string {
	if len(a) != len(b) {
		return "No"
	}
	if len(a) % 2 == 0 {
		if checkEven(a, b) {
			return "Yes"
		} else {
			return "No"
		}
	} else {
		if checkEven(a, b) && checkMiddle(a, b) {
			return "Yes"
		} else {
			return "No"
		}
	}
}

func checkEven(a, b []int) bool {
	//1 2 3 4 5 6
	//2 6 3 4 1 5

	mp := make(map[[2]int]int)
	n := len(a)

	for i:=0; i < n/2; i++ {
		opposite := a[n - i - 1]
		current := a[i]

		var max int
		var min int
		if current > opposite {
			max = current
			min = opposite
		} else {
			max = opposite
			min = current
		}
		mp[[2]int{max, min}]++
	}

	for i := 0; i < n/2; i++ {
		opposite := b[n - i - 1]
		current := b[i]

		var max int
		var min int
		if current > opposite {
			max = current
			min = opposite
		} else {
			max = opposite
			min = current
		}
		arr := [2]int{max, min}

		mp[arr]--
	}

	for _, v := range mp {
		if v != 0 {
			return false
		}
	}

	return true
}

func checkMiddle(a, b []int) bool {
	return a[len(a) / 2] == b[len(b) / 2]
}

package main

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

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '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 main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		B := readNNums(reader, n)

		res := solve(n, A, B)

		if res {
			fmt.Println("Yes")
		} else {
			fmt.Println("No")
		}
	}
}

func solve(n int, A []int, B []int) bool {
	if n%2 == 1 {
		if A[n/2] != B[n/2] {
			return false
		}
	}

	cnt := make(map[Pair]int)

	for i := 0; i < n; i++ {
		a := Pair{min(A[i], A[n-1-i]), max(A[i], A[n-1-i])}
		cnt[a]++
		b := Pair{min(B[i], B[n-1-i]), max(B[i], B[n-1-i])}
		cnt[b]--
	}

	for _, v := range cnt {
		if v != 0 {
			return false
		}
	}

	return true
}

type Pair struct {
	first, second int
}

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
}


package main

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

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

func main() {
	defer writer.Flush()
	// redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

type treenode struct {
	node, subtreeSize, subtreeXor int
}

func solve() {
	var n, k int
	scan(&n, &k)
	a := make([]int, n)
	for i := range a {
		scan(&a[i])
	}
	adj := make([][]int, n)
	for i := 0; i < n-1; i++ {
		u, v := -1, -1
		scan(&u, &v)
		adj[u-1] = append(adj[u-1], v-1)
		adj[v-1] = append(adj[v-1], u-1)
	}

	tree := maketree(a, adj)
	if tree[0].subtreeXor == 0 {
		printf("YES\n")
		return
	}
	if k == 2 {
		printf("NO\n")
		return
	}
	xorcnt := makePrefixSums(tree, tree[0].subtreeXor)
	zerocnt := makePrefixSums(tree, 0)
	for i := 1; i < n; i++ {
		if tree[i].subtreeXor == tree[0].subtreeXor &&
			(xorcnt[n-1]-xorcnt[i+tree[i].subtreeSize-1] > 0 || zerocnt[i-1]-zerocnt[0] > 0) {
			printf("YES\n")
			return
		}
	}
	printf("NO\n")
}

func makePrefixSums(tree []treenode, x int) []int {
	pref := make([]int, len(tree))
	if tree[0].subtreeXor == x {
		pref[0] = 1
	}
	for i := 1; i < len(tree); i++ {
		pref[i] = pref[i-1]
		if tree[i].subtreeXor == x {
			pref[i]++
		}
	}
	return pref
}

func maketree(xor []int, adj [][]int) []treenode {
	tree := make([]treenode, 0, len(adj))

	var globalnum int
	var dfs func(int, int) treenode

	dfs = func(curr, prev int) treenode {
		tree = append(tree, treenode{
			node:        curr,
			subtreeSize: 1,
			subtreeXor:  xor[curr],
		})
		idx := len(tree) - 1
		globalnum++
		for _, next := range adj[curr] {
			if next != prev {
				tr := dfs(next, curr)
				tree[idx].subtreeSize += tr.subtreeSize
				tree[idx].subtreeXor ^= tr.subtreeXor
			}
		}
		return tree[idx]
	}
	dfs(0, -1)

	return tree
}

package main

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

const MOD int64 = 1000000007

var n, k int

var a []int

var gr [][]int

func main() {

	read := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscan(read, &t)

	for i := 0; i < t; i++ {

		fmt.Fscan(read, &n, &k)

		a = make([]int, n)

		for j := 0; j < n; j++ {
			fmt.Fscan(read, &a[j])
		}

		gr = make([][]int, n + 1)

		for j := 0; j < n - 1; j++ {
			var u, v int
			fmt.Fscan(read, &u, &v)
			u--
			v--

			gr[u] = append(gr[u], v)
			gr[v] = append(gr[v], u)
		}

		fmt.Println(solve())
	}
}

func solve() string {

	cnt = 0

	var v int
	for i := 0; i < n; i++ {
		v ^= a[i]
	}

	if v == 0 {
		return "YES"
	}

	dfs(0, -1, v)

	if cnt >= 2 && cnt <= k - 1 {
		return "YES"
	}
	return "NO"
}

var cnt int

func dfs(v, p int, xor int) int {

	ch := gr[v]

	var x int

	var sons int

	for j := 0; j < len(ch); j++ {
		if ch[j] == p {
			continue
		}
		sons++
		a := dfs(ch[j], v, xor)

		if a == - 1 {
			return -1
		}

		x ^= a
	}

	if sons == 0  {
		if a[v] == xor {
			cnt++

			if cnt >= 2 {
				return -1
			}

			return 0
		}
		return a[v]
	}

	x ^= a[v]

	if x == xor {
		cnt++
	}
	if cnt >= 2 {
		return -1
	}

	if x == xor {
		return 0
	}

	return x
}

package main

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

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

	var T, n, k, v, w int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		xor := 0
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
			xor ^= a[i]
		}
		g := make([][]int, n)
		for i := 1; i < n; i++ {
			Fscan(in, &v, &w)
			v--
			w--
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}

		var f func(int, int) int
		f = func(v, fa int) (c int) {
			for _, w := range g[v] {
				if w != fa {
					c += f(w, v)
					a[v] ^= a[w]
				}
			}
			if c > 0 {
				if a[v] == 0 {
					c++
				}
			} else if a[v] == xor {
				c++
			}
			return
		}
		if xor == 0 || k > 2 && f(0, -1) > 1 {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

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

package main

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

const MOD int64 = 1000000007

var n, k int

var a []int

var gr [][]int

func main() {

	read := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscan(read, &t)

	for i := 0; i < t; i++ {

		fmt.Fscan(read, &n, &k)

		a = make([]int, n)

		for j := 0; j < n; j++ {
			fmt.Fscan(read, &a[j])
		}

		gr = make([][]int, n + 1)

		for j := 0; j < n - 1; j++ {
			var u, v int
			fmt.Fscan(read, &u, &v)
			u--
			v--

			gr[u] = append(gr[u], v)
			gr[v] = append(gr[v], u)
		}

		fmt.Println(solve())
	}
}

func solve() string {

	cnt = 0

	var v int
	for i := 0; i < n; i++ {
		v ^= a[i]
	}

	if v == 0 {
		return "YES"
	}

	dfs(0, -1, v)

	if cnt >= 2 && cnt <= k - 1 {
		return "YES"
	}
	return "NO"
}

var cnt int

func dfs(v, p int, xor int) int {

	ch := gr[v]

	var x int

	var sons int

	for j := 0; j < len(ch); j++ {
		if ch[j] == p {
			continue
		}
		sons++
		a := dfs(ch[j], v, xor)

		if a == - 1 {
			return -1
		}

		x ^= a
	}

	if sons == 0  {
		if a[v] == xor {
			cnt++

			if cnt >= 2 {
				return -1
			}

			return 0
		}
		return a[v]
	}

	x ^= a[v]

	if x == xor {
		cnt++
		return 0
	}
	if cnt >= 2 {
		return -1
	}

	return x
}

package main

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

const MOD int64 = 1000000007

var n, k int

var a []int

var gr [][]int

func main() {

	read := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscan(read, &t)

	for i := 0; i < t; i++ {

		fmt.Fscan(read, &n, &k)

		a = make([]int, n)

		for j := 0; j < n; j++ {
			fmt.Fscan(read, &a[j])
		}

		gr = make([][]int, n + 1)

		for j := 0; j < n - 1; j++ {
			var u, v int
			fmt.Fscan(read, &u, &v)
			u--
			v--

			gr[u] = append(gr[u], v)
			gr[v] = append(gr[v], u)
		}

		fmt.Println(solve())
	}
}

func solve() string {

	cnt = 0

	var v int
	for i := 0; i < n; i++ {
		v ^= a[i]
	}

	if v == 0 {
		return "YES"
	}

	dfs(0, -1, v)

	if cnt >= 2 && cnt <= k - 1 {
		return "YES"
	}
	return "NO"
}

var cnt int

func dfs(v, p int, xor int) int {

	ch := gr[v]

	var x int

	var sons int

	for j := 0; j < len(ch); j++ {
		if ch[j] == p {
			continue
		}
		sons++
		a := dfs(ch[j], v, xor)

		if a == - 1 {
			return -1
		}

		x ^= a
	}

	if sons == 0  {
		if a[v] == xor {
			cnt++

			if cnt >= 2 {
				return -1
			}

			return 0
		}
		return a[v]
	}

	x ^= a[v]

	if x == xor {
		cnt++
	}
	if cnt >= 2 {
		return -1
	}

	return x
}

package main

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

const MOD int64 = 1000000007

var n, k int

var a []int

var gr [][]int

func main() {

	read := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscan(read, &t)

	for i := 0; i < t; i++ {

		fmt.Fscan(read, &n, &k)

		a = make([]int, n)

		for j := 0; j < n; j++ {
			fmt.Fscan(read, &a[j])
		}

		gr = make([][]int, n + 1)

		for j := 0; j < n - 1; j++ {
			var u, v int
			fmt.Fscan(read, &u, &v)
			u--
			v--

			gr[u] = append(gr[u], v)
			gr[v] = append(gr[v], u)
		}

		fmt.Println(solve())
	}
}

func solve() string {

	cnt = 0

	var v int
	for i := 0; i < n; i++ {
		v ^= a[i]
	}

	if v == 0 {
		return "YES"
	}

	dfs(0, -1, v)

	if cnt >= 2 && cnt <= k - 1 {
		return "YES"
	}
	return "NO"
}

var cnt int

func dfs(v, p int, xor int) int {

	ch := gr[v]

	var x int

	var sons int

	for j := 0; j < len(ch); j++ {
		if ch[j] == p {
			continue
		}
		sons++
		a := dfs(ch[j], v, xor)

		if a == - 1 {
			return -1
		}

		x ^= a
	}

	if sons == 0  {
		if a[v] == xor {
			cnt++

			if cnt >= 2 {
				return -1
			}
		}
		return 0
	}

	x ^= a[v]

	if x == xor {
		cnt++
	}
	if cnt >= 2 {
		return -1
	}

	return x
}

package main

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

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

func main() {
	defer writer.Flush()
	// redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

type treenode struct {
	node, subtreeSize, subtreeXor int
}

func solve() {
	var n, k int
	scan(&n, &k)
	a := make([]int, n)
	for i := range a {
		scan(&a[i])
	}
	adj := make([][]int, n)
	for i := 0; i < n-1; i++ {
		u, v := -1, -1
		scan(&u, &v)
		adj[u-1] = append(adj[u-1], v-1)
		adj[v-1] = append(adj[v-1], u-1)
	}

	tree := maketree(a, adj)
	if tree[0].subtreeXor == 0 {
		printf("YES\n")
		return
	}
	if k == 2 {
		printf("NO\n")
		return
	}
	prefXorCnt := make([]int, n)
	prefXorCnt[0] = 1
	for i := 1; i < n; i++ {
		prefXorCnt[i] = prefXorCnt[i-1]
		if tree[i].subtreeXor == tree[0].subtreeXor {
			prefXorCnt[i]++
		}
	}
	for i := 1; i < n; i++ {
		if tree[i].subtreeXor == tree[0].subtreeXor &&
			prefXorCnt[n-1]-prefXorCnt[i+tree[i].subtreeSize-1] > 0 {
			printf("YES\n")
			return
		}
	}
	printf("NO\n")
}

func maketree(xor []int, adj [][]int) []treenode {
	tree := make([]treenode, 0, len(adj))

	var globalnum int
	var dfs func(int, int) treenode

	dfs = func(curr, prev int) treenode {
		tree = append(tree, treenode{
			node:        curr,
			subtreeSize: 1,
			subtreeXor:  xor[curr],
		})
		idx := len(tree) - 1
		globalnum++
		for _, next := range adj[curr] {
			if next != prev {
				tr := dfs(next, curr)
				tree[idx].subtreeSize += tr.subtreeSize
				tree[idx].subtreeXor ^= tr.subtreeXor
			}
		}
		return tree[idx]
	}
	dfs(0, -1)

	return tree
}

package main

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

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

func main() {
	defer writer.Flush()
	// redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

type treenode struct {
	node, subtreeSize, subtreeXor int
}

func solve() {
	var n, k int
	scan(&n, &k)
	a := make([]int, n)
	for i := range a {
		scan(&a[i])
	}
	adj := make([][]int, n)
	for i := 0; i < n-1; i++ {
		u, v := -1, -1
		scan(&u, &v)
		adj[u-1] = append(adj[u-1], v-1)
		adj[v-1] = append(adj[v-1], u-1)
	}

	tree := maketree(a, adj)
	if tree[0].subtreeXor == 0 {
		printf("YES\n")
		return
	}
	if k == 2 {
		printf("NO\n")
		return
	}
	prefXorCnt := make([]int, n)
	prefXorCnt[0] = 1
	for i := 1; i < n; i++ {
		prefXorCnt[i] = prefXorCnt[i-1]
		if tree[i].subtreeXor == tree[0].subtreeXor {
			prefXorCnt[i]++
		}
	}
	for i := 1; i < n; i++ {
		if tree[i].subtreeXor == tree[0].subtreeXor &&
			prefXorCnt[n-1]-prefXorCnt[i+tree[i].subtreeSize-1] > 0 {
			printf("YES\n")
			return
		}
	}
	printf("NO\n")
}

func maketree(a []int, adj [][]int) []treenode {
	tree := make([]treenode, 0, len(adj))

	var globalnum int
	var dfs func(int, int) treenode

	dfs = func(curr, prev int) treenode {
		tree = append(tree, treenode{
			node:        globalnum,
			subtreeSize: 1,
			subtreeXor:  a[globalnum],
		})
		idx := len(tree) - 1
		globalnum++
		for _, next := range adj[curr] {
			if next != prev {
				tr := dfs(next, curr)
				tree[idx].subtreeSize += tr.subtreeSize
				tree[idx].subtreeXor ^= tr.subtreeXor
			}
		}
		return tree[idx]
	}
	dfs(0, -1)

	return tree
}


package main

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

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

func main() {
	defer writer.Flush()
// 	redirectInputToFile(reader, "input.txt")

	T := 1
	// scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n int
	scan(&n)
	var qs = make([][3]int, n)
	for i := range qs {
		scan(&qs[i][0], &qs[i][1])
		if qs[i][0] == 2 {
			scan(&qs[i][2])
		}
	}
	var (
		arr            = make([][2]int, 0)
		numOf          = make(map[int]int)
		transformsInto = make(map[[2]int][2]int)
	)
	for _, q := range qs {
		typ, x, y := q[0], q[1], q[2]
		if typ == 1 {
			arr = append(arr, [2]int{x, numOf[x]})
		} else if x != y {
			transformsInto[[2]int{x, numOf[x]}] = [2]int{y, numOf[y]}
			numOf[x]++
		}
	}
	var walk func(curr [2]int) [2]int
	walk = func(curr [2]int) [2]int {
		if _, ok := transformsInto[curr]; !ok || transformsInto[curr] == curr {
			transformsInto[curr] = curr
		} else {
			transformsInto[curr] = walk(transformsInto[curr])
		}
		return transformsInto[curr]
	}
	for _, i := range arr {
		walk(i)
		printf("%v ", transformsInto[i][0])
	}
	printf("\n")
}

package main

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

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

func main() {
	defer writer.Flush()
// 	redirectInputToFile(reader, "input.txt")

	T := 1
	// scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n int
	scan(&n)
	var qs = make([][3]int, n)
	for i := range qs {
		scan(&qs[i][0], &qs[i][1])
		if qs[i][0] == 2 {
			scan(&qs[i][2])
		}
	}
	var (
		N   = 0
		pos = [5e5 + 1][]int{}
	)
	for _, q := range qs {
		typ, x, y := q[0], q[1], q[2]
		if typ == 1 {
			pos[x] = append(pos[x], N)
			N++
		} else if x != y {
			if len(pos[x]) > len(pos[y]) {
				pos[x], pos[y] = pos[y], pos[x]
			}
			pos[y] = append(pos[y], pos[x]...)
			pos[x] = pos[x][:0:0]
		}
	}
	var ans = make([]int, N)
	for x := 1; x <= 5e5; x++ {
		for _, i := range pos[x] {
			ans[i] = x
		}
	}
	for _, i := range ans {
		printf("%v ", i)
	}
}

package main

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

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

func main() {
	defer writer.Flush()
// 	redirectInputToFile(reader, "input.txt")

	T := 1
	// scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n int
	scan(&n)
	var qs = make([][3]int, n)
	for i := range qs {
		scan(&qs[i][0], &qs[i][1])
		if qs[i][0] == 2 {
			scan(&qs[i][2])
		}
	}
	var (
		N   = 0
		pos = [5e5 + 1][]int{}
	)
	for _, q := range qs {
		typ, x, y := q[0], q[1], q[2]
		if typ == 1 {
			pos[x] = append(pos[x], N)
			N++
		} else if x != y {
			if len(pos[x]) > len(pos[y]) {
				pos[x], pos[y] = pos[y], pos[x]
			}
			pos[y] = append(pos[y], pos[x]...)
			pos[x] = pos[x][:0]
		}
	}
	var ans = make([]int, N)
	for x := 1; x <= 5e5; x++ {
		for _, i := range pos[x] {
			ans[i] = x
		}
	}
	for _, i := range ans {
		printf("%v ", i)
	}
}

package main

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

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

	var q, op int
	Fscan(in, &q)
	type pair struct{ x, y int }
	qs := make([]pair, q)
	for i := range qs {
		Fscan(in, &op, &qs[i].x)
		if op == 2 {
			Fscan(in, &qs[i].y)
		}
	}

	pa := [5e5 + 1]int{}
	ans := make([]int, 0, 5e5)
	for i := q - 1; i >= 0; i-- {
		x, y := qs[i].x, qs[i].y
		if y == 0 {
			if pa[x] > 0 {
				x = pa[x]
			}
			ans = append(ans, x)
		} else {
			if pa[y] > 0 {
				y = pa[y]
			}
			pa[x] = y
		}
	}
	for i := len(ans) - 1; i >= 0; i-- {
		Fprint(out, ans[i], " ")
	}
}

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

package main

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

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

func main() {
	defer writer.Flush()
// 	redirectInputToFile(reader, "input.txt")

	T := 1
	// scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n int
	scan(&n)
	var qs = make([][3]int, n)
	for i := range qs {
		scan(&qs[i][0], &qs[i][1])
		if qs[i][0] == 2 {
			scan(&qs[i][2])
		}
	}
	var (
		arr            = make([][2]int, 0)
		numOf          = make(map[int]int)
		transformsInto = make(map[[2]int][2]int)
	)
	for _, q := range qs {
		typ, x, y := q[0], q[1], q[2]
		if typ == 1 {
			arr = append(arr, [2]int{x, numOf[x]})
		} else {
			transformsInto[[2]int{x, numOf[x]}] = [2]int{y, numOf[y]}
			numOf[x]++
		}
	}
	var walk func(curr [2]int) [2]int
	walk = func(curr [2]int) [2]int {
		if _, ok := transformsInto[curr]; !ok || transformsInto[curr] == curr {
			transformsInto[curr] = curr
		} else {
			transformsInto[curr] = walk(transformsInto[curr])
		}
		return transformsInto[curr]
	}
	for _, i := range arr {
		walk(i)
		printf("%v ", transformsInto[i][0])
	}
	printf("\n")
}


package main

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

type AB struct {
	a int
	b int
}

func solve() {
	n := readInt()
	k1 := readInt()
	k2 := readInt()
	a := readIntArray(n)
	b := readIntArray(n)
	heap := NewMinHeap(n)
	for i := 0; i < n; i++ {
		heap.Add(i, -abs(a[i]-b[i]))
	}
	for k1 > 0 || k2 > 0 {
		ind := heap.ArgMin()
		f := a[ind]
		v := b[ind]
		if f > v {
			if k1 > 0 {
				f--
				k1--
			} else if k2 > 0 {
				v++
				k2--
			}
		} else if v > f {
			if k1 > 0 {
				f++
				k1--
			} else if k2 > 0 {
				v--
				k2--
			}
		} else {
			if k1 > 0 {
				f++
				k1--
			} else if k2 > 0 {
				v--
				k2--
			}
		}
		a[ind] = f
		b[ind] = v

		heap.Update(ind, -abs(f-v))
	}
	total := int64(0)
	for i := 0; i < n; i++ {
		total += int64(a[i]-b[i]) * int64(a[i]-b[i])
	}
	fmt.Println(total)
}

type MinHeap struct {
	a    []int
	vmap []int
	imap []int
	n    int
	pos  int
}

const INF = math.MaxInt32

func NewMinHeap(m int) *MinHeap {
	heap := &MinHeap{}
	m += 2
	heap.n = m
	heap.a = make([]int, heap.n)
	heap.vmap = make([]int, heap.n)
	heap.imap = make([]int, heap.n)
	heap.pos = 1
	for i := 0; i < heap.n; i++ {
		heap.a[i] = INF
		heap.vmap[i] = -1
		heap.imap[i] = -1
	}
	return heap
}

func (h *MinHeap) Add(ind, x int) int {
	ret := h.imap[ind]
	if h.imap[ind] < 0 {
		h.a[h.pos] = x
		h.vmap[h.pos] = ind
		h.imap[ind] = h.pos
		h.pos++
		h.up(h.pos - 1)
	}
	if ret != -1 {
		return h.a[ret]
	}
	return x
}

func (h *MinHeap) Update(ind, x int) int {
	ret := h.imap[ind]
	if h.imap[ind] < 0 {
		h.a[h.pos] = x
		h.vmap[h.pos] = ind
		h.imap[ind] = h.pos
		h.pos++
		h.up(h.pos - 1)
	} else {
		h.a[ret] = x
		h.up(ret)
		h.down(ret)
	}
	return x
}

func (h *MinHeap) Remove(ind int) int {
	if h.pos == 1 {
		return INF
	}
	if h.imap[ind] == -1 {
		return INF
	}

	h.pos--
	rem := h.imap[ind]
	ret := h.a[rem]
	h.vmap[rem] = h.vmap[h.pos]
	h.imap[h.vmap[h.pos]] = rem
	h.imap[ind] = -1
	h.a[rem] = h.a[h.pos]
	h.a[h.pos] = INF
	h.vmap[h.pos] = -1

	h.up(rem)
	h.down(rem)
	return ret
}

func (h *MinHeap) Min() int {
	return h.a[1]
}

func (h *MinHeap) ArgMin() int {
	return h.vmap[1]
}

func (h *MinHeap) Size() int {
	return h.pos - 1
}

func (h *MinHeap) up(cur int) {
	for c, p := cur, cur>>1; p >= 1 && h.a[p] > h.a[c]; c, p = c>>1, p>>1 {
		h.a[p], h.a[c] = h.a[c], h.a[p]
		h.imap[h.vmap[p]], h.imap[h.vmap[c]] = h.imap[h.vmap[c]], h.imap[h.vmap[p]]
		h.vmap[p], h.vmap[c] = h.vmap[c], h.vmap[p]
	}
}

func (h *MinHeap) down(cur int) {
	for c := cur; 2*c < h.pos; {
		b := 0
		if h.a[2*c] < h.a[2*c+1] {
			b = 2 * c
		} else {
			b = 2*c + 1
		}
		if h.a[b] < h.a[c] {
			h.a[c], h.a[b] = h.a[b], h.a[c]
			h.imap[h.vmap[c]], h.imap[h.vmap[b]] = h.imap[h.vmap[b]], h.imap[h.vmap[c]]
			h.vmap[c], h.vmap[b] = h.vmap[b], h.vmap[c]
			c = b
		} else {
			break
		}
	}
}

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

func max(a, b int64) int64 {
	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()
}

// 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"
	"container/heap"
	"fmt"
	"os"
)

// B problem B
type B struct {
	q     IntHeap
	total int
}

// An IntHeap is a max-heap of ints.
type IntHeap []int

func (h IntHeap) Len() int            { return len(h) }
func (h IntHeap) Less(i, j int) bool  { return h[i] > h[j] }
func (h IntHeap) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

func (b *B) readInput() {
	inp := bufio.NewReader(os.Stdin)

	// read n
	var n, k1, k2, tmp int
	fmt.Fscanf(inp, "%d %d %d\n", &n, &k1, &k2)

	b.total = k1 + k2
	b.q = make(IntHeap, 0, n)

	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(inp, "%d", &a[i])
	}
	fmt.Fscanf(inp, "\n")
	for i := 0; i < n; i++ {
		fmt.Fscanf(inp, "%d", &tmp)
		if tmp = a[i] - tmp; tmp >= 0 {
			heap.Push(&b.q, tmp)
		} else {
			heap.Push(&b.q, -tmp)
		}
	}
}

func (b *B) solve() {
	total := b.total
	q := b.q

	for total > 0 {
		total--
		if q[0] > 0 {
			q[0]--
		} else {
			q[0]++
		}
		heap.Fix(&b.q, 0)
	}

	var sum int64
	for i := range q {
		sum += int64(q[i]) * int64(q[i])
	}
	fmt.Println(sum)
}

func main() {
	b := &B{}
	b.readInput()
	b.solve()
}

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
)

type IntHeap []int64

func (h IntHeap) Len() int            { return len(h) }
func (h IntHeap) Less(i, j int) bool  { return h[i] > h[j] }
func (h IntHeap) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) { *h = append(*h, x.(int64)) }
func (h *IntHeap) Pop() interface{}   { r := (*h)[len(*h)-1]; *h = (*h)[:len(*h)-1]; return r }

var in = bufio.NewScanner(os.Stdin)

func nextInt() int64 {
	in.Scan()
	var r, f int64
	r, f = 0, 1
	for _, c := range in.Bytes() {
		if c == '-' {
			f = -1
			continue
		}
		r *= 10
		r += int64(c - '0')
	}
	return r * f
}

func main() {
	in.Split(bufio.ScanWords)
	n, k := nextInt(), nextInt()+nextInt()
	a := make(IntHeap, n)
	for i := range a {
		a[i] = nextInt()
	}
	for i := range a {
		a[i] -= nextInt()
		if a[i] < 0 {
			a[i] = -a[i]
		}
	}
	heap.Init(&a)
	for ; k > 0; k-- {
		v := heap.Pop(&a).(int64)
		if v == 0 {
			if k%2 == 0 {
				fmt.Println(0)
			} else {
				fmt.Println(1)
			}
			return
		}
		heap.Push(&a, v-1)
	}
	var r int64
	for len(a) > 0 {
		v := heap.Pop(&a).(int64)
		r += v * v
	}
	fmt.Println(r)
}

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
)

type IntHeap []int

func (h IntHeap) Len() int            { return len(h) }
func (h IntHeap) Less(i, j int) bool  { return h[i] > h[j] }
func (h IntHeap) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *IntHeap) Pop() interface{}   { r := (*h)[len(*h)-1]; *h = (*h)[:len(*h)-1]; return r }

var in = bufio.NewScanner(os.Stdin)

func nextInt() int {
	in.Scan()
	r, f := 0, 1
	for _, c := range in.Bytes() {
		if c == '-' {
			f = -1
			continue
		}
		r *= 10
		r += int(c - '0')
	}
	return r * f
}

func main() {
	in.Split(bufio.ScanWords)
	n, k := nextInt(), nextInt()+nextInt()
	a := make(IntHeap, n)
	for i := range a {
		a[i] = nextInt()
	}
	for i := range a {
		a[i] -= nextInt()
		if a[i] < 0 {
			a[i] = -a[i]
		}
	}
	heap.Init(&a)
	for ; k > 0; k-- {
		v := heap.Pop(&a).(int)
		if v == 0 {
			if k%2 == 0 {
				fmt.Println(0)
			} else {
				fmt.Println(1)
			}
			return
		}
		heap.Push(&a, v-1)
	}
	r := 0
	for len(a) > 0 {
		v := heap.Pop(&a).(int)
		r += v * v
	}
	fmt.Println(r)
}


package main

import (
	"fmt"
)

const (
	N = 2010
)

var t, n int
var a []int

func solve() {
	sum := 0
	for i := 0; i < n; i++ {
		sum += a[i]
	}
	total := 0x3f3f3f3f
	for cnt := n; cnt > 0; cnt-- {
		b := sum / cnt
		if sum%cnt != 0 {
			continue
		} else {
			now, tt, flag, ans := 0, 0, true, 0
			for i := 0; i < n; i++ {
				now += a[i]
				tt++

				if now == b {
					if ans < tt {
						ans = tt
					}
					now = 0
					tt = 0
				} else if now > b {
					flag = false
					break
				}
			}
			if flag {
				if total > ans {
					total = ans
				}
			}
		}
	}
	fmt.Println(total)
}

func main() {
	fmt.Scan(&t)
	for ; t != 0; t-- {
		fmt.Scan(&n)
		a = make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Scan(&a[i])
		}
		solve()
	}
}

package main

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

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

func solve() string {
	var n int
	fmt.Fscan(in, &n)

	var sum int
	v := make([]int, n)
	for i := range v {
		fmt.Fscan(in, &v[i])
		sum += v[i]
	}

	var res = n
	for numOfGroups := 1; numOfGroups <= n; numOfGroups++ {
		if sum%numOfGroups != 0 {
			continue
		}

		target := sum / numOfGroups
		cnt, maxCnt, curSum := 0, 0, 0
		for j := 0; j < n; j++ {
			curSum += v[j]
			cnt++
			if curSum > target {
				maxCnt = n
				break
			}
			if curSum == target {
				if cnt > maxCnt {
					maxCnt = cnt
				}
				curSum = 0
				cnt = 0
			}
		}
		if maxCnt < res {
			res = maxCnt
		}
	}

	return strconv.Itoa(res)
}

func main() {
	defer out.Flush()
	var t int
	for fmt.Fscanln(in, &t); t > 0; t-- {
		fmt.Fprintln(out, solve())
	}
}

// util functions
func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

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

func makeMatrix(n, m int) [][]int {
	x := make([][]int, n)
	for i := range x {
		x[i] = make([]int, m)
	}
	return x
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func solve() {
	var n int
	scanf("%d\n", &n)
	a := make([]int, n)
	for i:=0; i<n; i++ {
		scanf("%d ", &a[i])
	}
	scanf("\n")

	var res int = n
	prefSum := 0
	for i:=0; i<n; i++ {
		prefSum += a[i]
		j := i+1
		pos := true
		curSum := 0
		mxLen := i+1
		prev := i
		for j<n {
			curSum += a[j]
			if curSum > prefSum {
				pos = false
				break
			}
			if curSum == prefSum {
				curLen := j - prev
				if curLen > mxLen {
					mxLen = curLen
				}
				prev = j
				j++
				curSum = 0
				continue
			}
			j++
		}

		if pos && curSum == 0 {
			lastLen := j - prev
			if lastLen > mxLen {
				mxLen = lastLen
			}
			if mxLen < res {
				res = mxLen
			}
		}
	}
	printf("%d\n", res)
}

func main() {
	defer writer.Flush()

	var T int
	scanf("%d\n", &T)
	for tt:=1; tt<=T; tt++ {
		solve()
	}
}

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() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {

		var n int
		fmt.Fscan(r, &n)

		a := make([]int, n)

		for i := range a {
			fmt.Fscan(r, &a[i])
		}

		m := n
		sum := 0
		for i := range a {
			sum += a[i]

			rm := 0
			rsum := 0
			rcnt := 0
			dont := false

			for _, j := range a {
				rsum += j
				rcnt++

				if rsum == sum {
					rm = max(rm, rcnt)
					rsum = 0
					rcnt = 0
				}

				if rsum > sum {
					dont = true
					break
				}
			}

			if rsum != 0 || dont {
				continue
			}

			m = min(m, rm)
		}
		fmt.Fprintln(w, m)
	}

}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var num int
	fmt.Scan(&num)
	for i := 0; i < num; i++ {

		var num1 int
		fmt.Scan(&num1)

		var numArr []int
		for i := 0; i < num1; i++ {
			var num2 int
			fmt.Scan(&num2)
			numArr = append(numArr, num2)
		}
		var res int
		res = thri(numArr)
		fmt.Println(res)
	}

}

// [2,6,10,11,19,22]
func thri(arr []int) int {
	pre := 0
	next := 1
	var prefixSum []int
	sum := 0
	for i := 0; i < len(arr); i++ {
		sum += arr[i]
		prefixSum = append(prefixSum, sum)
	}
	var maxSum int
	pre2 := 0
	var arrCount []int
	var realCount []int
	flag := false
	for pre < len(prefixSum) {
		maxSum = prefixSum[pre]

		for next < len(prefixSum) {
			if maxSum < prefixSum[next]-prefixSum[pre2] {
				break
			} else if maxSum > prefixSum[next]-prefixSum[pre2] {
				next++
			} else {
				if next == len(prefixSum)-1 {
					flag = true
				}
				arrCount = append(arrCount, next-pre2)
				pre2 = next
				next++
			}
		}

		if flag {
			arrCount = append(arrCount, pre+1)
			sort.Ints(arrCount)
			realCount = append(realCount, arrCount[len(arrCount)-1])

			arrCount = nil
			flag = false
		}

		pre++
		pre2 = pre
		next = pre2 + 1
	}
	sort.Ints(realCount)
	if len(realCount) == 0 {
		return len(arr)
	}
	return realCount[0]
}

package main

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

const mod = 998244353
const mx = int64(1e9 + 7)

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n int
	Fscan(in, &n)
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		Fscan(in, &arr[i])
	}
	sum := int64(0)
	ans := n + 1
	for i := 0; i < n; i++ {
		sum += arr[i]
		cur := int64(0)
		curMax := i + 1
		prev := i
		for j := i + 1; j < n; j++ {
			cur += arr[j]
			if cur == sum {
				curMax = max(j-prev, curMax)
				cur = 0
				prev = j
			}
		}
		if cur == 0 {
			ans = min(ans, curMax)
		}
	}
	if ans != n+1 {
		Fprintln(out, ans)
		return
	}
	Fprintln(out, 1)
}

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

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

func main() {
	stdin := os.Stdin
	stdout := os.Stdout
	in := bufio.NewReader(stdin)
	out := bufio.NewWriter(stdout)
	n := 1
	Fscan(in, &n)
	for i := 0; i < n; i++ {
		solve(in, out)
	}
}

package main

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

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	scanner := bufio.NewScanner(os.Stdin)
	// mod := int64(998244353998244353)
	for a := 0; a < t; a++ {
		if scanner.Scan() {
			scanner.Text()
		}
		var s string
		var arr []int
		if scanner.Scan() {
			s = scanner.Text()
			temp := strings.Split(s, " ")
			for _, v := range temp {
				x, _ := strconv.Atoi(v)
				arr = append(arr, x)
			}
		}
		n := len(arr)
		var thickness int = math.MaxInt32
		for i := 1; i <= n; i++ { // for each length of 1st segment
			var sum int
			for j := 0; j < i; j++ {
				sum += arr[j]
			}

			// find next segment with `sum`
			var curSum, curLength int
			maxLength := i
			var possible bool = true
			for j := i; j < n; j++ {
				curSum += arr[j]
				curLength++
				if curSum == sum { // go to next segment
					curSum = 0
					if curLength > maxLength {
						maxLength = curLength
					}
					curLength = 0
				} else if curSum > sum || j == n-1 { // not possible
					possible = false
					break
				}
			}

			if possible && maxLength < thickness {
				thickness = maxLength
			}
		}

		fmt.Println(thickness)
	}
}

package main

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

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

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)
		aa := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i])
		}

		var sum int64
		z := n
		for i := 0; i < n; i++ {
			sum += aa[i]
			f := true
			mx := i + 1
			var tally int64
			start := i
			for j := i + 1; j < n; j++ {
				tally += aa[j]
				if tally > sum {
					f = false
					break
				} else if tally == sum {
					mx = max(mx, j-start)
					tally = 0
					start = j
				}
			}

			if f && tally == 0 {
				z = min(z, mx)
			}
		}

		fmt.Fprintln(out, z)
	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)

const INF = 1000000000
const N = 2e5 + 5

func main() {
	sc.Buffer(make([]byte, 500000*1+1), 500000)
	sc.Split(bufio.ScanWords)

	T := nextInt()
	for T > 0 {
		T--
		n := nextInt()
		a := make([]int, n)
		sum := 0
		for i := 0; i < n; i++ {
			a[i] = nextInt()
			sum += a[i]
		}
		f := func(m int) (bool, int) {
			if sum%m != 0 {
				return false, 0
			}
			res := 0
			for i, t, s := 0, 0, 0; i < n; i++ {
				if t+a[i] < sum/m {
					t += a[i]
				} else if t+a[i] == sum/m {
					t = 0
					res = max(res, i-s+1)
					s = i + 1
				} else {
					return false, 0
				}
			}
			return true, res
		}
		ans := INF
		for i := 1; i <= n; i++ {
			b, v := f(i)
			if b {
				ans = min(ans, v)
			}
		}
		fmt.Println(ans)
	}
}

func max(args ...int) int {
	res := args[0]
	for _, v := range args {
		if v > res {
			res = v
		}
	}
	return res
}

func min(args ...int) int {
	res := args[0]
	for _, v := range args {
		if v < res {
			res = v
		}
	}
	return res
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

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

package main

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

var a [2010]int
var n int

func calc(len, sum int, out *bufio.Writer) int {
	mx := len
// 	if len == 2 {
// 		Fprintln(out, "test ", sum)
// 	}
	for i := len + 1; i <= n; {
		cur, idx := 0, -1
		for j := i; j <= n; j++ {
			cur += a[j]
			if cur == sum {
				idx = j
				mx = max(mx, j-i+1)
				break
			} else if cur > sum {
				return n + 1
			}
		}
// 		if len == 2 {
// 		    Fprintln(out, cur, " ", idx, " ", mx)
// 		}
		if idx == -1 {
			return n + 1
		}
		i = idx + 1
	}
	return mx
}

func solve(_r io.Reader, _w io.Writer) {

	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T int
	Fscan(in, &T)
	for ; T > 0; T-- {
		Fscan(in, &n)
		res := n
		for i := 1; i <= n; i++ {
			Fscan(in, &a[i])
		}
		for i := 1; i <= n; i++ {
			sum, len := 0, i
			for j := 1; j <= i; j++ {
				sum += a[j]
			}
			res = min(res, calc(len, sum, out))
		}
		Fprintln(out, res)
	}

}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

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

func gcd(x, y int) int {
	if y == 0 {
		return x
	}
	return gcd(y, x%y)
}

func lcm(x, y int) int {
	return x / gcd(x, y) * y
}

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

package main

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

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

func c1740(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T int
	var n int
	var x int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)

		r := make([]int, n)
		s := 0
		d := map[int]int{}
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			r[i] = x
			s += x
			d[s] = i
		}

		//Fprintln(out, r)
		//Fprintln(out, d)

		ans := n
		t := 0

	loop:
		for i := 0; i < n; i++ {
			t += r[i]
			if s%t == 0 {
				mx := i+1
				l := 0
				for nx := t; nx <= s; nx += t {
					if _, ok := d[nx]; !ok {
						continue loop
					}
					if d[nx] - l  > mx {
						mx = d[nx]-l
					}
					l = d[nx]
				}
				//Fprintln(out, i, mx)
				if mx < ans {
					ans = mx
				}
			}
		}

		Fprintln(out, ans)
	}
}

package main

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

const cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	arr := make([]int, n)
	for i := range arr {
		fmt.Fscan(in, &arr[i])
	}
	curSum := 0
	ans := n
	for i := range arr {
		curSum += arr[i]
		success := true
		curAns := i + 1
		localSum := 0
		localAns := 0
		for i := i + 1; i < n; i++ {
			localSum += arr[i]
			localAns++
			if localSum > curSum {
				success = false
				break
			} else if localSum == curSum {
				localSum = 0
				curAns = max(curAns, localAns)
				localAns = 0
			}
		}
		if localSum == 0 && success {
			ans = min(ans, curAns)
		}
	}
	fmt.Fprintln(out, ans)
}

func main() {
	defer out.Flush()
	t := 1
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		solve()
	}
}

type SqrtInt interface {
	SignedSqrt | UnsignedSqrt
}

type SignedSqrt interface {
	~int | ~int32 | ~int64
}

type UnsignedSqrt interface {
	~uint | ~uint32 | ~uint64 | ~uintptr
}

func sqrt[T SqrtInt](x T) T {
	l, r := T(0), T(2000000123)
	for l < r {
		mid := (l + r) / 2
		if mid*mid > x {
			r = mid
		} else {
			l = mid + 1
		}
	}
	return l - 1
}

func ConvertInt(val string, base, toBase int) (string, error) {
	i, err := strconv.ParseInt(val, base, 64)
	if err != nil {
		return "", err
	}
	return strconv.FormatInt(i, toBase), nil
}

type set[T Ordered] struct {
	itms map[T]struct{}
}

func (s *set[T]) init() {
	if s.itms == nil {
		s.itms = make(map[T]struct{})
	}
}

func (s *set[T]) add(item T) *set[T] {
	s.init()
	s.itms[item] = exists
	return s
}

func (s *set[T]) remove(item T) *set[T] {
	s.init()
	delete(s.itms, item)
	return s
}

func (s *set[T]) has(item T) bool {
	s.init()
	_, c := s.itms[item]
	return c
}

func (s *set[T]) sortedItems() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	sort.Slice(items, func(i, j int) bool {
		return items[i] < items[j]
	})
	return items
}

func (s *set[T]) items() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	return items
}

func (s *set[T]) size() int {
	s.init()
	return len(s.itms)
}

var exists = struct{}{}

const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1

func reverseStr(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Ordered interface {
	Integer | Float | ~string
}

func abs[T Signed](x T) T {
	if x < 0 {
		return -x
	}
	return x
}

func min[T Ordered](x, y T) T {
	if x < y {
		return x
	}
	return y
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

var (
	in  = getReader()
	out = getWriter()
)

func getReader() *bufio.Reader {
	if cf {
		return bufio.NewReader(os.Stdin)
	} else {
		in, _ := os.Open("input.txt")
		return bufio.NewReader(in)
	}
}

func getWriter() *bufio.Writer {
	if cf {
		return bufio.NewWriter(os.Stdout)
	} else {
		out, _ := os.Create("output.txt")
		return bufio.NewWriter(out)
	}
}

package main

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

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

func readInt() int {
	var i int
	fmt.Fscan(reader, &i)
	return i
}

func max(u, v int) int {
	if u < v {
		u = v
	}

	return u
}

func min(u, v int) int {
	if u > v {
		u = v
	}

	return u
}

func main(){
	defer writer.Flush()
	T := readInt()

	for ;T > 0; T-- {
		n := readInt()
		a := make([]int, n)
		for i := 0; i < n; i++ {
			a[i] = readInt()
		}
		ans := n
		cur := 0
		firstMove := 1

		for ;firstMove <= n; firstMove++ {
			cur += a[firstMove - 1]
			c, cnt, l := 0, 0, firstMove
			complete := true

			for i := firstMove; i < n; i++ {
				c += a[i]
				cnt++
				if c == cur {
					l = max(l, cnt)
					cnt = 0
					c = 0
				}
				if c > cur {
					complete = false
					break
				}
			}

			complete = complete && (c == 0)
			if complete {
				ans = min(ans, l)
			}
		}
		fmt.Fprintln(writer, ans)
	}
}

package main

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

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

func solve() string {
	var n int
	fmt.Fscan(in, &n)

	var sum int
	v := make([]int, n)
	for i := range v {
		fmt.Fscan(in, &v[i])
		sum += v[i]
	}

	var res = n
	for numOfGroups := 1; numOfGroups < n; numOfGroups++ {
		if sum%numOfGroups != 0 {
			continue
		}

		target := sum / numOfGroups
		cnt, maxCnt, curSum := 0, 0, 0
		for j := 0; j < n; j++ {
			curSum += v[j]
			cnt++
			if curSum > target {
				maxCnt = n
				break
			}
			if curSum == target {
				if cnt > maxCnt {
					maxCnt = cnt
				}
				curSum = 0
				cnt = 0
			}
		}
		if maxCnt < res {
			res = maxCnt
		}
	}

	return strconv.Itoa(res)
}

func main() {
	defer out.Flush()
	var t int
	for fmt.Fscanln(in, &t); t > 0; t-- {
		fmt.Fprintln(out, solve())
	}
}

// util functions
func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

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

func makeMatrix(n, m int) [][]int {
	x := make([][]int, n)
	for i := range x {
		x[i] = make([]int, m)
	}
	return x
}

package main

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

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

func solve() string {
	var n int
	fmt.Fscan(in, &n)

	var sum int
	v := make([]int, n)
	for i := range v {
		fmt.Fscan(in, &v[i])
		sum += v[i]
	}

	var res = n
	for i := 1; i < n; i++ {
		if sum%i != 0 {
			continue
		}

		target := sum / i
		cnt, maxCnt, curSum := 0, 0, 0
		for j := 0; j < n; j++ {
			curSum += v[j]
			cnt++
			if curSum == target {
				curSum = 0
				if cnt > maxCnt {
					maxCnt = cnt
				}
				cnt = 0
			}
			if curSum > target {
				maxCnt = 2001
				break
			}
		}
		if maxCnt < res {
			res = maxCnt
		}
	}

	return strconv.Itoa(res)
}

func main() {
	defer out.Flush()
	var t int
	for fmt.Fscanln(in, &t); t > 0; t-- {
		fmt.Fprintln(out, solve())
	}
}

// util functions
func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

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

func makeMatrix(n, m int) [][]int {
	x := make([][]int, n)
	for i := range x {
		x[i] = make([]int, m)
	}
	return x
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func solve() {
	var n int
	scanf("%d\n", &n)
	a := make([]int, n)
	for i:=0; i<n; i++ {
		scanf("%d ", &a[i])
	}
	scanf("\n")

	var res int = n
	prefSum := 0
	for i:=0; i<n; i++ {
		prefSum += a[i]
		j := i+1
		pos := true
		curSum := 0
		mxLen := i+1
		prev := i
		for j<n {
			curSum += a[j]
			if curSum > prefSum {
				pos = false
				break
			}
			if curSum == prefSum {
				curLen := j - prev
				if curLen > mxLen {
					mxLen = curLen
				}
				j++
				curSum = 0
				prev = j
				continue
			}
			j++
		}

		if pos && curSum == 0 {
			lastLen := j - prev
			if lastLen > mxLen {
				mxLen = lastLen
			}
			if mxLen < res {
				res = mxLen
			}
		}
	}
	printf("%d\n", res)
}

func main() {
	defer writer.Flush()

	var T int
	scanf("%d\n", &T)
	for tt:=1; tt<=T; tt++ {
		solve()
	}
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func solve() {
	var n int
	scanf("%d\n", &n)
	a := make([]int, n)
	for i:=0; i<n; i++ {
		scanf("%d ", &a[i])
	}
	scanf("\n")

	var res int = n
	prefSum := 0
	for i:=0; i<n; i++ {
		prefSum += a[i]
		j := i+1
		pos := true
		curSum := 0
		mxLen := i+1
		prev := i
		for j<n {
			curSum += a[j]
			if curSum > prefSum {
				pos = false
				break
			}
			if curSum == prefSum {
				curLen := j - prev
				if curLen > mxLen {
					mxLen = curLen
				}
				j++
				curSum = 0
				prev = j
				continue
			}
			j++
		}

		if pos && curSum == 0 {
			if mxLen < res {
				res = mxLen
			}
		}
	}
	printf("%d\n", res)
}

func main() {
	defer writer.Flush()

	var T int
	scanf("%d\n", &T)
	for tt:=1; tt<=T; tt++ {
		solve()
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var num int
	fmt.Scan(&num)
	for i := 0; i < num; i++ {

		var num1 int
		fmt.Scan(&num1)

		var numArr []int
		for i := 0; i < num1; i++ {
			var num2 int
			fmt.Scan(&num2)
			numArr = append(numArr, num2)
		}
		var res int
		res = thri(numArr)
		fmt.Println(res)
	}

}

func thri(arr []int) int {
	pre := 0
	next := 1
	var prefixSum []int
	sum := 0
	for i := 0; i < len(arr); i++ {
		sum += arr[i]
		prefixSum = append(prefixSum, sum)
	}
	var maxSum int
	pre2 := 0
	var arrCount []int
	var realCount []int
	flag := false
	for pre < len(prefixSum) {
		maxSum = prefixSum[pre]

		for next < len(prefixSum) {
			if maxSum < prefixSum[next]-prefixSum[pre2] {
				break
			} else if maxSum > prefixSum[next]-prefixSum[pre2] {
				next++
			} else {
				if next == len(prefixSum)-1 {
					flag = true
				}
				arrCount = append(arrCount, next-pre2)
				pre2 = next
				next++
			}
		}

		if flag {
			sort.Ints(arrCount)
			realCount = append(realCount, arrCount[len(arrCount)-1])

			arrCount = nil
			flag = false
		}

		pre++
		pre2 = pre
		next = pre2 + 1
	}
	sort.Ints(realCount)
	if len(realCount) == 0 {
		return len(arr)
	}

	return realCount[0]
}