package main

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

func ss() {
	n := NextInt()
	a := make([]int64, n)
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = NextInt64()
	}
	for i := 0; i < n; i++ {
		b[i] = NextInt64()
	}
	ans := int64(0)
	for i := 1; i < n; i++ {
		ans += MinInt64(AbsInt64(b[i-1]-a[i])+AbsInt64(a[i-1]-b[i]), AbsInt64(a[i-1]-a[i])+AbsInt64(b[i-1]-b[i]))
	}
	Out(ans, "\n")
}

func Solve() {
	for T := NextInt(); T > 0; T-- {
		ss()
	}
}

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

func In(a ...interface{}) {
	_, _ = fmt.Fscan(in, a...)
}

func main() {
	Solve()
	out.Flush()
}

func NextInt() int {
	var x int
	_, _ = fmt.Fscan(in, &x)
	return x
}

func NextInt64() int64 {
	var x int64
	_, _ = fmt.Fscan(in, &x)
	return x
}

func Out(a ...interface{}) {
	_, _ = fmt.Fprint(out, a...)
}

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

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

package main

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

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

func A(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriterSize(_w, 102400000)
	defer out.Flush()

	var c int
	var n int
	Fscan(in, &c)
	for t := 0; t < c; t++ {
		Fscan(in, &n)
		a, b := make([]int64, n), make([]int64, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		for i := range b {
			Fscan(in, &b[i])
		}
		ans := int64(0)
		for i := 1; i < n; i++ {
			old := abs(a[i]-a[i-1]) + abs(b[i]-b[i-1])
			swap := abs(b[i]-a[i-1]) + abs(a[i]-b[i-1])
			if swap < old {
				ans += swap
				a[i], b[i] = b[i], a[i]
			} else {
				ans += old
			}
		}
		Fprintln(out, ans)
	}
}

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

package main

import (
	"bufio"
	"fmt"
	"os"
	"runtime/debug"
)

/* begin of MyIO */
const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()
	neg := false
	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	if neg {
		return -x
	}
	return
}

func (myio *MyIO) ReadInt64() (x int64) {
	rc := myio.rc
	b := rc()
	neg := false

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int64(b&15)
	}
	if neg {
		return -x
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

var myio *MyIO

func ri() int {
	return myio.ReadInt()
}

func ri64() int64 {
	return myio.ReadInt64()
}

func ria(_n int) []int {
	a := make([]int, _n)
	for i := range a {
		a[i] = ri()
	}
	return a
}

/* End of MyIO */

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

func sol() {
	n := ri()
	a := ria(n)
	b := ria(n)

	for i := 0; i < n; i++ {
		if a[i] > b[i] {
			a[i], b[i] = b[i], a[i]
		}
	}
	var res int64 = 0
	for i := 1; i < n; i++ {
		res += int64(abs(a[i]-a[i-1])) + int64(abs(b[i]-b[i-1]))
	}

	fmt.Println(res)
}

func main() {
	debug.SetGCPercent(-1)
	in, out := os.Stdin, os.Stdout
	myio = NewIO(in, out)
	defer myio.Flush()

	var t = 1
	t = ri()
	for ; t > 0; t-- {
		sol()
	}
}

package main

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

func main() {

	reader := bufio.NewReader(os.Stdin)
	writter := bufio.NewWriter(os.Stdout)
	defer writter.Flush()

	t := 0
	fmt.Fscan(reader, &t)

	for t > 0 {
		n := 0
		fmt.Fscan(reader, &n)

		a := make([]int, n)
		b := make([]int, n)

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

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

		// for i := range result {
		// 	fmt.Fprintf(writter, "%d ", result[i])
		// }
		// fmt.Fprintln(writter)

		fmt.Fprintln(writter, solve(n, a, b))

		t--
	}

	// n := 6
	// a := []int{72, 101, 108, 108, 111, 44}
	// b := []int{10, 87, 111, 114, 108, 100}

	// fmt.Println(solve(n, a, b))

	//ethereum.Ethereum()

}

func solve(n int, a, b []int) int64 {

	sum := int64(0)

	for i := range a {
		if a[i] > b[i] {
			a[i], b[i] = b[i], a[i]
		}
	}

	// fmt.Println(a)
	// fmt.Println(b)

	for i := 1; i < n; i++ {
		sum += int64(math.Abs(float64(a[i] - a[i-1])))
	}

	for i := 1; i < n; i++ {
		sum += int64(math.Abs(float64(b[i] - b[i-1])))
	}

	return sum
}

package main

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

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

func (in *CF1661A) 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 *CF1661A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1661A
Date: 19/05/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1661/A
Problem: CF1661A
**/
func (in *CF1661A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		a := make([]int, n)
		for i := 0; i < n; i++ {
			a[i] = in.NextInt()
		}

		b := make([]int, n)
		for i := 0; i < n; i++ {
			b[i] = in.NextInt()
		}

		sum := int64(0)
		for i := 0; i < n; i++ {
			if a[i] > b[i] {
				a[i], b[i] = b[i], a[i]
			}

		}

		for i:=1;i<n;i++{
			sum += int64(math.Abs(float64(a[i-1])-float64(a[i])))
			sum += int64(math.Abs(float64(b[i-1])-float64(b[i])))
		}
		fmt.Println(sum)
	}
}

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

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

package main

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

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

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

func main() {
	defer out.Flush()

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

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

		a := make([]int64, n)
		b := make([]int64, n)

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

		var ans int64 = 0

		for i := 1; i < n; i++ {
			if abs(a[i-1]-a[i])+abs(b[i-1]-b[i]) <= abs(a[i-1]-b[i])+abs(b[i-1]-a[i]) {
				ans += abs(a[i-1]-a[i]) + abs(b[i-1]-b[i])
			} else {
				ans += abs(a[i-1]-b[i]) + abs(b[i-1]-a[i])
			}
		}

		fmt.Fprintln(out, ans)
	}
}

package main

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

const (
	INF = 3e5 + 10
)

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

	var t int
	Fscan(in, &t)

	for k := 0; k < t; k++ {
		var n int
		Fscan(in, &n)

		a := make([]int64, n)
		b := make([]int64, n)

		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
		}
		for i := 0; i < n; i++ {
			Fscan(in, &b[i])
		}

		dp := make([][]int64, n)

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

		for i := 1; i < n; i++ {
			dp[i][0] = min(dp[i-1][0]+abs(a[i]-a[i-1])+abs(b[i]-b[i-1]), dp[i-1][1]+abs(a[i]-b[i-1])+abs(b[i]-a[i-1]))

			dp[i][1] = min(dp[i-1][0]+abs(b[i]-a[i-1])+abs(a[i]-b[i-1]), dp[i-1][1]+abs(b[i]-b[i-1])+abs(a[i]-a[i-1]))
		}

		Fprintln(out, min(dp[n-1][0], dp[n-1][1]))
	}

}

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

}

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

}

package main

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

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 readInt64(in *bufio.Reader) int64 {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.ParseInt(nStr, 10, 64)
	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
}
func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

func main() {
	ip := bufio.NewReader(os.Stdin)
	t := readInt(ip)
	for t > 0 {
		t--
		_ = readInt(ip)
		a := readArrInt64(ip)
		b := readArrInt64(ip)
		ans := int64(0)
		for i := range a {
			if i == 0 {
				continue
			}
			ans += min(abs(a[i]-a[i-1])+abs(b[i]-b[i-1]), abs(a[i]-b[i-1])+abs(b[i]-a[i-1]))
		}
		fmt.Println(ans)
	}
}

func min(i, j int64) int64 {
	if i > j {
		return j
	}
	return i
}

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

package main

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

var reader = bufio.NewReader(os.Stdin)

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

var writer = bufio.NewWriter(os.Stdout)

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 a = make([]int, n)
	for i := range a {
		scan(&a[i])
	}
	var b = make([]int, n)
	for i := range b {
		scan(&b[i])
	}
	for i := 1; i < n; i++ {
		if abs(a[i]-a[i-1])+abs(b[i]-b[i-1]) > abs(b[i]-a[i-1])+abs(a[i]-b[i-1]) {
			a[i], b[i] = b[i], a[i]
		}
	}
	var ans int64
	for i := 1; i < n; i++ {
		ans += int64(abs(a[i] - a[i-1]))
	}
	for i := 1; i < n; i++ {
		ans += int64(abs(b[i] - b[i-1]))
	}
	printf("%v\n", ans)
}

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

package main

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

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

	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int64, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		b := make([]int64, n)
		for i := range b {
			Fscan(in, &b[i])
		}
		ans := int64(0)
		for i := 1; i < n; i++ {
			ans += min(abs(a[i]-a[i-1])+abs(b[i]-b[i-1]), abs(a[i]-b[i-1])+abs(b[i]-a[i-1]))
		}
		Fprintln(out, ans)
	}
}

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

func min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

package main

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

