You are given a sequence of $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$. You are also given $$$x$$$ integers $$$1, 2, \dots, x$$$.You are asked to insert each of the extra integers into the sequence $$$a$$$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence.The score of the resulting sequence $$$a'$$$ is the sum of absolute differences of adjacent elements in it $$$\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$$$.What is the smallest possible score of the resulting sequence $$$a'$$$?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n, x \le 2 \cdot 10^5$$$) — the length of the sequence and the number of extra integers. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$.

For each testcase, print a single integer — the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it.

NoteHere are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score.   $$$\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$$$  $$$\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$$$  $$$6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$$$  $$$1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$$$

package main

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

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

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

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var n, x int64
		Fscan(in, &n, &x)
		a := make([]int64, n+x)
		ans := L0
		ma := L0
		mi := int64(math.MaxInt64)

		for i := L0; i < n; i++ {
			Fscan(in, &a[i])
			if a[i] > ma {
				ma = a[i]
			}
			if a[i] < mi {
				mi = a[i]
			}
			if i > 0 {
				ans += abs(int64(a[i] - a[i-1]))
			}
		}

		ans += int64(Min(Min(a[0]-L1, a[n-1]-L1), (mi-1)*2))

		if x > ma {
			ans += int64(Min(Min(x-a[0], x-a[n-1]), (x-ma)*2))
		}

		Fprintln(out, ans)
	}
}

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

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

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

package main

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

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

func main() {
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var n, x int
		fmt.Fscan(in, &n, &x)
		arr := make([]int, n)
		minn, maxn := math.MaxInt32, math.MinInt32
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			minn = min(arr[i], minn)
			maxn = max(arr[i], maxn)
		}
		cur := int64(0)

		if n == 1 {
			if arr[0] >= 1 && arr[0] <= x {
				cur = int64(x - 1)
			} else {
				cur = int64(arr[0] - 1)
			}
		} else {
			for i := 0; i < n-1; i++ {
				cur += int64(abs(arr[i] - arr[i+1]))
			}
			if minn <= 1 && x <= maxn {

			}
			if 1 < minn {
				num1 := abs(min(arr[0], arr[n-1]) - 1)
				num2 := math.MaxInt32
				for i := 0; i < n-1; i++ {
					num2 = min(num2, (min(arr[i], arr[i+1])-1)*2)
				}
				cur += int64(min(num1, num2))
			}
			if x > maxn {
				num1 := abs(x - max(arr[0], arr[n-1]))
				num2 := math.MaxInt32
				for i := 0; i < n-1; i++ {
					num2 = min(num2, (x-max(arr[i], arr[i+1]))*2)
				}
				cur += int64(min(num1, num2))
			}
		}

		fmt.Fprintln(out, cur)
	}
}

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

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

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

package main

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

const MOD int64 = 1000000007

var n, x int

var a []int

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

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

func main() {

	read := bufio.NewReader(os.Stdin)

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

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

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

		a = make([]int, n)

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

		solve()
	}
}

type pair struct {
	v int
	idx []int
}

//a b c d

//20 12 -- 4
//4  2  -- 4

func solve()  {

	var min int = 1e9
	var max int = 0

	var sum int64

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

	if x < min {

		var m int = 1e9

		if abs(1 - a[0]) < m {
			m = abs(1 - a[0])
		}
		if abs(x - a[0]) < m {
			m = abs(x - a[0])
		}
		if abs(1 - a[n-1]) < m {
			m = abs(1 - a[n-1])
		}
		if abs(x - a[n - 1]) < m {
			m = abs(x- a[n-1])
		}

		for i := 0; i < n - 1; i++ {
			l := a[i]
			r := a[i + 1]

			temp := abs(l - 1) + abs(x - r) - abs(r - l)

			if temp < m {
				m = temp
			}
		}

		sum += int64(m)
		sum += int64(x - 1)

		fmt.Println(sum)

	} else if x >= min && x <= max {

		if min == 1 {
			fmt.Println(sum)

		} else if min == 2 {
			aa := abs(1 - a[0])
			bb := abs(1 - a[n - 1])
			cc := 2

			if cc < bb && cc < aa {
				sum +=2
			} else {
				sum += int64(minf(aa, bb))
			}

			fmt.Println(sum)

		} else {
			//1 .... min - 1
			var m int = 1e9

			if abs(1 - a[0]) < m {
				m = abs(1 - a[0])
			}
			if abs(min - 1 - a[0]) < m {
				m = abs(min - 1 - a[0])
			}
			if abs(1 - a[n - 1]) < m {
				m = abs(1 - a[n - 1])
			}
			if abs(min - 1 - a[n - 1]) < m {
				m = abs(min - 1 - a[n - 1])
			}

			if abs(1 - min) + 1 < m {
				m = abs(1 - min) + 1
			}

			if abs(min) < m {
				m = abs(min)
			}

			//1  .... min - 1
			//min     min + 1

			sum += int64(m)
			sum += int64(min - 2)

			fmt.Println(sum)
		}
	} else {
		var m int = 1e9

		if abs(max + 1 - a[0]) < m {
			m = abs(max + 1 - a[0])
		}
		if abs(x - a[0]) < m {
			m = abs(x - a[0])
		}
		if abs(max + 1 - a[n - 1]) < m {
			m = abs(max + 1 - a[n - 1])
		}
		if abs(x - a[n - 1]) < m {
			m = abs(x - a[n - 1])
		}

		if abs(x - max) + 1 < m {
			m = abs(x - max) + 1
		}

		if abs(max - 1 - x) < m {
			m = abs(max - 1 - x)
		}

		//max - 1        max
		//max + 1   ..... x

		sum += int64(m)
		sum += int64(x - max - 1)

		if min == 1 {

			fmt.Println(sum)

		} else if min == 2 {

			aa := abs(1 - a[0])
			bb := abs(1 - a[n - 1])
			cc := 2

			if cc < bb && cc < aa {
				sum +=2
			} else {
				sum += int64(minf(aa, bb))
			}

			fmt.Println(sum)

		} else {

			var m int = 1e9

			if abs(1 - a[0]) < m {
				m = abs(1 - a[0])
			}
			if abs(min - 1 - a[0]) < m {
				m = abs(min - 1 - a[0])
			}
			if abs(1 - a[n - 1]) < m {
				m = abs(1 - a[n - 1])
			}
			if abs(min - 1 - a[n - 1]) < m {
				m = abs(min - 1 - a[n - 1])
			}

			if abs(1 - min) + 1 < m {
				m = abs(1 - min) + 1
			}

			if abs(min) < m {
				m = abs(min)
			}

			sum += int64(m)
			sum += int64(min - 2)

			fmt.Println(sum)

		}

	}
}

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

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

package main

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

var i int64

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

	return b
}

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

func reverse(s []int64) {
	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		s[i], s[j] = s[j], s[i]
	}
}

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

	defer w.Flush()

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

	for ; t > 0; t-- {
		var n int64
		var x int64

		fmt.Fscan(r, &n, &x)

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

		var ans int64 = 1e18
		var current int64 = 0

		for i = 0; i < n-1; i++ {
			current += abs(a[i] - a[i+1])
		}

		for j := 0; j < 2; j++ {
			mn := abs(a[0] - 1)
			ans = min(ans, current+abs(a[0]-x)+x-1)

			for i = 0; i < n-1; i++ {
				ans = min(ans, current+mn-abs(a[i]-a[i+1])+abs(a[i]-x)+abs(a[i+1]-x))
				ans = min(ans, current-abs(a[i]-a[i+1])+abs(a[i]-x)+abs(a[i+1]-1)+(x-1))
				mn = min(mn, 0-abs(a[i]-a[i+1])+abs(a[i]-1)+abs(a[i+1]-1))
			}

			ans = min(ans, current+mn+abs(a[n-1]-x))
			reverse(a)
		}

		fmt.Fprintln(w, ans)
	}
}

package main

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

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

	var T, n, x int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &x)
		a := make([]int, n)
		mi, mx := int(1e9), 0
		ans := int64(0)
		for i := range a {
			Fscan(in, &a[i])
			mi = min(mi, a[i])
			mx = max(mx, a[i])
			if i > 0 {
				ans += int64(abs(a[i] - a[i-1]))
			}
		}
		ans += int64(min(min(a[0], a[n-1])-1, mi*2-2))
		if mx < x {
			ans += int64(min(x-max(a[0], a[n-1]), (x-mx)*2))
		}
		Fprintln(out, ans)
	}
}

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

func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}
func max(a, b int) int {
	if b > a {
		return b
	}
	return a
}
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 CF1671D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T, n, x, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &x, &v)
		st, mi, mx, pre := v, v, v, v
		ans := int64(0)
		for ; n > 1; n-- {
			Fscan(in, &v)
			if v < mi {
				mi = v
			} else if v > mx {
				mx = v
			}
			ans += int64(abs(v - pre))
			pre = v
		}
		ans += int64(min(min(st, v)-1, mi*2-2))
		if mx < x {
			ans += int64(min(x-max(st, v), (x-mx)*2))
		}
		Fprintln(out, ans)
	}
}

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

func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}
func max(a, b int) int {
	if b > a {
		return b
	}
	return a
}
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 CF1671D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T, n, x int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &x)
		a := make([]int, n)
		mi, mx := int(1e9), 0
		ans := int64(0)
		for i := range a {
			Fscan(in, &a[i])
			mi = min(mi, a[i])
			mx = max(mx, a[i])
			if i > 0 {
				ans += int64(abs(a[i] - a[i-1]))
			}
		}
		ans += int64(min(min(a[0], a[n-1])-1, mi*2-2))
		if mx < x {
			ans += int64(min(x-max(a[0], a[n-1]), (x-mx)*2))
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

const mod = 998244353

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n int
	var x int64
	fmt.Fscan(in,&n,&x)
	arr := make([]int64,n)
	for i := 0;i < n;i++{
		fmt.Fscan(in,&arr[i])
	}
	cur := int64(0)
	for i := 1;i < n;i++{
		cur += abs(arr[i] - arr[i - 1])
	}
	ans := int64(1e18)
	for z := 0; z < 2; z++ {
		mn := arr[0] - 1
		ans = min(ans,cur + abs(arr[0] - x) + x - 1)
		for i := 0;i < n - 1;i++{
			ans = min(ans,cur - abs(arr[i + 1] - arr[i]) + abs(arr[i + 1] - x) + abs(arr[i] - 1) + (x - 1))
			ans = min(ans,cur - abs(arr[i + 1] - arr[i]) + mn + abs(arr[i] - x) + abs(arr[i + 1] - x))
			mn = min(mn,0 - abs(arr[i + 1] - arr[i]) + abs(arr[i] - 1) + abs(arr[i + 1] - 1))
		}
		ans = min(ans,cur + mn + abs(arr[n - 1] - x))
		reverse(arr)
	}
	fmt.Fprintln(out,ans)
}
func reverse(arr []int64){
	n := len(arr)
	for i := 0;i < len(arr) / 2;i++{
		arr[i],arr[n - i - 1] = arr[n - i - 1],arr[i]
	}
}

func abs(a int64)int64{
	if a > 0{
		return a
	}
	return -a
}
func sum(a,b int64)int64{
	return (b + a) * (b - a + 1) / 2
}

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

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

package main

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

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

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

	t, n, x := 0, 0, int64(0)
	Fscan(in, &t)
	for ; t > 0; t-- {
		Fscan(in, &n, &x)
		a := make([]int64, n)
		mx, mi := int64(-1e10), int64(1e10)
		ans := int64(0)
		for i := range a {
			Fscan(in, &a[i])
			mx = max(mx, a[i])
			mi = min(mi, a[i])
			if i > 0 {
				ans += abs(a[i] - a[i-1])
			}
		}
		ans += min((mi-1)*2, min(a[0]-1, a[n-1]-1))
		mx = min(mx, x)
		ans += min((x-mx)*2, min(max(0, x-a[0]), max(0, x-a[n-1])))
		Fprintln(out, ans)
	}
}

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

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

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

