package main

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

// github.com/EndlessCheng/codeforces-go
func CF1422B(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	abs := func(x int) int {
		if x < 0 {
			return -x
		}
		return x
	}
	var T, n, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		a := make([][]int, n)
		for i := range a {
			a[i] = make([]int, m)
			for j := range a[i] {
				Fscan(in, &a[i][j])
			}
		}
		s := int64(0)
		for i := 0; 2*i < n; i++ {
			if 2*i+1 == n {
				for j, v := range a[i][:m/2] {
					s += int64(abs(v - a[i][m-1-j]))
				}
				continue
			}
			for j := 0; 2*j < m; j++ {
				b := []int{a[i][j], a[n-1-i][j]}
				if 2*j+1 == m {
					s += int64(abs(b[0] - b[1]))
					continue
				}
				b = append(b, a[i][m-1-j], a[n-1-i][m-1-j])
				sort.Ints(b)
				s += int64(b[3]-b[1]) + int64(b[2]-b[0])
			}
		}
		Fprintln(out, s)
	}
}

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

package main

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

func main() {
	t := ReadInt()
	for i := 0; i < t; i++ {
		n, m := ReadInt(), ReadInt()
		matrix := make([][]int64, n)
		for i := 0; i < n; i++ {
			matrix[i] = ReadInts64(m)
		}
		fmt.Println(solve(n, m, matrix))
	}
}

func solve(n, m int, matrix [][]int64) int64 {
	ans := int64(0)
	for i := 0; i < (n+1)/2; i++ {
		for j := 0; j < (m+1)/2; j++ {
			a, b := n-i-1, m-j-1
			if i == a && j == b {
				continue
			}
			if i == a || j == b {
				ans += Abs(matrix[i][j] - matrix[a][b])
				continue
			}

			ary := []int64{
				matrix[i][j],
				matrix[a][j],
				matrix[i][b],
				matrix[a][b],
			}
			sort.Slice(ary, func(i, j int) bool {
				return ary[i] < ary[j]
			})
			median := ary[len(ary)/2]
			for i := 0; i < len(ary); i++ {
				ans += Abs(ary[i] - median)
			}
		}
	}
	return ans
}

func Abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

var reader = bufio.NewReader(os.Stdin)

func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(reader, a...); err != nil {
		panic(err)
	}
}
func ReadString() (s string) { Scan(&s); return }
func ReadInt() (i int)       { Scan(&i); return }
func ReadInts64(n int) []int64 {
	v := make([]int64, n)
	for i := 0; i < n; i++ {
		Scan(&v[i])
	}
	return v
}

package main

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

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

func scan(a ...interface{}) (n int, err error) {
	return fmt.Fscan(in, a...)
}

var err error

func next() (n string) {
	scan(&n)
	return n
}

func nextInt() (n int) {
	scan(&n)
	return n
}

func nextLong() (n int64) {
	scan(&n)
	return n
}

func nextDouble() (n float64) {
	scan(&n)
	return n
}

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

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

func solve() {
	n, m := nextInt(), nextInt()
	arr := make([][]int, n)
	for i := range arr {
		arr[i] = make([]int, m)
		for j := range arr[i] {
			arr[i][j] = nextInt()
		}
	}
	var res int64
	for i := 0; i < n/2; i++ {
		for j := 0; j < m/2; j++ {
			nums := []int{arr[i][j], arr[n-i-1][j], arr[n-i-1][m-j-1], arr[i][m-j-1]}
			sort.Ints(nums)
			res += int64(nums[1] - nums[0] + nums[2] - nums[1] + nums[3] - nums[1])
		}
	}
	if n%2 == 1 {
		for i := 0; i < m/2; i++ {
			res += int64(Abs(arr[n/2][i] - arr[n/2][m-i-1]))
		}
	}

	if m%2 == 1 {
		for i := 0; i < n/2; i++ {
			res += int64(Abs(arr[i][m/2] - arr[n-i-1][m/2]))
		}
	}

	printf("%d\n", res)
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	name := "in"
	file1, err := os.Open(name + ".in")
	if err == nil {
		in = bufio.NewReaderSize(file1, 8192)
	}
	// file2, err := os.Create(name + ".out")
	// if err != nil {
	// 	log.Fatal(err)
	// }
	// out = bufio.NewWriterSize(file2, 8192)
	defer out.Flush()
	// start := time.Now()

	for t, i := nextInt(), 1; t > 0; t, i = t-1, i+1 {
		// printf("Case #%d:\n", i)
		solve()
	}

	// printf("%v\n", time.Now().Sub(start))
}

package main

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

var reader = bufio.NewReader(os.Stdin)

func main() {
	var t int
	fmt.Fscan(reader, &t)

	for i := 0; i < t; i++ {
		var n, m int
		fmt.Fscan(reader, &n, &m)

		matrix := make([][]int, n)
		used := make([][]bool, n)

		for j := 0; j < n; j++ {
			for k := 0; k < m; k++ {
				var x int
				fmt.Fscan(reader, &x)
				matrix[j] = append(matrix[j], x)
				used[j] = append(used[j], false)
			}
		}

		var cost int64 = 0
		for j := 0; j < n; j++ {
			for k := 0; k < m; k++ {
				if used[j][k] {
					continue
				}

				parts := []int{}
				xs := []int{j, j, n - j - 1, n - j - 1}
				ys := []int{k, m - k - 1, k, m - k - 1}

				for l := 0; l < 4; l++ {
					if used[xs[l]][ys[l]] {
						continue
					}

					parts = append(parts, matrix[xs[l]][ys[l]])
					used[xs[l]][ys[l]] = true
				}

				sort.Ints(parts)
				median := parts[len(parts)/2]
				for _, p := range parts {
					cost += int64(abs(p - median))
				}

				// fmt.Println(parts)
			}
		}

		fmt.Println(cost)
	}
}

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

	return a
}

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

	return b
}

func second_largest(a, b, c, d int) int {
	arr := []int{a, b, c, d}

	sort.Ints(arr)

	return arr[1]
}

package main

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

func main() {
	t := ReadInt()
	for i := 0; i < t; i++ {
		n, m := ReadInt(), ReadInt()
		matrix := make([][]int, n)
		for i := 0; i < n; i++ {
			matrix[i] = ReadInts(m)
		}
		fmt.Println(solve(n, m, matrix))
	}
}

func solve(n, m int, matrix [][]int) int {
	ans := 0
	for i := 0; i < (n+1)/2; i++ {
		for j := 0; j < (m+1)/2; j++ {
			a, b := n-i-1, m-j-1
			if i == a && j == b {
				continue
			}
			if i == a || j == b {
				ans += Abs(matrix[i][j] - matrix[a][b])
				continue
			}

			ary := []int{
				matrix[i][j],
				matrix[a][j],
				matrix[i][b],
				matrix[a][b],
			}
			sort.Ints(ary)
			median := ary[len(ary)/2]
			for i := 0; i < len(ary); i++ {
				ans += Abs(ary[i] - median)
			}
		}
	}
	return ans
}

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

var reader = bufio.NewReader(os.Stdin)

func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(reader, a...); err != nil {
		panic(err)
	}
}
func ReadString() (s string) { Scan(&s); return }
func ReadInt() (i int)       { Scan(&i); return }
func ReadInts(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		Scan(&v[i])
	}
	return v
}

package main

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

func main() {
	t := ReadInt()
	for i := 0; i < t; i++ {
		n, m := ReadInt(), ReadInt()
		matrix := make([][]int, n)
		for i := 0; i < n; i++ {
			matrix[i] = ReadInts(m)
		}
		fmt.Println(solve(n, m, matrix))
	}
}

func solve(n, m int, matrix [][]int) int {
	ans := 0
	for i := 0; i < (n+1)/2; i++ {
		for j := 0; j < (m+1)/2; j++ {
			a, b := n-i-1, m-j-1
			ary := []int{
				matrix[i][j],
				matrix[a][j],
				matrix[i][b],
				matrix[a][b],
			}
			sort.Ints(ary)
			median := ary[len(ary)/2]
			c := 0
			for i := 0; i < len(ary); i++ {
				c += Abs(ary[i] - median)
			}
			if i == a || j == b {
				c /= 2
			}
			ans += c
		}
	}
	return ans
}

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