/*
2
4
50 100 50 50
1 50 1 1
2
1 10
10 1
---
198
0
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	a := make([]int, n)

	for i := range a {
		scanf("%d", &a[i])
	}
	scanf("\n")

	b := make([]int, n)

	for i := range b {
		scanf("%d", &b[i])
	}
	scanf("\n")

	sum := int64(0)

	for i := 0; i < n-1; i++ {
		if abs(a[i]-a[i+1])+abs(b[i]-b[i+1]) < abs(b[i]-a[i+1])+abs(a[i]-b[i+1]) {
			sum += int64(abs(a[i]-a[i+1]) + abs(b[i]-b[i+1]))
		} else {
			sum += int64(abs(b[i]-a[i+1]) + abs(a[i]-b[i+1]))
		}
	}

	fmt.Fprintln(writer, sum)
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

package main

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

func abs(n int64) int64 {
	if n < 0 {
		return n * -1
	}
	return n
}

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)

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

		b := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &b[i])
		}

		var tot int64
		for i := 1; i < n; i++ {
			d := abs(a[i-1]-a[i]) + abs(b[i-1]-b[i])
			if abs(a[i-1]-b[i])+abs(b[i-1]-a[i]) < d {
				d = abs(a[i-1]-b[i]) + abs(b[i-1]-a[i])
			}
			tot += d
		}
		fmt.Fprintln(out, tot)
	}
}

package main

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

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

func find_sum(a, b []int64, m int) int64 {
	var sum int64
	for j := 0; j < m; j++ {
		if a[j] < b[j] {
			a[j], b[j] = b[j], a[j]
		}

		if j > 0 {
			sum += abs(a[j] - a[j-1])
			sum += abs(b[j] - b[j-1])
		}
	}

	return sum
}

func read_ab(s *bufio.Scanner, a, b []int64) int {
	s.Scan()
	m, _ := strconv.Atoi(s.Text())

	for j := 0; j < m; j++ {
		s.Scan()
		a[j],_ = strconv.ParseInt(s.Text(), 10, 64)
	}

	for j := 0; j < m; j++ {
		s.Scan()
		b[j],_ = strconv.ParseInt(s.Text(), 10, 64)
	}

	return m
}

func main() {
	a := make([]int64, 25)
	b := make([]int64, 25)

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	output := bufio.NewWriter(os.Stdout)
	defer output.Flush()

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

	for i := 0; i < n; i++ {
		m := read_ab(scanner, a, b)
		sum := find_sum(a, b, m)

		b := strconv.AppendInt(nil, sum, 10)
		b = append(b, '\n')
		output.Write(b)
	}
}

package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"bufio"
	"strconv"

	"runtime/pprof"
)

var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")

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

func find_sum(a, b []int64, m int) int64 {
	var sum int64
	for j := 0; j < m; j++ {
		if a[j] < b[j] {
			a[j], b[j] = b[j], a[j]
		}

		if j > 0 {
			sum += abs(a[j] - a[j-1])
			sum += abs(b[j] - b[j-1])
		}
	}

	return sum
}

func read_ab(s *bufio.Scanner, a, b []int64) int {
	s.Scan()
	m, _ := strconv.ParseInt(s.Text(), 10, 64)

	for j := 0; j < int(m); j++ {
		s.Scan()
		a[j],_ = strconv.ParseInt(s.Text(), 10, 64)
	}

	for j := 0; j < int(m); j++ {
		s.Scan()
		b[j],_ = strconv.ParseInt(s.Text(), 10, 64)
	}

	return int(m)
}

func main() {

	flag.Parse()
	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	a := make([]int64, 25)
	b := make([]int64, 25)

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

	scanner.Scan()
	n,_ := strconv.ParseInt(scanner.Text(), 10, 64)

	for i := 0; i < int(n); i++ {
		m := read_ab(scanner, a, b)
		sum := find_sum(a, b, m)

		fmt.Printf("%d\n", sum)
	}
}

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()
		a := make([]int64, n)
		b := make([]int64, n)
		for i := range a {
			a[i] = _int64()
		}
		for i := range b {
			b[i] = _int64()
		}
		printf("%d\n", sol(a, b))
	}
}

func sol(a, b []int64) int64 {
	for i := range a {
		if b[i] < a[i] {
			a[i], b[i] = b[i], a[i]
		}
	}
	ret := int64(0)
	for i := range a {
		if i == 0 {
			continue
		}
		ret += abs(a[i] - a[i-1])
		ret += abs(b[i] - b[i-1])
	}
	return ret
}

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

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()
		a := make([]int64, n)
		b := make([]int64, n)
		for i := range a {
			a[i] = _int64()
		}
		for i := range b {
			b[i] = _int64()
		}
		printf("%d\n", sol(a, b))
	}
}

func sol(a, b []int64) int64 {
	for i := range a {
		if i == 0 {
			continue
		}
		noswap := abs(b[i]-b[i-1]) + abs(a[i]-a[i-1])
		swap := abs(b[i]-a[i-1]) + abs(a[i]-b[i-1])
		if swap < noswap {
			a[i], b[i] = b[i], a[i]
		}
	}
	ret := int64(0)
	for i := range a {
		if i == 0 {
			continue
		}
		ret += abs(a[i] - a[i-1])
		ret += abs(b[i] - b[i-1])
	}
	return ret
}

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

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := 0
	for scanner.Scan() {
		n, _ = strconv.Atoi(scanner.Text())
		break
	}

	answers := make([]int64, n)

	for i := 0; i < n; i++ {
		scanner.Scan()
		scanner.Text()

		scanner.Scan()
		sarr1 := strings.Split(scanner.Text(), " ")
		scanner.Scan()
		sarr2 := strings.Split(scanner.Text(), " ")

		arr1, arr2 := make([]int64, len(sarr1)), make([]int64, len(sarr2))
		for j := 0; j < len(arr1); j++ {
			arr1[j], _ = strconv.ParseInt(sarr1[j], 10, 64)
			arr2[j], _ = strconv.ParseInt(sarr2[j], 10, 64)
		}

		sum := float64(0)

		for j := 0; j < len(arr1)-1; j++ {
			if math.Abs(float64(arr1[j]-arr1[j+1]))+math.Abs(float64(arr2[j]-arr2[j+1])) < math.Abs(float64(arr1[j]-arr2[j+1]))+math.Abs(float64(arr2[j]-arr1[j+1])) {
				sum += math.Abs(float64(arr1[j]-arr1[j+1])) + math.Abs(float64(arr2[j]-arr2[j+1]))
				continue
			}

			sum += math.Abs(float64(arr1[j]-arr2[j+1])) + math.Abs(float64(arr2[j]-arr1[j+1]))
			arr1[j+1], arr2[j+1] = arr2[j+1], arr1[j+1]
		}

		answers[i] = int64(sum)
	}

	for _, answer := range answers {
		fmt.Println(answer)
	}
}

package main

import (
	"fmt"
)

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

func min(n, m int64) int64 {
	if m > n {
		return n
	}

	return m
}

func main() {
	var n int
	var a, b [25]int64

	fmt.Scanf("%d\n", &n)

	for i := 0; i < n; i++ {
		var sum int64
		var m int

		fmt.Scanf("%d\n", &m)

		for j := 0; j < m; j++ {
			fmt.Scanf("%d", &a[j])
		}

		fmt.Scanf("\n")

		for j := 0; j < m; j++ {
			fmt.Scanf("%d", &b[j])
		}

		fmt.Scanf("\n")

		for j := 0; j < m-1; j++ {
			mid := abs(a[i + 1] - a[i])
			midd := abs(b[i + 1] - b[i])
			middd := abs(a[i + 1] - b[i])
			mi := abs(b[i + 1] - a[i])

			sum += min(mi + middd, midd + mid)
		}

		fmt.Printf("%d\n", sum)
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var n int

	fmt.Scanf("%d\n", &n)

	for i:=0; i<n; i++ {
		var m, sum int

		fmt.Scanf("%d\n", &m)

		a := make([]int, m)
		b := make([]int, m)

		for j:=0; j < m; j++ {
			fmt.Scanf("%d", &a[j])
		}

		fmt.Scanf("\n")

		for j:=0; j < m; j++ {
			fmt.Scanf("%d", &b[j])
		}

		fmt.Scanf("\n")

		for j:=0; j < m; j++ {
			if a[j] < b[j] {
				a[j], b[j] = b[j], a[j]
			}
		}

		for j:=0; j < m-1; j++ {
			sum += int(math.Abs(float64(a[j]-a[j+1])))
			sum += int(math.Abs(float64(b[j]-b[j+1])))
		}

		fmt.Printf("%d\n", sum)
	}
}

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()
		a := make([]int64, n)
		b := make([]int64, n)
		for i := range a {
			a[i] = _int64()
		}
		for i := range b {
			b[i] = _int64()
		}
		printf("%d\n", sol(a, b))
	}
}

func sol(a, b []int64) int64 {
	for i := range a {
		if i == 0 {
			continue
		}
		if b[i] < a[i] {
			a[i], b[i] = b[i], a[i]
		}
	}
	ret := int64(0)
	for i := range a {
		if i == 0 {
			continue
		}
		ret += abs(a[i] - a[i-1])
		ret += abs(b[i] - b[i-1])
	}
	return ret
}

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

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()
		a := make([]int, n)
		b := make([]int, n)
		for i := range a {
			a[i] = _int()
		}
		for i := range b {
			b[i] = _int()
		}
		printf("%d\n", sol(a, b))
	}
}

func sol(a, b []int) int {
	for i := range a {
		if i == 0 {
			continue
		}
		noswap := abs(b[i]-b[i-1]) + abs(a[i]-a[i-1])
		swap := abs(b[i]-a[i-1]) + abs(a[i]-b[i-1])
		if swap < noswap {
			a[i], b[i] = b[i], a[i]
		}
	}
	ret := 0
	for i := range a {
		if i == 0 {
			continue
		}
		ret += abs(a[i] - a[i-1])
		ret += abs(b[i] - b[i-1])
	}
	return ret
}

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

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()
		a := make([]int, n)
		for i := range a {
			a[i] = _int()
		}
		b := make([]int, n)
		for i := range b {
			b[i] = _int()
		}
		printf("%d\n", sol(a, b))
	}
}

func sol(a, b []int) int {
	for i := range a {
		if b[i] < a[i] {
			a[i], b[i] = b[i], a[i]
		}
	}
	ret := 0
	for i := 0; i < len(a)-1; i++ {
		ret += abs(a[i+1] - a[i])
		ret += abs(b[i+1] - b[i])
	}
	return ret
}

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

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := 0
	for scanner.Scan() {
		n, _ = strconv.Atoi(scanner.Text())
		break
	}

	answers := make([]int64, n)

	for i := 0; i < n; i++ {
		scanner.Scan()
		scanner.Text()

		scanner.Scan()
		sarr1 := strings.Split(scanner.Text(), " ")
		scanner.Scan()
		sarr2 := strings.Split(scanner.Text(), " ")

		arr1, arr2 := make([]int64, len(sarr1)), make([]int64, len(sarr2))
		for j := 0; j < len(arr1); j++ {
			arr1[j], _ = strconv.ParseInt(sarr1[j], 10, 64)
			arr2[j], _ = strconv.ParseInt(sarr2[j], 10, 64)
		}

		sum := float64(0)

		for j := 0; j < len(arr1)-1; j++ {
			if math.Abs(float64(arr1[j]-arr2[j+1])) < math.Abs(float64(arr1[j]-arr1[j+1])) {
				sum += math.Abs(float64(arr1[j]-arr2[j+1])) + math.Abs(float64(arr2[j]-arr1[j+1]))
				// arr1[j+1], arr2[j+1] = arr2[j+1], arr1[j+1]
			} else {
				sum += math.Abs(float64(arr1[j]-arr1[j+1])) + math.Abs(float64(arr2[j]-arr2[j+1]))
			}
		}

		answers[i] = int64(sum)
	}

	for _, answer := range answers {
		fmt.Println(answer)
	}
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := 0
	for scanner.Scan() {
		n, _ = strconv.Atoi(scanner.Text())
		break
	}

	answers := make([]int, n)

	for i := 0; i < n; i++ {
		scanner.Scan()
		scanner.Text()

		scanner.Scan()
		sarr1 := strings.Split(scanner.Text(), " ")
		scanner.Scan()
		sarr2 := strings.Split(scanner.Text(), " ")

		arr1, arr2 := make([]int, len(sarr1)), make([]int, len(sarr2))
		for j := 0; j < len(arr1); j++ {
			arr1[j], _ = strconv.Atoi(sarr1[j])
			arr2[j], _ = strconv.Atoi(sarr2[j])
		}

		sum := float64(0)

		for j := 0; j < len(arr1)-1; j++ {
			if math.Abs(float64(arr1[j]-arr2[j+1])) < math.Abs(float64(arr1[j]-arr1[j+1])) {
				sum += math.Abs(float64(arr1[j]-arr2[j+1])) + math.Abs(float64(arr2[j]-arr1[j+1]))
				// arr1[j+1], arr2[j+1] = arr2[j+1], arr1[j+1]
			} else {
				sum += math.Abs(float64(arr1[j]-arr1[j+1])) + math.Abs(float64(arr2[j]-arr2[j+1]))
			}
		}

		answers[i] = int(sum)
	}

	for _, answer := range answers {
		fmt.Println(answer)
	}
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := 0
	for scanner.Scan() {
		n, _ = strconv.Atoi(scanner.Text())
		break
	}

	answers := make([]int, n)

	for i := 0; i < n; i++ {
		scanner.Scan()
		scanner.Text()

		scanner.Scan()
		sarr1 := strings.Split(scanner.Text(), " ")
		scanner.Scan()
		sarr2 := strings.Split(scanner.Text(), " ")

		arr1, arr2 := make([]int, len(sarr1)), make([]int, len(sarr2))
		for j := 0; j < len(arr1); j++ {
			arr1[j], _ = strconv.Atoi(sarr1[j])
			arr2[j], _ = strconv.Atoi(sarr2[j])
		}

		sum := float64(0)

		for j := 0; j < len(arr1)-1; j++ {
			if math.Abs(float64(arr1[j]-arr2[j+1])) < math.Abs(float64(arr1[j]-arr1[j+1])) {
				arr1[j+1], arr2[j+1] = arr2[j+1], arr1[j+1]
			}

			sum += math.Abs(float64(arr1[j]-arr1[j+1])) + math.Abs(float64(arr2[j]-arr2[j+1]))
		}

		answers[i] = int(sum)
	}

	for _, answer := range answers {
		fmt.Println(answer)
	}
}

package main

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

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

func A(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriterSize(_w, 102400000)
	defer out.Flush()

	var c int
	var n int
	Fscan(in, &c)
	for t := 0; t < c; t++ {
		Fscan(in, &n)
		a, b := make([]int, n), make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		for i := range b {
			Fscan(in, &b[i])
		}
		ans := 0
		for i := 1; i < n; i++ {
			old := abs(a[i]-a[i-1]) + abs(b[i]-b[i-1])
			swap := abs(b[i]-a[i-1]) + abs(a[i]-b[i-1])
			if swap < old {
				ans += swap
				a[i], b[i] = b[i], a[i]
			} else {
				ans += old
			}
		}
		Fprintln(out, ans)
	}
}

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

package main

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

func ss() {
	n := NextInt()
	a := make([]int, n)
	b := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = NextInt()
	}
	for i := 0; i < n; i++ {
		b[i] = NextInt()
	}
	ans := 0
	for i := 1; i < n; i++ {
		ans += MinInt(AbsInt(b[i-1]-a[i])+AbsInt(a[i-1]-b[i]), AbsInt(a[i-1]-a[i])+AbsInt(b[i-1]-b[i]))
	}
	Out(ans, "\n")
}

func Solve() {
	for T := NextInt(); T > 0; T-- {
		ss()
	}
}

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

func In(a ...interface{}) {
	_, _ = fmt.Fscan(in, a...)
}

func main() {
	Solve()
	out.Flush()
}

func NextInt() int {
	var x int
	_, _ = fmt.Fscan(in, &x)
	return x
}

func Out(a ...interface{}) {
	_, _ = fmt.Fprint(out, a...)
}

func Printf(format string, a ...interface{}) {
	_, _ = fmt.Fprintf(out, format, a...)
}

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

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

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

package main

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

func main() {

	reader := bufio.NewReader(os.Stdin)
	writter := bufio.NewWriter(os.Stdout)
	defer writter.Flush()

	t := 0
	fmt.Fscan(reader, &t)

	for t > 0 {
		n := 0
		fmt.Fscan(reader, &n)

		a := make([]int, n)
		b := make([]int, n)

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

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

		// for i := range result {
		// 	fmt.Fprintf(writter, "%d ", result[i])
		// }
		// fmt.Fprintln(writter)

		fmt.Fprintln(writter, solve(n, a, b))

		t--
	}

	// n := 6
	// a := []int{72, 101, 108, 108, 111, 44}
	// b := []int{10, 87, 111, 114, 108, 100}

	// fmt.Println(solve(n, a, b))

	//ethereum.Ethereum()

}

func solve(n int, a, b []int) int {

	sum := 0

	for i := range a {
		if a[i] > b[i] {
			a[i], b[i] = b[i], a[i]
		}
	}

	// fmt.Println(a)
	// fmt.Println(b)

	for i := 1; i < n; i++ {
		sum += int(math.Abs(float64(a[i] - a[i-1])))
	}

	for i := 1; i < n; i++ {
		sum += int(math.Abs(float64(b[i] - b[i-1])))
	}

	return sum
}

package main

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

func main() {

	reader := bufio.NewReader(os.Stdin)
	writter := bufio.NewWriter(os.Stdout)
	defer writter.Flush()

	t := 0
	fmt.Fscan(reader, &t)

	for t > 0 {
		n := 0
		fmt.Fscan(reader, &n)

		a := make([]int, n)
		b := make([]int, n)

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

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

		// for i := range result {
		// 	fmt.Fprintf(writter, "%d ", result[i])
		// }
		// fmt.Fprintln(writter)

		fmt.Fprintln(writter, solve(n, a, b))

		t--
	}

	// n := 6
	// a := []int{72, 101, 108, 108, 111, 44}
	// b := []int{10, 87, 111, 114, 108, 100}

	// fmt.Println(solve(n, a, b))

	//ethereum.Ethereum()

}

func solve(n int, a, b []int) float64 {

	sum := float64(0)

	for i := range a {
		if a[i] > b[i] {
			a[i], b[i] = b[i], a[i]
		}
	}

	// fmt.Println(a)
	// fmt.Println(b)

	for i := 1; i < n; i++ {
		sum += math.Abs(float64(a[i] - a[i-1]))
	}

	for i := 1; i < n; i++ {
		sum += math.Abs(float64(b[i] - b[i-1]))
	}

	return sum
}

package main

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

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

func (in *CF1661A) 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 *CF1661A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1661A
Date: 19/05/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1661/A
Problem: CF1661A
**/
func (in *CF1661A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		a := make([]int, n)
		for i := 0; i < n; i++ {
			a[i] = in.NextInt()
		}

		b := make([]int, n)
		for i := 0; i < n; i++ {
			b[i] = in.NextInt()
		}

		sum := 0
		for i := 0; i < n; i++ {
			if a[i] > b[i] {
				a[i], b[i] = b[i], a[i]
			}

		}

		for i:=1;i<n;i++{
			sum += int(math.Abs(float64(a[i-1])-float64(a[i])))
			sum += int(math.Abs(float64(b[i-1])-float64(b[i])))
		}
		fmt.Println(sum)
	}
}

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

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