package main

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

var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)

func slove() {
	defer w.Flush()
	var n int
	var x int64
	fmt.Fscan(r, &n, &x)
	nums := make([]int64, n)
	var ans int64
	var max_v, min_v int64
	min_v = math.MaxInt64
	for i := range nums {
		fmt.Fscan(r,&nums[i])
		if i > 0 {
			ans += abs(nums[i] - nums[i-1])
		}
		if nums[i] > max_v {
			max_v = nums[i]
		}
		if nums[i] < min_v {
			min_v = nums[i]
		}
	}
	if x > max_v {
		ans += min((x-max_v) * 2, min(x-nums[0], x-nums[n-1]))
	}
	if min_v > 1 {
		ans += min((min_v - 1) * 2, min(nums[0]-1, nums[n-1]-1))
	}
	fmt.Fprintln(w, ans)
}

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

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

package main

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

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

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

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var n, x int64
		Fscan(in, &n, &x)
		a := make([]int64, n+x)
		ans := L0
		ma := L0
		mi := int64(math.MaxInt64)

		for i := L0; i < n; i++ {
			Fscan(in, &a[i])
			if a[i] > ma {
				ma = a[i]
			}
			if a[i] < mi {
				mi = a[i]
			}
			if i > 0 {
				ans += abs(int64(a[i] - a[i-1]))
			}
		}

		ans += int64(Min(Min(a[0]-L1, a[n-1]-L1), (mi-1)*2))

		if x > ma {
			ans += int64(Min(x-a[0], x-a[n-1]))
		}

		Fprintln(out, ans)
	}
}

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

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

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

package main

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

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

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

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var n, x int64
		Fscan(in, &n, &x)
		a := make([]int64, n+x)
		ans := L0
		ma := L0
		mi := int64(math.MaxInt)

		for i := L0; i < n; i++ {
			Fscan(in, &a[i])
			if a[i] > ma {
				ma = a[i]
			}
			if a[i] < mi {
				mi = a[i]
			}
			if i > 0 {
				ans += abs(int64(a[i] - a[i-1]))
			}
		}

		ans += int64(Min(Min(a[0]-L1, a[n-1]-L1), (mi-1)*2))

		if x > ma {
			ans += int64(Min(x-a[0], x-a[n-1]))
		}

		Fprintln(out, ans)
	}
}

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

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

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

package main

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

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

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

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var n, x int
		Fscan(in, &n, &x)
		a := make([]int, n+x)
		ans := L0
		ma := 0
		mi := math.MaxInt

		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
			if a[i] > ma {
				ma = a[i]
			}
			if a[i] < mi {
				mi = a[i]
			}
			if i > 0 {
				ans += abs(int64(a[i] - a[i-1]))
			}
		}

		ans += int64(Min(Min(a[0]-1, a[n-1]-1), int(mi-1)*2))

		if x > ma {
			ans += int64(Min(x-a[0], x-a[n-1]))
		}

		Fprintln(out, ans)
	}
}

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

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

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

package main

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

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

func main() {
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var n, x int
		fmt.Fscan(in, &n, &x)
		arr := make([]int, n)
		minn, maxn := math.MaxInt32, math.MinInt32
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			minn = min(arr[i], minn)
			maxn = max(arr[i], maxn)
		}
		cur := 0

		if n == 1 {
			if arr[0] >= 1 && arr[0] <= x {
				cur = x - 1
			} else {
				cur = arr[0] - 1
			}
		} else {
			for i := 0; i < n-1; i++ {
				cur += abs(arr[i] - arr[i+1])
			}
			if minn <= 1 && x <= maxn {

			}
			if 1 < minn {
				num1 := abs(min(arr[0], arr[n-1]) - 1)
				num2 := math.MaxInt32
				for i := 0; i < n-1; i++ {
					num2 = min(num2, (min(arr[i], arr[i+1])-1)*2)
				}
				cur += min(num1, num2)
			}
			if x > maxn {
				num1 := abs(x - max(arr[0], arr[n-1]))
				num2 := math.MaxInt32
				for i := 0; i < n-1; i++ {
					num2 = min(num2, (x-max(arr[i], arr[i+1]))*2)
				}
				cur += min(num1, num2)
			}
		}

		fmt.Fprintln(out, cur)
	}
}

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

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

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

package main

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

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

func main() {
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var n, x int
		fmt.Fscan(in, &n, &x)
		arr := make([]int, n)
		minn, maxn := math.MaxInt32, math.MinInt32
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			minn = min(arr[i], minn)
			maxn = max(arr[i], maxn)
		}
		cur := 0

		if n == 1 {
			if arr[0] >= 1 && arr[0] <= x {
				cur = x - 1
			} else {
				cur = arr[0] - 1
			}
		} else {
			for i := 0; i < n-1; i++ {
				cur += abs(arr[i] - arr[i+1])
			}
			if minn <= 1 && x <= maxn {

			}
			if 1 < minn {
				num1 := abs(min(arr[0], arr[n-1]) - 1)
				num2 := math.MaxInt32
				for i := 0; i < n-1; i++ {
					num2 = min(num2, (min(arr[i], arr[i+1])-1)*2)
				}
				cur += min(num1, num2)
			}
			if x > maxn {
				num1 := abs(x - max(arr[0], arr[n-1]))
				num2 := math.MaxInt32
				for i := 0; i < n-1; i++ {
					num2 = min(num2, (x-min(arr[i], arr[i+1]))*2)
				}
				cur += min(num1, num2)
			}
		}

		fmt.Fprintln(out, cur)
	}
}

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

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

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

package main

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

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

func main() {
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var n, x int
		fmt.Fscan(in, &n, &x)
		arr := make([]int, n)
		minn, maxn := math.MaxInt32, math.MinInt32
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			minn = min(arr[i], minn)
			maxn = max(arr[i], maxn)
		}
		cur := 0
		for i := 0; i < n-1; i++ {
			cur += abs(arr[i] - arr[i+1])
		}
		if minn <= 1 && x <= maxn {

		}
		if 1 < minn {
			num1 := abs(min(arr[0], arr[n-1]) - 1)
			num2 := math.MaxInt32
			for i := 0; i < n-1; i++ {
				num2 = min(num2, (min(arr[i], arr[i+1])-1)*2)
			}
			cur += min(num1, num2)
		}
		if x > maxn {
			num1 := abs(x - max(arr[0], arr[n-1]))
			num2 := math.MaxInt32
			for i := 0; i < n-1; i++ {
				num2 = min(num2, (x-min(arr[i], arr[i+1]))*2)
			}
			cur += min(num1, num2)
		}
		fmt.Fprintln(out, cur)
	}
}

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

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

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

package main

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

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

func main() {
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var n, x int
		fmt.Fscan(in, &n, &x)
		arr := make([]int, n)
		minn, maxn := math.MaxInt32, math.MinInt32
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			minn = min(arr[i], minn)
			maxn = max(arr[i], maxn)
		}
		cur := 0
		for i := 0; i < n-1; i++ {
			cur += abs(arr[i] - arr[i+1])
		}
		if minn <= 1 && x <= maxn {

		}
		if 1 < minn {
			num1 := min(arr[0], arr[n-1]) - 1
			num2 := math.MaxInt32
			for i := 0; i < n-1; i++ {
				num2 = min(num2, (min(arr[i], arr[i+1])-1)*2)
			}
			cur += min(num1, num2)
		}
		if x > maxn {
			num1 := x - max(arr[0], arr[n-1])
			num2 := math.MaxInt32
			for i := 0; i < n-1; i++ {
				num2 = min(num2, (x-min(arr[i], arr[i+1]))*2)
			}
			cur += min(num1, num2)
		}
		fmt.Fprintln(out, cur)
	}
}

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

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

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

package main

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

const MOD int64 = 1000000007

var n, x int

var a []int

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

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

func main() {

	read := bufio.NewReader(os.Stdin)

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

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

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

		a = make([]int, n)

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

		solve()
	}
}

type pair struct {
	v int
	idx []int
}

//a b c d

//20 12 -- 4
//4  2  -- 4

func solve()  {

	var min int = 1e9
	var max int = 0

	var sum int64

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

	if x < min {

		s := do(1, x)

		sum += s

		fmt.Println(sum)

	} else if x >= min && x <= max {

		if min == 1 {
			fmt.Println(sum)

		} else if min == 2 {
			aa := abs(1 - a[0])
			bb := abs(1 - a[n - 1])
			cc := 2

			if cc < bb && cc < aa {
				sum +=2
			} else {
				sum += int64(minf(aa, bb))
			}

			fmt.Println(sum)

		} else {
			//1 .... min - 1
			var m int = 1e9

			if abs(1 - a[0]) < m {
				m = abs(1 - a[0])
			}
			if abs(min - 1 - a[0]) < m {
				m = abs(min - 1 - a[0])
			}
			if abs(1 - a[n - 1]) < m {
				m = abs(1 - a[n - 1])
			}
			if abs(min - 1 - a[n - 1]) < m {
				m = abs(min - 1 - a[n - 1])
			}

			if abs(1 - min) + 1 < m {
				m = abs(1 - min) + 1
			}

			if abs(min) < m {
				m = abs(min)
			}

			//1  .... min - 1
			//min     min + 1

			sum += int64(m)
			sum += int64(min - 2)

			fmt.Println(sum)
		}
	} else {
		var m int = 1e9

		if abs(max + 1 - a[0]) < m {
			m = abs(max + 1 - a[0])
		}
		if abs(x - a[0]) < m {
			m = abs(x - a[0])
		}
		if abs(max + 1 - a[n - 1]) < m {
			m = abs(max + 1 - a[n - 1])
		}
		if abs(x - a[n - 1]) < m {
			m = abs(x - a[n - 1])
		}

		if abs(x - max) + 1 < m {
			m = abs(x - max) + 1
		}

		if abs(max - 1 - x) < m {
			m = abs(max - 1 - x)
		}

		//max - 1        max
		//max + 1   ..... x

		sum += int64(m)
		sum += int64(x - max - 1)

		if min == 1 {

			fmt.Println(sum)

		} else if min == 2 {

			aa := abs(1 - a[0])
			bb := abs(1 - a[n - 1])
			cc := 2

			if cc < bb && cc < aa {
				sum +=2
			} else {
				sum += int64(minf(aa, bb))
			}

			fmt.Println(sum)

		} else {

			var m int = 1e9

			if abs(1 - a[0]) < m {
				m = abs(1 - a[0])
			}
			if abs(min - 1 - a[0]) < m {
				m = abs(min - 1 - a[0])
			}
			if abs(1 - a[n - 1]) < m {
				m = abs(1 - a[n - 1])
			}
			if abs(min - 1 - a[n - 1]) < m {
				m = abs(min - 1 - a[n - 1])
			}

			if abs(1 - min) + 1 < m {
				m = abs(1 - min) + 1
			}

			if abs(min) < m {
				m = abs(min)
			}

			sum += int64(m)
			sum += int64(min - 2)

			fmt.Println(sum)

		}

	}
}

func do(l, r int) int64  {
	var m int = 1e9
	var k int

	var s int64

	for i := 0; i < n - 1; i++ {
		l := a[i]
		r := a[i + 1]

		temp := abs(l - 1) + abs(x - r)

		if temp < m {
			m = temp
			k = abs(r - l)
		}
	}

	if abs(l - a[0]) < m {
		m = abs(l - a[0])
		k = 0
	}
	if abs(r - a[0]) < m {
		m = abs(r - a[0])
		k = 0
	}
	if abs(l - a[n-1]) < m {
		m = abs(l - a[n-1])
		k = 0
	}
	if abs(r - a[n - 1]) < m {
		m = abs(r- a[n-1])
		k = 0
	}

	s -= int64(k)
	s += int64(m)
	s += int64(r - l)

	return s
}

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

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