var reader = bufio.NewReader(os.Stdin)

func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(reader, a...); err != nil {
		panic(err)
	}
}
func ReadString() (s string) { Scan(&s); return }
func ReadInt() (i int)       { Scan(&i); return }
func ReadInts(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		Scan(&v[i])
	}
	return v
}

package main

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

var reader = bufio.NewReader(os.Stdin)

func main() {
	var t int
	fmt.Fscan(reader, &t)

	for i := 0; i < t; i++ {
		var n, m int
		fmt.Fscan(reader, &n, &m)

		matrix := make([][]int, n)

		for j := 0; j < n; j++ {
			for k := 0; k < m; k++ {
				var x int
				fmt.Fscan(reader, &x)

				matrix[j] = append(matrix[j], x)
			}
		}

		moves := 0
		for j := 0; j < (n+1)/2; j++ {
			for k := 0; k < (m+1)/2; k++ {
				curr := matrix[j][k]
				right := matrix[j][m-k-1]
				down := matrix[n-j-1][k]
				right_down := matrix[n-j-1][m-k-1]

				val := second_largest(curr, right, down, right_down)

				if (n - j - 1) == j {
					moves += abs(val-curr) + abs(val-right)
				} else {
					moves += abs(val-curr) + abs(val-right) + abs(val-down) + abs(val-right_down)
				}
			}
		}

		fmt.Println(moves)
	}
}

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

	return a
}

func second_largest(a, b, c, d int) int {
	arr := []int{a, b, c, d}

	sort.Ints(arr)

	fmt.Println(arr)

	return arr[2]
}

package main

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

var reader = bufio.NewReader(os.Stdin)

func main() {
	var t int
	fmt.Fscan(reader, &t)

	for i := 0; i < t; i++ {
		var n, m int
		fmt.Fscan(reader, &n, &m)

		matrix := make([][]int, n)

		for j := 0; j < n; j++ {
			for k := 0; k < m; k++ {
				var x int
				fmt.Fscan(reader, &x)

				matrix[j] = append(matrix[j], x)
			}
		}

		moves := 0
		for j := 0; j < (n+1)/2; j++ {
			for k := 0; k < (m+1)/2; k++ {
				curr := matrix[j][k]
				right := matrix[j][m-k-1]
				down := matrix[n-j-1][k]
				right_down := matrix[n-j-1][m-k-1]

				val := second_largest(curr, right, down, right_down)

				if (n - j - 1) == j {
					moves += abs(val-curr) + abs(val-right)
				} else {
					moves += abs(val-curr) + abs(val-right) + abs(val-down) + abs(val-right_down)
				}
			}
		}

		fmt.Println(moves)
	}
}

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

	return a
}

func second_largest(a, b, c, d int) int {
	arr := []int{a, b, c, d}

	sort.Ints(arr)

	return arr[2]
}

package main

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

var reader = bufio.NewReader(os.Stdin)

func main() {
	var t int
	fmt.Fscan(reader, &t)

	for i := 0; i < t; i++ {
		var n, m int
		fmt.Fscan(reader, &n, &m)

		matrix := make([][]int, n)

		for j := 0; j < n; j++ {
			for k := 0; k < m; k++ {
				var x int
				fmt.Fscan(reader, &x)
				matrix[j] = append(matrix[j], x)
			}
		}

		var cost int64 = 0

		for j := 0; j < (n+1)/2; j++ {
			for k := 0; k < (m+1)/2; k++ {
				curr := matrix[j][k]
				right := matrix[j][m-k-1]
				down := matrix[n-j-1][k]
				right_down := matrix[n-j-1][m-k-1]

				val := second_largest(curr, right, down, right_down)

				if (n - j - 1) == j {
					cost += int64(abs(val-curr) + abs(val-right))
				} else {
					cost += int64(abs(val-curr) + abs(val-right) + abs(val-down) + abs(val-right_down))
				}
			}
		}

		fmt.Println(cost)
	}
}

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

	return a
}

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

	return b
}

func second_largest(a, b, c, d int) int {
	arr := []int{a, b, c, d}

	sort.Ints(arr)

	return arr[1]
}

package main

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

var reader = bufio.NewReader(os.Stdin)

func main() {
	var t int
	fmt.Fscan(reader, &t)

	for i := 0; i < t; i++ {
		var n, m int
		fmt.Fscan(reader, &n, &m)

		matrix := make([][]int, n)

		for j := 0; j < n; j++ {
			for k := 0; k < m; k++ {
				var x int
				fmt.Fscan(reader, &x)

				matrix[j] = append(matrix[j], x)
			}
		}

		moves := 0
		for j := 0; j < (n+1)/2; j++ {
			for k := 0; k < (m+1)/2; k++ {
				curr := matrix[j][k]
				right := matrix[j][m-k-1]
				down := matrix[n-j-1][k]
				right_down := matrix[n-j-1][m-k-1]

				val := second_largest(curr, right, down, right_down)

				if (n - j - 1) == j {
					moves += abs(val-curr) + abs(val-right)
				} else {
					moves += abs(val-curr) + abs(val-right) + abs(val-down) + abs(val-right_down)
				}
			}
		}

		fmt.Println(moves)
	}
}

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

	return a
}

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

	return b
}

func second_largest(a, b, c, d int) int {
	arr := []int{a, b, c, d}

	sort.Ints(arr)

	return arr[1]
}

package main

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

var reader = bufio.NewReader(os.Stdin)

func main() {
	var t int
	fmt.Fscan(reader, &t)

	for i := 0; i < t; i++ {
		var n, m int
		fmt.Fscan(reader, &n, &m)

		matrix := make([][]int, n)

		for j := 0; j < n; j++ {
			for k := 0; k < m; k++ {
				var x int
				fmt.Fscan(reader, &x)

				matrix[j] = append(matrix[j], x)
			}
		}

		moves := 0
		for j := 0; j < (n+1)/2; j++ {
			for k := 0; k < (m+1)/2; k++ {
				if (n - j - 1) == j {
					curr := matrix[j][k]
					right := matrix[j][m-k-1]

					val := min(curr, right)

					moves += abs(val-curr) + abs(val-right)
				} else {
					curr := matrix[j][k]
					right := matrix[j][m-k-1]
					down := matrix[n-j-1][k]
					right_down := matrix[n-j-1][m-k-1]

					val := second_largest(curr, right, down, right_down)

					moves += abs(val-curr) + abs(val-right) + abs(val-down) + abs(val-right_down)
				}
			}
		}

		fmt.Println(moves)
	}
}

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

	return a
}

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

	return b
}

func second_largest(a, b, c, d int) int {
	arr := []int{a, b, c, d}

	sort.Ints(arr)

	return arr[2]
}

package main

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

var reader = bufio.NewReader(os.Stdin)

func main() {
	var t int
	fmt.Fscan(reader, &t)

	for i := 0; i < t; i++ {
		var n, m int
		fmt.Fscan(reader, &n, &m)

		matrix := make([][]int, n)

		for j := 0; j < n; j++ {
			for k := 0; k < m; k++ {
				var x int
				fmt.Fscan(reader, &x)
				matrix[j] = append(matrix[j], x)
			}
		}

		moves := 0
		for j := 0; j < n/2; j++ {
			for k := 0; k < m/2; k++ {
				curr := matrix[j][k]
				right := matrix[j][m-k-1]
				down := matrix[n-j-1][k]
				right_down := matrix[n-j-1][m-k-1]

				val := second_largest(curr, right, down, right_down)

				moves += abs(val-curr) + abs(val-right) + abs(val-down) + abs(val-right_down)
			}
		}

		if n%2 == 1 {
			row := n / 2
			for j := 0; j < m/2; j++ {
				moves += abs(matrix[row][j] - matrix[row][m-j-1])
			}
		}

		fmt.Println(moves)
	}
}

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

	return a
}

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

	return b
}