package main

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

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

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

func main() {
	defer out.Flush()

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

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

		a := make([]int, n)
		b := make([]int, n)

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

		ans := 0

		for i := 1; i < n; i++ {
			if abs(a[i-1]-a[i])+abs(b[i-1]-b[i]) <= abs(a[i-1]-b[i])+abs(b[i-1]-a[i]) {
				ans += abs(a[i-1]-a[i]) + abs(b[i-1]-b[i])
			} else {
				ans += abs(a[i-1]-b[i]) + abs(b[i-1]-a[i])
			}
		}

		fmt.Fprintln(out, ans)
	}
}

package main

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

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 readInt64(in *bufio.Reader) int64 {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.ParseInt(nStr, 10, 64)
	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
}
func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

func main() {
	ip := bufio.NewReader(os.Stdin)
	t := readInt(ip)
	for t > 0 {
		t--
		_ = readInt(ip)
		a := readArrInt(ip)
		b := readArrInt(ip)
		ans := 0
		for i := range a {
			if i == 0 {
				continue
			}
			ans += min(abs(a[i]-a[i-1])+abs(b[i]-b[i-1]), abs(a[i]-b[i-1])+abs(b[i]-a[i-1]))
		}
		fmt.Println(ans)
	}
}

func max(i, j int) int {
	if i > j {
		return i
	}
	return j
}

func min(i, j int) int {
	if i > j {
		return j
	}
	return i
}

func reverseSlice(_slice []int) {
	n := len(_slice)
	for i := 0; i < n/2; i++ {
		_slice[i], _slice[n-i-1] = _slice[n-i-1], _slice[i]
	}
}

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


package main

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

var _, _, _ = main, rw, do

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

func rw(r io.Reader, w io.Writer) {
	var t int = 1
	fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		var n int
		fmt.Fscan(r, &n)
		a := make([]int, n)
		for i := range a {
			fmt.Fscan(r, &a[i])
		}
		b := make([]int, n)
		for i := range b {
			fmt.Fscan(r, &b[i])
		}
		w1 := do(n, a, b)
		fmt.Fprintln(w, w1)
	}
}