package main

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

const MOD int64 = 1000000007

var n, x int

var a []int

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

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

func main() {

	read := bufio.NewReader(os.Stdin)

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

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

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

		a = make([]int, n)

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

		solve()
	}
}

type pair struct {
	v int
	idx []int
}

//a b c d

//20 12 -- 4
//4  2  -- 4

func solve()  {

	var min int = 1e9
	var max int = 0

	var sum int64

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

	if x < min {

		s := do(1, x)

		sum += s

		fmt.Println(sum)

	} else if x >= min && x <= max {

		if min == 1 {
			fmt.Println(sum)

		} else if min == 2 {
			aa := abs(1 - a[0])
			bb := abs(1 - a[n - 1])
			cc := 2

			if cc < bb && cc < aa {
				sum +=2
			} else {
				sum += int64(minf(aa, bb))
			}

			fmt.Println(sum)

		} else {
			//1 .... min - 1
			var m int = 1e9

			if abs(1 - a[0]) < m {
				m = abs(1 - a[0])
			}
			if abs(min - 1 - a[0]) < m {
				m = abs(min - 1 - a[0])
			}
			if abs(1 - a[n - 1]) < m {
				m = abs(1 - a[n - 1])
			}
			if abs(min - 1 - a[n - 1]) < m {
				m = abs(min - 1 - a[n - 1])
			}

			if abs(1 - min) + 1 < m {
				m = abs(1 - min) + 1
			}

			sum += int64(m)
			sum += int64(min - 2)

			fmt.Println(sum)
		}
	} else {
		var m int = 1e9

		if abs(max + 1 - a[0]) < m {
			m = abs(max + 1 - a[0])
		}
		if abs(x - a[0]) < m {
			m = abs(x - a[0])
		}
		if abs(max + 1 - a[n - 1]) < m {
			m = abs(max + 1 - a[n - 1])
		}
		if abs(x - a[n - 1]) < m {
			m = abs(x - a[n - 1])
		}

		if abs(x - max) + 1 < m {
			m = abs(x - max) + 1
		}

		sum += int64(m)
		sum += int64(x - max - 1)

		if min == 1 {

			fmt.Println(sum)

		} else if min == 2 {

			aa := abs(1 - a[0])
			bb := abs(1 - a[n - 1])
			cc := 2

			if cc < bb && cc < aa {
				sum +=2
			} else {
				sum += int64(minf(aa, bb))
			}

			fmt.Println(sum)

		} else {

			var m int = 1e9

			if abs(1 - a[0]) < m {
				m = abs(1 - a[0])
			}
			if abs(min - 1 - a[0]) < m {
				m = abs(min - 1 - a[0])
			}
			if abs(1 - a[n - 1]) < m {
				m = abs(1 - a[n - 1])
			}
			if abs(min - 1 - a[n - 1]) < m {
				m = abs(min - 1 - a[n - 1])
			}

			if abs(1 - min) + 1 < m {
				m = abs(1 - min) + 1
			}

			sum += int64(m)
			sum += int64(min - 2)

			fmt.Println(sum)

		}

	}
}

func do(l, r int) int64  {
	var m int = 1e9
	var k int

	var s int64

	for i := 0; i < n - 1; i++ {
		l := a[i]
		r := a[i + 1]

		temp := abs(l - 1) + abs(x - r)

		if temp < m {
			m = temp
			k = abs(r - l)
		}
	}

	if abs(l - a[0]) < m {
		m = abs(l - a[0])
		k = 0
	}
	if abs(r - a[0]) < m {
		m = abs(r - a[0])
		k = 0
	}
	if abs(l - a[n-1]) < m {
		m = abs(l - a[n-1])
		k = 0
	}
	if abs(r - a[n - 1]) < m {
		m = abs(r- a[n-1])
		k = 0
	}

	s -= int64(k)
	s += int64(m)
	s += int64(r - l)

	return s
}

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

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

package main

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

const MOD int64 = 1000000007

var n, x int

var a []int

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

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

func main() {

	read := bufio.NewReader(os.Stdin)

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

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

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

		a = make([]int, n)

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

		solve()
	}
}

type pair struct {
	v int
	idx []int
}

//a b c d

//20 12 -- 4
//4  2  -- 4

func solve()  {

	var min int = 1e9
	var max int = 0

	var sum int64

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

	if x < min {

		s := do(1, x)

		sum += s

		fmt.Println(sum)

	} else if x >= min && x <= max {

		if min == 1 {
			fmt.Println(sum)

		} else if min == 2 {
			aa := abs(1 - a[0])
			bb := abs(1 - a[n - 1])
			cc := 1

			if cc < bb && cc < aa {
				sum +=1
			} else {
				sum += int64(minf(aa, bb))
			}

			fmt.Println(sum)

		} else {
			//1 .... min - 1
			var m int = 1e9

			if abs(1 - a[0]) < m {
				m = abs(1 - a[0])
			}
			if abs(min - 1 - a[0]) < m {
				m = abs(min - 1 - a[0])
			}
			if abs(1 - a[n - 1]) < m {
				m = abs(1 - a[n - 1])
			}
			if abs(min - 1 - a[n - 1]) < m {
				m = abs(min - 1 - a[n - 1])
			}

			if abs(1 - min) + 1 < m {
				m = abs(1 - min) + 1
			}

			sum += int64(m)
			sum += int64(min - 2)

			fmt.Println(sum)
		}
	} else {
		var m int = 1e9

		if abs(max + 1 - a[0]) < m {
			m = abs(max + 1 - a[0])
		}
		if abs(x - a[0]) < m {
			m = abs(x - a[0])
		}
		if abs(max + 1 - a[n - 1]) < m {
			m = abs(max + 1 - a[n - 1])
		}
		if abs(x - a[n - 1]) < m {
			m = abs(x - a[n - 1])
		}

		if abs(x - max) + 1 < m {
			m = abs(x - max) + 1
		}

		sum += int64(m)
		sum += int64(x - max - 1)

		if min == 1 {

			fmt.Println(sum)

		} else if min == 2 {

			aa := abs(1 - a[0])
			bb := abs(1 - a[n - 1])
			cc := 1

			if cc < bb && cc < aa {
				sum +=1
			} else {
				sum += int64(minf(aa, bb))
			}

			fmt.Println(sum)

		} else {

			var m int = 1e9

			if abs(1 - a[0]) < m {
				m = abs(1 - a[0])
			}
			if abs(min - 1 - a[0]) < m {
				m = abs(min - 1 - a[0])
			}
			if abs(1 - a[n - 1]) < m {
				m = abs(1 - a[n - 1])
			}
			if abs(min - 1 - a[n - 1]) < m {
				m = abs(min - 1 - a[n - 1])
			}

			if abs(1 - min) + 1 < m {
				m = abs(1 - min) + 1
			}

			sum += int64(m)
			sum += int64(min - 2)

			fmt.Println(sum)

		}

	}
}

func do(l, r int) int64  {
	var m int = 1e9
	var k int

	var s int64

	for i := 0; i < n - 1; i++ {
		l := a[i]
		r := a[i + 1]

		temp := abs(l - 1) + abs(x - r)

		if temp < m {
			m = temp
			k = abs(r - l)
		}
	}

	if abs(l - a[0]) < m {
		m = abs(l - a[0])
		k = 0
	}
	if abs(r - a[0]) < m {
		m = abs(r - a[0])
		k = 0
	}
	if abs(l - a[n-1]) < m {
		m = abs(l - a[n-1])
		k = 0
	}
	if abs(r - a[n - 1]) < m {
		m = abs(r- a[n-1])
		k = 0
	}

	s -= int64(k)
	s += int64(m)
	s += int64(r - l)

	return s
}

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

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

package main

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

const MOD int64 = 1000000007

var n, x int

var a []int

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

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

func main() {

	read := bufio.NewReader(os.Stdin)

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

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

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

		a = make([]int, n)

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

		solve()
	}
}

type pair struct {
	v int
	idx []int
}

//a b c d

//20 12 -- 4
//4  2  -- 4

func solve()  {

	var min int = 1e9
	var max int = 0

	var sum int64

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

	if x < min {

		s := do(1, x)

		sum += s

		fmt.Println(sum)

	} else if x >= min && x <= max {

		if min == 1 {
			fmt.Println(sum)

		} else if min == 2 {
			v := minf(abs(1 - a[0]), minf(abs(1 - a[n - 1]), 2))

			sum += int64(v)

			fmt.Println(sum)

		} else {
			//1 .... min - 1
			var m int = 1e9

			if abs(1 - a[0]) < m {
				m = abs(1 - a[0])
			}
			if abs(min - 1 - a[0]) < m {
				m = abs(min - 1 - a[0])
			}
			if abs(1 - a[n - 1]) < m {
				m = abs(1 - a[n - 1])
			}
			if abs(min - 1 - a[n - 1]) < m {
				m = abs(min - 1 - a[n - 1])
			}

			if abs(1 - min) + 2 < m {
				m = abs(1 - min) + 2
			}

			sum += int64(m)
			sum += int64(min - 2)

			fmt.Println(sum)
		}
	} else {
		var m int = 1e9

		if abs(max + 1 - a[0]) < m {
			m = abs(max + 1 - a[0])
		}
		if abs(x - a[0]) < m {
			m = abs(x - a[0])
		}
		if abs(max + 1 - a[n - 1]) < m {
			m = abs(max + 1 - a[n - 1])
		}
		if abs(x - a[n - 1]) < m {
			m = abs(x - a[n - 1])
		}

		if abs(x - max) + 2 < m {
			m = abs(x - max) + 2
		}

		sum += int64(m)
		sum += int64(x - max - 1)

		if min == 1 {

			fmt.Println(sum)

		} else if min == 2 {

			v := minf(abs(1 - a[0]), minf(abs(1 - a[n - 1]), 2))

			sum += int64(v)

			fmt.Println(sum)

		} else {

			var m int = 1e9

			if abs(1 - a[0]) < m {
				m = abs(1 - a[0])
			}
			if abs(min - 1 - a[0]) < m {
				m = abs(min - 1 - a[0])
			}
			if abs(1 - a[n - 1]) < m {
				m = abs(1 - a[n - 1])
			}
			if abs(min - 1 - a[n - 1]) < m {
				m = abs(min - 1 - a[n - 1])
			}

			if abs(1 - min) + 2 < m {
				m = abs(1 - min) + 2
			}

			sum += int64(m)
			sum += int64(min - 2)

			fmt.Println(sum)

		}

	}
}

func do(l, r int) int64  {
	var m int = 1e9
	var k int

	var s int64

	for i := 0; i < n - 1; i++ {
		l := a[i]
		r := a[i + 1]

		temp := abs(l - 1) + abs(x - r)

		if temp < m {
			m = temp
			k = abs(r - l)
		}
	}

	if abs(l - a[0]) < m {
		m = abs(l - a[0])
		k = 0
	}
	if abs(r - a[0]) < m {
		m = abs(r - a[0])
		k = 0
	}
	if abs(l - a[n-1]) < m {
		m = abs(l - a[n-1])
		k = 0
	}
	if abs(r - a[n - 1]) < m {
		m = abs(r- a[n-1])
		k = 0
	}

	s -= int64(k)
	s += int64(m)
	s += int64(r - l)

	return s
}

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

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

package main

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