func second_largest(a, b, c, d int) int {
	arr := []int{a, b, c, d}

	sort.Ints(arr)

	return arr[1]
}

package main

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

var reader = bufio.NewReader(os.Stdin)

func main() {
	var t int
	fmt.Fscan(reader, &t)

	for i := 0; i < t; i++ {
		var n, m int
		fmt.Fscan(reader, &n, &m)

		matrix := make([][]int, n)
		used := make([][]bool, n)

		for j := 0; j < n; j++ {
			for k := 0; k < m; k++ {
				var x int
				fmt.Fscan(reader, &x)
				matrix[j] = append(matrix[j], x)
				used[j] = append(used[j], false)
			}
		}

		cost := 0
		for j := 0; j < n; j++ {
			for k := 0; k < m; k++ {
				if used[j][k] {
					continue
				}

				parts := []int{}
				xs := []int{j, j, n - j - 1, n - j - 1}
				ys := []int{k, m - k - 1, k, m - k - 1}

				for l := 0; l < 4; l++ {
					if used[xs[l]][ys[l]] {
						continue
					}

					parts = append(parts, matrix[xs[l]][ys[l]])
					used[xs[l]][ys[l]] = true
				}

				sort.Ints(parts)
				median := parts[len(parts)/2]

				for _, p := range parts {
					cost += abs(p - median)
				}

				// fmt.Println(parts)
			}
		}

		fmt.Println(cost)
	}
}

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

	return a
}

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

	return b
}

func second_largest(a, b, c, d int) int {
	arr := []int{a, b, c, d}

	sort.Ints(arr)

	return arr[1]
}

package main

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

var reader = bufio.NewReader(os.Stdin)

func main() {
	var t int
	fmt.Fscan(reader, &t)

	for i := 0; i < t; i++ {
		var n, m int
		fmt.Fscan(reader, &n, &m)

		matrix := make([][]int, n)

		for j := 0; j < n; j++ {
			for k := 0; k < m; k++ {
				var x int
				fmt.Fscan(reader, &x)
				matrix[j] = append(matrix[j], x)
			}
		}

		var cost int64 = 0

		for j := 0; j < n/2; j++ {
			for k := 0; k < m/2; k++ {
				curr := matrix[j][k]
				right := matrix[j][m-k-1]
				down := matrix[n-j-1][k]
				right_down := matrix[n-j-1][m-k-1]

				val := second_largest(curr, right, down, right_down)

				cost += int64(abs(val-curr) + abs(val-right) + abs(val-down) + abs(val-right_down))
			}
		}

		if n%2 == 1 {
			row := n / 2
			for j := 0; j < m/2; j++ {
				cost += int64(abs(matrix[row][j] - matrix[row][m-j-1]))
			}
		}

		fmt.Println(cost)
	}
}

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

	return a
}

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

	return b
}

func second_largest(a, b, c, d int) int {
	arr := []int{a, b, c, d}

	sort.Ints(arr)

	return arr[1]
}


package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
)
const (
	MAXN=int(1e5+7)
)
func main(){
	input,err:=ioutil.ReadAll(os.Stdin)
	if err!=nil{
		return
	}
	inputReader:=bytes.NewReader(input)
	var N int
	_,_=fmt.Fscanf(inputReader,"%d\n",&N)
	du:=make([]int,MAXN,MAXN)
	for i:=1;i<N;i++ {
		var x,y int
		_,_=fmt.Fscanf(inputReader,"%d %d\n",&x,&y)
		//fmt.Println(x,y)
		du[x]++
		du[y]++
	}
	hasAns:=true
	for i:=1;i<=N;i++{
		if du[i]==2{
			hasAns=false
		}
	}
	if hasAns{
		fmt.Println("YES")
	}else{
		fmt.Println("NO")
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n := 0
	fmt.Fscanf(in, "%d\n", &n)
	//	fmt.Println(n)
	edges := make([]int, n)
	for i := 0; i < n-1; i++ {
		a, b := 0, 0
		fmt.Fscanf(in, "%d %d\n", &a, &b)
		a--
		b--
		edges[a]++
		edges[b]++
	}
	ok := true
	for i := 0; i < n; i++ {
		if edges[i] == 2 {
			ok = false
		}
	}
	if ok {
		fmt.Fprintf(out, "YES\n")
		return
	}
	fmt.Fprintf(out, "NO\n")
	return
}
func minstep(a, b byte) int {
	return min(int(a-b+26)%26, int(b-a+26)%26)
}

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

package main

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

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

    var n int
    fmt.Fscan(reader, &n)
    deg := make([]int, n)
    for i := 1; i < n; i++ {
        var x,y int
        fmt.Fscan(reader, &x, &y)
        deg[x - 1]++
        deg[y - 1]++
    }
    ans := true
    for i := 0; i < n; i++ {
        if deg[i] == 2 {
            ans = false
        }
    }
    if ans {
        fmt.Println("YES")
    } else {
        fmt.Println("NO")
    }
}

package main

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

func main() {
	//var read, _ = os.Open("dat.txt")
	//var write, _ = os.Create("result.txt")
	var read = bufio.NewReader(os.Stdin)
	var write = bufio.NewWriter(os.Stdout)
	defer write.Flush()
	var n int
	fmt.Fscan(read, &n)
	var a = make([]int, n)
	for i := 0; i < n-1; i++ {
		var u, v int
		fmt.Fscan(read, &u, &v)
		u--
		v--
		a[u]++
		a[v]++
	}
	var res = "YES"
	for i := 0; i < n; i++ {
		if a[i] == 2 {
			res = "NO"
			break
		}
	}
	fmt.Fprint(write, res)
	//read.Close()
	//write.Close()
}

package main

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

func main() {
	//var read, _ = os.Open("dat.txt")
	//var write, _ = os.Create("result.txt")
	var read = bufio.NewReader(os.Stdin)
	var write = bufio.NewWriter(os.Stdout)
	defer write.Flush()
	var n int
	fmt.Fscan(read, &n)
	var a = make([]int, n)
	for i := 0; i < n-1; i++ {
		var u, v int
		fmt.Fscan(read, &u, &v)
		u = u - 1
		v = v - 1
		a[u] = a[u] + 1
		a[v] = a[v] + 1
	}
	var res = "YES"
	for i := 0; i < n-1; i++ {
		if a[i] == 2 {
			res = "NO"
			break
		}
	}
	fmt.Fprint(write, res)
	//read.Close()
	//write.Close()
}


package main

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

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