func do(n int, a, b []int) string {
	ok := true
	for i := range a {
		if a[i] != b[i] {
			ok = false
		}
	}
	if ok {
		return "YES"
	}
	for i := range a {
		if a[i] == b[i] {
			continue
		}
		j := (i + 1) % n
		if a[i] > b[i] || b[i] > b[j]+1 {
			return "NO"
		}
	}
	return "YES"
}

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 n int
		fmt.Fscan(in, &n)

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

		f := true
		for i := range aa {
			if aa[i] > bb[i] || (aa[i] != bb[i] && bb[i] > bb[i+1]+1) {
				f = false
				break
			}
		}
		if f {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

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 n int
		fmt.Fscan(in, &n)

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

		f := true
		for i := range aa {
			if aa[i] > bb[i] {
				f = false
				break
			}
		}
		for i := range aa {
			if aa[i] != bb[i] && bb[i] > bb[i+1]+1 {
				f = false
				break
			}
		}
		if f {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

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 Ok(aArr, bArr []int, n, minInd int) bool {
	for i := 1; i < n; i++ {
		checkInd := (minInd - i + n) % n
		if aArr[checkInd] < bArr[checkInd] && aArr[(checkInd+1)%n]+1 < bArr[checkInd] {
			return false
		}
		aArr[checkInd] = bArr[checkInd]
	}
	return true
}

func Solve(reader *bufio.Reader) {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for i_ := 0; i_ < t; i_++ {
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		aArr := make([]int, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			aArr[i], _ = strconv.Atoi(scanner.Text())
		}
		bArr := make([]int, n)
		minB := 1000000007
		for i := 0; i < n; i++ {
			scanner.Scan()
			bArr[i], _ = strconv.Atoi(scanner.Text())
			if bArr[i] < minB {
				minB = bArr[i]
			}
		}

		minDis := 1000000007
		for i := 0; i < n; i++ {
			dis := bArr[i] - aArr[i]
			if dis < minDis {
				minDis = dis
			}
		}

		if minDis < 0 {
			wr.WriteString("NO\n")
		} else {
			for i := 0; i < n; i++ {
				if aArr[i] < minB {
					aArr[i] = minB
				}
			}
			minDis := 1000000007
			minInd := 0
			for i := 0; i < n; i++ {
				dis := bArr[i] - aArr[i]
				disInd := i
				if dis < minDis {
					minDis = dis
					minInd = disInd
				}
			}
			if Ok(aArr, bArr, n, minInd) {
				wr.WriteString("YES\n")
			} else {
				wr.WriteString("NO\n")
			}
		}
	}
}

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

package main

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

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 Ok(aArr, bArr []int, n, minInd int) bool {
	for i := 1; i < n; i++ {
		checkInd := (minInd - i + n) % n
		if aArr[checkInd] < bArr[checkInd] && aArr[(checkInd+1)%n]+1 < bArr[checkInd] {
			return false
		}
		aArr[checkInd] = bArr[checkInd]
	}
	return true
}

func Solve(reader *bufio.Reader) {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for i_ := 0; i_ < t; i_++ {
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		aArr := make([]int, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			aArr[i], _ = strconv.Atoi(scanner.Text())
		}
		bArr := make([]int, n)
		minB := 1000000007
		for i := 0; i < n; i++ {
			scanner.Scan()
			bArr[i], _ = strconv.Atoi(scanner.Text())
			if bArr[i] < minB {
				minB = bArr[i]
			}
		}

		minDis := 1000000007
		minInd := 0
		for i := 0; i < n; i++ {
			dis := bArr[i] - aArr[i]
			disInd := i
			if dis < minDis {
				minDis = dis
				minInd = disInd
			}
		}

		if minDis < 0 {
			wr.WriteString("NO\n")
		} else if minDis == 0 {
			if Ok(aArr, bArr, n, minInd) {
				wr.WriteString("YES\n")
			} else {
				wr.WriteString("NO\n")
			}
		} else {
			for i := 0; i < n; i++ {
				if aArr[i] < minB {
					aArr[i] = minB
				}
			}
			minDis := 1000000007
			minInd := 0
			for i := 0; i < n; i++ {
				dis := bArr[i] - aArr[i]
				disInd := i
				if dis < minDis {
					minDis = dis
					minInd = disInd
				}
			}
			if Ok(aArr, bArr, n, minInd) {
				wr.WriteString("YES\n")
			} else {
				wr.WriteString("NO\n")
			}
		}
	}
}

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

package main

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

func Solve(reader io.Reader, writer io.Writer) {
	var t int
	fmt.Fscanln(reader, &t)
	for ; t > 0; t-- {
		var n int
		fmt.Fscanln(reader, &n)

		a := make([]int, n)
		b := make([]int, n)

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

		for i := 0; i < n; i++ {
			fmt.Fscanf(reader, "%d", &b[i])
		}
		fmt.Fscanln(reader)

		f := true
		for i := n - 1; i < 2*n; i++ {
			if a[i%n] > b[i%n] || (a[i%n] < b[i%n] && b[i%n] > b[(i+1)%n]+1) {
				f = false
				break
			}
		}
		if f {
			fmt.Fprintln(writer, "YES")
		} else {
			fmt.Fprintln(writer, "NO")
		}
	}
}

func main() {
	// f, err := os.OpenFile("data.txt", os.O_RDONLY, 0644)
	// if err != nil {
	// 	os.Exit(1)
	// }
	// defer f.Close()
	// reader := bufio.NewReader(f)
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	Solve(reader, writer)
	writer.Flush()
}

package main

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

var a, b, f [400005]int

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var T int
	fmt.Fscan(in, &T)
	for c := 0; c < T; c++ {
		var n int
		fmt.Fscan(in, &n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			a[i+n] = a[i]
		}
		a[n*2] = a[0]
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &b[i])
			b[i+n] = b[i]
		}
		b[n*2] = b[0]
		p := n - 1
		failed := false
		for i := 0; i < n; i++ {
			if a[i] <= a[i+1] {
				p = i
				break
			}
		}
		f[p] = 0
		for i := p; i <= n+p+1; i++ {
			f[i+1] = 0
			if f[i] == 1 && a[i] < a[i-1]-1 {
				a[i] = a[i-1] - 1
				f[i+1] = 1
			}
			if a[i] > b[i] {
				failed = true
				break
			}
			if a[i] < b[i] {
				a[i] = b[i]
				f[i+1] = 1
			}
		}
		if failed {
			fmt.Fprintln(out, "NO")
		} else {
			fmt.Fprintln(out, "YES")
		}
	}
}

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 n int
		fmt.Fscan(in, &n)

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

		f := true
		for i := range aa {
			if aa[i] == bb[i] || bb[i] <= bb[i+1]+1 {
				continue
			}
			f = false
			break
		}
		if f {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

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 n int
		fmt.Fscan(in, &n)

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

		f := true
		for i := range aa {
			if aa[i] != bb[i] && bb[i] > bb[i+1]+1 {
				f = false
				break
			}
		}
		if f {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

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 Ok(aArr, bArr []int, n, minInd int) bool {
	for i := 1; i < n; i++ {
		checkInd := (minInd - i + n) % n
		if aArr[checkInd] < bArr[checkInd] && aArr[(checkInd+1)%n]+1 < bArr[checkInd] {
			return false
		}
		aArr[checkInd] = bArr[checkInd]
	}
	return true
}

func Solve(reader *bufio.Reader) {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for i_ := 0; i_ < t; i_++ {
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		aArr := make([]int, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			aArr[i], _ = strconv.Atoi(scanner.Text())
		}
		bArr := make([]int, n)
		minB := 1000000007
		for i := 0; i < n; i++ {
			scanner.Scan()
			bArr[i], _ = strconv.Atoi(scanner.Text())
			if bArr[i] < minB {
				minB = bArr[i]
			}
		}

		minDis := 1000000007
		minInd := 0
		for i := 0; i < n; i++ {
			dis := bArr[i] - aArr[i]
			disInd := i
			if dis < minDis {
				minDis = dis
				minInd = disInd
			}
		}

		if minDis < 0 {
			wr.WriteString("NO\n")
		} else if minDis == 0 {
			if Ok(aArr, bArr, n, minInd) {
				wr.WriteString("YES\n")
			} else {
				wr.WriteString("NO\n")
			}
		} else {
			for i := 0; i < n; i++ {
				if aArr[i] < minB {
					aArr[i] = minB
					minInd = i
				}
			}

			if Ok(aArr, bArr, n, minInd) {
				wr.WriteString("YES\n")
			} else {
				wr.WriteString("NO\n")
			}
		}
	}
}

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

package main

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

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 Ok(aArr, bArr []int, n, minInd int) bool {
	for i := 1; i < n; i++ {
		checkInd := (minInd - i + n) % n
		if aArr[checkInd] < bArr[checkInd] && aArr[(checkInd+1)%n]+1 < bArr[checkInd] {
			return false
		}
		aArr[checkInd] = bArr[checkInd]
	}
	return true
}

func Solve(reader *bufio.Reader) {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for i_ := 0; i_ < t; i_++ {
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		aArr := make([]int, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			aArr[i], _ = strconv.Atoi(scanner.Text())
		}
		bArr := make([]int, n)
		minB := 1000000007
		for i := 0; i < n; i++ {
			scanner.Scan()
			bArr[i], _ = strconv.Atoi(scanner.Text())
			if bArr[i] < minB {
				minB = bArr[i]
			}
		}

		minDis := 1000000007
		minInd := 0
		for i := 0; i < n; i++ {
			dis := bArr[i] - aArr[i]
			disInd := i
			if dis < minDis {
				minDis = dis
				minInd = disInd
			}
		}

		if minDis < 0 {
			wr.WriteString("NO\n")
		} else {
			for i := 0; i < n; i++ {
				if aArr[i] < minB {
					aArr[i] = minB
					minInd = i
				}
			}

			if Ok(aArr, bArr, n, minInd) {
				wr.WriteString("YES\n")
			} else {
				wr.WriteString("NO\n")
			}
		}
	}
}

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

package main

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

func Solve(reader io.Reader, writer io.Writer) {
	var t int
	fmt.Fscanln(reader, &t)
	for ; t > 0; t-- {
		var n int
		fmt.Fscanln(reader, &n)

		a := make([]int, n)
		b := make([]int, n)

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

		for i := 0; i < n; i++ {
			fmt.Fscanf(reader, "%d", &b[i])
		}
		fmt.Fscanln(reader)

		f := true
		for i := n - 1; i < 2*n; i++ {
			if a[i%n] != b[i%n] && b[(i+1)%n] < b[i%n]-1 {
				f = false
				break
			}
		}
		if f {
			fmt.Fprintln(writer, "YES")
		} else {
			fmt.Fprintln(writer, "NO")
		}
	}
}

func main() {
	// f, err := os.OpenFile("data.txt", os.O_RDONLY, 0644)
	// if err != nil {
	// 	os.Exit(1)
	// }
	// defer f.Close()
	// reader := bufio.NewReader(f)
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	Solve(reader, writer)
	writer.Flush()
}

package main

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

func Solve(reader io.Reader, writer io.Writer) {
	var t int
	fmt.Fscanln(reader, &t)
	for ; t > 0; t-- {
		var n int
		fmt.Fscanln(reader, &n)

		a := make([]int, n)
		b := make([]int, n)

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

		for i := 0; i < n; i++ {
			fmt.Fscanf(reader, "%d", &b[i])
		}
		fmt.Fscanln(reader)

		f := true
		for i := n - 1; i < 2*n; i++ {
			if a[i%n] < b[i%n] && b[(i+1)%n] < b[i%n]-1 {
				f = false
				break
			}
		}
		if f {
			fmt.Fprintln(writer, "YES")
		} else {
			fmt.Fprintln(writer, "NO")
		}
	}
}

func main() {
	// f, err := os.OpenFile("data.txt", os.O_RDONLY, 0644)
	// if err != nil {
	// 	os.Exit(1)
	// }
	// defer f.Close()
	// reader := bufio.NewReader(f)
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	Solve(reader, writer)
	writer.Flush()
}

package main

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

var _, _, _ = main, rw, do

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

func rw(r io.Reader, w io.Writer) {
	var t int = 1
	fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		var n int
		fmt.Fscan(r, &n)
		a := make([]int, n)
		for i := range a {
			fmt.Fscan(r, &a[i])
		}
		b := make([]int, n)
		for i := range b {
			fmt.Fscan(r, &b[i])
		}
		w1 := do(n, a, b)
		fmt.Fprintln(w, w1)
	}
}

func do(n int, a, b []int) string {
	ok := true
	for i := range a {
		if a[i] > b[i] {
			return "NO"
		} else if a[i] < b[i] {
			ok = false
		}
	}
	if ok {
		return "YES"
	}
	for i := range a {
		j := (i + 1) % n
		if b[i] > b[j]+1 {
			return "NO"
		}
	}
	return "YES"
}


package main

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

type point struct {
	y, x int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	readint := func() int {
		sc.Scan()
		x, _ := strconv.Atoi(string(sc.Bytes()))
		return x
	}

	n := readint()
	m := readint()

	M := make([]byte, n*m)
	for y := 0; y < n; y++ {
		sc.Scan()
		copy(M[y*m:], sc.Bytes())
	}

	show := func() {
		nl := []byte{'\n'}
		for y := 0; y < n; y++ {
			os.Stdout.Write(M[y*m : (y+1)*m])
			os.Stdout.Write(nl)
		}
	}
	fail := func() {
		os.Stdout.Write([]byte("Not unique\n"))
	}

	at := func(y, x int) byte {
		if y < 0 || y >= n || x < 0 || x >= m {
			return 0
		}
		return M[y*m+x]
	}

	D := []point{{y: 1}, {y: -1}, {x: 1}, {x: -1}}
	// ny,nx = only empty neighbor
	nbAt := func(y, x int) (ny, nx int, ok bool) {
		cnt := 0

		for _, d := range D {
			if at(y+d.y, x+d.x) == '.' {
				cnt++
				ny = y + d.y
				nx = x + d.x
			}
		}

		if cnt != 1 {
			return 0, 0, false
		}
		return ny, nx, true
	}

	paint := func(p, q point) {
		if p.y == q.y {
			if p.x > q.x {
				p.x, q.x = q.x, p.x
			}
			y := p.y
			M[y*m+p.x] = '<'
			M[y*m+q.x] = '>'
		} else {
			x := p.x
			if p.y > q.y {
				p.y, q.y = q.y, p.y
			}
			M[p.y*m+x] = '^'
			M[q.y*m+x] = 'v'
		}
	}

	var good []point
	dotCnt := 0

	for y := 0; y < n; y++ {
		for x := 0; x < m; x++ {
			if at(y, x) != '.' {
				continue
			}
			dotCnt++
			_, _, ok := nbAt(y, x)
			if ok {
				good = append(good, point{y, x})
			}
		}
	}

	for i := 0; i < len(good); i++ {
		p := good[i]
		y, x := p.y, p.x
		if at(y, x) != '.' {
			continue
		}
		ny, nx, ok := nbAt(y, x)
		if !ok {
			continue
		}

		q := point{ny, nx}
		paint(q, p)
		dotCnt -= 2
		if dotCnt == 0 {
			break
		}

		for _, d := range D {
			for _, p := range []point{p, q} {
				_, _, ok := nbAt(p.y+d.y, p.x+d.x)
				if ok {
					good = append(good, point{p.y + d.y, p.x + d.x})
				}
			}
		}
	}

	if dotCnt == 0 {
		show()
	} else {
		fail()
	}
}

package main

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

type point struct {
	y, x int
}

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

	sc.Scan()
	var n, m int
	fmt.Sscanf(sc.Text(), "%d %d", &n, &m)

	M := make([]byte, n*m)
	for y := 0; y < n; y++ {
		sc.Scan()
		copy(M[y*m:], sc.Bytes())
	}

	show := func() {
		nl := []byte{'\n'}
		for y := 0; y < n; y++ {
			os.Stdout.Write(M[y*m : (y+1)*m])
			os.Stdout.Write(nl)
		}
	}
	fail := func() {
		os.Stdout.Write([]byte("Not unique\n"))
	}

	at := func(y, x int) byte {
		if y < 0 || y >= n || x < 0 || x >= m {
			return 0
		}
		return M[y*m+x]
	}

	D := []point{{y: 1}, {y: -1}, {x: 1}, {x: -1}}
	// ny,nx = only empty neighbor
	nbAt := func(y, x int) (ny, nx int, ok bool) {
		cnt := 0

		for _, d := range D {
			if at(y+d.y, x+d.x) == '.' {
				cnt++
				ny = y + d.y
				nx = x + d.x
			}
		}

		if cnt != 1 {
			return 0, 0, false
		}
		return ny, nx, true
	}

	paint := func(p, q point) {
		if p.y == q.y {
			if p.x > q.x {
				p.x, q.x = q.x, p.x
			}
			y := p.y
			M[y*m+p.x] = '<'
			M[y*m+q.x] = '>'
		} else {
			x := p.x
			if p.y > q.y {
				p.y, q.y = q.y, p.y
			}
			M[p.y*m+x] = '^'
			M[q.y*m+x] = 'v'
		}
	}

	var good []point
	dotCnt := 0

	for y := 0; y < n; y++ {
		for x := 0; x < m; x++ {
			if at(y, x) != '.' {
				continue
			}
			dotCnt++
			_, _, ok := nbAt(y, x)
			if ok {
				good = append(good, point{y, x})
			}
		}
	}

	for i := 0; i < len(good); i++ {
		p := good[i]
		y, x := p.y, p.x
		if at(y, x) != '.' {
			continue
		}
		ny, nx, ok := nbAt(y, x)
		if !ok {
			continue
		}

		q := point{ny, nx}
		paint(q, p)
		dotCnt -= 2
		if dotCnt == 0 {
			break
		}

		for _, d := range D {
			for _, p := range []point{p, q} {
				a, b, ok := nbAt(p.y+d.y, p.x+d.x)
				if ok {
					good[i] = point{a, b}
					i--
					goto loop
				}
			}
		}
	loop:
	}

	if dotCnt == 0 {
		show()
		return
	}
	if len(good) == 0 {
		fail()
		return
	}
}