const mod = 998244353

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n int
	var x int64
	fmt.Fscan(in,&n,&x)
	arr := make([]int64,n)
	for i := 0;i < n;i++{
		fmt.Fscan(in,&arr[i])
	}
	cur := int64(0)
	for i := 1;i < n;i++{
		cur += abs(arr[i] - arr[i - 1])
	}
	ans := int64(1e18)
	for z := 0; z < 2; z++ {
		mn := arr[0] - 1
		ans = min(ans,cur + abs(arr[0] - x) + x - 1)
		for i := 0;i < n - 1;i++{
			ans = min(ans,cur - abs(arr[i + 1] - arr[i]) + abs(arr[i + 1] - x) + abs(arr[i] - 1) + (x - 1))
			ans = min(ans,cur - abs(arr[i + 1] - arr[i]) + mn + abs(arr[i] - x) + abs(arr[i + 1] - x))
			mn = min(mn,cur - abs(arr[i + 1] - arr[i]) + abs(arr[i] - 1) + abs(arr[i + 1] - 1))
		}
		ans = min(ans,cur + mn + abs(arr[n - 1] - x))
		reverse(arr)
	}
	fmt.Fprintln(out,ans)
}
func reverse(arr []int64){
	n := len(arr)
	for i := 0;i < len(arr) / 2;i++{
		arr[i],arr[n - i - 1] = arr[n - i - 1],arr[i]
	}
}

func abs(a int64)int64{
	if a > 0{
		return a
	}
	return -a
}
func sum(a,b int64)int64{
	return (b + a) * (b - a + 1) / 2
}

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

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


You are given a graph with $$$3 \cdot n$$$ vertices and $$$m$$$ edges. You are to find a matching of $$$n$$$ edges, or an independent set of $$$n$$$ vertices.A set of edges is called a matching if no two edges share an endpoint.A set of vertices is called an independent set if no two vertices are connected with an edge.

The first line contains a single integer $$$T \ge 1$$$ — the number of graphs you need to process. The description of $$$T$$$ graphs follows. The first line of description of a single graph contains two integers $$$n$$$ and $$$m$$$, where $$$3 \cdot n$$$ is the number of vertices, and $$$m$$$ is the number of edges in the graph ($$$1 \leq n \leq 10^{5}$$$, $$$0 \leq m \leq 5 \cdot 10^{5}$$$). Each of the next $$$m$$$ lines contains two integers $$$v_i$$$ and $$$u_i$$$ ($$$1 \leq v_i, u_i \leq 3 \cdot n$$$), meaning that there is an edge between vertices $$$v_i$$$ and $$$u_i$$$. It is guaranteed that there are no self-loops and no multiple edges in the graph. It is guaranteed that the sum of all $$$n$$$ over all graphs in a single test does not exceed $$$10^{5}$$$, and the sum of all $$$m$$$ over all graphs in a single test does not exceed $$$5 \cdot 10^{5}$$$.

Print your answer for each of the $$$T$$$ graphs. Output your answer for a single graph in the following format. If you found a matching of size $$$n$$$, on the first line print "Matching" (without quotes), and on the second line print $$$n$$$ integers — the indices of the edges in the matching. The edges are numbered from $$$1$$$ to $$$m$$$ in the input order. If you found an independent set of size $$$n$$$, on the first line print "IndSet" (without quotes), and on the second line print $$$n$$$ integers — the indices of the vertices in the independent set. If there is no matching and no independent set of the specified size, print "Impossible" (without quotes). You can print edges and vertices in any order. If there are several solutions, print any. In particular, if there are both a matching of size $$$n$$$, and an independent set of size $$$n$$$, then you should print exactly one of such matchings or exactly one of such independent sets.

Matching
2
IndSet
1
IndSet
2 4
Matching
1 15

NoteThe first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer.The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly $$$n$$$.The fourth graph does not have an independent set of size 2, but there is a matching of size 2.

package main

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

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

	var T, n, m, v, w int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		es := []int{}
		vis := make([]bool, 3*n+1)
		for i := 1; i <= m; i++ {
			Fscan(in, &v, &w)
			if len(es) < n && !vis[v] && !vis[w] {
				vis[v] = true
				vis[w] = true
				es = append(es, i)
			}
		}
		if len(es) == n {
			Fprintln(out, "Matching")
			for _, i := range es {
				Fprint(out, i, " ")
			}
		} else {
			Fprintln(out, "IndSet")
			for i, c := 1, 0; i <= 3*n; i++ {
				if !vis[i] {
					Fprint(out, i, " ")
					if c++; c == n {
						break
					}
				}
			}
		}
		Fprintln(out)
	}
}

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

package main

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

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

	var T, n, m, v, w int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		var e []interface{}
		vis := make([]bool, 3*n+1)
		for i := 1; i <= m; i++ {
			if Fscan(in, &v, &w); len(e) < n && !vis[v] && !vis[w] {
				vis[v] = true
				vis[w] = true
				e = append(e, i)
			}
		}
		if len(e) == n {
			Fprintln(out, "Matching")
			Fprintln(out, e...)
		} else {
			Fprintln(out, "IndSet")
			for i, c := 1, 0; i <= 3*n; i++ {
				if !vis[i] {
					Fprint(out, i, " ")
					if c++; c == n {
						break
					}
				}
			}
		}
		Fprintln(out)
	}
}

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

package main

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

var edges [100000]int
var vertices [300000]bool

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)

	writer := bytes.NewBuffer(make([]byte, 0, 10000000))

	var T int
	fmt.Fscanf(reader, "%d\n", &T)
	for i := 0;i < T;i ++ {
		var n, m int
		fmt.Fscanf(reader, "%d %d\n", &n, &m)
		for j := 0;j < 3 * n;j ++ {
			vertices[j] = false
		}
		count := 0
		for j := 0;j < m;j ++ {
			var u, v int
			fmt.Fscanf(reader, "%d %d\n", &u, &v)
			u, v = u - 1, v - 1
			if !vertices[u] && !vertices[v] {
				if count < n {
					edges[count] = j + 1
				}
				count ++
				vertices[u], vertices[v] = true, true
			}
		}
		if count >= n {
			fmt.Fprintf(writer, "Matching\n")
			for i := 0;i < n;i ++ {
				fmt.Fprintf(writer, "%d ", edges[i])
			}
		} else {
			fmt.Fprintf(writer, "IndSet\n")
			for i, c := 0, 0; c < n;i ++ {
				if !vertices[i] {
					fmt.Fprintf(writer, "%d ", i + 1)
					c ++
				}
			}
		}
		fmt.Fprintf(writer, "\n")
	}
	writer.WriteTo(os.Stdout)
}

package main

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

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)

	var T int
	fmt.Fscanf(reader, "%d\n", &T)
	for i := 0;i < T;i ++ {
		var n, m int
		fmt.Fscanf(reader, "%d %d\n", &n, &m)
		vertices := make([]bool, 3 * n)
		count := 0
		edges := make([]int, 0, n)
		for j := 0;j < m;j ++ {
			var u, v int
			fmt.Fscanf(reader, "%d %d\n", &u, &v)
			u, v = u - 1, v - 1
			if !vertices[u] && !vertices[v] && count < n {
				count ++
				vertices[u], vertices[v] = true, true
				edges = append(edges, j + 1)
			}
		}
		if count == n {
			fmt.Printf("Matching\n")
			for i := 0;i < n;i ++ {
				fmt.Printf("%d ", edges[i])
			}
		} else {
			fmt.Printf("IndSet\n")
			for i, c := 0, 0; c < n;i ++ {
				if !vertices[i] {
					fmt.Printf("%d ", i)
					c ++
				}
			}
		}
		fmt.Printf("\n")
	}
}

package main

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

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

	var T, n, m, v, w int
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		es := []int{}
		vis := make([]bool, 3*n+1)
		for i := 1; i <= m; i++ {
			Fscan(in, &v, &w)
			if !vis[v] && !vis[w] {
				vis[v] = true
				vis[w] = true
				if es = append(es, i); len(es) == n {
					Fprintln(out, "Matching")
					for _, i := range es {
						Fprint(out, i, " ")
					}
					Fprintln(out)
					continue o
				}
			}
		}
		Fprintln(out, "IndSet")
		for i, c := 1, 0; i <= 3*n; i++ {
			if !vis[i] {
				Fprint(out, i, " ")
				if c++; c == n {
					Fprintln(out)
					break
				}
			}
		}
	}
}

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

package main

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

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)

	var T int
	fmt.Fscanf(reader, "%d\n", &T)
	for i := 0;i < T;i ++ {
		var n, m int
		fmt.Fscanf(reader, "%d %d\n", &n, &m)
		vertices := make([]bool, 3 * n)
		count := 0
		edges := make([]int, 0, 2 * n)
		for j := 0;j < m;j ++ {
			var u, v int
			fmt.Fscanf(reader, "%d %d\n", &u, &v)
			u, v = u - 1, v - 1
			if !vertices[u] && !vertices[v] {
				count ++
				vertices[u], vertices[v] = true, true
				edges = append(edges, j + 1)
			}
		}
		if count >= n {
			fmt.Printf("Matching\n")
			for i := 0;i < n;i ++ {
				fmt.Printf("%d ", edges[i])
			}
		} else {
			fmt.Printf("IndSet\n")
			for i, c := 0, 0; c < n;i ++ {
				if !vertices[i] {
					fmt.Printf("%d ", i)
					c ++
				}
			}
		}
		fmt.Printf("\n")
	}
}


There is an array $$$a$$$ with $$$n-1$$$ integers. Let $$$x$$$ be the bitwise XOR of all elements of the array. The number $$$x$$$ is added to the end of the array $$$a$$$ (now it has length $$$n$$$), and then the elements are shuffled.You are given the newly formed array $$$a$$$. What is $$$x$$$? If there are multiple possible values of $$$x$$$, you can output any of them.

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of integers in the resulting array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 127$$$) — the elements of the newly formed array $$$a$$$. Additional constraint on the input: the array $$$a$$$ is made by the process described in the statement; that is, some value of $$$x$$$ exists.

For each test case, output a single integer — the value of $$$x$$$, as described in the statement. If there are multiple possible values of $$$x$$$, output any of them.

NoteIn the first test case, one possible array $$$a$$$ is $$$a=[2, 5, 4]$$$. Then $$$x = 2 \oplus 5 \oplus 4 = 3$$$ ($$$\oplus$$$ denotes the bitwise XOR), so the new array is $$$[2, 5, 4, 3]$$$. Afterwards, the array is shuffled to form $$$[4, 3, 2, 5]$$$.In the second test case, one possible array $$$a$$$ is $$$a=[1, 10, 6, 10]$$$. Then $$$x = 1 \oplus 10 \oplus 6 \oplus 10 = 7$$$, so the new array is $$$[1, 10, 6, 10, 7]$$$. Afterwards, the array is shuffled to form $$$[6, 1, 10, 7, 10]$$$.In the third test case, all elements of the array are equal to $$$6$$$, so $$$x=6$$$.In the fourth test case, one possible array $$$a$$$ is $$$a=[100, 100]$$$. Then $$$x = 100 \oplus 100 = 0$$$, so the new array is $$$[100, 100, 0]$$$. Afterwards, the array is shuffled to form $$$[100, 100, 0]$$$. (Note that after the shuffle, the array can remain the same.)