func p(a ...interface{})                { fmt.Fprint(writer, a...) }
func pln(a ...interface{})              { fmt.Fprintln(writer, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func scan(a ...interface{})            { fmt.Fscan(reader, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func main() {
	defer writer.Flush()

	var n, a int
	left, right := 0, 0
	scan(&n)
	for i := 1; i <= n; i++ {
		scan(&a)
		if a == 0 {
			left = i
		} else if a == 1 {
			right = i
		}
	}

	if left < right {
		pln(left)
	} else {
		pln(right)
	}
	return
}

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 main() {
	//STDOUT MUST BE FLUSHED MANUALLY
	defer writer.Flush()
	var n int
	scanf("%d\n", &n)
	doors := make([]int, n)
	for i := range doors {
		scanf("%d ", &doors[i])
	}
	var zeros, ones int
	for _, door := range doors {
		if door == 0 {
			zeros++
		} else {
			ones++
		}
	}
	var cnt int
	for i, door := range doors {
		if door == 0 {
			zeros--
		} else {
			ones--
		}
		if zeros == 0 || ones == 0 {
			cnt = i + 1
			break
		}
	}
	printf("%d\n", cnt)
}

// A. The Doors
/*
Three years have passes and nothing changed. It is still raining in London, and Mr. Black has to close all the doors in his home in order to not be flooded. Once, however, Mr. Black became so nervous that he opened one door, then another, then one more and so on until he opened all the doors in his house.

There are exactly two exits from Mr. Black's house, let's name them left and right exits. There are several doors in each of the exits, so each door in Mr. Black's house is located either in the left or in the right exit. You know where each door is located. Initially all the doors are closed. Mr. Black can exit the house if and only if all doors in at least one of the exits is open. You are given a sequence in which Mr. Black opened the doors, please find the smallest index 𝑘 such that Mr. Black can exit the house after opening the first 𝑘 doors.

We have to note that Mr. Black opened each door at most once, and in the end all doors became open.

Input
The first line contains integer 𝑛 (2≤𝑛≤200000) — the number of doors.

The next line contains 𝑛 integers: the sequence in which Mr. Black opened the doors. The 𝑖-th of these integers is equal to 0 in case the 𝑖-th opened door is located in the left exit, and it is equal to 1 in case it is in the right exit.

It is guaranteed that there is at least one door located in the left exit and there is at least one door located in the right exit.

Output
Print the smallest integer 𝑘 such that after Mr. Black opened the first 𝑘 doors, he was able to exit the house.

Examples
Input
5
0 0 1 0 0

Output
3

Input
4
1 0 0 1

Output
3

Note
In the first example the first two doors are from the left exit, so when Mr. Black opened both of them only, there were two more closed door in the left exit and one closed door in the right exit. So Mr. Black wasn't able to exit at that moment.

When he opened the third door, all doors from the right exit became open, so Mr. Black was able to exit the house.

In the second example when the first two doors were opened, there was open closed door in each of the exit.

With three doors opened Mr. Black was able to use the left exit.
*/
package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	input1, _ := reader.ReadString('\n')
	n, _ := strconv.ParseInt(strings.TrimSpace(input1), 10, 64)

	input2, _ := reader.ReadString('\n')
	arr := strings.Split(strings.TrimSpace(input2), " ")

	var i, z, o int64
	for ; i < n; i++ {
		v, _ := strconv.ParseInt(arr[i], 10, 64)
		if v == 0 {
			z = i + 1
		} else {
			o = i + 1
		}
	}

	if z < o {
		fmt.Print(z)
	} else {
		fmt.Print(o)
	}
}

package main

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

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

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

	mat := make([]int,n)
	for i:=0;i < n;i++{
		fmt.Fscanf(buf, "%d", &mat[i])
	}
	fmt.Fscanf(buf, "\n")
	for i:= n-2;i >=0;i --{
		if mat[i]!=mat[i+1]{
			fmt.Printf("%d\n",i+1)
			return
		}
	}

}

	package main

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

	func sortStringWithBuble(s string) string  {
		b := []byte(s)
		for i := 0 ;i  < len(s);i++{
			for j := i + 1 ;j < len(s)  ;j++{
				if(b[i] > b[j]){
					temp := b[j];
					b[j] = b[i]
					b[i] = temp
				}
			}
		}
		rt := string(b)
		return rt
	}
	func min1(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 n int
		var x int
		fmt.Fscanf(in,"%d\n",&n)
		l,r := 0,0
		for i := 0 ; i < n;i++{
			fmt.Fscanf(in,"%d",&x)
			if(x == 0){
				l = i + 1
			}else{
				r = i + 1
			}
		}
		fmt.Fprintf(out,"%d\n",min1(l,r))

	}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n := 0
	fmt.Fscanf(in, "%d\n", &n)
	ans := 0
	a := make([]int, n)
	last0 := -1
	last1 := -1
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &a[i])
		if a[i] == 0 {
			last0 = i
		} else {
			last1 = i
		}
	}
	if last0 < last1 {
		ans = last0
	} else {
		ans = last1
	}
	fmt.Fprintf(out, "%d\n", ans+1)
	return
}

package main

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

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

	var n int
	fmt.Fscanln(in, &n)

	lastLeft := 0
	lastRight := 0
	for i := 1; i <= n; i++ {
		var door rune
		fmt.Fscan(in, &door)

		if door == 0 {
			lastLeft = i
		} else if door == 1 {
			lastRight = i
		} else {
			fmt.Println("hell", door)
		}
	}

	if lastLeft < lastRight {
		fmt.Println(lastLeft)
	} else {
		fmt.Println(lastRight)
	}
}

// Author: sighduck
// URL: https://codeforces.com/contest/1143/problem/A

package main

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

func Solve(n int, doors []int) int {
	var zeros, ones int
	for i, elem := range doors {
		if elem == 0 {
			zeros = i + 1
		} else if elem == 1 {
			ones = i + 1
		}
	}
	if zeros < ones {
		return zeros
	}
	return ones
}

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

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

	fmt.Fprintf(writer, "%d\n", Solve(n, doors))
}


package main

import "bufio"
import "os"
import "io/ioutil"
import "fmt"

func changeHour(cat int, hour int) int {
	if cat == 12 {
		if (hour >= 1) && (hour <= 12) {
			return hour
		}
		if hour%10 == 0 {
			return 10
		}
		return hour % 10
	}
	if hour <= 23 {
		return hour
	}
	return hour % 10
}

func changeMinute(min int) int {
	if min <= 59 {
		return min
	}
	return min % 10
}

func main() {
	var cat, hour, min int
	readInt(&cat)
	readInt(&hour)
	readInt(&min)

	h := changeHour(cat, hour)
	m := changeMinute(min)
	fmt.Printf("%02d:%02d\n", h, m)
}

var out *bufio.Writer
var iPointer int
var nPointer int
var iBuffer []byte

func init() {
	iPointer = 0
	iBuffer, _ = ioutil.ReadAll(os.Stdin)
	nPointer = len(iBuffer)
}

func isDigit(c byte) bool {
	return (c >= '0') && (c <= '9')
}

func readInt(v *int) bool {
	neg := false
	for (iPointer < nPointer) && !isDigit(iBuffer[iPointer]) {
		neg = (iBuffer[iPointer] == '-')
		iPointer++
	}
	if iPointer >= nPointer {
		return false
	}
	var a int
	for (iPointer < nPointer) && isDigit(iBuffer[iPointer]) {
		a = a*10 + int(iBuffer[iPointer]-'0')
		iPointer++
	}
	if neg {
		*v = -a
	} else {
		*v = a
	}
	return true
}

// http://codeforces.com/contest/722/problem/0
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

const (
	MOD = 1e9 + 7
)

type (

)

func main () {
	var (
		N int
		str string
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &N)
	fmt.Fscanln(input, &str)

	ans := []rune(str)
	if N == 12 {
		if ans[0] > '1' {
			if ans[1] == '0' {
				ans[0] = '1'
			} else {
				ans[0] = '0'
			}
		}

		if ans[0] == '1' && ans[1] > '2' {
			ans[1] = '0'
		}

		if ans[0] == '0' && ans[1] == '0' {
			ans[1] = '1'
		}
	} else {
		if ans[1] > '9' || ans[1] < '0' {
			ans[1] = '0'
		}

		if ans[0] > '2' || ans[0] < '0' {
			ans[0] = '0'
		}

		if ans[0] == '2' && ans[1] > '3' {
			ans[1] = '0'
		}
	}

	if ans[3] > '5' || ans[3] < '0' {
		ans[3] = '0'
	}

	fmt.Println(string(ans))
}

package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main() {
	var time_format int
	var time string
	fmt.Scanf("%d\n%s", &time_format, &time)

	new_time := []byte(time)
	time_list := strings.Split(time, ":")
	hours, _ := strconv.Atoi(time_list[0])
	minutes, _ := strconv.Atoi(time_list[1])
	if minutes > 59 {
		new_time[3] = '1'
	}
	if time_format == 12 {
		if hours < 1 {
			new_time[1] = '1'
		} else if hours > 12 {
			if time[1] != '0' {
				new_time[0] = '0'
			} else {
				new_time[0] = '1'
			}
		}
	} else if time_format == 24 && hours > 23 {
		new_time[0] = '0'
	}
	fmt.Println(string(new_time))
}

package main

import (
        "fmt"
        "strconv"
        "strings"
)