package main

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

type point struct {
	y, x int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	readint := func() int {
		sc.Scan()
		x, _ := strconv.Atoi(string(sc.Bytes()))
		return x
	}

	n := readint()
	m := readint()

	M := make([]byte, n*m)
	for y := 0; y < n; y++ {
		sc.Scan()
		copy(M[y*m:], sc.Bytes())
	}

	show := func() {
		nl := []byte{'\n'}
		for y := 0; y < n; y++ {
			os.Stdout.Write(M[y*m : (y+1)*m])
			os.Stdout.Write(nl)
		}
	}
	fail := func() {
		os.Stdout.Write([]byte("Not unique\n"))
	}

	at := func(y, x int) byte {
		if y < 0 || y >= n || x < 0 || x >= m {
			return 0
		}
		return M[y*m+x]
	}

	D := []point{{y: 1}, {y: -1}, {x: 1}, {x: -1}}
	// ny,nx = only empty neighbor
	nbAt := func(y, x int) (ny, nx int, ok bool) {
		cnt := 0

		for _, d := range D {
			if at(y+d.y, x+d.x) == '.' {
				cnt++
				ny = y + d.y
				nx = x + d.x
			}
		}

		if cnt != 1 {
			return 0, 0, false
		}
		return ny, nx, true
	}

	paint := func(p, q point) {
		if p.y == q.y {
			if p.x > q.x {
				p.x, q.x = q.x, p.x
			}
			y := p.y
			M[y*m+p.x] = '<'
			M[y*m+q.x] = '>'
		} else {
			x := p.x
			if p.y > q.y {
				p.y, q.y = q.y, p.y
			}
			M[p.y*m+x] = '^'
			M[q.y*m+x] = 'v'
		}
	}

	var good []point
	dotCnt := 0

	for y := 0; y < n; y++ {
		for x := 0; x < m; x++ {
			if at(y, x) != '.' {
				continue
			}
			dotCnt++
			_, _, ok := nbAt(y, x)
			if ok {
				good = append(good, point{y, x})
			}
		}
	}

	for i := 0; i < len(good); i++ {
		p := good[i]
		y, x := p.y, p.x
		if at(y, x) != '.' {
			continue
		}
		ny, nx, ok := nbAt(y, x)
		if !ok {
			continue
		}

		q := point{ny, nx}
		paint(q, p)
		dotCnt -= 2
		if dotCnt == 0 {
			break
		}

		for _, d := range D {
			for _, p := range []point{p, q} {
				a, b, ok := nbAt(p.y+d.y, p.x+d.x)
				if ok {
					good[i] = point{a, b}
					i--
					goto loop
				}
			}
		}
	loop:
	}

	if dotCnt == 0 {
		show()
	} else {
		fail()
	}
}

package main

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

type point struct {
	y, x int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	readint := func() int {
		sc.Scan()
		x, _ := strconv.Atoi(string(sc.Bytes()))
		return x
	}

	n := readint()
	m := readint()

	M := make([]byte, n*m)
	for y := 0; y < n; y++ {
		sc.Scan()
		copy(M[y*m:], sc.Bytes())
	}

	show := func() {
		nl := []byte{'\n'}
		for y := 0; y < n; y++ {
			os.Stdout.Write(M[y*m : (y+1)*m])
			os.Stdout.Write(nl)
		}
	}
	fail := func() {
		os.Stdout.Write([]byte("Not unique\n"))
	}

	at := func(y, x int) byte {
		if y < 0 || y >= n || x < 0 || x >= m {
			return 0
		}
		return M[y*m+x]
	}

	D := []point{{y: 1}, {y: -1}, {x: 1}, {x: -1}}
	// ny,nx = only empty neighbor
	nbAt := func(y, x int) (ny, nx int, ok bool) {
		cnt := 0

		for _, d := range D {
			if at(y+d.y, x+d.x) == '.' {
				cnt++
				ny = y + d.y
				nx = x + d.x
			}
		}

		if cnt != 1 {
			return 0, 0, false
		}
		return ny, nx, true
	}

	paint := func(p, q point) {
		if p.y == q.y {
			if p.x > q.x {
				p.x, q.x = q.x, p.x
			}
			y := p.y
			M[y*m+p.x] = '<'
			M[y*m+q.x] = '>'
		} else {
			x := p.x
			if p.y > q.y {
				p.y, q.y = q.y, p.y
			}
			M[p.y*m+x] = '^'
			M[q.y*m+x] = 'v'
		}
	}

	var good []point
	dotCnt := 0

	for y := 0; y < n; y++ {
		for x := 0; x < m; x++ {
			if at(y, x) != '.' {
				continue
			}
			dotCnt++
			_, _, ok := nbAt(y, x)
			if ok {
				good = append(good, point{y, x})
			}
		}
	}

	for i := 0; i < len(good); i++ {
		p := good[i]
		y, x := p.y, p.x
		if at(y, x) != '.' {
			continue
		}
		ny, nx, ok := nbAt(y, x)
		if !ok {
			continue
		}

		q := point{ny, nx}
		paint(q, p)
		dotCnt -= 2
		if dotCnt == 0 {
			break
		}

		for _, d := range D {
			for _, p := range []point{p, q} {
				a, b, ok := nbAt(p.y+d.y, p.x+d.x)
				if ok {
					good = append(good, point{a, b})
				}
			}
		}
	}

	if dotCnt == 0 {
		show()
	} else {
		fail()
	}
}


package main

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

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

	var n, tmp int64
	var slice []int64

	fmt.Fscanf(in, "%d\n", &n)
	for i := int64(0); i < n; i++ {
		fmt.Fscanf(in, "%d", &tmp)
		slice = append(slice, tmp)
	}

	fmt.Println(solution(n, slice))
}

func solution(n int64, slice []int64) (numNeg, numPos int64) {
	var tmpNumPos, tmpNumNeg int64

	prod := int64(1)
	for i := int64(0); i < n; i++ {
		if prod*slice[i] > 0 {
			tmpNumPos++
			prod = 1
		} else {
			tmpNumNeg++
			prod = -1
		}
	}

	numPos = tmpNumPos
	numNeg = tmpNumNeg

	for i := int64(1); i < n; i++ {
		if slice[i-1] > 0 {
			tmpNumPos--
		} else {
			tmpPos := tmpNumPos
			tmpNumPos = tmpNumNeg - 1
			tmpNumNeg = tmpPos
		}
		numPos += tmpNumPos
		numNeg += tmpNumNeg
	}

	return
}

package main

import (
    "bufio"
    "fmt"
    "os"
)
const(
    mod int = 1e9 + 7
    N = 1e6
)
func main() {
    in := bufio.NewReader(os.Stdin)
    var n, a int
    var ansn, ansp, last int64
    var cnt [2]int64
    fmt.Fscan(in, &n)
    b := 1
    for i := 1; i <= n; i++ {
        fmt.Fscan(in, &a)
        if a == 0 {
            cnt[0] = 0
            cnt[1] = 0
            continue
        }
        if a < 0 {
            cnt[b] += int64(i) - last
            last = int64(i)
            b ^= 1
        }
        ansn += cnt[b ^ 1]
        ansp += int64(i) - last + cnt[b]
    }
    fmt.Println(ansn, ansp)
}

package main

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

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

	var n, tmp int
	var slice []int

	fmt.Fscanf(in, "%d\n", &n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &tmp)
		slice = append(slice, tmp)
	}

	fmt.Println(solution(n, slice))
}

func solution(n int, slice []int) (numNeg, numPos int) {
	for i := 0; i < n; i++ {
		prod := 1
		for j := i; j < n; j++ {
			prod *= slice[j]
			if prod > 0 {
				numPos++
			} else {
				numNeg++
			}
		}
	}
	return
}