package main

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

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadSlice('\n')
	nStr = bytes.ReplaceAll(nStr, []byte{'\r'}, []byte{})
	nStr = bytes.ReplaceAll(nStr, []byte{'\n'}, []byte{})
	n, _ := strconv.Atoi(string(nStr))
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadSlice('\n')
	line = bytes.ReplaceAll(line, []byte{'\r'}, []byte{})
	line = bytes.ReplaceAll(line, []byte{'\n'}, []byte{})
	numbs := strings.Split(string(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 main() {
	in := bufio.NewReaderSize(os.Stdin, 1024)
	var total int
	//var l int
	var data []int

	total = readInt(in)

	for i:=0; i<total; i++ {
		_ = readInt(in)
		data = readArrInt(in)
		fmt.Println(data[0])
	}
}

package main

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

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadSlice('\n')
	nStr = bytes.ReplaceAll(nStr, []byte{'\r'}, []byte{})
	nStr = bytes.ReplaceAll(nStr, []byte{'\n'}, []byte{})
	n, _ := strconv.Atoi(string(nStr))
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadSlice('\n')
	line = bytes.ReplaceAll(line, []byte{'\r'}, []byte{})
	line = bytes.ReplaceAll(line, []byte{'\n'}, []byte{})
	numbs := strings.Split(string(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 main() {
	in := bufio.NewReader(os.Stdin)
	var total int
	//var l int
	var data []int

	total = readInt(in)

	for i:=0; i<total; i++ {
		_ = readInt(in)
		data = readArrInt(in)
		fmt.Println(data[0])
	}
}

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 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 main() {
	in := bufio.NewReader(os.Stdin)
	var total int
	//var l int
	var data []int

	total = readInt(in)

	for i:=0; i<total; i++ {
		_ = readInt(in)
		data = readArrInt(in)
		fmt.Println(data[0])
	}
}

package main

import (
	"fmt"
	"os"
)

var (
	b = make([]byte, 1)
)

func readInt() int {
	res := 0
	for {
		os.Stdin.Read(b)
		if b[0] == ' ' {
			continue
		}
		if b[0] == '\n' {
			break
		}
		res = res * 10 + int(b[0]) - 48
	}

	return res
}

func readIntSkip() int {
	var res int
	var n int
	for {
		n, _ = os.Stdin.Read(b)
		if n == 0 {
			return res
		}
		if b[0] == '\n' {
			break
		}
		if b[0]<48 || b[0]>57 {
			for b[0] != '\n' {
				os.Stdin.Read(b)
			}
			break
		}
		res = res * 10 + int(b[0]) - 48
	}

	return res
}

func main() {
	var total int
	var l int

	total = readIntSkip()

	for i:=0; i<total; i++ {
		l = readIntSkip()
		l = readIntSkip()
		fmt.Println(l)
	}
}

package main

import (
	"fmt"
	"os"
)

var (
	b = make([]byte, 1)
)

func readInt() int {
	res := 0
	for {
		os.Stdin.Read(b)
		if b[0]<48 || b[0]>57 {
			break
		}
		res = res * 10 + int(b[0]) - 48
	}

	return res
}

func readIntSkip() int {
	var res int
	var n int
	for {
		n, _ = os.Stdin.Read(b)
		if n == 0 {
			return res
		}
		if b[0] == '\n' {
			break
		}
		if b[0]<48 || b[0]>57 {
			for b[0] != '\n' {
				os.Stdin.Read(b)
			}
			break
		}
		res = res * 10 + int(b[0]) - 48
	}

	return res
}

func main() {
	var total int
	var l int

	total = readIntSkip()

	for i:=0; i<total; i++ {
		l = readIntSkip()
		l = readIntSkip()
		fmt.Println(l)
	}
}

package main

import (
	"fmt"
	"os"
)

var (
	b = make([]byte, 1)
)

func readInt() int {
	res := 0
	fmt.Scan(&res)
	return res
	for {
		os.Stdin.Read(b)
		if b[0]<48 || b[0]>57 {
			break
		}
		res = res * 10 + int(b[0]) - 48
	}

	return res
}

func readIntSkip(l int) int {
	var res int
	var a int
	fmt.Scan(&res)
	for i:=1; i<l; i++ {
		fmt.Scan(&a)
	}
	return res
	var n int
	for {
		n, _ = os.Stdin.Read(b)
		if n == 0 {
			return res
		}
		if b[0] == '\n' {
			break
		}
		if b[0]<48 || b[0]>57 {
			for b[0] != '\n' {
				os.Stdin.Read(b)
			}
			break
		}
		res = res * 10 + int(b[0]) - 48
	}

	return res
}

func main() {
	var total int
	var l int

	total = readInt()

	for i:=0; i<total; i++ {
		l = readInt()
		l = readIntSkip(l)
		fmt.Println(l)
	}
}

package main

import (
	"fmt"
)

func readInt() int {
	var a int
	fmt.Scan(&a)
	return a
}

func readIntP(res *int) {
	fmt.Scan(res)
}

func readIntSlice(l int) []int {
	res := make([]int, l)
	for i:=0; i<l; i++ {
		fmt.Scan(&res[i])
	}
	return res
}

func readIntSliceP(res []int) {
	for i:=0; i<len(res); i++ {
		fmt.Scan(&res[i])
	}
}

func readSkip(l int) {
	var a int
	for i:=0; i<l; i++ {
		fmt.Scan(&a)
	}
}

func main() {
	var total int
	var l int
	var res int
	var j int

	fmt.Scan(&total)

	for i:=0; i<total; i++ {
		fmt.Scan(&l)
		fmt.Scan(&res)
		fmt.Println(res)
		for j=1; j<l; j++ {
			fmt.Scan(&res)
		}
	}
}

package main

import (
	"fmt"
)

func readInt() int {
	var a int
	fmt.Scan(&a)
	return a
}

func readIntP(res *int) {
	fmt.Scan(res)
}

func readIntSlice(l int) []int {
	res := make([]int, l)
	for i:=0; i<l; i++ {
		fmt.Scan(&res[i])
	}
	return res
}

func readIntSliceP(res []int) {
	for i:=0; i<len(res); i++ {
		fmt.Scan(&res[i])
	}
}

func readSkip(l int) {
	var a int
	for i:=0; i<l; i++ {
		fmt.Scan(&a)
	}
}

func main() {
	total := readInt()
	var l int
	var res int

	for i:=0; i<total; i++ {
		readIntP(&l)
		readIntP(&res)
		fmt.Println(res)
		readSkip(l-1)
	}
}

package main

import (
	"fmt"
)

func readInt() int {
	var a int
	fmt.Scan(&a)
	return a
}

func readIntSlice(l int) []int {
	res := make([]int, l)
	for i:=0; i<l; i++ {
		fmt.Scan(&res[i])
	}
	return res
}

func readIntSliceP(res []int) {
	for i:=0; i<len(res); i++ {
		fmt.Scan(&res[i])
	}
}

func readSkip(l int) {
	var a int
	for i:=0; i<l; i++ {
		fmt.Scan(&a)
	}
}

func main() {
	total := readInt()

	for i:=0; i<total; i++ {
		l := readInt()
		fmt.Println(readInt())
		readSkip(l-1)
	}
}
//
//func process(data []int) int {
//	for i:=0; i<len(data); i++ {
//		res := 0
//		for j:=0; j<len(data); j++ {
//			if j == i {
//				continue
//			}
//			res = res ^ data[j]
//		}
//		if res == data[i] {
//			return data[i]
//		}
//	}
//
//	return 0
//}

package main

import (
	"fmt"
)

func readInt() int {
	var a int
	fmt.Scan(&a)
	return a
}

func readIntSlice(l int) []int {
	res := make([]int, l)
	for i:=0; i<l; i++ {
		fmt.Scan(&res[i])
	}
	return res
}

func main() {
	total := readInt()

	for i:=0; i<total; i++ {
		l := readInt()
		data := readIntSlice(l)
		fmt.Println(data[0])
	}
}
//
//func process(data []int) int {
//	for i:=0; i<len(data); i++ {
//		res := 0
//		for j:=0; j<len(data); j++ {
//			if j == i {
//				continue
//			}
//			res = res ^ data[j]
//		}
//		if res == data[i] {
//			return data[i]
//		}
//	}
//
//	return 0
//}

package main

import (
	"fmt"
)

func readInt() int {
	var a int
	fmt.Scan(&a)
	return a
}

func readIntSlice(l int) []int {
	res := make([]int, l)
	for i:=0; i<l; i++ {
		fmt.Scan(&res[i])
	}
	return res
}

func main() {
	total := readInt()
	datas := make([][]int, total)

	for i:=0; i<total; i++ {
		l := readInt()
		datas[i] = readIntSlice(l)
	}

	for _, data := range datas {
		fmt.Println(data[0])
	}
}
//
//func process(data []int) int {
//	for i:=0; i<len(data); i++ {
//		res := 0
//		for j:=0; j<len(data); j++ {
//			if j == i {
//				continue
//			}
//			res = res ^ data[j]
//		}
//		if res == data[i] {
//			return data[i]
//		}
//	}
//
//	return 0
//}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var n int
	n = readInt(in)
	for i := 0; i < n; i++ {
		_ = readInt(in)
		arr := readArrInt(in)
		fmt.Println(arr[0])
	}
}

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

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

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

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
}

package main

import (
	"fmt"
	"os"
)

func main() {
	var t int
	fmt.Fscan(os.Stdin, &t)
	for t != 0 {
		var n int
		fmt.Fscan(os.Stdin, &n)
		arr := make([]int, n)
		for i := range arr {
			fmt.Fscan(os.Stdin, &arr[i])
		}
		fmt.Println(arr[0])
		t -= 1
	}
}

package main

import (
	"fmt"
)

func solve() {
	var n, a int
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a)

	}
	fmt.Println(a)
	//fmt.Println("here is the num" + strconv.Itoa(a))
}

func main() {

	T := 0
	fmt.Scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

package main

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

var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)

func slove() {
	var n int
	fmt.Fscan(r, &n)
	nums := make([]int, n)
	for i:=0;i<n;i++ {
		fmt.Fscan(r, &nums[i])
	}
	fmt.Fprintln(w, nums[0])
}

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

package main
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Fscan(in,&n)
		var x int
		for j:=0;j<n;j++{
			fmt.Fscan(in,&x)
		}
		fmt.Fprintln(out,x)
	}
}

package main

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

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

func scan(a ...interface{}) {
	fmt.Fscan(in, a...)
}
func printf(f string, a ...interface{}) {
	fmt.Fprintf(out, f, a...)
}
func solve() {
	var n int
	scan(&n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		scan(&a[i])
	}
	printf("%d\n", a[0])
}
func main() {
	defer out.Flush()
	n := 0
	scan(&n)
	for i := 0; i < n; i++ {
		solve()
	}
}

package main

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

var (
	reader  = bufio.NewReader(os.Stdin)
	t, n, a int
)

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a)
		}
		fmt.Println(a)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func gf() float64 { f,e := strconv.ParseFloat(gs(),64); if e != nil {panic(e)}; return f }
func gbs() []byte { return []byte(gs()) }
func gfs(n int) []float64  { res := make([]float64,n); for i:=0;i<n;i++ { res[i] = gf() }; return res }
func gss(n int) []string  { res := make([]string,n); for i:=0;i<n;i++ { res[i] = gs() }; return res }
func gi64() int64     { i,e := strconv.ParseInt(gs(),10,64); if e != nil {panic(e)}; return i }
func gis64(n int) []int64  { res := make([]int64,n); for i:=0;i<n;i++ { res[i] = gi64() }; return res }