func main() {
        var format int
        fmt.Scanln(&format)
        var str string
        fmt.Scanln(&str)
        t := strings.Split(str, ":")
        aStr, bStr := t[0], t[1]
        a, _ := strconv.Atoi(aStr)
        b, _ := strconv.Atoi(bStr)
        if format == 12 {
                if a > 12 || a == 0 {
                        a %= 10
                        if a == 0 {
                                aStr = "10"
                        } else {
                                aStr = fmt.Sprintf("0%v", a)
                        }
                }
        } else if format == 24 {
                if a > 23 {
                        a %= 10
                        aStr = fmt.Sprintf("0%v", a)
                }
        }
        if b > 59 {
                b %= 10
                bStr = fmt.Sprintf("0%v", b)
        }
        fmt.Println(aStr + ":" + bStr)
}

package main

import (
	"fmt"
)

func main() {
	var format, hour, minute int
	fmt.Scanf("%d\n", &format)
	fmt.Scanf("%d:%d", &hour, &minute)
	if minute > 59 {
		minute = 10 + minute%10
	}
	if format == 24 {
		if hour > 23 {
			hour = hour % 10
		}
	}
	if format == 12 {
		if hour/10 > 1 {
			if hour%10 == 0 {
				hour = 10 + hour%10
			} else {
				hour = hour % 10
			}
		}
		if hour > 12 {
			hour = hour % 10
		}
		if hour == 0 {
			hour = 10
		}
	}
	fmt.Printf("%02d:%02d\n", hour, minute)
}

package main

import "bufio"
import "os"
import "io/ioutil"
import "fmt"

func changeHour(cat int, hour int) int {
	if cat == 12 {
		if (hour >= 1) && (hour <= 12) {
			return hour
		}
		hour = hour % 10
		if hour == 0 {
			hour++
		}
		return hour
	}
	if hour <= 23 {
		return hour
	}
	return hour % 10
}

func changeMinute(min int) int {
	if min <= 59 {
		return min
	}
	return min % 10
}

func main() {
	var cat, hour, min int
	readInt(&cat)
	readInt(&hour)
	readInt(&min)

	h := changeHour(cat, hour)
	m := changeMinute(min)
	fmt.Printf("%02d:%02d\n", h, m)
}

var out *bufio.Writer
var iPointer int
var nPointer int
var iBuffer []byte

func init() {
	iPointer = 0
	iBuffer, _ = ioutil.ReadAll(os.Stdin)
	nPointer = len(iBuffer)
}

func isDigit(c byte) bool {
	return (c >= '0') && (c <= '9')
}

func readInt(v *int) bool {
	neg := false
	for (iPointer < nPointer) && !isDigit(iBuffer[iPointer]) {
		neg = (iBuffer[iPointer] == '-')
		iPointer++
	}
	if iPointer >= nPointer {
		return false
	}
	var a int
	for (iPointer < nPointer) && isDigit(iBuffer[iPointer]) {
		a = a*10 + int(iBuffer[iPointer]-'0')
		iPointer++
	}
	if neg {
		*v = -a
	} else {
		*v = a
	}
	return true
}

// http://codeforces.com/contest/722/problem/0
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

const (
	MOD = 1e9 + 7
)

type (

)

func main () {
	var (
		N int
		str string
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &N)
	fmt.Fscanln(input, &str)

	ans := []rune(str)
	if N == 12 {
		if ans[0] > '1' || ans[0] < '0' {
			ans[0] = '0'
		}

		if ans[1] > '9' || ans[1] < '0' {
			ans[1] = '0'
		}

		if ans[0] == '1' && ans[1] > '2' {
			ans[1] = '0'
		}

		if ans[0] == '0' && ans[1] == '0' {
			ans[1] = '1'
		}
	} else {
		if ans[1] > '9' || ans[1] < '0' {
			ans[1] = '0'
		}

		if ans[0] > '2' || ans[0] < '0' {
			ans[0] = '0'
		}

		if ans[0] == '2' && ans[1] > '3' {
			ans[1] = '0'
		}
	}

	if ans[3] > '5' || ans[3] < '0' {
		ans[3] = '0'
	}

	fmt.Println(string(ans))
}

// http://codeforces.com/contest/722/problem/0
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

const (
	MOD = 1e9 + 7
)

type (

)

func main () {
	var (
		N int
		str string
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &N)
	fmt.Fscanln(input, &str)

	ans := []rune(str)
	if N == 12 {
		if ans[0] > '1' || ans[0] < '0' {
			ans[0] = '0'
		}

		if ans[0] == '1' && ans[1] > '2' {
			ans[1] = '0'
		}

		if ans[3] > '5'  {
			ans[3] = '0'
		}

	} else {
		if ans[0] > '2' || ans[0] < '0' {
			ans[0] = '0'
		}

		if ans[0] == '2' && ans[1] > '4' {
			ans[1] = '0'
		}

		if ans[3] > '5'  {
			ans[3] = '0'
		}
	}

	fmt.Println(string(ans))
}

package main

import (
	"fmt"
)

func main() {
	var format, hour, minute int
	fmt.Scanf("%d\n", &format)
	fmt.Scanf("%d:%d", &hour, &minute)
	if minute > 59 {
		minute = 10 + minute%10
	}
	if format == 24 {
		if hour > 23 {
			hour = hour % 10
		}
	}
	if format == 12 {
		if hour > 12 {
			hour = hour % 10
		}
	}
	fmt.Printf("%02d:%d\n", hour, minute)
}

package main

import (
	"fmt"
)

func main() {
	var format, hour, minute int
	fmt.Scanf("%d\n", &format)
	fmt.Scanf("%d:%d", &hour, &minute)
	if minute > 59 {
		minute = 10 + minute%10
	}
	if format == 24 {
		if hour > 23 {
			hour = hour % 10
		}
	}
	if format == 12 {
		if hour > 12 {
			hour = hour % 10
		}
	}
	fmt.Printf("%02d:%02d\n", hour, minute)
}

package main

import (
	"fmt"
)

func main() {
	var format, hour, minute int
	fmt.Scanf("%d\n", &format)
	fmt.Scanf("%d:%d", &hour, &minute)
	if minute > 59 {
		minute = 10 + minute%10
	}
	if format == 24 {
		if hour > 23 {
			hour = hour % 10
		}
	}
	if format == 12 {
		if hour > 12 {
			hour = hour % 10
		}
		if hour == 0 {
			hour = 1
		}
	}
	fmt.Printf("%02d:%02d\n", hour, minute)
}

package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main() {
	var time_format int
	var time string
	fmt.Scanf("%d\n%s", &time_format, &time)

	new_time := []byte(time)
	time_list := strings.Split(time, ":")
	hours, _ := strconv.Atoi(time_list[0])
	minutes, _ := strconv.Atoi(time_list[1])
	if minutes > 59 {
		new_time[3] = '1'
	}
	if (time_format == 12 && hours > 12) || (time_format == 24 && hours > 23) {
		new_time[0] = '0'
	}
	fmt.Println(string(new_time))
}

package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main() {
	var time_format int
	var time string
	fmt.Scanf("%d\n%s", &time_format, &time)

	new_time := []byte(time)
	time_list := strings.Split(time, ":")
	hours, _ := strconv.Atoi(time_list[0])
	minutes, _ := strconv.Atoi(time_list[1])
	if minutes > 59 {
		new_time[3] = '1'
	}
	if time_format == 12 && hours < 1 {
		new_time[1] = '1'
	} else if (time_format == 12 && (hours > 12 || hours < 1)) || (time_format == 24 && hours > 23) {
		new_time[0] = '0'
	}
	fmt.Println(string(new_time))
}


package main

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

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

func main() {
	defer out.Flush()
	inn.Buffer([]byte{}, 10000000)
	inn.Split(bufio.ScanWords)
	//prep
	n, s, t := rd(), rds(), rds()
	var sum int
	a := make([]int, n)
	for i := range a {
		if s[i] != t[i] {
			if s[i] == '1' {
				a[i] = 1
			} else {
				a[i] = -1
			}
		}
		sum += a[i]
	}
	if sum != 0 {
		Fprint(out, -1)
		return
	}
	cal := func(x int) int {
		var cur, mx int
		for _, it := range a {
			cur = max(cur+it*x, it*x)
			mx = max(mx, cur)
		}
		return mx
	}
	Fprint(out, max(cal(1), cal(-1)))
}