package main

import (
	"fmt"
)

func main() {
	var n,x,y,d,t int
	fmt.Scanf("%d\n",&t)
	for i := 0; i<t;i++{
		fmt.Scanf("%d %d %d %d\n",&n, &x, &y, &d)
		ans := -1
		if x == y {
			ans = 0
		}
		if y == 1 {
			ans = divUp(x-1,d)
		}
		if y == n {
			ans = divUp(n-x,d)
		}
		if checkDiv(abs(y-x), d){
			ans= abs(y-x)/d
		}
		if checkDiv(y-1,d) {
			temp := divUp(x-1,d) + (y-1)/d
			if ans > temp || ans == -1{
				ans = temp
			}
		}
		if checkDiv(n-y,d) {
			temp := divUp(n-x,d) + (n-y)/d
			if ans>temp || ans == -1 {
				ans = temp
			}
		}
		fmt.Println(ans)
	}
}

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

func divUp(a,b int) int {
	ans :=0
	if a%b != 0 {
		ans +=1
	}
	ans += a/b
	return ans
}

func checkDiv(a, b int) bool {
	if a<b {
		return false
	}
	if a<b {
		c:= a
		a = b
		b = c
	}
	return a%b  == 0
}

package main

import (
	"fmt"
)

func AbsInt(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

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

// gao
// stage-1: x, x-d, x+d, x-2d, x+2d, ...
// stage-2: 1, n
// stage-3: 1+d, n-d, 1+2d, n-2d, ...
func gao(n, x, y, d int) int {
	delta := AbsInt(x - y)
	if delta%d == 0 {
		return delta / d
	}
	stepsLeft, stepsRight := (x+d-1)/d, (n-x+d-1)/d
	if y == 1 {
		return stepsLeft
	}
	if y == n {
		return stepsRight
	}

	if (y-1)%d == 0 {
		if (n-y)%d == 0 {
			return MinInt(stepsLeft+(y-1)/d, stepsRight+(n-y)/d)
		}
		return stepsLeft + (y-1)/d
	} else if (n-y)%d == 0 {
		return stepsRight + (n-y)/d
	}
	return -1
}

func main() {
	var t, n, x, y, d int
	_, _ = fmt.Scanln(&t)
	for i := 0; i < t; i++ {
		_, _ = fmt.Scanln(&n, &x, &y, &d)
		fmt.Printf("%d\n", gao(n, x, y, d))
	}
}

package main

import (
	"fmt"
    "math"
)

func calcPressTime( diff int, distBy1Press int, startTimes int64 ) (ans int64, cant bool) {

    // fmt.Println(startTimes)

    if diff < 0 || distBy1Press < 1{
        return -1, true
    }

    if diff % distBy1Press == 0 {
        return (startTimes + int64(diff / distBy1Press)), false
    } else {
        return -1, true
    }
}

func main() {
	var (
		t,n,x,y,d int
        minAns int64 = 10e10
	)
    fmt.Scan(&t)
    // fmt.Printf("got:%d\n", t )

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

        minAns = 10e10

    	fmt.Scan(&n, &x, &y, &d)

        if x == y {
            fmt.Println(0)
        } else if y == 1 {
            fmt.Println(int(math.Ceil(float64(x-y)/float64(d))))
        } else if y == n {
            fmt.Println(int(math.Ceil(float64(y-x)/float64(d))))
        } else {

            // try up
            ans, cant := calcPressTime( y - x, d, 0 )
            if cant == false && ans < minAns{
                minAns = ans
            }

            // try down
            ans, cant = calcPressTime( x - y, d, 0 )
            if cant == false && ans < minAns{
                minAns = ans
            }

            // up and return
            ans, cant = calcPressTime( n - y, d, int64(math.Ceil(float64(n-x)/float64(d))) )
            if cant == false && ans < minAns{
                minAns = ans
            }

            // down and return
            ans, cant = calcPressTime( y - 1, d, int64(math.Ceil(float64(x-1)/float64(d))) )
            if cant == false && ans < minAns{
                minAns = ans
            }

            if minAns == 10e10 {
                minAns = -1
            }
            fmt.Println(minAns)
        }
    }
}

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() {
	defer writer.Flush()
	var n, x, y, d, t, t1, t2, T int
	scanf("%d\n", &T)
	for t = 0; t < T; t++ {
		scanf("%d %d %d %d\n", &n, &x, &y, &d)

		if (y-x)%d == 0 {
			if x > y {
				x, y = y, x
			}
			printf("%d\n", (y-x)/d)
			continue
		}

		t1 = 1081111111
		if (y-1)%d == 0 {
			t1 = (x-1+d-1)/d + (y-1)/d
		}

		t2 = 1081111111
		if (n-y)%d == 0 {
			t2 = (n-x+d-1)/d + (n-y)/d
		}

		if t2 < t1 {
			t1 = t2
		}

		if t1 == 1081111111 {
			printf("%d\n", -1)
		} else {
			printf("%d\n", t1)
		}

	}
}

package main

import "fmt"

func move(x,y,d int64) (ans int64) {
    if (x < y) {
        tmp := y
        y = x
        x = tmp
    }
    ans = 1 << 55
    if ((x - y) % d == 0) {
        ans = (x - y) / d;
    }
    return
}
func min(a, b int64) int64 {
    if a < b {
        return a
    }
    return b
}
func main() {
    var t int
    fmt.Scan(&t)

    for k := 0; k < t; k++ {
		var n,x,y,d int64

		fmt.Scan(&n,&x,&y,&d)

		ans := move(x,y,d)
		p1 := move(1,y,d) + ((x - 1) / d)

		if ((x - 1) % d != 0) {
		    p1 = p1 + 1
		}

		p2 := move(n,y,d) + ((n - x) / d)

		if ((n - x) % d != 0) {
		    p2 = p2 + 1
		}

		ans = min(ans,min(p1,p2))

		if (ans >= (1 << 55)) {
		    ans = -1
		}

		fmt.Println(ans)

	}
}

package main

import (
	"fmt"
	"bufio"
	"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 abs(val int) int {
	if (val < 0) {
		return -val
	}
	return val
}

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

func main(){
	defer writer.Flush()
	var t,n,d,x,y, ans int
	scanf("%d\n", &t)
	for t > 0 {
		scanf("%d %d %d %d\n", &n, &x, &y, &d)
		if (abs(x-y) % d == 0) {
			printf("%d\n", abs(x-y) / d)
		} else {
			ans = -1
			if ((y-1) % d == 0) {
				ans = (y-1)/d + (x-1 + d-1)/d
			}
			if ((n-y) % d == 0) {
				if (ans == -1){
					ans = (n-y)/d + (n-x + d-1)/d
				} else {
					ans = min(ans, (n-y)/d + (n-x + d-1)/d)
				}
			}
			printf("%d\n", ans)
		}
		t--
	}
}

package main

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

func abs(a,b int64) int64 {
	if a>b{
		return a-b
	}
	return b-a
}
func main(){
	buf := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscanf(buf, "%d\n", &t)
	for i := 1 ;i <= t;i++{
		var n ,d,x,y int64
		fmt.Fscanf(buf, "%d %d %d %d\n", &n,&x,&y,&d)
		var ans1,ans2 int64
		axy := abs(x,y)
		if axy % d == 0{
			fmt.Printf("%d\n",axy/d)
			continue
		}
		ans1= -1
		ans2 = -1
		if abs(1,y)%d == 0{
			ans1 = abs(1,y)/d
			if x != 1{
				ans1 += (x-2)/d+1
			}
		}
		if abs(n,y)%d == 0{
			ans2 = abs(n,y)/d
			if x !=n{
				ans2 += (n-x-1)/d+1
			}
		}
		if ans1 == -1{
			fmt.Printf("%d\n",ans2)
		}else if ans2==-1{
			fmt.Printf("%d\n",ans1)
		}else if ans1 <= ans2{
			fmt.Printf("%d\n",ans1)
		}else{
			fmt.Printf("%d\n",ans2)
		}

	}
}

package main

import (
    "fmt"
//    "os"
)

func max(x, y int64) int64 {
    if x < y {
        return y
    }
    return x
}

func min(x, y int64) int64 {
    if x > y {
        return y
    }
    return x
}

func ldist(x, d int64) int64 {
    if x == 1 {
        return 0
    }
    return (x - 2)/d + 1
}

func rdist(x, d, n int64) int64 {
    return ldist(n - x + 1, d)
}

func dist(n, x, y, d int64) int64 {
    s1 := (x - 1) % d
    s2 := (n - 1) % d
    yd := (y - 1) % d
    if yd == s1 {
        n := x/d
        m := y/d
        dist := n - m
        if dist < 0 {
            dist = -dist
        }
        return dist
    }
    if yd == s2 {
        if s2 == 0 {
            d1 := ldist(x, d) + ldist(y, d)
            d2 := rdist(x, d, n) + rdist(y, d, n)
            return min(d1, d2)
        } else {
            return rdist(x, d, n) + rdist(y, d, n)
        }
    }
    if yd == 0 {
        return ldist(x, d) + ldist(y, d)
    }
    return -1
}

func main() {
    var t int64
    /*r, err := os.Open("tt")
    if err != nil {
        return
    }*/
    fmt.Scanln(&t)
    for i := int64(0); i < t; i++ {
        var n, x, y, d int64
        fmt.Scanln(&n, &x, &y, &d)
        fmt.Println(dist(n, x, y, d))
    }
}

package main

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

func getInt (s *bufio.Scanner) int {
	s.Scan()
	r, _ :=strconv.Atoi(s.Text())
	return r
}

func main() {
	s := bufio.NewScanner(os.Stdin)
	s.Split(bufio.ScanWords)
	n := getInt(s)
	for i := 0; i < n ; i++  {
		n,x,y,d := getInt(s),getInt(s), getInt(s), getInt(s)
		c := -1
		if y==x {
			c=0
		} else {
			a := y-x
		    if a<0 {a*=-1}
		    if a%d==0 {
		    	c=a/d
			} else {
				e, f := -1, -1
				if (y-1)%d == 0 {
					e = (x-1)/d + 1 + (y-1)/d
				}
				if (n-y)%d == 0 {
					f = (n-x)/d + 1 + (n-y)/d
				}
				if e != -1 && f != -1 {
					if e > f {
						c = f
					} else {
						c = e
					}
				} else if e != -1 {
					c = e
				} else {
					c = f
				}
			}
		}
		fmt.Println(c)
	}
}

package main

import "fmt"

func move(x,y,d int64) (ans int64) {
    if (x < y) {
        tmp := y
        y = x
        x = tmp
    }
    ans = 1 << 55
    if ((x - y) % d == 0) {
        ans = (x - y) / d;
    }
    return
}
func min(a, b int64) int64 {
    if a < b {
        return a
    }
    return b
}
func main() {
    var t int
    fmt.Scan(&t)

    for k := 0; k < t; k++ {
		var n,x,y,d int64

		fmt.Scan(&n,&x,&y,&d)

		ans := move(x,y,d)
		p1 := move(1,y,d) + ((y - 1) / d)

		if ((y - 1) % d != 0) {
		    p1 = p1 + 1
		}

		p2 := move(n,y,d) + ((n - y) / d)

		if ((n - y) % d != 0) {
		    p2 = p2 + 1
		}

		ans = min(ans,min(p1,p2))

		if (ans >= (1 << 55)) {
		    ans = -1
		}

		fmt.Println(ans)

	}
}