func ia(m int) []int { return make([]int,m) }
func iai(m int,v int) []int { a := make([]int,m); for i:=0;i<m;i++ { a[i] = v }; return a }
func twodi(n int,m int,v int) [][]int {
	r := make([][]int,n); for i:=0;i<n;i++ { x := make([]int,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill2(m int) ([]int,[]int) { a,b := ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i] = gi(),gi()}; return a,b }
func fill3(m int) ([]int,[]int,[]int) { a,b,c := ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i] = gi(),gi(),gi()}; return a,b,c }
func fill4(m int) ([]int,[]int,[]int,[]int) { a,b,c,d := ia(m),ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i],d[i] = gi(),gi(),gi(),gi()}; return a,b,c,d }
func abs(a int) int { if a < 0 { return -a }; return a }
func rev(a []int) { i,j := 0,len(a)-1; for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
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 maxarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr(a []int) int { ans := 0; for _,aa := range(a) { ans += aa }; return ans }
func zeroarr(a []int) { for i:=0; i<len(a); i++ { a[i] = 0 } }
func powmod(a,e,mod int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint(a,e int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd(a,b int) int { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended(a,b int) (int,int,int) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv(a,m int) (int,bool) { g,x,_ := gcdExtended(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func vecintstring(a []int) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.Itoa(a) }; return strings.Join(astr," ") }
func makefact(n int,mod int) ([]int,[]int) {
	fact,factinv := make([]int,n+1),make([]int,n+1)
	fact[0] = 1; for i:=1;i<=n;i++ { fact[i] = fact[i-1] * i % mod }
	factinv[n] = powmod(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * (i+1) % mod }
	return fact,factinv
}

func ia64(m int) []int64 { return make([]int64,m) }
func iai64(m int,v int64) []int64 { a := make([]int64,m); for i:=0;i<m;i++ { a[i] = v }; return a }
func twodi64(n int,m int,v int64) [][]int64 {
	r := make([][]int64,n); for i:=0;i<n;i++ { x := make([]int64,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill264(m int) ([]int64,[]int64) { a,b := ia64(m),ia64(m); for i:=0;i<m;i++ {a[i],b[i] = gi64(),gi64()}; return a,b }
func fill364(m int) ([]int64,[]int64,[]int64) { a,b,c := ia64(m),ia64(m),ia64(m); for i:=0;i<m;i++ {a[i],b[i],c[i] = gi64(),gi64(),gi64()}; return a,b,c }
func fill464(m int) ([]int64,[]int64,[]int64,[]int64) { a,b,c,d := ia64(m),ia64(m),ia64(m),ia64(m); for i:=0;i<m;i++ {a[i],b[i],c[i],d[i] = gi64(),gi64(),gi64(),gi64()}; return a,b,c,d }
func abs64(a int64) int64 { if a < 0 { return -a }; return a }
func rev64(a []int64) { i,j := 0,len(a)-1; for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
func max64(a,b int64) int64 { if a > b { return a }; return b }
func min64(a,b int64) int64 { if a > b { return b }; return a }
func maxarr64(a []int64) int64 { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr64(a []int64) int64 { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr64(a []int64) int64 { ans := int64(0); for _,aa := range(a) { ans += aa }; return ans }
func zeroarr64(a []int64) { for i:=0; i<len(a); i++ { a[i] = 0 } }
func powmod64(a,e,mod int64) int64 { res, m := int64(1), a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint64(a,e int64) int64 { res, m := int64(1), a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd64(a,b int64) int64 { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended64(a,b int64) (int64,int64,int64) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended64(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv64(a,m int64) (int64,bool) { g,x,_ := gcdExtended64(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func vecint64string(a []int64) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.FormatInt(a,10) }; return strings.Join(astr," ") }
func makefact64(n int,mod int64) ([]int64,[]int64) {
	fact,factinv := make([]int64,n+1),make([]int64,n+1)
	fact[0] = 1; for i:=1;i<=n;i++ { fact[i] = fact[i-1] * int64(i) % mod }
	factinv[n] = powmod64(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * int64(i+1) % mod }
	return fact,factinv
}
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi();
	for tt:=1;tt<=T;tt++ { N := gi(); A := gis(N); fmt.Fprintln(wrtr,A[0]) }
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1698A
Date: 9/07/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1698/A
Problem: CF1698A
**/
func (in *CF1698A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		ans := 0
		len := in.NextInt()
		for i := 0; i < len; i++ {
			 ans = in.NextInt()
		}
		fmt.Println(ans)
	}
}

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

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

package main

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

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

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

	var T int
	var n int
	var x int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		for i := 0; i < n; i++ {
			Fscan(in, &x)
		}
		Fprintln(out, x)
	}
}

package main

import "fmt"

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

package main

import "fmt"

const maxn int = 10000

func main() {
	var T int
	fmt.Scan(&T)
	var a [maxn]int
	for ; T > 0; T-- {
		var n int
		sum := 0
		fmt.Scan(&n)
		for i := 0; i < n; i++ {
			var x int
			fmt.Scan(&x)
			a[i] = x
			sum ^= x
		}
		for i := 0; i < n; i++ {
			if sum^a[i] == a[i] {
				fmt.Printf("%d\n", a[i])
				break
			}
		}

	}

}

package main

import (
	"fmt"
)

// var in = bufio.NewReader(os.Stdin)

// func readInt() int {
// 	res := 0
// 	for c, _ := in.ReadByte(); c >= '0' && c <= '9'; c, _ = in.ReadByte() {
// 		res = res*10 + int(c-'0')
// 	}
// 	return res
// }

func main() {
	var t int
	fmt.Scan(&t)
	for ; t > 0; t-- {
		var n int
		fmt.Scan(&n)
		var x int
		for i := 0; i < n; i++ {
			fmt.Scan(&x)
		}
		fmt.Println(x)
	}
}

package main

import (
	"fmt"
	//"math"
)

func main() {
	var n int
	fmt.Scan(&n)
	for k := 0; k < n; k++ {
		var t int
		fmt.Scan(&t)
		ar := make([]int, t)
		for i := 0; i < t; i++ {
			fmt.Scan(&ar[i])
		}
		fmt.Println(ar[0])
	}
}

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)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i])
		}

		for i := 0; i < n; i++ {
			var x int
			for j := 0; j < n; j++ {
				if i == j {
					continue
				}
				x ^= aa[j]
			}

			if x^aa[i] == 0 {
				fmt.Fprintln(out, x)
				break
			}
		}
	}
}

package main

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

var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)

func solve() {
	var n int
	fmt.Fscan(r, &n)
	arr := make([]int, n)
	for i := range arr {
		fmt.Fscan(r, &arr[i])
	}
	fmt.Fprintln(w, arr[0])
}
func main() {
	defer w.Flush()
	var t int
	fmt.Fscan(r, &t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

import (
	"fmt"
	"os"
)

var (
	b = make([]byte, 1)
)

func readInt() int {
	res := 0
	for {
		os.Stdin.Read(b)
		if b[0] == ' ' {
			continue
		}
		if b[0] == '\n' {
			break
		}
		res = res * 10 + int(b[0]) - 48
	}

	return res
}

func readIntSkip() int {
	var res int
	var n int
	for {
		n, _ = os.Stdin.Read(b)
		if n == 0 {
			return res
		}
		if b[0] == '\n' {
			break
		}
		if b[0]<48 || b[0]>57 {
			for b[0] != '\n' {
				os.Stdin.Read(b)
			}
			break
		}
		res = res * 10 + int(b[0]) - 48
	}

	return res
}

func main() {
	var total int
	var l int

	total = readInt()
	fmt.Println(total)

	for i:=0; i<total; i++ {
		l = readInt()
		l = readIntSkip()
		fmt.Println(l)
	}
}

package main

import (
	"fmt"
	"os"
)

var (
	b = make([]byte, 1)
)

func readInt() int {
	res := 0
	for {
		os.Stdin.Read(b)
		if b[0] == ' ' {
			continue
		}
		if b[0] == '\n' {
			break
		}
		res = res * 10 + int(b[0]) - 48
	}

	return res
}

func readIntSkip() int {
	var res int
	var n int
	for {
		n, _ = os.Stdin.Read(b)
		if n == 0 {
			return res
		}
		if b[0] == '\n' {
			break
		}
		if b[0]<48 || b[0]>57 {
			for b[0] != '\n' {
				os.Stdin.Read(b)
			}
			break
		}
		res = res * 10 + int(b[0]) - 48
	}

	return res
}

func main() {
	var total int
	var l int

	total = readInt()

	for i:=0; i<total; i++ {
		l = readInt()
		l = readIntSkip()
		fmt.Println(l)
	}
}

package main

import (
	"fmt"
	"os"
)

var (
	b = make([]byte, 1)
)

func readInt() int {
	res := 0
	for {
		os.Stdin.Read(b)
		if b[0]<48 || b[0]>57 {
			break
		}
		res = res * 10 + int(b[0]) - 48
	}

	return res
}

func readIntSkip() int {
	var res int
	var n int
	for {
		n, _ = os.Stdin.Read(b)
		if n == 0 {
			return res
		}
		if b[0] == '\n' {
			break
		}
		if b[0]<48 || b[0]>57 {
			for b[0] != '\n' {
				os.Stdin.Read(b)
			}
			break
		}
		res = res * 10 + int(b[0]) - 48
	}

	return res
}

func main() {
	var total int
	var l int

	total = readIntSkip()
	fmt.Println(total)
	l = readIntSkip()
	fmt.Println(l)
	l = readIntSkip()
	fmt.Println(l)
	fmt.Println(100)
	return

	for i:=0; i<total; i++ {
		l = readInt()
		l = readIntSkip()
		fmt.Println(l)
	}
}

package main

import (
	"fmt"
	"os"
)

var (
	b = make([]byte, 1)
)

func readInt() int {
	res := 0
	for {
		os.Stdin.Read(b)
		if b[0]<48 || b[0]>57 {
			break
		}
		res = res * 10 + int(b[0]) - 48
	}

	return res
}

func readIntSkip() int {
	var res int
	var n int
	for {
		n, _ = os.Stdin.Read(b)
		if n == 0 {
			return res
		}
		if b[0] == '\n' {
			break
		}
		if b[0]<48 || b[0]>57 {
			for b[0] != '\n' {
				os.Stdin.Read(b)
			}
			break
		}
		res = res * 10 + int(b[0]) - 48
	}

	return res
}

func main() {
	var total int
	var l int

	total = readInt()
	fmt.Println(total)
	l = readInt()
	fmt.Println(l)
	l = readIntSkip()
	fmt.Println(l)
	fmt.Println(100)
	return

	for i:=0; i<total; i++ {
		l = readInt()
		l = readIntSkip()
		fmt.Println(l)
	}
}

package main

import (
	"fmt"
	"os"
)

var (
	b = make([]byte, 1)
)

func readInt() int {
	res := 0
	for {
		os.Stdin.Read(b)
		if b[0]<48 || b[0]>57 {
			break
		}
		res = res * 10 + int(b[0]) - 48
	}

	return res
}

func readIntSkip(l int) int {
	var res int
	var a int
	fmt.Scan(&res)
	for i:=1; i<l; i++ {
		fmt.Scan(&a)
	}
	return res
	var n int
	for {
		n, _ = os.Stdin.Read(b)
		if n == 0 {
			return res
		}
		if b[0] == '\n' {
			break
		}
		if b[0]<48 || b[0]>57 {
			for b[0] != '\n' {
				os.Stdin.Read(b)
			}
			break
		}
		res = res * 10 + int(b[0]) - 48
	}

	return res
}

func main() {
	var total int
	var l int

	total = readInt()

	for i:=0; i<total; i++ {
		l = readInt()
		l = readIntSkip(l)
		fmt.Println(l)
	}
}

package main

import (
	"fmt"
	"os"
)

var (
	b = make([]byte, 1)
)

func readInt() int {
	res := 0
	for {
		os.Stdin.Read(b)
		if b[0]<48 || b[0]>57 {
			break
		}
		res = res * 10 + int(b[0]) - 48
	}

	return res
}

func readIntSkip() int {
	res := 0
	var n int
	for {
		n, _ = os.Stdin.Read(b)
		if n == 0 {
			return res
		}
		if b[0] == '\n' {
			break
		}
		if b[0]<48 || b[0]>57 {
			for b[0] != '\n' {
				os.Stdin.Read(b)
			}
			break
		}
		res = res * 10 + int(b[0]) - 48
	}

	return res
}

func main() {
	var total int
	var l int

	total = readInt()

	for i:=0; i<total; i++ {
		l = readInt()
		l = readIntSkip()
		fmt.Println(l)
	}
}

package main

import (
	"fmt"
	"os"
)

var (
	b = make([]byte, 1)
)

func readInt() int {
	res := 0
	for {
		os.Stdin.Read(b)
		if b[0]<48 || b[0]>57 {
			break
		}
		res = res * 10 + int(b[0]) - 48
	}

	return res
}

func readIntSkip() int {
	res := 0
	for {
		os.Stdin.Read(b)
		if b[0] == '\n' {
			break
		}
		if b[0]<48 || b[0]>57 {
			for b[0] != '\n' {
				os.Stdin.Read(b)
			}
			break
		}
		res = res * 10 + int(b[0]) - 48
	}

	return res
}

func main() {
	var total int
	var l int

	total = readInt()

	for i:=0; i<total; i++ {
		l = readInt()
		l = readIntSkip()
		fmt.Println(l)
	}
}

package main

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

type reader struct {
	reader *bufio.Reader
}
func (r *reader) readInt() int {
	str, _ := r.reader.ReadString('\n')
	val, _ := strconv.Atoi(strings.Trim(str, "\n"))
	return val
}
func (r *reader) readIntSlice(l int) []int {
	data, _ := r.reader.ReadString('\n')
	res := make([]int, 0, l)
	for _, s := range strings.Split(strings.Trim(data, "\n"), " ") {
		v, _ := strconv.Atoi(s)
		res = append(res, v)
	}

	return res
}

func main() {
	var reader = &reader{
		reader: bufio.NewReader(os.Stdin),
	}
	total := reader.readInt()
	datas := make([][]int, total)

	for i:=0; i<total; i++ {
		l := reader.readInt()
		datas[i] = reader.readIntSlice(l)
	}

	for _, data := range datas {
		fmt.Println(data[0])
		//fmt.Println(process(data))
		//fmt.Println("res", data, process(data))
	}
}

func process(data []int) int {
	for i:=0; i<len(data); i++ {
		res := 0
		for j:=0; j<len(data); j++ {
			if j == i {
				continue
			}
			res = res ^ data[j]
		}
		if res == data[i] {
			return data[i]
		}
	}

	return 0
}

package main

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

type reader struct {
	reader *bufio.Reader
}
func (r *reader) readInt() int {
	str, _ := r.reader.ReadString('\n')
	val, _ := strconv.Atoi(strings.Trim(str, "\n"))
	return val
}
func (r *reader) readIntSlice(l int) []int {
	data, _ := r.reader.ReadString('\n')
	res := make([]int, 0, l)
	for _, s := range strings.Split(strings.Trim(data, "\n"), " ") {
		v, _ := strconv.Atoi(s)
		res = append(res, v)
	}

	return res
}

func main() {
	var reader = &reader{
		reader: bufio.NewReader(os.Stdin),
	}
	total := reader.readInt()
	datas := make([][]int, total)

	for i:=0; i<total; i++ {
		l := reader.readInt()
		datas[i] = reader.readIntSlice(l)
	}

	for _, data := range datas {
		fmt.Println(process(data))
		//fmt.Println("res", data, process(data))
	}
}

func process(data []int) int {
	for i:=0; i<len(data); i++ {
		res := 0
		for j:=0; j<len(data); j++ {
			if j == i {
				continue
			}
			res = res ^ data[j]
		}
		if res == data[i] {
			return data[i]
		}
	}

	return 0
}

package main

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

type reader struct {
	reader *bufio.Reader
}
func (r *reader) readInt() int {
	str, _ := r.reader.ReadString('\n')
	val, _ := strconv.Atoi(strings.Trim(str, "\n"))
	return val
}
func (r *reader) readIntSlice(l int) []int {
	data, _ := r.reader.ReadString('\n')
	res := make([]int, l)
	for i, s := range strings.Split(strings.Trim(data, "\n"), " ") {
		v, _ := strconv.Atoi(s)
		res[i] = v
	}

	return res
}

func main() {
	var reader = &reader{
		reader: bufio.NewReader(os.Stdin),
	}
	total := reader.readInt()
	datas := make([][]int, total)

	for i:=0; i<total; i++ {
		l := reader.readInt()
		datas[i] = reader.readIntSlice(l)
	}

	for _, data := range datas {
		fmt.Println(process(data))
		//fmt.Println("res", data, process(data))
	}
}

func process(data []int) int {
	for i:=0; i<len(data); i++ {
		res := 0
		for j:=0; j<len(data); j++ {
			if j == i {
				continue
			}
			res = res ^ data[j]
		}
		if res == data[i] {
			return data[i]
		}
	}

	return 0
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanf("%d", &n)
	fmt.Println("Total cases: ", n)
	for i := 0; i < n; i++ {
		var t int
		fmt.Scanf("%d", &t)
		fmt.Println("Length: ", t)
		var answer int
		fmt.Scanf("%d", &answer)
		fmt.Println("Answer: ", answer)
		for j := 1; j < t; j++ {
			var bg int
			fmt.Scanf("%d", &bg)
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanf("%d", &n)
	for i := 0; i < n; i++ {
		var t int
		fmt.Scanf("%d", &t)
		var answer int
		fmt.Scanf("%d", &answer)
		fmt.Println(answer)
		for j := 1; j < t; j++ {
			var answer int
			fmt.Scanf("%d", &answer)
		}
	}
}

package main

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

var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)

func slove() {
	var n int
	fmt.Fscan(r, &n)
	left, right := 1, n
	for left < right{
		m := left + right
		m /= 2
		fmt.Fprintf(w, "? %d %d\n", left, m)
		w.Flush()
		cnt := 0
		for i:=left;i<=m;i++ {
			var a int
			fmt.Fscan(r, &a)
			if a <= m && a >= left {
				cnt++
			}
		}
		if cnt % 2 == 0 {
			left = m + 1
		} else {
			right = m
		}
	}
	fmt.Fprintln(w, left)
}

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


Parsa has a humongous tree on $$$n$$$ vertices.On each vertex $$$v$$$ he has written two integers $$$l_v$$$ and $$$r_v$$$.To make Parsa's tree look even more majestic, Nima wants to assign a number $$$a_v$$$ ($$$l_v \le a_v \le r_v$$$) to each vertex $$$v$$$ such that the beauty of Parsa's tree is maximized.Nima's sense of the beauty is rather bizarre. He defines the beauty of the tree as the sum of $$$|a_u - a_v|$$$ over all edges $$$(u, v)$$$ of the tree.Since Parsa's tree is too large, Nima can't maximize its beauty on his own. Your task is to find the maximum possible beauty for Parsa's tree.

The first line contains an integer $$$t$$$ $$$(1\le t\le 250)$$$ — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(2\le n\le 10^5)$$$ — the number of vertices in Parsa's tree. The $$$i$$$-th of the following $$$n$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ $$$(1 \le l_i \le r_i \le 10^9)$$$. Each of the next $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$ $$$(1 \le u , v \le n, u\neq v)$$$ meaning that there is an edge between the vertices $$$u$$$ and $$$v$$$ in Parsa's tree. It is guaranteed that the given graph is a tree. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

For each test case print the maximum possible beauty for Parsa's tree.

NoteThe trees in the example:  In the first test case, one possible assignment is $$$a = \{1, 8\}$$$ which results in $$$|1 - 8| = 7$$$.In the second test case, one of the possible assignments is $$$a = \{1, 5, 9\}$$$ which results in a beauty of $$$|1 - 5| + |5 - 9| = 8$$$

package main

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

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

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

		tree := make([][]int, n)
		dp := make([]node, n)

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

			dp[i] = node{calc: false}
			_, _ = fmt.Fscan(in, &dp[i].left, &dp[i].right)
		}

		for i := 1; i < n; i++ {
			var u, v int
			_, _ = fmt.Fscan(in, &u, &v)

			u--
			v--

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

		dfs(tree, dp, 0, -1)

		ans := dp[0].ldp
		if ans < dp[0].rdp {
			ans = dp[0].rdp
		}

		fmt.Println(ans)
	}
}

func dfs(tree [][]int, dp []node, n int, p int) {
	if dp[n].calc {
		return
	}

	for _, m := range tree[n] {
		if m == p {
			continue
		}

		dfs(tree, dp, m, n)

		if dp[m].ldp+abs(dp[n].left-dp[m].left) > dp[m].rdp+abs(dp[n].left-dp[m].right) {
			dp[n].ldp += dp[m].ldp + abs(dp[n].left-dp[m].left)
		} else {
			dp[n].ldp += dp[m].rdp + abs(dp[n].left-dp[m].right)
		}

		if dp[m].ldp+abs(dp[n].right-dp[m].left) > dp[m].rdp+abs(dp[n].right-dp[m].right) {
			dp[n].rdp += dp[m].ldp + abs(dp[n].right-dp[m].left)
		} else {
			dp[n].rdp += dp[m].rdp + abs(dp[n].right-dp[m].right)
		}
	}
}

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

type node struct {
	calc bool

	left  int64
	right int64

	ldp int64
	rdp int64
}

package main

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

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

	var T, n, v, w int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]struct{ l, r int }, n)
		for i := range a {
			Fscan(in, &a[i].l, &a[i].r)
		}
		type nb struct{ to, wt int }
		g := make([][]int, n)
		for i := 1; i < n; i++ {
			Fscan(in, &v, &w)
			v--
			w--
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}
		var f func(v, fa int) (s0, s1 int64)
		f = func(v, fa int) (s0, s1 int64) {
			for _, w := range g[v] {
				if w != fa {
					p, q := f(w, v)
					s0 += max(abs(a[v].l-a[w].l)+p, abs(a[v].l-a[w].r)+q)
					s1 += max(abs(a[v].r-a[w].l)+p, abs(a[v].r-a[w].r)+q)
				}
			}
			return
		}
		Fprintln(out, max(f(0, -1)))
	}
}

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

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

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

package main

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

type Scanner struct {
	bScanner *bufio.Scanner
}

func NewScanner() *Scanner {
	bScanner := bufio.NewScanner(os.Stdin)
	bScanner.Split(bufio.ScanWords)
	bScanner.Buffer([]byte{}, math.MaxInt32)
	return &Scanner{
		bScanner,
	}
}
func (s *Scanner) Next() string {
	s.bScanner.Scan()
	return s.bScanner.Text()
}
func (s *Scanner) NextInt() int {
	s.bScanner.Scan()
	res, err := strconv.Atoi(s.bScanner.Text())
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) NextInt64() int64 {
	s.bScanner.Scan()
	res, err := strconv.ParseInt(s.bScanner.Text(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) NextFloat() float64 {
	s.bScanner.Scan()
	res, err := strconv.ParseFloat(s.bScanner.Text(), 64)
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) NextInts(N int) []int {
	res := make([]int, N)
	for i := 0; i < N; i++ {
		res[i] = s.NextInt()
	}
	return res
}
func (s *Scanner) NextInt64s(N int) []int64 {
	res := make([]int64, N)
	for i := 0; i < N; i++ {
		res[i] = s.NextInt64()
	}
	return res
}
func Min(nums ...int) int {
	ans := math.MaxInt32
	for _, num := range nums {
		if num < ans {
			ans = num
		}
	}
	return ans
}
func Max(nums ...int64) int64 {
	ans := int64(math.MinInt64)
	for _, num := range nums {
		if num > ans {
			ans = num
		}
	}
	return ans
}

func InitInts1D(n int, val int64) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = val
	}
	return res
}
func InitInts2D(n, m int, val int64) [][]int64 {
	res := make([][]int64, n)
	for i := 0; i < n; i++ {
		res[i] = InitInts1D(m, val)
	}
	return res
}

type StepState struct {
	step, dist int
}
type PriorityQueue []*StepState

func (pq PriorityQueue) Len() int { return len(pq) }
func (pq PriorityQueue) Less(i, j int) bool {
	if pq[i].step != pq[j].step {
		return pq[i].step < pq[j].step
	}
	return pq[i].dist < pq[j].dist
}
func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}
func (pq *PriorityQueue) Push(x interface{}) {
	item := x.(*StepState)
	*pq = append(*pq, item)
}
func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	*pq = old[0 : n-1]
	return item
}