//-------------------template------------------
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func maxll(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func minll(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func rd() (x int) {
	inn.Scan()
	x, _ = strconv.Atoi(string(inn.Bytes()))
	return x
}
func rdll() (x int64) {
	inn.Scan()
	x, _ = strconv.ParseInt(string(inn.Bytes()), 10, 64)
	return x
}
func rds() string {
	inn.Scan()
	return string(inn.Bytes())
}

package main

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

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

	n := readNum(reader)
	s, _ := reader.ReadString('\n')
	t, _ := reader.ReadString('\n')
	fmt.Println(solve(n, s, t))
}

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

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

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

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

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

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

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

	return i
}

func solve(n int, s, t string) int {
	A := make([]int, n)
	var sum int
	for i := 0; i < n; i++ {
		if s[i] > t[i] {
			A[i] = 1
		} else if s[i] < t[i] {
			A[i] = -1
		}
		sum += A[i]
	}

	if sum != 0 {
		return -1
	}

	get := func(x int) int {
		var cur int
		var best int

		for i := 0; i < n; i++ {
			cur += x * A[i]
			if best < cur {
				best = cur
			}
			if cur < 0 {
				cur = 0
			}
		}
		return best
	}

	return max(get(1), get(-1))
}

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

package main

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

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

	n := readNum(reader)
	s, _ := reader.ReadString('\n')
	t, _ := reader.ReadString('\n')
	fmt.Println(solve(n, s, t))
}

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

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

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

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

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

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

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

	return i
}

func solve(n int, s, t string) int {
	A := make([]int, n)
	var sum int
	for i := 0; i < n; i++ {
		if s[i] > t[i] {
			A[i] = 1
		} else if s[i] < t[i] {
			A[i] = -1
		}
		sum += A[i]
	}

	if sum != 0 {
		return -1
	}

	get := func(x int) int {
		var cur int
		var best int

		for i := 0; i < n; i++ {
			cur += x * A[i]
			if best < cur {
				best = cur
			}
			if cur < 0 {
				cur = 0
			}
		}
		return best
	}

	return max(get(1), get(-1))
}

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


package main

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

var facts = map[int]*big.Int{
	0: big.NewInt(1),
	1: big.NewInt(1),
	2: big.NewInt(2),
}

func fact(n int) *big.Int {
	if facts[n] != nil {
		return facts[n]
	}
	m := big.NewInt(int64(n))
	f := fact(n - 1)
	m.Mul(m, f)
	facts[n] = m
	return facts[n]
}

func choose(n, k int) *big.Int {
	factN := fact(n)
	factK := fact(k)
	factNMinusK := fact(n - k)
	res := big.NewInt(0)
	res.Div(factN, factK)
	res.Div(res, factNMinusK)
	return res
}

func calc(n int) (*big.Int, *big.Int, *big.Int) {
	if n == 2 {
		return big.NewInt(1), big.NewInt(0), big.NewInt(1)
	} else {
		a, b, _ := calc(n - 2)
		c1 := choose(n-1, n/2)
		c2 := choose(n-2, n/2)
		return c1.Add(c1, b), c2.Add(c2, a), big.NewInt(1)
	}
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	txt, _ := reader.ReadString('\n')
	q, _ := strconv.Atoi(strings.TrimSpace(txt))
	for ; q > 0; q-- {
		txt, _ = reader.ReadString('\n')
		n, _ := strconv.Atoi(strings.TrimSpace(txt))
		a, b, z := calc(n)
		mod := big.NewInt(998244353)
		fmt.Println(a.Mod(a, mod), b.Mod(b, mod), z.Mod(z, mod))
	}
}

package main

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

func ncr(n, mod int) [][]int {
	comb := make([][]int, n)
	for i := range comb {
		comb[i] = make([]int, n)
		comb[i][0] = 1
		if i == 0 { continue }
		for j := 1; j < n; j++ {
			comb[i][j] = (comb[i-1][j-1] + comb[i-1][j]) % mod
		}
	}
	return comb
}

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

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

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

		n = N/2
		dp := make([][]int, n + 1)
		comb := ncr(N, mod)

		dp[1] = make([]int, 3)
		dp[1][0] = 1
		dp[1][1] = 0
		dp[1][2] = 1

		for i := 2; i <= n; i++ {
			dp[i] = make([]int, 3)
			dp[i][0] = (comb[2*i - 1][i] + dp[i-1][1]) % mod
			dp[i][1] = (comb[2*i - 2][i - 2] + dp[i-1][0]) % mod
			dp[i][2] = 1
		}

		fmt.Fprintf(w, "%d %d %d\n", dp[n][0], dp[n][1], dp[n][2])
	}
}

package main

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

const M = 998244353

var COMBINATIONS = [][]int{}

func init() {
	N := 60

	cur := []int{1}
	COMBINATIONS = append(COMBINATIONS, cur)

	for n := 0; n < N; n++ {
		tmp := make([]int, len(cur) + 1)

		tmp[0] = 1
		for i := 0; i + 1 < len(cur); i++ {
			tmp[i + 1] = (cur[i] + cur[i + 1]) % M
		}
		tmp[len(tmp) - 1] = 1

		cur = tmp
		COMBINATIONS = append(COMBINATIONS, cur)
	}
}

func combinations(n, k int) int {
	return COMBINATIONS[n][k]
}

func solve(n int) (int, int, int) {
	a, b := 1, 0

	for cards := 4; cards <= n; cards += 2 {
		c1 := combinations(cards - 2, cards / 2)
		c2 := combinations(cards - 2, cards / 2 - 1)
		a, b = (c1 + c2 + b) % M, (c1 + a) % M
	}

	return a, b, 1
}

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

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

	for ; t > 0; t-- {
		var n int
		fmt.Fscan(in, &n)

		x, y, z := solve(n)
		fmt.Println(x, y, z)
	}
}

package main

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

const MOD = 998244353

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)

		dp := make([][3]int64, n+1)
		dp[2][0] = 1
		dp[2][1] = 0
		dp[2][2] = 1

		var walk func(n int)
		walk = func(n int) {
			if n == 2 {
				return
			}
			walk(n - 2)

			a := new(big.Int).Binomial(int64(n-1), int64(n/2)).Int64()
			b := new(big.Int).Binomial(int64(n-2), int64(n/2)).Int64()
			dp[n][0] = (a + int64(dp[n-2][1])) % MOD
			dp[n][1] = (b + int64(dp[n-2][0])) % MOD
			dp[n][2] = dp[n-2][2] % MOD
		}

		walk(n)
		fmt.Fprintln(out, dp[n][0], dp[n][1], dp[n][2])
	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)

const mod = 998244353
const N = 61

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

	c := make([][]int, N)
	for i := 0; i < N; i++ {
		c[i] = make([]int, N)
	}
	f := make([]int, N)
	func() {
		c[0][0] = 1
		for i := 1; i < N; i++ {
			for j := 0; j < N; j++ {
				if 0 == j {
					c[i][j] = 1
				} else {
					c[i][j] = (c[i-1][j-1] + c[i-1][j]) % mod
				}
			}
		}

		f[2] = 1
		f[4] = 3
		for i := 6; i < N; i += 2 {
			f[i] = (c[i-1][i/2-1] + c[i-4][i/2-3] + f[i-4]) % mod
		}
	}()
	T := nextInt()
	for T > 0 {
		T--
		n := nextInt()
		// 2022-10-12 20:14:57
		// 2022-10-12 20:22:43
		// 2022-10-12 22:36:22
		fmt.Println(f[n], ((c[n][n/2]-f[n]-1)%mod+mod)%mod, 1)
		// 2022-10-12 22:51:14
	}
}