package main

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

func abs(a,b int64) int64 {
	if a>b{
		return a-b
	}
	return b-a
}
func main(){

	buf := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscanf(buf, "%d\n", &t)
	for i := 1 ;i <= t;i++{
		var n ,d,x,y int64
		fmt.Fscanf(buf, "%d %d %d %d\n", &n,&x,&y,&d)
		var ans1,ans2 int64
		axy := abs(x,y)
		if axy % d == 0{
			fmt.Printf("%d\n",axy/d)
			continue
		}
		ans1= -1
		ans2 = -1
		if abs(1,y)%d == 0{
			ans1 = abs(1,y)/d
			if x != 1{
				ans1 += (x-2)/d+1
			}
		}
		if abs(n,y)%d == 0{
			ans2 = abs(n,y)/d
			if x !=n{
				ans2 += (n-x-1)/d+1
			}
		}
		if ans1 == -1{
			fmt.Printf("%d\n",ans2)
		}else if ans2==-1{
			fmt.Printf("%d\n",ans1)
		}else if ans1 <= ans2{
			fmt.Printf("%d\n",ans1)
		}else{
			fmt.Printf("%d\n",ans1)
		}

	}
}

package main

import (
    "fmt"
)

func max(x, y int64) int64 {
    if x < y {
        return y
    }
    return x
}

func min(x, y int64) int64 {
    if x > y {
        return y
    }
    return x
}

func ldist(x, d int64) int64 {
    if x % d == 0 {
        return x/d
    } else {
        return x/d + 1
    }
}

func rdist(x, d, n int64) int64 {
    return ldist(n - x + 1, d)
}

func dist(n, x, y, d int64) int64 {
    s1 := x % d
    s2 := n % d
    if y % d == s1 {
        n := x/d
        m := y/d
        dist := n - m
        if dist < 0 {
            dist = -dist
        }
        return dist
    }
    if y % d == s2 {
        if s2 == 0 {
            d1 := ldist(x, d) + ldist(y, d)
            d2 := rdist(x, d, n) + rdist(y, d, n)
            return min(d1, d2)
        } else {
            return rdist(x, d, n) + rdist(y, d, n)
        }
    }
    if y % d == 0 {
        return ldist(x, d) + ldist(y, d)
    }
    return -1
}

func main() {
    var t int64
    fmt.Scanln(&t)
    for i := int64(0); i < t; i++ {
        var n, x, y, d int64
        fmt.Scanln(&n, &x, &y, &d)
        fmt.Println(dist(n, x - 1, y - 1, d))
    }
}

package main

import (
	"fmt"
)

func AbsInt(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

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

// gao
// stage-1: x, x-d, x+d, x-2d, x+2d, ...
// stage-2: 1, n
// stage-3: 1+d, n-d, 1+2d, n-2d, ...
func gao(n, x, y, d int) int {
	delta := AbsInt(x - y)
	if delta%d == 0 {
		return delta / d
	}
	stepsLeft, stepsRight := (x+d-1)/d, (n-x+d-1)/d
	if y == 1 {
		return stepsLeft
	}
	if y == n {
		return stepsRight
	}

	if (y-1)%d == 0 {
		if (n-y)%d == 0 {
			return MinInt(stepsLeft+(y-1)/d, stepsRight+(n-y)/d)
		}
		return stepsLeft + (y-1)/d
	} else if (n-y)%d == 0 {
		return stepsRight + (n-y)%d
	}
	return -1
}

func main() {
	var t, n, x, y, d int
	_, _ = fmt.Scanln(&t)
	for i := 0; i < t; i++ {
		_, _ = fmt.Scanln(&n, &x, &y, &d)
		fmt.Printf("%d\n", gao(n, x, y, d))
	}
}

package main

import (
	"fmt"
    "math"
)

func calcPressTime( diff int, distBy1Press int, startTimes int64 ) (ans int64, cant bool) {

    // fmt.Println(startTimes)

    if diff < 0 || distBy1Press < 1{
        return -1, true
    }

    if diff % distBy1Press == 0 {
        return (startTimes + int64(diff / distBy1Press)), false
    } else {
        return -1, true
    }
}

func main() {
	var (
		n,x,y,d int
        minAns int64 = 10e10
	)
	fmt.Scan(&n, &x, &y, &d)

    if x == y {
        fmt.Println(0)
        return
    }

    // try up
    ans, cant := calcPressTime( y - x, d, 0 )
    if cant == false && ans < minAns{
        minAns = ans
    }

    // try down
    ans, cant = calcPressTime( x - y, d, 0 )
    if cant == false && ans < minAns{
        minAns = ans
    }

    // up and return
    ans, cant = calcPressTime( n - y, d, int64(math.Ceil(float64(n-x)/float64(d))) )
    if cant == false && ans < minAns{
        minAns = ans
    }

    // down and return
    ans, cant = calcPressTime( y - 1, d, int64(math.Ceil(float64(x-1)/float64(d))) )
    if cant == false && ans < minAns{
        minAns = ans
    }

    if minAns == 10e10 {
        minAns = -1
    }

	fmt.Println(minAns)
}

package main

import (
	"fmt"
    "math"
)

func calcPressTime( diff int, distBy1Press int, startTimes int64 ) (ans int64, cant bool) {

    // fmt.Println(startTimes)

    if diff < 0 || distBy1Press < 1{
        return -1, true
    }

    if diff % distBy1Press == 0 {
        return (startTimes + int64(diff / distBy1Press)), false
    } else {
        return -1, true
    }
}

func main() {
	var (
		n,x,y,d int
        minAns int64 = 10e10
	)
	fmt.Scan(&n, &x, &y, &d)

    // try up
    ans, cant := calcPressTime( y - x, d, 0 )
    if cant == false && ans < minAns{
        minAns = ans
    }

    // try down
    ans, cant = calcPressTime( x - y, d, 0 )
    if cant == false && ans < minAns{
        minAns = ans
    }

    // up and return
    ans, cant = calcPressTime( n - y, d, int64(math.Ceil(float64(n-x)/float64(d))) )
    if cant == false && ans < minAns{
        minAns = ans
    }

    // down and return
    ans, cant = calcPressTime( y - 1, d, int64(math.Ceil(float64(x-1)/float64(d))) )
    if cant == false && ans < minAns{
        minAns = ans
    }

    if minAns == 10e10 {
        minAns = -1
    }

	fmt.Println(minAns)
}

package main

import (
	"fmt"
    "math"
)

func calcPressTime( diff int, distBy1Press int, startTimes int64 ) (ans int64, cant bool) {

    // fmt.Println(startTimes)

    if diff < 0 || distBy1Press < 1{
        return -1, true
    }

    if diff % distBy1Press == 0 {
        return (startTimes + int64(diff / distBy1Press)), false
    } else {
        return -1, true
    }
}

func main() {
	var (
		t,n,x,y,d int
        minAns int64 = 10e10
	)
    fmt.Scan(&t)
    // fmt.Printf("got:%d\n", t )

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

        minAns = 10e10

    	fmt.Scan(&n, &x, &y, &d)

        if x == y {
            fmt.Println(0)
        } else if y == 1 {
            fmt.Println(int(math.Ceil(float64(n-x)/float64(d))))
        } else if y == n {
            fmt.Println(int(math.Ceil(float64(n-x)/float64(d))))
        } else {

            // try up
            ans, cant := calcPressTime( y - x, d, 0 )
            if cant == false && ans < minAns{
                minAns = ans
            }

            // try down
            ans, cant = calcPressTime( x - y, d, 0 )
            if cant == false && ans < minAns{
                minAns = ans
            }

            // up and return
            ans, cant = calcPressTime( n - y, d, int64(math.Ceil(float64(n-x)/float64(d))) )
            if cant == false && ans < minAns{
                minAns = ans
            }

            // down and return
            ans, cant = calcPressTime( y - 1, d, int64(math.Ceil(float64(x-1)/float64(d))) )
            if cant == false && ans < minAns{
                minAns = ans
            }

            if minAns == 10e10 {
                minAns = -1
            }
            fmt.Println(minAns)
        }
    }
}

package main

import (
	"fmt"
    "math"
)

func calcPressTime( diff int, distBy1Press int, startTimes int64 ) (ans int64, cant bool) {

    // fmt.Println(startTimes)

    if diff < 0 || distBy1Press < 1{
        return -1, true
    }

    if diff % distBy1Press == 0 {
        return (startTimes + int64(diff / distBy1Press)), false
    } else {
        return -1, true
    }
}

func main() {
	var (
		n,x,y,d int
        minAns int64 = 10e10
	)
	fmt.Scan(&n, &x, &y, &d)

    if x == y {
        fmt.Println(0)
        return
    } else if y == 1 {
        fmt.Println(int(math.Ceil(float64(x-1)/float64(d))))
        return
    } else if y == n {
        fmt.Println(int(math.Ceil(float64(n-x)/float64(d))))
        return
    }

    // try up
    ans, cant := calcPressTime( y - x, d, 0 )
    if cant == false && ans < minAns{
        minAns = ans
    }

    // try down
    ans, cant = calcPressTime( x - y, d, 0 )
    if cant == false && ans < minAns{
        minAns = ans
    }

    // up and return
    ans, cant = calcPressTime( n - y, d, int64(math.Ceil(float64(n-x)/float64(d))) )
    if cant == false && ans < minAns{
        minAns = ans
    }

    // down and return
    ans, cant = calcPressTime( y - 1, d, int64(math.Ceil(float64(x-1)/float64(d))) )
    if cant == false && ans < minAns{
        minAns = ans
    }

    if minAns == 10e10 {
        minAns = -1
    }

	fmt.Println(minAns)
}

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() {
	defer writer.Flush()
	var n, x, y, d, t, t1, t2, T int
	scanf("%d\n", &T)
	for t = 0; t < T; t++ {
		scanf("%d %d %d %d\n", &n, &x, &y, &d)
		// if x > y {
		// 	x, y = y, x
		// }
		if (y-x)%d == 0 {
			printf("%d\n", (y-x)/d)
			continue
		}

		t1 = 1081111111
		if (y-1)%d == 0 {
			t1 = (x-1+d-1)/d + (y-1)/d
		}

		t2 = 1081111111
		if (n-y)%d == 0 {
			t2 = (n-x+d-1)/d + (n-y)/d
		}

		if t2 < t1 {
			t1 = t2
		}

		if t1 == 1081111111 {
			printf("%d\n", -1)
		} else {
			printf("%d\n", t1)
		}

	}
}

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() {
	defer writer.Flush()
	var n, x, y, d, t, t1, t2, T int
	scanf("%d\n", &T)
	for t = 0; t < T; t++ {
		scanf("%d %d %d %d\n", &n, &x, &y, &d)
		if x > y {
			x, y = y, x
		}
		if (y-x)%d == 0 {
			printf("%d\n", (y-x)/d)
			continue
		}

		t1 = 1081111111
		if (y-1)%d == 0 {
			t1 = (x-1+d-1)/d + (y-1)/d
		}

		t2 = 1081111111
		if (n-y)%d == 0 {
			t2 = (n-y+d-1)/d + (n-y)/d
		}

		if t2 < t1 {
			t1 = t2
		}

		if t1 == 1081111111 {
			printf("%d\n", -1)
		} else {
			printf("%d\n", t1)
		}

	}
}

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() {
	defer writer.Flush()
	var n, x, y, d, t, t1, t2, T int
	scanf("%d\n", &T)
	for t = 0; t < T; t++ {
		scanf("%d %d %d %d\n", &n, &x, &y, &d)
		if x > y {
			x, y = y, x
		}
		if (y-x)%d == 0 {
			printf("%d\n", (y-x)/d)
			continue
		}

		t1 = 1081111111
		if (y-1)%d == 0 {
			t1 = (x-1+d-1)/d + (y-1)/d
		}

		t2 = 1081111111
		if (n-y)%d == 0 {
			t2 = (n-x+d-1)/d + (n-y)/d
		}

		if t2 < t1 {
			t1 = t2
		}

		if t1 == 1081111111 {
			printf("%d\n", -1)
		} else {
			printf("%d\n", t1)
		}

	}
}

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() {
	defer writer.Flush()
	var n, x, y, d, t, t1, t2, T int
	scanf("%d\n", &T)
	for t = 0; t < T; t++ {
		scanf("%d %d %d %d\n", &n, &x, &y, &d)
		if x > y {
			x, y = y, x
		}
		if (y-x)%d == 0 {
			printf("%d\n", (y-x)/d)
			continue
		}

		t1 = 1081111111
		if (y-1)%d == 0 {
			t1 = (x-1+d-1)/d + (y-1)/d
		}

		t2 = 1081111111
		if (n-y)%d == 0 {
			t1 = (n-y+d-1)/d + (n-y)/d
		}

		if t2 < t1 {
			t1 = t2
		}

		if t1 == 1081111111 {
			printf("%d\n", -1)
		} else {
			printf("%d\n", t1)
		}

	}
}