type span struct {
	l, r int64
}

var (
	spans []span
	edge  [][]int
)

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

var (
	dp [][]int64
)

func f(now, par, parSel int) int64 {
	if dp[now][parSel] == -1 {
		res := int64(0)
		parVal := int64(0)
		if now != 0 {
			if parSel == 0 {
				parVal = spans[par].l
			} else {
				parVal = spans[par].r
			}
		}

		notSelRes := int64(0)
		selRes := int64(0)
		if now != 0 {
			notSelRes = Abs(spans[now].l - parVal)
			selRes = Abs(spans[now].r - parVal)
		}
		for _, next := range edge[now] {
			if next == par {
				continue
			}

			notSelRes += f(next, now, 0)
			selRes += f(next, now, 1)
		}
		res = Max(notSelRes, selRes)
		dp[now][parSel] = res
	}
	return dp[now][parSel]
}

func main() {
	sc := NewScanner()
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	TC := sc.NextInt()
	for tc := 0; tc < TC; tc++ {
		N := sc.NextInt()
		spans = make([]span, 0)
		for i := 0; i < N; i++ {
			spans = append(spans, span{l: sc.NextInt64(), r: sc.NextInt64()})
		}

		edge = make([][]int, N)
		for i := 0; i < N-1; i++ {
			x := sc.NextInt()
			y := sc.NextInt()
			x--
			y--
			edge[x] = append(edge[x], y)
			edge[y] = append(edge[y], x)
		}
		dp = InitInts2D(N, 2, -1)
		fmt.Fprintln(wr, f(0, -1, 0))
	}

}