func max(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"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

const mod = 998244353
const N = 61

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

	c := make([][]int, N)
	for i := 0; i < N; i++ {
		c[i] = make([]int, N)
	}
	f := make([]int, N)
	func() {
		c[0][0] = 1
		for i := 1; i < N; i++ {
			for j := 0; j < N; j++ {
				if 0 == j {
					c[i][j] = 1
				} else {
					c[i][j] = (c[i-1][j-1] + c[i-1][j]) % mod
				}
			}
		}

		f[2] = 1
		f[4] = 3
		for i := 6; i < N; i += 2 {
			f[i] = (c[i-1][i/2-1] + c[i-4][i/2-3] + f[i-4]) % mod
		}
	}()
	T := nextInt()
	for T > 0 {
		T--
		n := nextInt()
		// 2022-10-12 20:14:57
		// 2022-10-12 20:22:43
		// 2022-10-12 22:36:22
		fmt.Println(f[n], (c[n][n/2]-f[n]-1)%mod, 1)
		// 2022-10-12 22:51:14
	}
}

func max(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 (
	"os"
	"bufio"
	"strconv"
	"sort"
	// "strings"
	// "fmt"
)

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

func ReadInt() int {
	scanner.Scan()
	ans, _ := strconv.Atoi(scanner.Text())
	return ans
}

func init() {
	// const maxCapacity = 500050
	// buf := make([]byte, maxCapacity)
	// scanner.Buffer(buf, maxCapacity)
	scanner.Split(bufio.ScanWords)
}

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

type point struct {
	x,y int
}

func main() {
	defer writer.Flush()

	t := ReadInt()

	maior := point{-1,-1}

	for ;t > 0; t-- {

		scanner.Scan()
		tipo := scanner.Text()

		if tipo == "+" {

			v := make([]int, 2)

			v[0] = ReadInt()
			v[1] = ReadInt()

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

			maior.x = max(maior.x, v[0])
			maior.y = max(maior.y, v[1])

		} else {

			x := ReadInt()
			y := ReadInt()

			if (x >= maior.x && y >= maior.y) || (y >= maior.x && x >= maior.y) {
				writer.WriteString("YES\n")
			} else{
				writer.WriteString("NO\n")

			}
		}
	}
}

package main

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

func solveSequential(scanner *bufio.Scanner, writer *bufio.Writer) {
	maxX := uint64(0)
	maxY := uint64(0)

	queries := readInt(scanner)
	for q := 0; q < queries; q++ {
		queryType := readString(scanner)
		if queryType == "+" {
			x := uint64(readInt64(scanner))
			y := uint64(readInt64(scanner))
			if y > x {
				maxX = max64(maxX, y)
				maxY = max64(maxY, x)
			} else {
				maxX = max64(maxX, x)
				maxY = max64(maxY, y)
			}
		} else if queryType == "?" {
			h := uint64(readInt64(scanner))
			w := uint64(readInt64(scanner))

			if h > w {
				h,w = w,h
			}

			if h >= maxY && w >= maxX {
				writef(writer, "YES\n")
			} else {
				writef(writer, "NO\n")
			}
		}
	}
}

func main() {
	var scanner *bufio.Scanner
	if len(os.Args) > 1 {
		reader, err := os.Open(os.Args[1])
		if err != nil {
			panic(err)
		}
		defer reader.Close()
		scanner = bufio.NewScanner(reader)
	} else {
		scanner = bufio.NewScanner(os.Stdin)
	}
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 0, 1024*1024), 1024*1024)

	var writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	solveSequential(scanner, writer)
}

//	GoLang shorthand methods for I/O go below
//	TODO wipe unused methods before submitting

func readString(sc *bufio.Scanner) string {
	if !sc.Scan() {
		panic("failed to scan next token")
	}

	return sc.Text()
}

func readInt64(sc *bufio.Scanner) int64 {
	if !sc.Scan() {
		panic("failed to scan next token")
	}

	res, err := strconv.ParseInt(sc.Text(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}

func readInt(sc *bufio.Scanner) int {
	return int(readInt64(sc))
}

func writef(writer *bufio.Writer, formatStr string, values ...interface{}) {
	out := fmt.Sprintf(formatStr, values...)
	_, err := writer.WriteString(out)
	if err != nil {
		panic(err)
	}
}

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

package main

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

func main() {
	start := time.Now()
	defer func() {
		run := time.Since(start)
		_ = run
		// fmt.Println("Run in", run)
	}()

	var stdin *bufio.Reader
	// LOCAL
	f, _ := os.Open("input.txt")
	stdin = bufio.NewReader(f)

	// ONLINE JUDGE
	stdin = bufio.NewReader(os.Stdin)

	var n, x, y, w, h int
	var c byte

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

	fmt.Fscanln(stdin, &n)
	for ; n > 0; n-- {
		// var s string
		// fmt.Fscan(stdin, &s)
		// fmt.Println(s)
		fmt.Fscanf(stdin, "%c %d %d\n", &c, &x, &y)
		x, y = swap(x, y)
		if c == '+' {
			w = max(w, x)
			h = max(h, y)
		} else {
			if x < w || y < h {
				fmt.Println("NO")
			} else {
				fmt.Println("YES")
			}
		}
	}

}

package main

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

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

func ReadInt() int {
	scanner.Scan()
	ans, _ := strconv.Atoi(scanner.Text())
	return ans
}

func init() {
	// const maxCapacity = 500050
	// buf := make([]byte, maxCapacity)
	// scanner.Buffer(buf, maxCapacity)
	scanner.Split(bufio.ScanWords)
}

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

type point struct {
	x,y int
}

func main() {
	defer writer.Flush()

	t := ReadInt()

	maiorX := point{-1,-1}
	maiorY := point{-1,-1}

	for ;t > 0; t-- {

		scanner.Scan()
		tipo := scanner.Text()

		if tipo == "+" {

			x := ReadInt()
			y := ReadInt()

			if x > maiorX.x {
				maiorX = point{x,y}
			} else if x == maiorX.x {
				maiorX = point{x, max(y, maiorX.y)}
			}

			if y > maiorY.y {
				maiorY = point{x,y}
			} else if y == maiorY.y {
				maiorY = point{max(x, maiorY.x), y}
			}

		} else {

			x := ReadInt()
			y := ReadInt()

			acc := 0

			if x >= maiorX.x && y >= maiorX.y {
				acc++
			} else if y >= maiorX.x && x >= maiorX.y {
				acc++
			}

			if x >= maiorY.x && y >= maiorY.y {
				acc++
			} else if y >= maiorY.x && x >= maiorY.y {
				acc++
			}

			if acc == 2 {
				writer.WriteString("YES\n")
			} else {
				writer.WriteString("NO\n")
			}

		}
	}
}


package main

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

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

func readInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	return t
}

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

	t := readInt(scanner)
	for i := 0; i < t; i++ {
		n := readInt(scanner)
		d := [3]int{0, 0, 0}

		for j := 0; j < n; j++ {
			d[readInt(scanner) % 3] += 1
		}

		ans := d[0]
		m := min(d[1], d[2])
		ans += m
		d[1] -= m
		d[2] -= m
		ans += d[1] / 3
		ans += d[2] / 3

		fmt.Println(ans)
	}
}

package main

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

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

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

	q, _ := strconv.Atoi(scanner.Text())

	for q > 0 {
		scanner.Scan()
		larr, _ := strconv.Atoi(scanner.Text())
		arr1 := 0
		arr2 := 0
		r := 0
		scanner.Scan()
		in := strings.Split(scanner.Text(), " ")
		count := 0
		for count < larr {
			a, _ := strconv.Atoi(in[count])

			switch a % 3 {
			case 1:
				arr1++
			case 2:
				arr2++
			case 0:
				r++
			}

			count++
		}

		diff := abs(arr1 - arr2)
		r += diff / 3
		r += int(math.Min(float64(arr1), float64(arr2)))
		fmt.Println(r)
		q--
	}
}

// Author: sighduck
// URL: https://codeforces.com/contest/1176/problem/B

package main

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