package main

import (
	"fmt"
)

func main() {
	var n,x,y,d,t int
	fmt.Scanf("%d\n",&t)
	for i := 0; i<t;i++{
		fmt.Scanf("%d %d %d %d\n",&n, &x, &y, &d)
		ans := -1
		if x == y {
			ans = 0
		}
		if checkDiv(y-x, d){
			ans= (y-x)/d
		}
		if checkDiv(y-1,d) {
			temp := divUp(x-1,d) + (y-1)/d
			if ans > temp || ans == -1{
				ans = temp
			}
		}
		if checkDiv(n-y,d) {
			temp := divUp(n-x,d) + (n-y)/d
			if ans>temp || ans == -1 {
				ans = temp
			}
		}
		fmt.Println(ans)
	}
}

func divUp(a,b int) int {
	ans :=0
	if a%b != 0 {
		ans +=1
	}
	ans += a/b
	return ans
}

func checkDiv(a, b int) bool {
	if a<b {
		return false
	}
	if a<b {
		c:= a
		a = b
		b = c
	}
	return a%b  == 0
}


package main

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

func main() {
	var n, max, min, currNumber, result int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &n)
	intArr := make([]int, n)
	min = -1
	for index := 0; index < n; index++ {
		fmt.Fscan(reader, &currNumber)
		if min > currNumber || min == -1 {
			min = currNumber
		} else if max < currNumber {
			max = currNumber
		}
		intArr[index] = currNumber
	}
	for _, element := range intArr {
		if element > min && element < max {
			result++
		}
	}
	fmt.Println(result)
}

package main

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

func solve() {
	a := readIntArray(readInt())
	sort.Ints(a)
	mm := map[int]bool{}
	for _, v := range a {
		mm[v] = true
	}
	prefix := make([]int, len(a))
	suffix := make([]int, len(a))
	for i := len(a) - 2; i >= 0; i-- {
		prefix[i] = prefix[i+1]
		if a[i] < a[i+1] {
			prefix[i]++
		}
	}
	for i := 1; i < len(a); i++ {
		suffix[i] = suffix[i-1]
		if a[i] > a[i-1] {
			suffix[i]++
		}
	}
	res := 0
	for i, _ := range a {
		if suffix[i] > 0 && prefix[i] > 0 {
			res++
		}
	}
	fmt.Println(res)
}

var scanner *bufio.Scanner

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

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

// IO

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

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

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

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

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

package main

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

func solve(a []int) int {
	sort.Ints(a)
	i, j := 0, len(a) - 1
	for i+1 < len(a) && a[i] == a[i+1] {
		i += 1
	}
	for j > 1 && a[j-1] == a[j] {
		j -= 1
	}
	r := j - i - 1
	if r < 0 {
		return 0
	}
	return r
}

func main() {
	cin := NewParser(bufio.NewReaderSize(os.Stdin, 1<<16))
	n := cin.nextInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = cin.nextInt()
	}
	fmt.Printf("%d", solve(a))
}

// some io

type ParserReader struct {
	r   io.ByteReader
	err error
	c   byte
}

func NewParser(r io.ByteReader) *ParserReader {
	return &ParserReader{r, nil, 0}
}

func (p *ParserReader) next() error {
	p.c, p.err = p.r.ReadByte()
	return p.err
}

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

func (p *ParserReader) nextUInt64() uint64 {
	for !isDigit(p.c) && p.err == nil {
		p.next()
	}
	var r uint64
	for isDigit(p.c) && p.err == nil {
		r = r*10 + uint64(p.c - '0')
		p.next()
	}
	return r
}

func (p *ParserReader) nextUInt() uint {
	for !isDigit(p.c) && p.err == nil {
		p.next()
	}
	var r uint
	for isDigit(p.c) && p.err == nil {
		r = r*10 + uint(p.c - '0')
		p.next()
	}
	return r
}

func (p *ParserReader) nextInt64() int64 {
	for p.c != '-' && !isDigit(p.c) && p.err == nil {
		p.next()
	}
	var sign bool
	if p.c == '-' {
		sign = true
	}
	r := int64(p.nextUInt64())
	if sign {
		return -r
	}
	return r
}

func (p *ParserReader) nextInt() int {
	for p.c != '-' && !isDigit(p.c) && p.err == nil {
		p.next()
	}
	var sign bool
	if p.c == '-' {
		sign = true
	}
	r := int(p.nextUInt())
	if sign {
		return -r
	}
	return r
}

package main

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

func main() {
    var n int
    fmt.Scan(&n)
    steward := make([]int, n)
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    for i:=0; scanner.Scan(); i++{
        steward[i], _ = strconv.Atoi(scanner.Text())
    }
    sort.Ints(steward)
    cnt := 0
    for i:=1; i+1<n; i++ {
        if steward[i] > steward[0] && steward[i] < steward[n-1] {
            cnt++
        }
    }
    fmt.Print(cnt)
}

package main

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

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

	mn := A[0]
	mx := A[0]
	mxCount := 1
	mnCount := 1
	for i := 1; i < N; i++ {
		if mn == A[i] {
			mnCount++
		} else if mn > A[i] {
			mn = A[i]
			mnCount = 1
		}
		if mx == A[i] {
			mxCount++
		} else if mx < A[i] {
			mx = A[i]
			mxCount = 1
		}
	}

	if mn == mx {
		fmt.Println(0)
		return
	}

	fmt.Println(N - mxCount - mnCount)

}

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

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

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

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

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

	return mp
}

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

package main

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

func main() {
	fsc := NewFastScanner()
	N := fsc.NextInt()
	A := make([]int, 0)
	for i := 0; i < N; i++ {
		A = append(A, fsc.NextInt())
	}
	sort.Ints(A)
	pre := A[0]
	last := A[N-1]
	res := 0
	for i := 1; i < N-1; i++ {
		if pre < A[i] && A[i] < last {
			res++
		}
	}
	fmt.Println(res)
}

//template
type FastScanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

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

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

//Max,Min
func IntMax(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func Int64Max(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}
func Float64Max(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

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

func Int64Min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}
func Float64Min(a, b float64) float64 {
	if a > b {
		return b
	}
	return a
}

//Gcd
func IntGcd(a, b int) int {
	if a < b {
		b, a = a, b
	}
	if b == 0 {
		return a
	}
	return IntGcd(b, a%b)
}
func Int64Gcd(a, b int64) int64 {
	if a < b {
		b, a = a, b
	}
	if b == 0 {
		return a
	}
	return Int64Gcd(b, a%b)
}

func IntAbs(a int) int {
	if a < 0 {
		a *= -1
	}
	return a
}

func Int64Abs(a int64) int64 {
	if a < 0 {
		a *= -1
	}
	return a
}

package main

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

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

	array := make([]int, n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 0; scanner.Scan(); i++ {
		a, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}
		array[i] = a
	}

	merge_sort(array, 0, n-1)

	start := 0
	end := n - 1
	for ; start < n; start++ {
		if array[start] > array[0] {
			break
		}
	}
	for ; end >= 0; end-- {
		if array[end] < array[n-1] {
			break
		}
	}
	if start <= end {
		fmt.Println(end - start + 1)
	} else {
		fmt.Println(0)
	}
}

func merge(buffer []int, start, middle, end int) {
	sorted := make([]int, end-start+1)

	i := start
	j := middle + 1
	k := 0
	for i <= middle && j <= end {
		if buffer[i] <= buffer[j] {
			sorted[k] = buffer[i]
			i++
		} else {
			sorted[k] = buffer[j]
			j++
		}
		k++
	}
	for i <= middle {
		sorted[k] = buffer[i]
		k++
		i++
	}
	for j <= end {
		sorted[k] = buffer[j]
		k++
		j++
	}

	i = start
	k = 0
	for i <= end {
		buffer[i] = sorted[k]
		i++
		k++
	}
}

func merge_sort(buffer []int, start, end int) {
	if end > start {
		middle := (end + start) / 2
		merge_sort(buffer, start, middle)
		merge_sort(buffer, middle+1, end)
		merge(buffer, start, middle, end)
	}
}

package main

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

func solve() {
	a := readIntArray(readInt())
	sort.Ints(a)
	mm := map[int]bool{}
	for _, v := range a {
		mm[v] = true
	}
	prefix := make([]int, len(a))
	for i := len(a) - 2; i >= 0; i-- {
		prefix[i] = prefix[i+1]
		if a[i] < a[i+1] {
			prefix[i]++
		}
	}
	res := 0
	for i, v := range a {
		if _, exists := mm[v-1]; exists && prefix[i] > 0 {
			res++
		}
	}
	fmt.Println(res)
}

var scanner *bufio.Scanner

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

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

// IO

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

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

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

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

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

package main

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

func main() {
    var n int
    fmt.Scan(&n)
    steward := make([]int, n)
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    for i:=0; scanner.Scan();  {
        steward[i], _ = strconv.Atoi(scanner.Text())
    }
    sort.Ints(steward)
    cnt := 0
    for i:=1; i+1<n; i++ {
        if steward[i] > steward[0] && steward[i] < steward[n-1] {
            cnt++
        }
    }
    fmt.Print(cnt)
}

package main

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

func main() {
	var n, max, min, currNumber, result int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &n)
	intArr := make([]int, n)
	min = 1
	for index := 0; index < n; index++ {
		fmt.Fscan(reader, &currNumber)
		if min > currNumber {
			min = currNumber
		} else if max < currNumber {
			max = currNumber
		}
		intArr[index] = currNumber
	}
	for _, element := range intArr {
		if element > min && element < max {
			result++
		}
	}
	fmt.Println(result)
}