package main

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

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

func Read() int {
	in.Scan()
	r := 0
	for _, v := range in.Bytes() {
		r = r*10 + int(v-'0')
	}
	return r
}

func init() {
	const MaxInt = int(^uint(0) >> 1)
	in.Buffer(make([]byte, 1<<12), MaxInt)
	in.Split(bufio.ScanWords)
}

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

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

const N = 100100

var (
	a [N][2]int
	g [N][]int
)

func dfs(v, p int) (int64, int64) {
	var r0, r1 int64
	for _, to := range g[v] {
		if to == p {
			continue
		}
		t0, t1 := dfs(to, v)
		r0 += max(t0 + abs(int64(a[v][0] - a[to][0])), t1 + abs(int64(a[v][0] - a[to][1])))
		r1 += max(t0 + abs(int64(a[v][1] - a[to][0])), t1 + abs(int64(a[v][1] - a[to][1])))
	}
	return r0, r1
}

func main() {
	defer out.Flush()
	for t := Read(); t > 0; t-- {
		n := Read()
		for i := 0; i < n; i++ {
			a[i], g[i] = [2]int{Read(), Read()}, []int{}
		}
		for i := 1; i < n; i++ {
			u, v := Read() - 1, Read() - 1
			g[u] = append(g[u], v)
			g[v] = append(g[v], u)
		}
		Fprintln(out, max(dfs(0, -1)));
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)
func CF1528A(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T, n, v, w int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]struct{ l, r int }, n)
		for i := range a {
			Fscan(in, &a[i].l, &a[i].r)
		}
		type nb struct{ to, wt int }
		g := make([][]int, n)
		for i := 1; i < n; i++ {
			Fscan(in, &v, &w)
			v--
			w--
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}
		var f func(v, fa int) (s0, s1 int64)
		f = func(v, fa int) (s0, s1 int64) {
			for _, w := range g[v] {
				if w != fa {
					p, q := f(w, v)
					s0 += max(abs(a[v].l-a[w].l)+p, abs(a[v].l-a[w].r)+q)
					s1 += max(abs(a[v].r-a[w].l)+p, abs(a[v].r-a[w].r)+q)
				}
			}
			return
		}
		Fprintln(out, max(f(0, -1)))
	}
}

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

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

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

package main

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

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

	var T, n, v, w int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([][2]int, n)
		for i := range a {
			Fscan(in, &a[i][0], &a[i][1])
		}
		type nb struct{ to, wt int }
		g := make([][]int, n)
		for i := 1; i < n; i++ {
			Fscan(in, &v, &w)
			v--
			w--
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}

		dp := make([][2]int64, n)
		for i := range dp {
			dp[i] = [2]int64{-1, -1}
		}
		var f func(v, fa, x int) int64
		f = func(v, fa, x int) (s int64) {
			if dp[v][x] >= 0 {
				return dp[v][x]
			}
			for _, w := range g[v] {
				if w != fa {
					s += max(int64(abs(a[v][x]-a[w][0]))+f(w, v, 0), int64(abs(a[v][x]-a[w][1]))+f(w, v, 1))
				}
			}
			dp[v][x] = s
			return
		}
		Fprintln(out, max(f(0, -1, 0), f(0, -1, 0)))
	}
}

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

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

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


One not particularly beautiful evening Valera got very bored. To amuse himself a little bit, he found the following game.He took a checkered white square piece of paper, consisting of n × n cells. After that, he started to paint the white cells black one after the other. In total he painted m different cells on the piece of paper. Since Valera was keen on everything square, he wondered, how many moves (i.e. times the boy paints a square black) he should make till a black square with side 3 can be found on the piece of paper. But Valera does not know the answer to this question, so he asks you to help him.Your task is to find the minimum number of moves, till the checkered piece of paper has at least one black square with side of 3. Otherwise determine that such move does not exist.

The first line contains two integers n and m (1 ≤ n ≤ 1000, 1 ≤ m ≤ min(n·n, 105)) — the size of the squared piece of paper and the number of moves, correspondingly.  Then, m lines contain the description of the moves. The i-th line contains two integers xi, yi (1 ≤ xi, yi ≤ n) — the number of row and column of the square that gets painted on the i-th move.  All numbers on the lines are separated by single spaces. It is guaranteed that all moves are different. The moves are numbered starting from 1 in the order, in which they are given in the input. The columns of the squared piece of paper are numbered starting from 1, from the left to the right. The rows of the squared piece of paper are numbered starting from 1, from top to bottom.

On a single line print the answer to the problem — the minimum number of the move after which the piece of paper has a black square with side 3. If no such move exists, print -1.

package main

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

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	n, m := scanInt(scanner), scanInt(scanner)
	count := make([][]int, n)
	for r, _ := range count {
		count[r] = make([]int, n)
	}
	dr := []int{-1, -1, -1, 0, 0, 0, 1, 1, 1}
	dc := []int{-1, 0, 1, -1, 0, 1, -1, 0, 1}
	for i := 0; i != m; i++ {
		x, y := scanInt(scanner)-1, scanInt(scanner)-1
		for j := 0; j < 9; j++ {
			r := x+dr[j]
			if r == -1 || r == n {
				continue
			}
			c := y+dc[j]
			if c == -1 || c == n {
				continue
			}
			count[r][c] += 1
			if count[r][c] == 9 {
				writer.WriteString(fmt.Sprintf("%d\n", i+1))
				return
			}
		}
	}
	writer.WriteString("-1\n")
}

package main

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

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	n, m := scanInt(scanner), scanInt(scanner)
	g := make([][]bool, n)
	for r, _ := range g {
		g[r] = make([]bool, n)
	}
	result := -1
	for i := 0; i != m; i++ {
		x, y := scanInt(scanner)-1, scanInt(scanner)-1
		g[x][y] = true
		found := false
		loop9:
		for r := x-2; r <= x; r++ {
			for c := y-2; c <= y; c++ {
				found = true
				loop3:
				for R := r; R < r+3; R++ {
					for C := c; C < c+3; C++ {
						if R < 0 || R >= n || C < 0 || C >= n || !g[R][C] {
							found = false
							break loop3
						}
					}
				}
				if found {
					break loop9
				}
			}
		}
		if found && result == -1 {
			result = i+1
		}
	}
	writer.WriteString(fmt.Sprintf("%d\n", result))
}


A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the system is not perfect and sometimes it happens that Geraldionians cannot express a certain sum of money with any set of banknotes. Of course, they can use any number of banknotes of each value. Such sum is called unfortunate. Gerald wondered: what is the minimum unfortunate sum?

The first line contains number n (1 ≤ n ≤ 1000) — the number of values of the banknotes that used in Geraldion.  The second line contains n distinct space-separated numbers a1, a2, ..., an (1 ≤ ai ≤ 106) — the values of the banknotes.

Print a single line — the minimum unfortunate sum. If there are no unfortunate sums, print  - 1.

package main

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

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

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

	n := nextInt()

	for i := 0; i < n; i++ {
		a := nextInt()
		if a == 1 {
			println(-1)
			return
		}
	}

	println(1)
}

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

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

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

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

func main() {
	z := bufio.NewScanner(os.Stdin)
	z.Split(bufio.ScanWords)
	z.Scan()
	n, _ := strconv.Atoi(z.Text())
	for i := 0; i < n; i++  {
		z.Scan()
		a, _ := strconv.Atoi(z.Text())
		if a == 1 {
			fmt.Println(-1)
			return
		}
	}
	fmt.Println(1)
}

package main

import (
     "fmt"
)

func main() {
        var n, i, a int
        var ok bool
        ok = false
        fmt.Scan(&n)
        for i = 0; i < n; i++ {
            fmt.Scan(&a)
            if a == 1 {
               ok = true
             }
        }
        if ok {
           fmt.Println("-1")
        }else{
           fmt.Println("1")
        }
}

package main

import "fmt"

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

	v := 0
	ok := false
	for i := 0; i < n; i++ {
		fmt.Scan(&v)
		if v == 1 {
			ok = true
		}
	}

	if ok {
		fmt.Println(-1)
	} else {
		fmt.Println(1)
	}

}

package main

import (
    "fmt"
    )

func main() {
    var n, i uint
    fmt.Scan(&n)

    find := false
    for i = 0; i < n; i++ {
        var a uint
        fmt.Scan(&a)
        if a == 1 {
            find = true
        }
    }

    if find {
        fmt.Println("-1")
    } else {
        fmt.Println("1")
    }
}

package main

import (
	"fmt"
)

func main() {
	var n, m int
	var res bool = false
	fmt.Scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &m)
		if m == 1 {
			res = true
		}
	}
	if res == true {
		fmt.Println("-1")
	} else {
		fmt.Println(1)
	}
}

package main

import (
    "fmt"
)

func main() {
    var n, m int
    var res bool = false
    fmt.Scan(&n)
    for i := 0; i < n; i++ {
        fmt.Scan(&m)
        if m == 1 {
            res = true
        }
    }
    if res == true {
        fmt.Println("-1")
    } else {
        fmt.Println("1")
    }
}

package main

import (
    "fmt"
    )

func main() {
    var n, i, sum uint
    fmt.Scan(&n)

    find := false
    for i = 0; i < n; i++ {
        var a uint
        fmt.Scan(&a)
        if a == 1 {
            find = true
        } else {
            sum += a
        }
    }

    if find {
        fmt.Println("-1")
    } else {
        fmt.Println(sum-1)
    }
}

package main

import (
	"fmt"
)

func main() {
	var n, m int
	var res bool = false
	fmt.Scanf("%d", &n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &m)
		if m == 1 {
			res = true
		}
	}
	if res {
		fmt.Println(-1)
	} else {
		fmt.Println(1)
	}
}

package main

import (
    "fmt"
)

func main() {
    var n, m int
    var res bool = false
    fmt.Scanf("%d", &n)
    for i := 0; i < n; i++ {
        fmt.Scanf("%d", &m)
        if m == 1 {
            res = true
        }
    }
    if res == true {
        fmt.Println("-1")
    } else {
        fmt.Println("1")
    }
}

package main

import (
	"fmt"
)

func main() {
	var n, m int
	var res bool = false
	fmt.Scanf("%d", &n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &m)
		if m == 1 {
			res = true
		}
	}
	if res == true {
		fmt.Println("-1")
	} else {
		fmt.Println(1)
	}
}

package main

import (
	"fmt"
)

func main() {
	var n, m int
	var res bool = false
	fmt.Scanf("%d", &n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &m)
		if m == 1 {
			res = true
		}
	}
	if res == true {
		fmt.Println(-1)
	} else {
		fmt.Println(1)
	}
}