func Solve(n int, a []int64) int {
	var remainders [3]int
	for i := 0; i < n; i++ {
		remainders[a[i] % 3] += 1
	}
	if remainders[1] > remainders[2] {
		return remainders[0] + remainders[2] + (remainders[1] - remainders[2])/3
	} else if remainders[2] > remainders[1] {
		return remainders[0] + remainders[1] + (remainders[2] - remainders[1])/3
	}
	return remainders[0] + remainders[1]
}

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

	var t int
	var n int

	fmt.Fscanf(reader, "%d\n", &t)
	for i := 0; i < t; i++ {
		fmt.Fscanf(reader, "%d\n", &n)
		a := make([]int64, n)
		for j := 0; j < n; j++ {
			fmt.Fscanf(reader, "%d", &a[j])
		}
		fmt.Fscanf(reader, "\n")
		fmt.Fprintf(writer, "%d\n", Solve(n, a))
	}
}

package main

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

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

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

	q, _ := strconv.Atoi(scanner.Text())

	for q > 0 {
		scanner.Scan()
		larr, _ := strconv.Atoi(scanner.Text())
		arr1 := 0
		arr2 := 0
		r := 0
		scanner.Scan()
		in := strings.Split(scanner.Text(), " ")
		count := 0
		for count < larr {
			a, _ := strconv.Atoi(in[count])

			switch a % 3 {
			case 1:
				arr1++
			case 2:
				arr2++
			case 0:
				r++
			}

			count++
		}

		diff := abs(arr1 - arr2)
		r += diff / 3
		r += int(math.Min(float64(arr1), float64(arr2)))
		println(r)
		q--
	}
}


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 _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	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 _string() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	for t := _int(); t > 0; t-- {
		n := _int()
		r := make([][5]int, n)
		for i := range r {
			for j := range r[i] {
				r[i][j] = _int()
			}
		}
		printf("%d\n", sol(n, r))
	}
}

func sol(n int, r [][5]int) int {
	skipCandidate := make([]bool, n)
	for i := range r {
		if skipCandidate[i] {
			continue
		}
		for offset := 1; offset < n; offset++ {
			j2 := (i + offset) % n
			sup := superior(r[i], r[j2])
			if sup == -1 {
				skipCandidate[i] = true
				skipCandidate[j2] = true
				break
			}
			if sup == 0 {
				skipCandidate[j2] = true
			}
			if sup == 1 {
				skipCandidate[i] = true
				break
			}
		}
		if !skipCandidate[i] {
			return i + 1
		}
	}
	for i := range skipCandidate {
		if skipCandidate[i] == false {
			return i + 1
		}
	}
	return -1
}

// -1 = none superior
// 0 = a superior
// 1 = b superior
func superior(a, b [5]int) int {
	as := 0
	bs := 0
	for i := range a {
		if a[i] < b[i] {
			as++
		}
		if b[i] < a[i] {
			bs++
		}
	}
	if as >= 3 {
		return 0
	}
	if bs >= 3 {
		return 1
	}
	return -1
}

package main

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

func superior(a, b []int) bool {
	c := 0
	for i := 0; i < 5; i++ {
		if a[i] < b[i] {
			c++
		}
	}
	return c >= 3
}

func main() {
	r := bufio.NewReaderSize(os.Stdin, 1024*1024)

	var T int

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

	for t := 0; t < T; t++ {
		var n int
		fmt.Fscanf(r, "%d\n", &n)

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

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

		skip := make([]bool, n)
		found := false
		for i := 0; i < n; i++ {
			if !skip[i] {
				for j := i + 1; j < n; j++ {
					if superior(arr[i], arr[j]) {
						skip[j] = true
					} else {
						skip[i] = true
						break
					}
				}

				if !skip[i] {
					j := 0
					for ; j < i; j++ {
						if !superior(arr[i], arr[j]) {
							break
						}
					}

					if j == i {
						found = true
						fmt.Println(i + 1)
						break
					}
				}
			}
		}

		if !found {
			fmt.Println(-1)
		}
	}
}

package main

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

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

func main() {
	var test int
	fmt.Fscan(in, &test)
	for i := 0; i < test; i++ {
		solve()
	}

}

func solve() {
	var n int
	fmt.Fscan(in, &n)
	var matrix = make([][]int, n)
	for i := 0; i < n; i++ {
		matrix[i] = make([]int, 5)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < 5; j++ {
			var temp int
			fmt.Fscan(in, &temp)
			matrix[i][j] = temp
		}
	}
	winner := 0
	for i := 1; i < n; i++ {
		maxBetter := 0
		for j := 0; j < 5; j++ {
			if matrix[i][j] < matrix[winner][j] {
				maxBetter++
			}
			if maxBetter >= 3 {
				winner = i
				break
			}
		}
	}
	for i := 0; i < n; i++ {
		if i == winner {
			continue
		}
		maxBetter := 0
		for j := 0; j < 5; j++ {
			if matrix[i][j] < matrix[winner][j] {
				maxBetter++
			}
			if maxBetter >= 3 {
				fmt.Println(-1)
				return
			}
		}
	}
	fmt.Println(winner + 1)

}

func printArray(arr []int) {
	for i := 1; i < len(arr); i++ {
		fmt.Print(arr[i], " ")
	}
	fmt.Println()
}

type runeSort []rune

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var test int
	fmt.Fscan(in, &test)
	for ; test > 0; test-- {
		var n int
		fmt.Fscan(in, &n)

		ar := make([][]int, n)
		for i := range ar {
			ar[i] = make([]int, 5)
			for j := range ar[i] {
				fmt.Fscan(in, &ar[i][j])
			}
		}

		if n == 1 {
			fmt.Fprintln(out, 1)
			continue
		}

		w := 0
		for i := range ar {
			if better(i, w, ar) {
				w = i
			}
		}

		isSups := true
		for i := range ar {
			if i != w {
				if better(i, w, ar) {
					isSups = false
				}
			}
		}

		if isSups {
			fmt.Fprintln(out, w+1)
		} else {
			fmt.Fprintln(out, -1)
		}
	}
}

func better(i, j int, ar [][]int) bool {
	t := 0
	for k := 0; k < 5; k++ {
		if ar[i][k] < ar[j][k] {
			t++
		}
	}
	return t >= 3
}

package main

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

type Scanner struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) *Scanner {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	return &Scanner{scanner}
}

func (s *Scanner) Word() string {
	s.Scan()
	return s.Text()
}

func (s *Scanner) Int() int {
	s.Scan()
	n, _ := strconv.Atoi(s.Text())
	return n
}

func (s *Scanner) Int64() int64 {
	s.Scan()
	n, _ := strconv.ParseInt(s.Text(), 10, 64)
	return n
}

func (s *Scanner) Words(count int) []string {
	words := make([]string, count)
	for i := range words {
		words[i] = s.Word()
	}
	return words
}

func (s *Scanner) Ints(count int) []int {
	ints := make([]int, count)
	for i := range ints {
		ints[i] = s.Int()
	}
	return ints
}

func (s *Scanner) Int64s(count int) []int64 {
	int64s := make([]int64, count)
	for i := range int64s {
		int64s[i] = s.Int64()
	}
	return int64s
}

type athlete []int

func (a athlete) superior(o athlete) bool {
	better := 0
	for i := range a {
		if a[i] < o[i] {
			better += 1
		}
	}
	return better >= 3
}

func canWin(bestPos int, a []athlete) bool {
	for i := range a {
		if i != bestPos && !a[bestPos].superior(a[i]) {
			return false
		}
	}
	return true
}

func solve(a []athlete) (int, bool) {
	bestPos := 0
	for i := range a {
		if a[i].superior(a[bestPos]) {
			bestPos = i
		}
	}

	if canWin(bestPos, a) {
		return bestPos, true
	}
	return -1, false
}

func main() {
	stdin := NewWordScanner(os.Stdin)
	tests := stdin.Int()

	for i := 0; i < tests; i += 1 {
		n := stdin.Int()
		a := make([]athlete, n)
		for j := range a {
			a[j] = stdin.Ints(5)
		}

		pos, found := solve(a)
		if found {
			fmt.Println(pos + 1)
		} else {
			fmt.Println(-1)
		}
	}
}