Recently, Masha was presented with a chessboard with a height of $$$n$$$ and a width of $$$m$$$.The rows on the chessboard are numbered from $$$1$$$ to $$$n$$$ from bottom to top. The columns are numbered from $$$1$$$ to $$$m$$$ from left to right. Therefore, each cell can be specified with the coordinates $$$(x,y)$$$, where $$$x$$$ is the column number, and $$$y$$$ is the row number (do not mix up).Let us call a rectangle with coordinates $$$(a,b,c,d)$$$ a rectangle lower left point of which has coordinates $$$(a,b)$$$, and the upper right one — $$$(c,d)$$$.The chessboard is painted black and white as follows:   An example of a chessboard. Masha was very happy with the gift and, therefore, invited her friends Maxim and Denis to show off. The guys decided to make her a treat — they bought her a can of white and a can of black paint, so that if the old board deteriorates, it can be repainted. When they came to Masha, something unpleasant happened: first, Maxim went over the threshold and spilled white paint on the rectangle $$$(x_1,y_1,x_2,y_2)$$$. Then after him Denis spilled black paint on the rectangle $$$(x_3,y_3,x_4,y_4)$$$.To spill paint of color $$$color$$$ onto a certain rectangle means that all the cells that belong to the given rectangle become $$$color$$$. The cell dyeing is superimposed on each other (if at first some cell is spilled with white paint and then with black one, then its color will be black).Masha was shocked! She drove away from the guests and decided to find out how spoiled the gift was. For this, she needs to know the number of cells of white and black color. Help her find these numbers!

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of test cases. Each of them is described in the following format: The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n,m \le 10^9$$$) — the size of the board. The second line contains four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, $$$y_2$$$ ($$$1 \le x_1 \le x_2 \le m, 1 \le y_1 \le y_2 \le n$$$) — the coordinates of the rectangle, the white paint was spilled on. The third line contains four integers $$$x_3$$$, $$$y_3$$$, $$$x_4$$$, $$$y_4$$$ ($$$1 \le x_3 \le x_4 \le m, 1 \le y_3 \le y_4 \le n$$$) — the coordinates of the rectangle, the black paint was spilled on.

Output $$$t$$$ lines, each of which contains two numbers — the number of white and black cells after spilling paint, respectively.

NoteExplanation for examples:The first picture of each illustration shows how the field looked before the dyes were spilled. The second picture of each illustration shows how the field looked after Maxim spoiled white dye (the rectangle on which the dye was spilled is highlighted with red). The third picture in each illustration shows how the field looked after Denis spoiled black dye (the rectangle on which the dye was spilled is highlighted with red).In the first test, the paint on the field changed as follows:  In the second test, the paint on the field changed as follows:  In the third test, the paint on the field changed as follows:  In the fourth test, the paint on the field changed as follows:  In the fifth test, the paint on the field changed as follows:

package main

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

func countWhite(x1,y1,x2,y2 int64)int64{
	var firstwhite bool =  false
	if (x1+y1)%2==0{
		firstwhite = true
	}
	var lenx int64= x2-x1+1
	var leny int64=y2-y1+1
	var ans int64 = 0
	if lenx%2==1{
		lenx--
		ans += (leny+1)/2
	}
	if lenx > 0{
		ans += (lenx/2)*leny
	}
	if firstwhite == false{
		return (x2-x1+1)*(y2-y1+1)-ans
	}
	return ans
}
func intct(x1,y1,x2,y2,x3,y3,x4,y4 int64)(bool,int64,int64,int64,int64){

	ok,nx1,nx2 := linein(x1,x2,x3,x4)
	if !ok {
		return false,0,0,0,0
	}
	ok,ny1,ny2 := linein(y1,y2,y3,y4)
	if !ok {
		return false,0,0,0,0
	}
	return true,nx1,ny1,nx2,ny2
}
func linein(x1,x2,x3,x4 int64)(bool,int64,int64)  {
	if x1 >= x3 && x1<=x4{
		if x2<=x4{
			return true,x1,x2
		} else{
			return true,x1,x4
		}
	}else if x3 >= x1&&x3 <= x2{
		if x4<=x2{
			return true,x3,x4
		} else{
			return true,x3,x2
		}
	}
	return false,-1,-1
}
func area(x1,y1,x2,y2 int64)int64  {
	return (x2-x1+1)*(y2-y1+1)
}
func main(){
	buf := bufio.NewReader(os.Stdin)
	var n int64
	var t int
	var m int64
	fmt.Fscanf(buf, "%d\n", &t)
	for i:=1 ;i <= t;i++{
		fmt.Fscanf(buf, "%d %d\n", &n,&m)
		var x1,x2,x3,x4 int64
		var y1,y2,y3,y4 int64
		fmt.Fscanf(buf, "%d %d %d %d\n", &x1,&y1,&x2,&y2)
		fmt.Fscanf(buf, "%d %d %d %d\n", &x3,&y3,&x4,&y4)
		var wcnt int64 = countWhite(1,1,m,n)
		var bcnt int64 = n*m-wcnt
	//	fmt.Printf("==  wcnt=%d bcnt=%d\n",wcnt,bcnt)
		wcnt -= countWhite(x1,y1,x2,y2)
		wcnt -= countWhite(x3,y3,x4,y4)
		bcnt -= area(x1,y1,x2,y2)-countWhite(x1,y1,x2,y2)
		bcnt -= area(x3,y3,x4,y4)-countWhite(x3,y3,x4,y4)
//		fmt.Printf("11==  wcnt=%d bcnt=%d %d %d %d %d\n",wcnt,bcnt,area(x1,y1,x2,y2),countWhite(x1,y1,x2,y2),area(x3,y3,x4,y4),countWhite(x3,y3,x4,y4))
		ok,inx1,iny1,inx2,iny2 := intct(x1,y1,x2,y2,x3,y3,x4,y4)
		if ok{
//			fmt.Printf("-> %d %d %d %d %d\n",inx1,iny1,inx2,iny2,countWhite(3,2,3,2))
			wcnt += countWhite(inx1,iny1,inx2,iny2)
			bcnt += area(inx1,iny1,inx2,iny2)-countWhite(inx1,iny1,inx2,iny2)
//			fmt.Printf("22==  wcnt=%d bcnt=%d\n",wcnt,bcnt)
		}
		var ansb int64= bcnt+area(x3,y3,x4,y4)
		fmt.Printf("%d %d\n",n*m-ansb,ansb)
	}

}

// If there are intersection, white part should be handle as two seperated parts.
package main

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

// (1, 1) is white
func calcBlock(x1, y1, x2, y2 int) (int64, int64) {
	width, height := x2-x1+1, y2-y1+1
	w, b := int64(width)*(int64(height)/2), int64(width)*(int64(height)/2)
	if height%2 == 1 {
		w, b = w+int64(width/2), b+int64(width/2)
		if width%2 == 1 {
			if (x2+y2)%2 == 0 {
				w++
			} else {
				b++
			}
		}
	}
	return w, b
}

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

func getIntsection(x1, y1, x2, y2, x3, y3, x4, y4 int) (int, int, int, int) {
	// fmt.Println("x1, y1, x2, y2, x3, y3, x4, y4", x1, y1, x2, y2, x3, y3, x4, y4)
	x5, y5 := max(x1, x3), max(y1, y3)
	x6, y6 := min(x2, x4), min(y2, y4)
	// fmt.Println(x5, y5, x6, y6)
	if (x5 > x6) || (y5 > y6) {
		return -1, -1, -1, -1
	}
	return x5, y5, x6, y6
}

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

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

	for i := 0; i < t; i++ {
		n, m := getNum(scanner), getNum(scanner)
		x1, y1, x2, y2 := getNum(scanner), getNum(scanner), getNum(scanner), getNum(scanner)
		x3, y3, x4, y4 := getNum(scanner), getNum(scanner), getNum(scanner), getNum(scanner)
		// fmt.Println("case", x1, y1, x2, y2, x3, y3, x4, y4)
		x5, y5, x6, y6 := getIntsection(x1, y1, x2, y2, x3, y3, x4, y4)
		hasIntsection := x5 != -1

		tw, tb := calcBlock(1, 1, m, n)
		// b1 gonna become w
		_, b1 := calcBlock(x1, y1, x2, y2)
		tw, tb = tw+b1, tb-b1
		if hasIntsection {
			// intb gonna "become" b
			_, intb := calcBlock(x5, y5, x6, y6)
			tw, tb = tw-intb, tb+intb
		}
		// w2 gonna become b
		w2, _ := calcBlock(x3, y3, x4, y4)
		tw, tb = tw-w2, tb+w2

		fmt.Println(tw, tb)
	}
}

// If there are intersection, white part should be handle as two seperated parts.
package main

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

// (1, 1) is white
func calcBlock(a, b, c, d int) (int, int) {
	width, height := c-a+1, d-b+1
	w, b := width*(height/2), width*(height/2)
	if height%2 == 1 {
		w, b = w+width/2, b+width/2
		if width%2 == 1 {
			if (c+d)%2 == 0 {
				w++
			} else {
				b++
			}
		}
	}
	return w, b
}

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

func getIntsection(x1, y1, x2, y2, x3, y3, x4, y4 int) (int, int, int, int) {
	// fmt.Println("x1, y1, x2, y2, x3, y3, x4, y4", x1, y1, x2, y2, x3, y3, x4, y4)
	x5, y5 := max(x1, x3), max(y1, y3)
	x6, y6 := min(x2, x4), min(y2, y4)
	// fmt.Println(x5, y5, x6, y6)
	if (x5 > x6) || (y5 > y6) {
		return -1, -1, -1, -1
	}
	return x5, y5, x6, y6
}

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	t := getNum(scanner)
	for i := 0; i < t; i++ {
		n, m := getNum(scanner), getNum(scanner)
		x1, y1, x2, y2 := getNum(scanner), getNum(scanner), getNum(scanner), getNum(scanner)
		x3, y3, x4, y4 := getNum(scanner), getNum(scanner), getNum(scanner), getNum(scanner)
		// fmt.Println("case", x1, y1, x2, y2, x3, y3, x4, y4)
		x5, y5, x6, y6 := getIntsection(x1, y1, x2, y2, x3, y3, x4, y4)
		hasIntsection := x5 != -1

		tw, tb := calcBlock(1, 1, m, n)
		// b1 gonna become w
		_, b1 := calcBlock(x1, y1, x2, y2)
		tw, tb = tw+b1, tb-b1
		if hasIntsection {
			// intb gonna "become" b
			_, intb := calcBlock(x5, y5, x6, y6)
			tw, tb = tw-intb, tb+intb
		}
		// w2 gonna become b
		w2, _ := calcBlock(x3, y3, x4, y4)
		tw, tb = tw-w2, tb+w2

		fmt.Println(tw, tb)
	}
}


You play a strategic video game (yeah, we ran out of good problem legends). In this game you control a large army, and your goal is to conquer $$$n$$$ castles of your opponent.Let's describe the game process in detail. Initially you control an army of $$$k$$$ warriors. Your enemy controls $$$n$$$ castles; to conquer the $$$i$$$-th castle, you need at least $$$a_i$$$ warriors (you are so good at this game that you don't lose any warriors while taking over a castle, so your army stays the same after the fight). After you take control over a castle, you recruit new warriors into your army — formally, after you capture the $$$i$$$-th castle, $$$b_i$$$ warriors join your army. Furthermore, after capturing a castle (or later) you can defend it: if you leave at least one warrior in a castle, this castle is considered defended. Each castle has an importance parameter $$$c_i$$$, and your total score is the sum of importance values over all defended castles. There are two ways to defend a castle:  if you are currently in the castle $$$i$$$, you may leave one warrior to defend castle $$$i$$$;  there are $$$m$$$ one-way portals connecting the castles. Each portal is characterised by two numbers of castles $$$u$$$ and $$$v$$$ (for each portal holds $$$u > v$$$). A portal can be used as follows: if you are currently in the castle $$$u$$$, you may send one warrior to defend castle $$$v$$$. Obviously, when you order your warrior to defend some castle, he leaves your army.You capture the castles in fixed order: you have to capture the first one, then the second one, and so on. After you capture the castle $$$i$$$ (but only before capturing castle $$$i + 1$$$) you may recruit new warriors from castle $$$i$$$, leave a warrior to defend castle $$$i$$$, and use any number of portals leading from castle $$$i$$$ to other castles having smaller numbers. As soon as you capture the next castle, these actions for castle $$$i$$$ won't be available to you.If, during some moment in the game, you don't have enough warriors to capture the next castle, you lose. Your goal is to maximize the sum of importance values over all defended castles (note that you may hire new warriors in the last castle, defend it and use portals leading from it even after you capture it — your score will be calculated afterwards).Can you determine an optimal strategy of capturing and defending the castles?

The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 \le n \le 5000$$$, $$$0 \le m \le \min(\frac{n(n - 1)}{2}, 3 \cdot 10^5)$$$, $$$0 \le k \le 5000$$$) — the number of castles, the number of portals and initial size of your army, respectively. Then $$$n$$$ lines follow. The $$$i$$$-th line describes the $$$i$$$-th castle with three integers $$$a_i$$$, $$$b_i$$$ and $$$c_i$$$ ($$$0 \le a_i, b_i, c_i \le 5000$$$) — the number of warriors required to capture the $$$i$$$-th castle, the number of warriors available for hire in this castle and its importance value. Then $$$m$$$ lines follow. The $$$i$$$-th line describes the $$$i$$$-th portal with two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le v_i < u_i \le n$$$), meaning that the portal leads from the castle $$$u_i$$$ to the castle $$$v_i$$$. There are no two same portals listed. It is guaranteed that the size of your army won't exceed $$$5000$$$ under any circumstances (i. e. $$$k + \sum\limits_{i = 1}^{n} b_i \le 5000$$$).

If it's impossible to capture all the castles, print one integer $$$-1$$$. Otherwise, print one integer equal to the maximum sum of importance values of defended castles.

NoteThe best course of action in the first example is as follows:  capture the first castle;  hire warriors from the first castle, your army has $$$11$$$ warriors now;  capture the second castle;  capture the third castle;  hire warriors from the third castle, your army has $$$13$$$ warriors now;  capture the fourth castle;  leave one warrior to protect the fourth castle, your army has $$$12$$$ warriors now. This course of action (and several other ones) gives $$$5$$$ as your total score.The best course of action in the second example is as follows:  capture the first castle;  hire warriors from the first castle, your army has $$$11$$$ warriors now;  capture the second castle;  capture the third castle;  hire warriors from the third castle, your army has $$$13$$$ warriors now;  capture the fourth castle;  leave one warrior to protect the fourth castle, your army has $$$12$$$ warriors now;  send one warrior to protect the first castle through the third portal, your army has $$$11$$$ warriors now. This course of action (and several other ones) gives $$$22$$$ as your total score.In the third example it's impossible to capture the last castle: you need $$$14$$$ warriors to do so, but you can accumulate no more than $$$13$$$ without capturing it.

package main

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

const (
	mod = 998244353
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n, m, k := 0, 0, 0
	fmt.Fscanf(in, "%d %d %d\n", &n, &m, &k)
	from := make([]int, n+1)
	a, b, c := make([]int, n+1), make([]int, n+1), make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(in, "%d %d %d\n", &a[i], &b[i], &c[i])
	}
	u, v := 0, 0
	for i := 0; i <= n; i++ {
		from[i] = i
	}
	for i := 0; i < m; i++ {
		fmt.Fscanf(in, "%d %d\n", &u, &v)
		if u > from[v] {
			from[v] = u
		}
	}
	//fmt.Println(from)

	to := make([][]int, n+1)
	for i := 1; i <= n; i++ {
		to[from[i]] = append(to[from[i]], c[i])
	}
	//fmt.Println(to)
	val := make([][]int, n+1) // val[i][j] send j warriors from i best benefit
	for i := 1; i <= n; i++ {
		if len(to[i]) > 0 {
			sort.Ints(to[i])
			cur := 0
			for j := 0; j < len(to[i]); j++ {
				cur += to[i][len(to[i])-1-j]
				val[i] = append(val[i], cur)
			}
		}
	}
	f := make(map[int]map[int]int)
	for i := 0; i <= n; i++ {
		f[i] = make(map[int]int)
	}
	f[0][k] = 0
	queue := [][]int{}
	queue = append(queue, []int{0, k})
	ans := -1
	//fmt.Println(val)
	for len(queue) > 0 {
		cur := queue[0]
		queue = queue[1:]
		x := cur[0]
		y := cur[1]
		if x == n {
			if f[x][y] > ans {
				ans = f[x][y]
			}
			continue
		}
		curVal := f[x][y]
		if y >= a[x+1] {
			nx := x + 1
			ny := y + b[nx]
			if _, ok := f[nx][ny]; !ok {
				f[nx][ny] = curVal
				queue = append(queue, []int{nx, ny})
			} else if f[nx][ny] < curVal {
				f[nx][ny] = curVal
				//queue = append(queue, []int{nx, ny})
			}
			for i := 0; i < len(val[nx]); i++ {
				if ny == 0 {
					break
				}
				ny--
				newVal := curVal + val[nx][i]
				if _, ok := f[nx][ny]; !ok {
					f[nx][ny] = newVal
					queue = append(queue, []int{nx, ny})
				} else if f[nx][ny] < newVal {
					f[nx][ny] = newVal
					//queue = append(queue, []int{nx, ny})
				}
			}
		}
	}
	fmt.Fprintf(out, "%d\n", ans)
}

func reverse(a []byte) []byte {
	ans := []byte{}
	for i := len(a) - 1; i >= 0; i-- {
		ans = append(ans, a[i])
	}
	return ans
}

func first(a int) int64 {
	ans := int64(0)
	base := int64(10)
	for a > 0 {
		tail := int64(a % 10)
		a /= 10
		ans += base * tail
		base *= 100
		ans %= mod
		base %= mod
	}
	return ans
}

func second(a int) int64 {
	ans := int64(0)
	base := int64(1)
	for a > 0 {
		tail := int64(a % 10)
		a /= 10
		ans += base * tail
		base *= 100
		ans %= mod
		base %= mod
	}
	return ans
}

package main

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

const (
	mod = 998244353
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n, m, k := 0, 0, 0
	fmt.Fscanf(in, "%d %d %d\n", &n, &m, &k)
	from := make([]int, n+1)
	a, b, c := make([]int, n+1), make([]int, n+1), make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(in, "%d %d %d\n", &a[i], &b[i], &c[i])
	}
	u, v := 0, 0
	for i := 0; i <= n; i++ {
		from[i] = i
	}
	for i := 0; i < m; i++ {
		fmt.Fscanf(in, "%d %d\n", &u, &v)
		if u > from[v] {
			from[v] = u
		}
	}
	//fmt.Println(from)

	to := make([][]int, n+1)
	for i := 1; i <= n; i++ {
		if from[i] != 0 {
			to[from[i]] = append(to[from[i]], c[i])
		}
	}
	//fmt.Println(to)
	val := make([][]int, n+1) // val[i][j] send j warriors from i best benefit
	for i := 1; i <= n; i++ {
		if len(to[i]) > 0 {
			sort.Ints(to[i])
			cur := 0
			for j := 0; j < len(to[i]); j++ {
				cur += to[i][len(to[i])-1-j]
				val[i] = append(val[i], cur)
			}
		}
	}
	f := make(map[int]map[int]int)
	for i := 0; i <= n; i++ {
		f[i] = make(map[int]int)
	}
	f[0][k] = 0
	queue := [][]int{}
	queue = append(queue, []int{0, k})
	ans := -1
	//fmt.Println(val)
	for len(queue) > 0 {
		cur := queue[0]
		queue = queue[1:]
		x := cur[0]
		y := cur[1]
		if x == n {
			if f[x][y] > ans {
				ans = f[x][y]
			}
			continue
		}
		curVal := f[x][y]
		if y >= a[x+1] {
			nx := x + 1
			ny := y + b[nx]
			if _, ok := f[nx][ny]; !ok {
				f[nx][ny] = curVal
				queue = append(queue, []int{nx, ny})
			} else if f[nx][ny] < curVal {
				f[nx][ny] = curVal
				queue = append(queue, []int{nx, ny})
			}
			for i := 0; i < len(val[nx]); i++ {
				if ny == 0 {
					break
				}
				ny--
				newVal := curVal + val[nx][i]
				if _, ok := f[nx][ny]; !ok {
					f[nx][ny] = newVal
					queue = append(queue, []int{nx, ny})
				} else if f[nx][ny] < curVal {
					f[nx][ny] = newVal
					queue = append(queue, []int{nx, ny})
				}
			}
		}
	}
	fmt.Fprintf(out, "%d\n", ans)
}

func reverse(a []byte) []byte {
	ans := []byte{}
	for i := len(a) - 1; i >= 0; i-- {
		ans = append(ans, a[i])
	}
	return ans
}

func first(a int) int64 {
	ans := int64(0)
	base := int64(10)
	for a > 0 {
		tail := int64(a % 10)
		a /= 10
		ans += base * tail
		base *= 100
		ans %= mod
		base %= mod
	}
	return ans
}

func second(a int) int64 {
	ans := int64(0)
	base := int64(1)
	for a > 0 {
		tail := int64(a % 10)
		a /= 10
		ans += base * tail
		base *= 100
		ans %= mod
		base %= mod
	}
	return ans
}


Diamond Miner is a game that is similar to Gold Miner, but there are $$$n$$$ miners instead of $$$1$$$ in this game.The mining area can be described as a plane. The $$$n$$$ miners can be regarded as $$$n$$$ points on the y-axis. There are $$$n$$$ diamond mines in the mining area. We can regard them as $$$n$$$ points on the x-axis. For some reason, no miners or diamond mines can be at the origin (point $$$(0, 0)$$$). Every miner should mine exactly one diamond mine. Every miner has a hook, which can be used to mine a diamond mine. If a miner at the point $$$(a,b)$$$ uses his hook to mine a diamond mine at the point $$$(c,d)$$$, he will spend $$$\sqrt{(a-c)^2+(b-d)^2}$$$ energy to mine it (the distance between these points). The miners can't move or help each other.The object of this game is to minimize the sum of the energy that miners spend. Can you find this minimum?

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\le t\le 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of miners and mines. Each of the next $$$2n$$$ lines contains two space-separated integers $$$x$$$ ($$$-10^8 \le x \le 10^8$$$) and $$$y$$$ ($$$-10^8 \le y \le 10^8$$$), which represent the point $$$(x,y)$$$ to describe a miner's or a diamond mine's position. Either $$$x = 0$$$, meaning there is a miner at the point $$$(0, y)$$$, or $$$y = 0$$$, meaning there is a diamond mine at the point $$$(x, 0)$$$. There can be multiple miners or diamond mines at the same point. It is guaranteed that no point is at the origin. It is guaranteed that the number of points on the x-axis is equal to $$$n$$$ and the number of points on the y-axis is equal to $$$n$$$. It's guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.

For each test case, print a single real number — the minimal sum of energy that should be spent. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-9}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-9}$$$.

NoteIn the first test case, the miners are at $$$(0,1)$$$ and $$$(0,-1)$$$, while the diamond mines are at $$$(1,0)$$$ and $$$(-2,0)$$$. If you arrange the miners to get the diamond mines in the way, shown in the picture, you can get the sum of the energy $$$\sqrt2 + \sqrt5$$$.

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	scanf := func(f string, a ...interface{}) {
		if _, err := fmt.Fscanf(reader, f, a...); err != nil {
			log.Fatal(err)
		}
	}
	printf := func(f string, a ...interface{}) {
		if _, err := fmt.Fprintf(writer, f, a...); err != nil {
			log.Fatal(err)
		}
	}
	abs := func(a int64) int64 {
		if a >= 0 {
			return a
		}
		return -a
	}
	_, _ = scanf, printf
	defer func() {
		if err := writer.Flush(); err != nil {
			log.Fatal(err)
		}
	}()
	// Code goes below

	var t int
	scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		var n int
		scanf("%d\n", &n)
		diamonds := make([]int64, 0, n)
		miners := make([]int64, 0, n)
		for j := 0; j < 2*n; j++ {
			var x, y int64
			scanf("%d %d\n", &x, &y)
			if x == 0 {
				miners = append(miners, abs(y))
			} else {
				diamonds = append(diamonds, abs(x))
			}
		}
		sort.Slice(diamonds, func(i, j int) bool {
			return diamonds[i] < diamonds[j]
		})
		sort.Slice(miners, func(i, j int) bool {
			return miners[i] < miners[j]
		})
		var totalDistance float64
		for j := 0; j < n; j++ {
			totalDistance += math.Hypot(float64(diamonds[j]), float64(miners[j]))
		}
		printf("%g\n", totalDistance)
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	scanf := func(f string, a ...interface{}) {
		if _, err := fmt.Fscanf(reader, f, a...); err != nil {
			log.Fatal(err)
		}
	}
	printf := func(f string, a ...interface{}) {
		if _, err := fmt.Fprintf(writer, f, a...); err != nil {
			log.Fatal(err)
		}
	}
	abs := func(a int64) int64 {
		if a >= 0 {
			return a
		}
		return -a
	}
	_, _ = scanf, printf
	defer func() {
		if err := writer.Flush(); err != nil {
			log.Fatal(err)
		}
	}()
	// Code goes below

	var t int
	scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		var n int
		scanf("%d\n", &n)
		diamonds := make([]int64, 0, n)
		miners := make([]int64, 0, n)
		for j := 0; j < 2*n; j++ {
			var x, y int64
			scanf("%d %d\n", &x, &y)
			if x == 0 {
				miners = append(miners, abs(y))
			} else {
				diamonds = append(diamonds, abs(x))
			}
		}
		sort.Slice(diamonds, func(i, j int) bool {
			return diamonds[i] < diamonds[j]
		})
		sort.Slice(miners, func(i, j int) bool {
			return miners[i] < miners[j]
		})
		var totalDistance float64
		//printf("case %d\n", i)
		for j := 0; j < n; j++ {
			distance := math.Hypot(float64(diamonds[j]), float64(miners[j]))
			//printf("distance: %g\n", distance)
			totalDistance += distance
		}
		printf("%g\n", totalDistance)
	}

}

package main

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

type Item struct {
	x int
	y int
	t bool
	dist float64
}

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

	k:= 0
	scanner.Scan()
	k,_ = strconv.Atoi(scanner.Text())
	for i:= 0; i<k ;i++ {
		scanner.Scan()
		num,_ := strconv.Atoi(scanner.Text())

		all := make([] Item, num*2)
		for j:=0; j<num*2 ;j++ {
			scanner.Scan()
			line := strings.Split(scanner.Text() ," ")
			x,_ := strconv.Atoi(line[0])
			y,_ := strconv.Atoi(line[1])

			cur := Item{x,y, x==0 , 0 }

			cur.dist = float64(x)/math.Sqrt2+ float64(y)/math.Sqrt2
			all[j]= cur
		}

		sort.Slice(all, func(i, j int) bool {
			return math.Abs(all[i].dist) < math.Abs(all[j].dist)
		})

		miner := make([] Item, 0)
		mine := make([] Item, 0)

		for _,val := range all {
			if val.t {
				miner = append(miner, val)
			} else {
				mine = append(mine, val)
			}
		}

		res := float64(0)

		for j:=0; j<num; j++ {

			res+=  math.Sqrt(math.Pow(float64(miner[j].x - mine[j].x) ,2.0) + math.Pow(float64(miner[j].y - mine[j].y) ,2.0));
		}

		os.Stdout.WriteString(strconv.FormatFloat(res, 'f', -1, 64) + "\n")

	}

}

package main

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

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

func solve() {
	var n, x, y int
	fmt.Fscan(in, &n)
	a := make([]int, 0)
	b := make([]int, 0)
	for i := 0; i < n*2; i++ {
		fmt.Fscan(in, &x, &y)
		if x == 0 {
			a = append(a, abs(y))
		} else {
			b = append(b, abs(x))
		}
	}
	sort.Ints(a)
	sort.Ints(b)
	var res float64
	for i := 0; i < n; i++ {
		res += calc(float64(a[i]), float64(b[i]))
	}
	fmt.Fprintf(out, "%.15f\n", res)
}

func calc(a, b float64) float64 {
	return math.Sqrt(a*a + b*b)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1495A(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var T, n, x, y int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		var a, b []int
		for n *= 2; n > 0; n-- {
			Fscan(in, &x, &y)
			if x < 0 {
				x = -x
			}
			if y < 0 {
				y = -y
			}
			if x == 0 {
				a = append(a, y)
			} else {
				b = append(b, x)
			}
		}
		sort.Ints(a)
		sort.Ints(b)
		s := .0
		for i, y := range a {
			s += math.Hypot(float64(b[i]), float64(y))
		}
		Fprintf(out, "%.15f\n", s)
	}
}

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

package main

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

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

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

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

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

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

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

func solveC() {
	var n int
	scan(&n)
	xx := make([]int, 0, n)
	yy := make([]int, 0, n)
	for i := 0; i < 2*n; i++ {
		x, y := 0, 0
		scan(&x, &y)
		if x < 0 {
			x = -x
		}
		if y < 0 {
			y = -y
		}
		if x == 0 {
			yy = append(yy, y)
		} else {
			xx = append(xx, x)
		}
	}
	sort.Slice(xx, func(i, j int) bool { return xx[i] < xx[j] })
	sort.Slice(yy, func(i, j int) bool { return yy[i] < yy[j] })

	var res float64
	for i := range xx {
		x, y := float64(xx[i]), float64(yy[i])
		res += math.Sqrt(x*x + y*y)
	}
	printf("%v\n", res)
}

package main

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

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

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

func yes() {
	p("YES")
}

func no() {
	p("NO")
}

func p(a ...interface{})            { fmt.Fprint(writer, a...) }
func pln(a ...interface{})          { fmt.Fprintln(writer, a...) }
func s(a ...interface{})            { fmt.Fscan(reader, a...) }
func pf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func sf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

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

func solve() {
	var n uint64 = 0
	s(&n)
	m := make([]uint64, n)
	a := make([]uint64, n)
	var i uint64 = 0
	var j uint64 = 0
	for i+j < n*2 {
		x, y := 0, 0
		s(&x, &y)
		if x == 0 {
			m[i] = uint64(math.Abs(float64(y)))
			i++
		} else {
			a[j] = uint64(math.Abs(float64(x)))
			j++
		}
	}
	sort.Slice(m, func(i, j int) bool { return m[i] < m[j] })
	sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })
	i = 0
	sum := 0.0
	for i < n {
		sum += math.Sqrt(float64(m[i]*m[i] + a[i]*a[i]))
		i++
	}
	pf("%.15f\n", sum)
}

package main

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

func getNextString(scanner *bufio.Scanner) string {
	if !scanner.Scan() {
		panic("scan failed")
	}
	return scanner.Text()
}
func atoi(s string) int                             { x, _ := strconv.Atoi(s); return x }
func getNextInt(scanner *bufio.Scanner) int         { return atoi(getNextString(scanner)) }
func atoi64(s string) int64                         { x, _ := strconv.ParseInt(s, 10, 64); return x }
func getNextInt64(scanner *bufio.Scanner) int64     { return atoi64(getNextString(scanner)) }
func atof64(s string) float64                       { x, _ := strconv.ParseFloat(s, 64); return x }
func getNextFloat64(scanner *bufio.Scanner) float64 { return atof64(getNextString(scanner)) }
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	t := getNextInt(scanner)
	for t > 0 {
		t--
		func() {
			n := getNextInt(scanner)
			xx := make([]int, 0)
			yy := make([]int, 0)
			for i := 0; i < n*2; i++ {
				x := getNextInt(scanner)
				y := getNextInt(scanner)
				if x < 0 {
					x = -x
				}
				if y < 0 {
					y = -y
				}
				if x == 0 {
					yy = append(yy, y)
					continue
				}
				xx = append(xx, x)
			}
			sort.Ints(xx)
			sort.Ints(yy)
			var ans float64
			for i := 0; i < n; i++ {
				ans += math.Sqrt(math.Pow(float64(xx[i]), 2) + math.Pow(float64(yy[i]), 2))
			}
			fmt.Fprintln(writer, ans)
		}()
	}
}

package main

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

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

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

func yes() {
	p("YES")
}

func no() {
	p("NO")
}

func p(a ...interface{})            { fmt.Fprint(writer, a...) }
func pln(a ...interface{})          { fmt.Fprintln(writer, a...) }
func s(a ...interface{})            { fmt.Fscan(reader, a...) }
func pf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func sf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

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

func solve() {
	n := 0
	s(&n)
	m := make([]int, n)
	a := make([]int, n)
	i := 0
	j := 0
	for f := 0; f < n*2; f++ {
		x, y := 0, 0
		s(&x, &y)
		if x == 0 {
			m[i] = int(math.Abs(float64(y)))
			i++
		} else {
			a[j] = int(math.Abs(float64(x)))
			j++
		}
	}
	sort.Ints(m)
	sort.Ints(a)
	sum := 0.0
	for i := 0; i < n; i++ {
		sum += math.Sqrt(float64(m[i]*m[i] + a[i]*a[i]))
	}
	pf("%.15f\n", sum)
}

package main

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

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

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

func yes() {
	p("YES")
}

func no() {
	p("NO")
}

func p(a ...interface{})   { fmt.Fprint(writer, a...) }
func pln(a ...interface{}) { fmt.Fprintln(writer, a...) }
func s(a ...interface{})   { fmt.Fscan(reader, a...) }

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

func solve() {
	n := 0
	s(&n)
	m := make([]int, n)
	a := make([]int, n)
	i := 0
	j := 0
	for i+j < n*2 {
		x, y := 0, 0
		s(&x, &y)
		if x == 0 {
			m[i] = int(math.Abs(float64(y)))
			i++
		} else {
			a[j] = int(math.Abs(float64(x)))
			j++
		}
	}
	sort.Ints(m)
	sort.Ints(a)
	sum := 0.0
	for i := 0; i < n; i++ {
		sum += math.Sqrt(float64(m[i]*m[i] + a[i]*a[i]))
	}
	pln(sum)
}


There are $$$n$$$ piranhas with sizes $$$a_1, a_2, \ldots, a_n$$$ in the aquarium. Piranhas are numbered from left to right in order they live in the aquarium.Scientists of the Berland State University want to find if there is dominant piranha in the aquarium. The piranha is called dominant if it can eat all the other piranhas in the aquarium (except itself, of course). Other piranhas will do nothing while the dominant piranha will eat them.Because the aquarium is pretty narrow and long, the piranha can eat only one of the adjacent piranhas during one move. Piranha can do as many moves as it needs (or as it can). More precisely:   The piranha $$$i$$$ can eat the piranha $$$i-1$$$ if the piranha $$$i-1$$$ exists and $$$a_{i - 1} < a_i$$$.  The piranha $$$i$$$ can eat the piranha $$$i+1$$$ if the piranha $$$i+1$$$ exists and $$$a_{i + 1} < a_i$$$. When the piranha $$$i$$$ eats some piranha, its size increases by one ($$$a_i$$$ becomes $$$a_i + 1$$$).Your task is to find any dominant piranha in the aquarium or determine if there are no such piranhas.Note that you have to find any (exactly one) dominant piranha, you don't have to find all of them.For example, if $$$a = [5, 3, 4, 4, 5]$$$, then the third piranha can be dominant. Consider the sequence of its moves:   The piranha eats the second piranha and $$$a$$$ becomes $$$[5, \underline{5}, 4, 5]$$$ (the underlined piranha is our candidate).  The piranha eats the third piranha and $$$a$$$ becomes $$$[5, \underline{6}, 5]$$$.  The piranha eats the first piranha and $$$a$$$ becomes $$$[\underline{7}, 5]$$$.  The piranha eats the second piranha and $$$a$$$ becomes $$$[\underline{8}]$$$. You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 3 \cdot 10^5$$$) — the number of piranhas in the aquarium. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the size of the $$$i$$$-th piranha. It is guaranteed that the sum of $$$n$$$ does not exceed $$$3 \cdot 10^5$$$ ($$$\sum n \le 3 \cdot 10^5$$$).

For each test case, print the answer: -1 if there are no dominant piranhas in the aquarium or index of any dominant piranha otherwise. If there are several answers, you can print any.

NoteThe first test case of the example is described in the problem statement.In the second test case of the example, there are no dominant piranhas in the aquarium.In the third test case of the example, the fourth piranha can firstly eat the piranha to the left and the aquarium becomes $$$[4, 4, 5, 4]$$$, then it can eat any other piranha in the aquarium.

package main

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

func main() {
	var t, n, x int
	//file, _ := os.OpenFile("tmp", os.O_RDONLY, os.ModePerm)
	in := bufio.NewReader(os.Stdin)

	fmt.Fscan(in, &t)
	//fmt.Sscanf(scanner.Text(), "%d\n", &t)
	for i := 0; i < t; i++ {

		fmt.Fscan(in, &n)
		pira := []int{}
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &x)
			pira = append(pira, x)
		}
		maxIL := -2
		maxL := 0
		maxIR := -2
		maxR := 0
		for i := 1; i < n; i++ {
			if maxL < pira[i] && pira[i-1] < pira[i] {
				maxIL = i
				maxL = pira[i]
			}
		}
		for i := n - 1; i > 0; i-- {
			if maxR < pira[i-1] && pira[i] < pira[i-1] {
				maxIR = i - 1
				maxR = pira[i-1]
			}
		}
		if maxR > maxL {
			fmt.Println(maxIR + 1)
		} else {
			fmt.Println(maxIL + 1)
		}
	}
}

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

package main

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

var T, n int

var _r = bufio.NewReader(os.Stdin)
var _w = bufio.NewWriter(os.Stdout)

func main() {
	for _, _ = fmt.Fscan(_r, &T); T > 0; T-- {
		_, _ = fmt.Fscan(_r, &n)
		a := make([]int, n+2)
		for i := 1; i <= n; i++ {
			_, _ = fmt.Fscan(_r, &a[i])
		}
		a[0] = 1e9 + 7
		a[n+1] = 1e9 + 7
		max_val := 0
		for i := 1; i <= n; i++ {
			if a[i] > max_val {
				max_val = a[i]
			}
		}
		for i := 1; i <= n; i++ {
			if a[i] == max_val && (a[i] > a[i-1] || a[i] > a[i+1]) {
				_, _ = fmt.Fprintln(_w, i)
				goto out
			}
		}
		_, _ = fmt.Fprintln(_w, -1)
	out:
		_ = _w.Flush()
	}

}

package main

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

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

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

func solveA() {
	var s string
	scanf("%s\n", &s)
	dig := int(s[0] - '0')
	len := len(s)
	ans := (dig - 1) * 10 + len * (len + 1) / 2
	printf("%d\n", ans)
}

func solveB() {
	n := 0
	scanf("%d\n", &n)
	v := make([]int, n)
	for i := 0; i < n; i++ {
		if i + 1 == n {
			scanf("%d\n", &v[i])
		} else {
			scanf("%d ", &v[i])
		}
	}
	fir := -1
	lst := -1
	for i := 0; i < n; i++ {
		if v[i] == 0 {
			continue
		}
		if fir == -1 {
			fir = i
		}
		lst = i
	}
	ans := 0
	for i := fir; i <= lst; i++ {
		ans += 1 - v[i]
	}
	printf("%d\n", ans)
}

func solveC() {
	n := 0
	scanf("%d\n", &n)
	v := make([]int, n)
	indMx := 0
	for i := 0; i < n; i++ {
		if i + 1 == n {
			scanf("%d\n", &v[i])
		} else {
			scanf("%d ", &v[i])
		}
		if v[i] > v[indMx] {
			indMx = i
		}
	}
	ans := -1
	for i := indMx; i < n; i++ {
		if v[i] != v[indMx] {
			continue
		}
		if i - 1 >= 0 && v[i - 1] != v[indMx] {
			ans = i + 1
			break
		}
		if i + 1 < n && v[i + 1] != v[indMx] {
			ans = i + 1
			break
		}
	}
	printf("%d\n", ans)
}

func main() {
	defer writer.Flush()
	var t int
	scanf("%d\n", &t)
	for ; t > 0; t-- {
		solveC()
	}
}

package main

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

func main() {
	defer _w.Flush()
	t := ReadInt()
	for i := 0; i < t; i++ {
		n := ReadInt()
		a := ReadInts(n)
		println(solve(n, a))
	}
}

func solve(n int, a []int) int {
	max := Max(a...)
	for i := 0; i < n; i++ {
		if a[i] != max {
			continue
		}
		if (i-1 >= 0 && a[i-1] < a[i]) || (i+1 < n && a[i] > a[i+1]) {
			return i + 1
		}
	}
	return -1
}

func Max(xs ...int) int {
	max := xs[0]
	for _, x := range xs[1:] {
		if max < x {
			max = x
		}
	}
	return max
}

var _r, _w = bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)

func print(a ...interface{})            { fmt.Fprint(_w, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(_w, f, a...) }
func println(a ...interface{})          { fmt.Fprintln(_w, a...) }
func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(_r, a...); err != nil {
		panic(err)
	}
}
func ReadString() (s string) { Scan(&s); return }
func ReadInt() (i int)       { Scan(&i); return }
func ReadInts(n int) (a []int) {
	for i := 0; i < n; i++ {
		a = append(a, ReadInt())
	}
	return
}
func ReadInt64() (i int64) { Scan(&i); return }
func ReadInt64s(n int) (a []int64) {
	for i := 0; i < n; i++ {
		a = append(a, ReadInt64())
	}
	return
}

package main

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

func solve() {
	t := readInt()

	for j := 0; j < t; j++ {
		n := readInt()
		a := readInt64Array(n)
		ans := -1
		best := a[0]
		for i := 0; i < n; i++ {
			best = max(best, a[i])
		}
		for i := 0; i < n; i++ {
			if a[i] == best {
				if solveCase(a, i) {
					ans = i + 1
					break
				}
			}
		}
		fmt.Println(ans)
	}
}

func solveCase(a []int64, x int) bool {
	l := x - 1
	r := x + 1
	cur := a[x]
	for {
		ok := false
		for l >= 0 && a[l] < cur {
			cur++
			l--
			ok = true
		}
		for r < len(a) && a[r] < cur {
			cur++
			r++
			ok = true
		}
		if !ok {
			break
		}
	}
	return l == -1 && r == len(a)
}

var scanner *bufio.Scanner

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

// IO

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

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

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

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

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

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

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

package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}

	solve := func(Case int) {
		var n int
		Fscan(in, &n)
		a := make([]int, n)
		mx := 0
		for i := range a {
			Fscan(in, &a[i])
			mx = max(mx, a[i])
		}

		for i, v := range a {
			if v == mx {
				if i>0 && v>a[i-1] || i+1<n && a[i+1] < v {
					Fprintln(out, i+1)
					return
				}
			}
		}

		Fprintln(out, -1)
		return
	}

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

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

package main

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

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	fmt.Fscan(input, &t)

	for o := 0; o < t; o++ {
		var n int
		fmt.Fscan(input, &n)

		arr := make([]int, n)
		mx := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
			if arr[i] > mx {
				mx = arr[i]
			}
		}

		ok := false
		for i := 0; i < n; i++ {
			if arr[i] == mx {

				if (i-1) >= 0 && arr[i-1] < mx {
					ok = true
					fmt.Println(i + 1)
					break
				}

				if (i+1) < n && arr[i+1] < mx {
					ok = true
					fmt.Println(i + 1)
					break
				}
			}
		}

		if ok == false {
			fmt.Println(-1)
		}
	}

}

//
// Created by aldair on 20/10/20.
// https://codeforces.com/problemset/problem/1433/C
//

package main

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

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

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

func remove(slice []int, i int) []int {
    return append(slice[:i], slice[i+1:]...)
}

func max(n1, n2 int) int {
    if n2 > n1 {
        return n2
    } else {
        return n1
    }
}

func main() {
    defer writer.Flush()

    var cases, n, piranha int
    scanf("%d\n", &cases)

    for; cases > 0; cases-- {
        scanf("%d\n", &n)
        piranhas := make([]int, 0, n)
        var maxi int
        for i := 0; i < n; i++ {
            scanf("%d", &piranha)
            piranhas = append(piranhas, piranha)
            maxi = max(maxi, piranha)
        }
        scanf("\n")

        var i int
        var dominant bool
        for; i < len(piranhas); i++ {
            if maxi == piranhas[i] {
                if i+1 < len(piranhas) && piranhas[i] > piranhas[i+1] || i-1 >= 0 && piranhas[i] > piranhas[i-1] {
                    dominant = true
                    break
                }
            }
        }
        if dominant {
            printf("%d\n", i+1)
        } else {
            printf("-1\n")
        }
    }
}

package main

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

func main() {
	var t, n, x int
	//file, _ := os.OpenFile("tmp", os.O_RDONLY, os.ModePerm)
	in := bufio.NewReader(os.Stdin)

	fmt.Fscan(in, &t)
	//fmt.Sscanf(scanner.Text(), "%d\n", &t)
	for i := 0; i < t; i++ {

		fmt.Fscan(in, &n)
		pira := []int{}
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &x)
			pira = append(pira, x)
		}
		maxI := -2
		minI := -2

		if n > 1 && pira[0] > pira[1] {
			maxI = 0
		}
		if n > 1 && pira[0] < pira[1] {
			minI = 0
		}
		for i := 1; i < n; i++ {
			if pira[i] > pira[i-1] {
				maxI = i
			}
		}
		for i := n - 1; i > 0; i-- {
			if pira[i] < pira[i-1] {
				minI = i - 1
			}
		}
		fmt.Println(max(maxI, minI) + 1)
	}
}

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

package main

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

func main() {
	var t, n int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	fmt.Sscanf(scanner.Text(), "%d", &t)
	for i := 0; i < t; i++ {
		scanner.Scan()
		fmt.Sscanf(scanner.Text(), "%d", &n)
		scanner.Scan()
		piranha := strings.Split(scanner.Text(), " ")
		maxI := -2
		minI := -2
		if n > 1 && piranha[0] > piranha[1] {
			maxI = 0
		}
		if n > 1 && piranha[0] < piranha[1] {
			minI = 0
		}
		for i := 1; i < n; i++ {
			if piranha[i] > piranha[i-1] {
				maxI = i
			}
			if piranha[i] < piranha[i-1] {
				minI = i
			}
		}
		fmt.Println(max(maxI, minI))
	}
}

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

package main

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

func main() {
	var t, n, x int
	//file, _ := os.OpenFile("tmp", os.O_RDONLY, os.ModePerm)
	in := bufio.NewReader(os.Stdin)

	fmt.Fscan(in, &t)
	//fmt.Sscanf(scanner.Text(), "%d\n", &t)
	for i := 0; i < t; i++ {

		fmt.Fscan(in, &n)
		pira := []int{}
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &x)
			pira = append(pira, x)
		}
		maxIL := -2
		maxL := 0
		maxIR := -2
		maxR := 0
		for i := 1; i < n; i++ {
			if maxL < pira[i] && pira[i-1] < pira[i] {
				maxIL = i
				maxL = pira[i]
			}
		}
		for i := n - 1; i > 0; i-- {
			if maxR < pira[i] && pira[i] < pira[i-1] {
				maxIR = i - 1
				maxR = pira[i]
			}
		}
		if maxIL == -2 {
			fmt.Println(maxIR + 1)
		} else {
			fmt.Println(maxIL + 1)
		}
	}
}

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

package main

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

func main() {
	var t, n, x int
	//file, _ := os.OpenFile("tmp", os.O_RDONLY, os.ModePerm)
	in := bufio.NewReader(os.Stdin)

	fmt.Fscan(in, &t)
	//fmt.Sscanf(scanner.Text(), "%d\n", &t)
	for i := 0; i < t; i++ {

		fmt.Fscan(in, &n)
		pira := []int{}
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &x)
			pira = append(pira, x)
		}
		maxIL := -2
		maxL := 0
		maxIR := -2
		maxR := 0
		isAllSame := true
		for i := 0; i < n; i++ {
			if maxL < pira[i] {
				maxIL = i
				maxL = pira[i]
			}
			if pira[i] != pira[0] {
				isAllSame = false
			}
		}
		for i := n - 1; i >= 0; i-- {
			if maxR < pira[i] {
				maxIR = i
				maxR = pira[i]
			}
		}
		if isAllSame {
			fmt.Println("-1")
		} else if maxIL == 0 {
			fmt.Println(maxIR + 1)
		} else {
			fmt.Println(maxIL + 1)
		}
	}
}

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

package main

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

func main() {
	var t, n, x int
	//file, _ := os.OpenFile("tmp", os.O_RDONLY, os.ModePerm)
	in := bufio.NewReader(os.Stdin)

	fmt.Fscan(in, &t)
	//fmt.Sscanf(scanner.Text(), "%d\n", &t)
	for i := 0; i < t; i++ {

		fmt.Fscan(in, &n)
		pira := []int{}
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &x)
			pira = append(pira, x)
		}
		maxIL := -2
		maxL := 0
		maxIR := -2
		maxR := 0
		isAllSame := true
		for i := 0; i < n; i++ {
			if maxL <= pira[i] {
				maxIL = i
				maxL = pira[i]
			}
			if pira[i] != pira[0] {
				isAllSame = false
			}
		}
		for i := n - 1; i >= 0; i-- {
			if maxR <= pira[i] {
				maxIR = i
				maxR = pira[i]
			}
		}
		if isAllSame {
			fmt.Println("-1")
		} else if maxIL == n-1 {
			fmt.Println(maxIR + 1)
		} else {
			fmt.Println(maxIL + 1)
		}
	}
}

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

package main

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

func main() {
	var t, n int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	fmt.Sscanf(scanner.Text(), "%d", &t)
	for i := 0; i < t; i++ {
		scanner.Scan()
		fmt.Sscanf(scanner.Text(), "%d", &n)
		scanner.Scan()
		piranha := strings.Split(scanner.Text(), " ")
		maxI := -2
		if n > 1 && piranha[0] > piranha[1] {
			maxI = 0
		}
		for i := 1; i < n; i++ {
			if piranha[i] > piranha[i-1] {
				maxI = i
			}
		}
		fmt.Println(maxI + 1)
	}
}

package main

import (
	"fmt"
)

func main() {
	var t, n int
	var pira [300011]int
	fmt.Scanf("%d", &t)
	for i := 0; i < t; i++ {
		fmt.Scanf("%d", &n)

		for i := 0; i < n; i++ {
			fmt.Scanf("%d", &pira[i])
		}
		maxI := -2
		minI := -2

		if n > 1 && pira[0] > pira[1] {
			maxI = 0
		}
		if n > 1 && pira[0] < pira[1] {
			minI = 0
		}
		for i := 1; i < n; i++ {
			if pira[i] > pira[i-1] {
				maxI = i
			}
		}
		for i := n - 1; i > 0; i-- {
			if pira[i] < pira[i-1] {
				minI = i - 1
			}
		}
		fmt.Println(max(maxI, minI) + 1)
	}
}

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

package main

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

func main() {
	var t, n int
	var pira []int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	fmt.Sscanf(scanner.Text(), "%d", &t)
	for i := 0; i < t; i++ {
		scanner.Scan()
		fmt.Sscanf(scanner.Text(), "%d", &n)
		scanner.Scan()
		piranha := strings.Split(scanner.Text(), " ")
		for i := 0; i < n; i++ {
			in, _ := strconv.Atoi(piranha[i])
			pira = append(pira, in)
		}
		maxI := -2
		minI := -2

		if n > 1 && pira[0] > pira[1] {
			maxI = 0
		}
		if n > 1 && pira[0] < pira[1] {
			minI = 0
		}
		for i := 1; i < n; i++ {
			if pira[i] > pira[i-1] {
				maxI = i
			}
		}
		for i := n - 1; i > 0; i-- {
			if pira[i] < pira[i-1] {
				minI = i - 1
			}
		}
		fmt.Println(max(maxI, minI) + 1)
	}
}

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

package main

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

func main() {
	var t, n int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	fmt.Sscanf(scanner.Text(), "%d", &t)
	for i := 0; i < t; i++ {
		scanner.Scan()
		fmt.Sscanf(scanner.Text(), "%d", &n)
		scanner.Scan()
		piranha := strings.Split(scanner.Text(), " ")
		maxI := -2
		minI := -2
		if n > 1 && piranha[0] > piranha[1] {
			maxI = 0
		}
		if n > 1 && piranha[0] < piranha[1] {
			minI = 0
		}
		for i := 1; i < n; i++ {
			if piranha[i] > piranha[i-1] {
				maxI = i
			}
			if piranha[i] < piranha[i-1] {
				minI = i
			}
		}
		fmt.Println(max(maxI, minI) + 1)
	}
}

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

package main

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

func main() {
	var t, n, x int
	//file, _ := os.OpenFile("tmp", os.O_RDONLY, os.ModePerm)
	in := bufio.NewReader(os.Stdin)

	fmt.Fscan(in, &t)
	//fmt.Sscanf(scanner.Text(), "%d\n", &t)
	for i := 0; i < t; i++ {

		fmt.Fscan(in, &n)
		pira := []int{}
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &x)
			pira = append(pira, x)
		}
		maxIL := -2
		maxL := 0
		maxIR := -2
		maxR := 0
		for i := 0; i < n; i++ {
			if maxL < pira[i] {
				maxIL = i
				maxL = pira[i]
			}
		}
		for i := n - 1; i >= 0; i-- {
			if maxR < pira[i] {
				maxIR = i
				maxR = pira[i]
			}
		}
		if maxIL == 0 {
			if maxIR == n-1 {
				fmt.Println(-1)
			} else {
				fmt.Println(maxIR + 1)
			}
		} else {
			fmt.Println(maxIL + 1)
		}
	}
}

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

package main

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

func main() {
	var t, n int
	var pira [300011]int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	fmt.Scanf(scanner.Text(), "%d", &t)
	for i := 0; i < t; i++ {
		scanner.Scan()
		fmt.Sscanf(scanner.Text(), "%d", &n)
		scanner.Scan()
		piranha := strings.Split(scanner.Text(), " ")
		for i := 0; i < n; i++ {
			pira[i], _ = strconv.Atoi(piranha[i])
		}
		maxI := -2
		minI := -2

		if n > 1 && pira[0] > pira[1] {
			maxI = 0
		}
		if n > 1 && pira[0] < pira[1] {
			minI = 0
		}
		for i := 1; i < n; i++ {
			if pira[i] > pira[i-1] {
				maxI = i
			}
		}
		for i := n - 1; i > 0; i-- {
			if pira[i] < pira[i-1] {
				minI = i - 1
			}
		}
		fmt.Println(max(maxI, minI) + 1)
	}
}

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

package main

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

func solve() {
	t := readInt()

	for j := 0; j < t; j++ {
		n := readInt()
		a := readInt64Array(n)
		ans := -1
		best := a[0]
		for i := 0; i < n; i++ {
			best = max(best, a[i])
		}
		for i := 0; i < n-1; i++ {
			if a[i] == best {
				if solveCase(a, i) {
					ans = i + 1
					break
				}
			}
		}
		fmt.Println(ans)
	}
}

func solveCase(a []int64, x int) bool {
	l := x - 1
	r := x + 1
	cur := a[x]
	for {
		ok := false
		for l >= 0 && a[l] < cur {
			cur++
			l--
			ok = true
		}
		for r < len(a) && a[r] < cur {
			cur++
			r++
			ok = true
		}
		if !ok {
			break
		}
	}
	return l == -1 && r == len(a)
}

var scanner *bufio.Scanner

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

// IO

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

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

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

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

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

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

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

package main

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

func solve() {
	t := readInt()

	for j := 0; j < t; j++ {
		n := readInt()
		a := readIntArray(n)
		ans := -1
		for i := 0; i < n - 1; i++ {
			if a[i] > a[i + 1] {
				if solveCase(a, i) {
					ans = i + 1
					break
				}
			}
		}
		if ans == -1 {
			if a[n - 1] > a[n - 2] && solveCase(a, n - 1) {
				ans = n
			}
		}
		fmt.Println(ans)
	}
}

func solveCase(a []int, x int) bool {
	l := x - 1
	r := x + 1
	cur := a[x]
	for {
		ok := false
		for l >= 0 && a[l] < cur {
			cur++
			l--
			ok = true
		}
		for r < len(a) && a[r] < cur {
			cur++
			r++
			ok = true
		}
		if !ok {
			break
		}
	}
	return l == -1 && r == len(a)
}

var scanner *bufio.Scanner

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

// IO

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

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

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

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

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

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

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

package main

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

func solve() {
	t := readInt()

	for j := 0; j < t; j++ {
		n := readInt()
		a := readInt64Array(n)
		ans := -1
		for i := 0; i < n - 1; i++ {
			if a[i] > a[i + 1] {
				if solveCase(a, i) {
					ans = i + 1
					break
				}
			}
		}
		if ans == -1 {
			if a[n - 1] > a[n - 2] && solveCase(a, n - 1) {
				ans = n
			}
		}
		fmt.Println(ans)
	}
}

func solveCase(a []int64, x int) bool {
	l := x - 1
	r := x + 1
	cur := a[x]
	for {
		ok := false
		for l >= 0 && a[l] < cur {
			cur++
			l--
			ok = true
		}
		for r < len(a) && a[r] < cur {
			cur++
			r++
			ok = true
		}
		if !ok {
			break
		}
	}
	return l == -1 && r == len(a)
}

var scanner *bufio.Scanner

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

// IO

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

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

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

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

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

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

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

package main

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

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	fmt.Fscan(input, &t)

	for o := 0; o < t; o++ {
		var n int
		fmt.Fscan(input, &n)

		arr := make([]int, n)
		mx := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
			if arr[i] > mx {
				mx = arr[i]
			}
		}

		ok := false
		for i := 0; i < n; i++ {
			if arr[i] == mx {

				if (i-1) > 0 && arr[i-1] < mx {
					ok = true
					fmt.Println(i + 1)
					break
				}

				if (i+1) < n && arr[i+1] < mx {
					ok = true
					fmt.Println(i + 1)
					break
				}
			}
		}

		if ok == false {
			fmt.Println(-1)
		}
	}

}

//
// Created by aldair on 20/10/20.
// https://codeforces.com/problemset/problem/1433/C
//

package main

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

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

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

func remove(slice []int, i int) []int {
    return append(slice[:i], slice[i+1:]...)
}

func main() {
    defer writer.Flush()

    var cases, n, size int
    scanf("%d\n", &cases)

    for; cases > 0; cases-- {
        scanf("%d\n", &n)
        piranhas := make([]int, 0, n)
        for i := 0; i < n; i++ {
            scanf("%d", &size)
            piranhas = append(piranhas, size)
        }
        scanf("\n")

        var i int
        var dominant bool
        for i = 0; i < len(piranhas); i++ {
            slc := piranhas[:]
            index := i
            dominant = false
            for; len(slc) != 1; {
                if index+1 < len(slc) && slc[index] > slc[index+1] {
                    slc = remove(slc, index+1)
                    slc[index]++
                } else if index-1 >= 0 && slc[index] > slc[index-1] {
                    slc = remove(slc, index-1)
                    index--
                    slc[index]++
                } else {
                    break
                }
            }
            if len(slc) == 1 {
                dominant = true
                break
            }
        }
        if dominant {
            printf("%d\n", i+1)
        } else {
            printf("-1\n")
        }
    }
}


A tree is an undirected connected graph in which there are no cycles. This problem is about non-rooted trees. A leaf of a tree is a vertex that is connected to at most one vertex.The gardener Vitaly grew a tree from $$$n$$$ vertices. He decided to trim the tree. To do this, he performs a number of operations. In one operation, he removes all leaves of the tree.  Example of a tree. For example, consider the tree shown in the figure above. The figure below shows the result of applying exactly one operation to the tree.  The result of applying the operation "remove all leaves" to the tree. Note the special cases of the operation:  applying an operation to an empty tree (of $$$0$$$ vertices) does not change it;  applying an operation to a tree of one vertex removes this vertex (this vertex is treated as a leaf);  applying an operation to a tree of two vertices removes both vertices (both vertices are treated as leaves). Vitaly applied $$$k$$$ operations sequentially to the tree. How many vertices remain?

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case is preceded by an empty line. Each test case consists of several lines. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 4 \cdot 10^5$$$, $$$1 \le k \le 2 \cdot 10^5$$$) — the number of vertices in the tree and the number of operations, respectively. Then $$$n - 1$$$ lines follow, each of them contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n$$$, $$$u \neq v$$$) which describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges. It is guaranteed that the sum of $$$n$$$ from all test cases does not exceed $$$4 \cdot 10^5$$$.

For each test case output on a separate line a single integer — the number of vertices that remain in the tree after applying $$$k$$$ operations.

NoteThe first test case is considered in the statement.The second test case contains a tree of two vertices. $$$200000$$$ operations are applied to it. The first one removes all two vertices, the other operations do not change the tree.In the third test case, a tree of three vertices is given. As a result of the first operation, only $$$1$$$ vertex remains in it (with the index $$$2$$$), the second operation makes the tree empty.

package main

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

type Edge struct {
	u, v int
}

type Graph struct {
	adj [][]int
	e   []*Edge
}

func NewGraph(n int) *Graph {
	adj := make([][]int, n+1)
	for i := range adj {
		adj[i] = make([]int, 0)
	}
	return &Graph{
		adj: adj,
		e:   make([]*Edge, 0),
	}
}

func (g *Graph) AddEdge(u, v int) {
	g.adj[u] = append(g.adj[u], len(g.e))
	g.e = append(g.e, &Edge{u, v})
}

type DPNode struct {
	id, dep int
}

type DepDP struct {
	dep [2]DPNode
}

func (d *DepDP) Update(v DPNode) {
	if v.dep >= d.dep[0].dep {
		d.dep[1] = d.dep[0]
		d.dep[0] = v
	} else if v.dep > d.dep[1].dep {
		d.dep[1] = v
	}
}

var (
	rr = bufio.NewReader(os.Stdin)
	ww = bufio.NewWriter(os.Stdout)

	T int

	g    *Graph
	n, k int

	dp []DepDP
)

func dfs(u, par int) {
	for _, i := range g.adj[u] {
		v := g.e[i].v
		if v == par {
			continue
		}
		dfs(v, u)
		dp[u].Update(DPNode{
			id:  v,
			dep: dp[v].dep[0].dep + 1,
		})
	}
}

func dfsPar(u, par int) {
	if par != 0 {
		if dp[par].dep[0].id != u {
			dp[u].Update(DPNode{
				id:  par,
				dep: dp[par].dep[0].dep + 1,
			})
		} else {
			dp[u].Update(DPNode{
				id:  par,
				dep: dp[par].dep[1].dep + 1,
			})
		}
	}

	for _, i := range g.adj[u] {
		v := g.e[i].v
		if v == par {
			continue
		}
		dfsPar(v, u)
	}
}

func main() {
	fmt.Fscan(rr, &T)
	for ; T > 0; T-- {
		fmt.Fscan(rr, &n, &k)
		g = NewGraph(n)

		for i := 1; i < n; i++ {
			var u, v int
			fmt.Fscan(rr, &u, &v)
			g.AddEdge(u, v)
			g.AddEdge(v, u)
		}

		dp = make([]DepDP, n+1)
		dfs(1, 0)
		dfsPar(1, 0)

		ans := 0
		for i := 1; i <= n; i++ {
			if dp[i].dep[1].dep >= k {
				ans++
			}
		}

		fmt.Fprintln(ww, ans)
	}
	_ = ww.Flush()
}

package main

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

var n, k int

var gr [][]int

var mp map[int]map[int]bool

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &n, &k)

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

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

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

		ans := solve()

		fmt.Println(ans)
	}
}

type item struct {
	v int
	round int
}

var seen []bool

func solve() int {

	if n == 1 {
		if k > 0 {
			return 0
		}
		return 1
	}

	seen = make([]bool, n + 1)

	var queue []*item
	for i := 1; i <= n; i++ {
		if len(gr[i]) == 1 {
			seen[i] = true
			queue = append(queue, &item{i, 1})
		}
	}

	mp = make(map[int]map[int]bool)

	for i := 1; i <= n; i++ {
		chidren := gr[i]

		mp[i] = make(map[int]bool)

		for j := 0; j < len(chidren); j++ {
			mp[i][chidren[j]] = true
		}
	}

	round := 1

	removed := 0

	for ;len(queue) > 0 && round <= k; {
		it := queue[0]
		queue = queue[1:]

		if it.round > round {
			round++
			if round > k {
				break
			}
		}

		m := mp[it.v]

		var key int
		for k, _ := range m {
			key = k
			break
		}
		delete(m, key)

		m = mp[key]

		delete(m, it.v)
		removed++

		if len(m) == 1 {
			if !seen[key] {
				seen[key] = true
				queue = append(queue, &item{key, it.round + 1})
			}
		}
	}

	return  n - removed
}

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

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

func GCD(a, b int) int {
	for b != 0 {
		t := b
		b = a % b
		a = t
	}
	return a
}

package main

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

func die(v ...interface{}) {
	_, _, line, _ := runtime.Caller(1)
	fmt.Printf("die line %d: %v\n", line, v)
	os.Exit(-1)
}

func check(b bool) {
	if !b {
		_, _, line, _ := runtime.Caller(1)
		fmt.Printf("check failed line %d\n", line)
		os.Exit(-1)
	}
}

type scanner struct {
	sc  *bufio.Scanner
	buf []byte
	max int
}

func newScanner() *scanner {
	rv := &scanner{max: bufio.MaxScanTokenSize}
	rv.sc = bufio.NewScanner(os.Stdin)
	rv.sc.Split(bufio.ScanWords)
	rv.buf = make([]byte, rv.max)
	rv.sc.Buffer(rv.buf, rv.max)
	return rv
}

func (s *scanner) nextInt() int {
	if !s.sc.Scan() {
		die(io.ErrUnexpectedEOF)
	}
	rv, err := strconv.Atoi(s.sc.Text())
	if err != nil {
		die(err)
	}
	return rv
}

func (s *scanner) nextInt64() int64 {
	if !s.sc.Scan() {
		die(io.ErrUnexpectedEOF)
	}
	rv, err := strconv.ParseInt(s.sc.Text(), 10, 64)
	if err != nil {
		die(err)
	}
	return rv
}

func (s *scanner) nextStr() string {
	if !s.sc.Scan() {
		die(io.ErrUnexpectedEOF)
	}
	return s.sc.Text()
}

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

const (
	INIT = -1
	INF  = 1e9
)

var (
	n       int
	k       int
	adjList [][]int
	ld      []int
)

func diam() []int {
	d1 := make([]int, n+1)
	for i := 0; i < n+1; i++ {
		d1[i] = INF
	}
	q := []int{1}
	d1[1] = 0
	for i := 0; i < len(q); i++ {
		u := q[i]
		for _, v := range adjList[u] {
			if d1[v] == INF {
				d1[v] = d1[u] + 1
				q = append(q, v)
			}
		}
	}
	s := 1 // furthest distance id from 1
	mx := 0
	for i := 2; i < len(d1); i++ {
		if mx < d1[i] {
			s = i
			mx = d1[i]
		}
	}

	//	fmt.Printf("s %d mx %d\n", s, mx)

	d2 := make([]int, n+1)
	p := make([]int, n+1)
	for i := 0; i < n+1; i++ {
		d2[i] = INF
	}
	d2[s] = 0
	q = []int{s}
	p[s] = s
	for i := 0; i < len(q); i++ {
		u := q[i]
		for _, v := range adjList[u] {
			if d2[v] == INF {
				d2[v] = d2[u] + 1
				p[v] = u
				q = append(q, v)
			}
		}
	}

	t := INIT
	mx = INIT
	for i := 1; i < len(d2); i++ {
		if mx < d2[i] {
			t = i
			mx = d2[i]
		}
	}
	check(t != INIT)

	//	fmt.Printf("s %d t %d dist %d\n", s, t, d2[t])

	rv := []int{t}
	l := d2[t]
	for i := 0; i < l; i++ {
		rv = append(rv, p[t])
		t = p[t]
	}

	return rv

}

func getLD(u, p int) int {
	rv := 0
	for _, v := range adjList[u] {
		if v == p {
			continue
		}
		d := getLD(v, u)
		rv = max(rv, d+1)
	}

	ld[u] = rv
	return rv
}

func main() {
	sc := newScanner()
	tc := sc.nextInt()

	for ; tc > 0; tc-- {
		n = sc.nextInt()
		k = sc.nextInt()

		adjList = make([][]int, n+1)
		for i := range adjList {
			adjList[i] = make([]int, 0)
		}

		for i := 0; i+1 < n; i++ {
			u := sc.nextInt()
			v := sc.nextInt()

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

		path := diam()
		//		fmt.Println(path)
		ld = make([]int, n+1)
		if (len(path))%2 == 0 {
			r1 := path[len(path)/2]
			r2 := path[len(path)/2-1]
			getLD(r1, r2)
			getLD(r2, r1)
		} else {
			r1 := path[len(path)/2]
			getLD(r1, INIT)
		}

		// for u := 1; u <= n; u++ {
		// 	fmt.Printf("u %d ld[u] %d\n", u, ld[u])
		// }
		// fmt.Println()

		ans := 0
		for u := 1; u <= n; u++ {
			if ld[u] >= k {
				ans++
			}
		}
		fmt.Println(ans)

	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)
var u []int
var v []int
var first[]int
var nex[]int
var cnt int
var d []int
func add(x, y int){
	cnt++
	u[cnt]=x
	v[cnt]=y
	nex[cnt]=first[x]
	first[x] = cnt
}
type Node struct{
	x int
	l int
}
func min(x, y int)int{
	if x<y{
		return x
	}
	return y
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)

	for c:=1;c<=t;c++{
		var n int
		var k int
		cnt=0
		fmt.Fscan(in,&n,&k)
		u = make([]int,3*n)
		v = make([]int,3*n)
		first = make([]int,4*n)
		nex = make([]int,4*n)
		d = make([]int,4*n)
		vis := make([]int,n+5)
		mp := map[int]int{}
		for i:=1;i<=n-1;i++{
			var x , y int
			fmt.Fscan(in,&x,&y)
			add(x,y)
			add(y,x)
			d[x]++
			d[y]++
		}
		qr := []Node{}
		for i:=1;i<=n;i++{
			if d[i]==1||d[i]==0{
				qr = append(qr,Node{i,1})
				vis[i]=1
			}
		}
		for len(qr)!=0{
			now := qr[0]
			mp[now.l]++
			qr = qr[1:]
			for k:=first[now.x];k!=0;k=nex[k]{
				if vis[v[k]]==0 {
					d[v[k]]--
					if d[v[k]] == 1 || d[v[k]] == 0{
						vis[v[k]] = 1
						qr = append(qr, Node{v[k], now.l + 1})
					}
				}
			}
		}
		ans:=n
		for i:=1;i<=min(k,n);i++{
			ans -=mp[i]
		}
		fmt.Fprintln(out,ans)
	}
}

package main

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

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

  var t int
  for fmt.Fscan(reader, &t); t > 0; t-- {
    var n, k int
    fmt.Fscan(reader, &n, &k)
    adj := make([][]int, n+1)
    degs := make([]int, n+1)
    done := make([]bool, n+1)
    time := make([]int, n+1)
    for i := 0; i < n-1; i++ {
      var u, v int
      fmt.Fscan(reader, &u, &v)
      adj[u] = append(adj[u], v)
      adj[v] = append(adj[v], u)
      degs[u]++
      degs[v]++
    }

    var queue []int
    for i := 1; i <= n; i++ {
      if degs[i] == 1 {
        queue = append(queue, i)
        time[i] = 1;
      }
    }
    j := 0
    for ; j < len(queue); j++ {
      v := queue[j]
      done[v] = true
      for _, u := range adj[v] {
        if !done[u] {
          degs[u]--
          if degs[u] == 1 {
            time[u] = time[v] + 1
            queue = append(queue, u)
          }
        }
      }
    }

    ans := 0
    for i := 1; i <= n; i++ {
      if time[i] > k {
        ans++
      }
    }
    fmt.Fprint(writer, ans, "\n")
  }
}

package main

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

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

	var T, n, k, v, w int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		g := make([][]int, n)
		d := 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)
			d[v]++
			d[w]++
		}
		if n < 3 {
			Fprintln(out, 0)
			continue
		}
		q := make([]int, 0, n)
		for i, d := range d {
			if d == 1 {
				q = append(q, i)
			}
		}
		for ; k > 0 && len(q) > 0; k-- {
			sz := len(q)
			for _, v := range q {
				for _, w := range g[v] {
					if d[w]--; d[w] == 1 {
						q = append(q, w)
					}
				}
			}
			q = q[sz:]
		}
		Fprintln(out, cap(q))
	}
}

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

package main

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

var g [4000005][]int

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var t, n, k, a, b int

	_, _ = fmt.Fscan(r, &t)

	for ; t > 0; t-- {
		_, _ = fmt.Fscan(r, &n, &k)
		deg := make([]int, n + 1)
		for i := 1; i < n; i++ {
			_, _ = fmt.Fscan(r, &a, &b)
			g[a] = append(g[a], b)
			g[b] = append(g[b], a)
			deg[a]++
			deg[b]++
		}

		curLeaves := make([]int, 0)
		newLeaves := make([]int, 0)
		alive := n
		for i := 1; i <= n; i++ {
			if deg[i] < 2 {
				curLeaves = append(curLeaves, i)
			}
		}

		for alive > 0 && k > 0 {
			for _, v := range curLeaves {
				for _, u := range g[v] {
					deg[u]--
					if deg[u] == 1 {
						newLeaves = append(newLeaves, u)
					}
				}
			}

			alive -= len(curLeaves)
			curLeaves = newLeaves
			newLeaves = make([]int, 0)
			k--
		}

		fmt.Println(alive)

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

}

package main

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

type Edge struct {
	u, v int
}

type Graph struct {
	adj [][]int
	e   []*Edge
}

func NewGraph(n int) *Graph {
	adj := make([][]int, n+1)
	for i := range adj {
		adj[i] = make([]int, 0)
	}
	return &Graph{
		adj: adj,
		e:   make([]*Edge, 0),
	}
}

func (g *Graph) AddEdge(u, v int) {
	g.adj[u] = append(g.adj[u], len(g.e))
	g.e = append(g.e, &Edge{u, v})
}

type DPNode struct {
	id, dep int
}

type DepDP struct {
	dep [2]DPNode
}

func (d *DepDP) Update(v DPNode) {
	if v.dep >= d.dep[0].dep {
		d.dep[1] = d.dep[0]
		d.dep[0] = v
	} else if v.dep > d.dep[1].dep {
		d.dep[1] = v
	}
}

var (
	rr = bufio.NewReader(os.Stdin)
	ww = bufio.NewWriter(os.Stdout)

	T int

	g    *Graph
	n, k int

	dp []DepDP
)

func dfs(u, par int) {
	for _, i := range g.adj[u] {
		v := g.e[i].v
		if v == par {
			continue
		}
		dfs(v, u)
		dp[u].Update(DPNode{
			id:  v,
			dep: dp[v].dep[0].dep + 1,
		})
	}
}

func dfsPar(u, par int) {
	if par != 0 {
		if dp[par].dep[0].id != u {
			dp[u].Update(DPNode{
				id:  par,
				dep: dp[par].dep[0].dep + 1,
			})
		} else {
			dp[u].Update(DPNode{
				id:  par,
				dep: dp[par].dep[1].dep + 1,
			})
		}
	}

	for _, i := range g.adj[u] {
		v := g.e[i].v
		if v == par {
			continue
		}
		dfsPar(v, u)
	}
}

func main() {
	fmt.Fscan(rr, &T)
	for ; T > 0; T-- {
		fmt.Fscan(rr, &n, &k)
		g = NewGraph(n)

		for i := 1; i < n; i++ {
			var u, v int
			fmt.Fscan(rr, &u, &v)
			g.AddEdge(u, v)
			g.AddEdge(v, u)
		}

		dp = make([]DepDP, n+1)
		dfs(1, 0)
		dfsPar(1, 0)

		fmt.Println(dp)
		ans := 0
		for i := 1; i <= n; i++ {
			if dp[i].dep[1].dep >= k {
				ans++
			}
		}

		fmt.Fprintln(ww, ans)
	}
	_ = ww.Flush()
}

package main

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

const Inf = 1000

type Edge struct {
	u, v int
}

type Graph struct {
	adj [][]int
	e   []*Edge
}

func NewGraph(n int) *Graph {
	adj := make([][]int, n+1)
	for i := range adj {
		adj[i] = make([]int, 0)
	}
	return &Graph{
		adj: adj,
		e:   make([]*Edge, 0),
	}
}

func (g *Graph) AddEdge(u, v int) {
	g.adj[u] = append(g.adj[u], len(g.e))
	g.e = append(g.e, &Edge{u, v})
}

var (
	rr = bufio.NewReader(os.Stdin)
	ww = bufio.NewWriter(os.Stdout)

	T int

	g    *Graph
	n, k int

	dp [][2]int
)

func addDp(u, dep int) {
	if dep >= dp[u][0] {
		dp[u][1] = dp[u][0]
		dp[u][0] = dep
	} else if dep > dp[u][1] {
		dp[u][1] = dep
	}
}

func dfs(u, par, dep int) {
	for _, i := range g.adj[u] {
		v := g.e[i].v
		if v == par {
			continue
		}
		dfs(v, u, dep + 1)
		addDp(u, dp[v][0] + 1)
		addDp(v, dep + 1)
	}
}

func main() {
	fmt.Fscan(rr, &T)
	for ; T > 0; T-- {
		fmt.Fscan(rr, &n, &k)
		g = NewGraph(n)

		for i := 1; i < n; i++ {
			var u, v int
			fmt.Fscan(rr, &u, &v)
			g.AddEdge(u, v)
			g.AddEdge(v, u)
		}

		dp = make([][2]int, n + 1)
		for i := 1; i <= n; i++ {
			if len(g.adj[i]) <= 1 {
				dfs(i, 0, 0)
				break
			}
		}

		ans := 0
		for i := 1; i <= n; i++ {
			if dp[i][1] >= k {
				ans++
			}
		}

		fmt.Fprintln(ww, ans)
	}
	_ = ww.Flush()
}

package main

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

const Inf = 1000

type Edge struct {
	u, v int
}

type Graph struct {
	adj [][]int
	e   []*Edge
}

func NewGraph(n int) *Graph {
	adj := make([][]int, n+1)
	for i := range adj {
		adj[i] = make([]int, 0)
	}
	return &Graph{
		adj: adj,
		e:   make([]*Edge, 0),
	}
}

func (g *Graph) AddEdge(u, v int) {
	g.adj[u] = append(g.adj[u], len(g.e))
	g.e = append(g.e, &Edge{u, v})
}

var (
	rr = bufio.NewReader(os.Stdin)
	ww = bufio.NewWriter(os.Stdout)

	T int

	g    *Graph
	n, k int

	dp [][2]int
)

func addDp(u, dep int) {
	if dep >= dp[u][0] {
		dp[u][1] = dp[u][0]
		dp[u][0] = dep
	} else if dep > dp[u][1] {
		dp[u][1] = dep
	}
}

func dfs(u, par, dep int) {
	for _, i := range g.adj[u] {
		v := g.e[i].v
		if v == par {
			continue
		}
		dfs(v, u, dep + 1)
		addDp(u, dp[v][0] + 1)
		addDp(v, dep + 1)
	}
}

func main() {
	fmt.Fscan(rr, &T)
	for ; T > 0; T-- {
		fmt.Fscan(rr, &n, &k)
		g = NewGraph(n)

		for i := 1; i < n; i++ {
			var u, v int
			fmt.Fscan(rr, &u, &v)
			g.AddEdge(u, v)
			g.AddEdge(v, u)
		}

		dp = make([][2]int, n + 1)
		for i := 1; i <= n; i++ {
			if len(g.adj[i]) <= 1 {
				dfs(i, 0, 1)
				addDp(i, 1)
				break
			}
		}

		ans := 0
		for i := 1; i <= n; i++ {
			if dp[i][1] >= k {
				ans++
			}
		}

		fmt.Fprintln(ww, ans)
	}
	_ = ww.Flush()
}

package main

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

const Inf = 1000

type Edge struct {
	u, v int
}

type Graph struct {
	adj [][]int
	e   []*Edge
}

func NewGraph(n int) *Graph {
	adj := make([][]int, n + 1)
	for i := range adj {
		adj[i] = make([]int, 0)
	}
	return &Graph{
		adj: adj,
		e:   make([]*Edge, 0),
	}
}

func (g *Graph) AddEdge(u, v int) {
	g.adj[u] = append(g.adj[u], len(g.e))
	g.e = append(g.e, &Edge{u, v})
}

var (
	rr = bufio.NewReader(os.Stdin)
	ww = bufio.NewWriter(os.Stdout)

	T int

	g    *Graph
	n, k int

	step []int
)

func bfs(start int) {
	step = make([]int, start + 1)
	queue := list.New()
	queue.PushBack(start)
	for queue.Len() > 0 {
		pop := queue.Front()
		u := pop.Value.(int)
		queue.Remove(pop)

		for _, i := range g.adj[u] {
			v := g.e[i].v
			if step[v] == 0 {
				step[v] = step[u] + 1
				queue.PushBack(v)
			}
		}
	}
}

func main() {
	fmt.Fscan(rr, &T)
	for ; T > 0; T-- {
		fmt.Fscan(rr, &n, &k)
		g = NewGraph(n + 1)

		for i := 1; i < n; i++ {
			var u, v int
			fmt.Fscan(rr, &u, &v)
			g.AddEdge(u, v)
			g.AddEdge(v, u)
		}

		for i := 1; i <= n; i++ {
			if len(g.adj[i]) <= 1 {
				g.AddEdge(n + 1, i)
			}
		}

		bfs(n + 1)

		ans := 0
		for i := 1; i <= n; i++ {
			if step[i] > k {
				ans++
			}
		}

		fmt.Fprintln(ww, ans)
	}
	_ = ww.Flush()
}

package main

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

const Inf = 1000

type Edge struct {
	u, v int
}

type Graph struct {
	adj [][]int
	e   []*Edge
}

func NewGraph(n int) *Graph {
	adj := make([][]int, n + 1)
	for i := range adj {
		adj[i] = make([]int, 0)
	}
	return &Graph{
		adj: adj,
		e:   make([]*Edge, 0),
	}
}

func (g *Graph) AddEdge(u, v int) {
	g.adj[u] = append(g.adj[u], len(g.e))
	g.e = append(g.e, &Edge{u, v})
}

var (
	rr = bufio.NewReader(os.Stdin)
	ww = bufio.NewWriter(os.Stdout)

	T int

	g    *Graph
	n, k int

	step []int
)

func bfs(start int) {
	step = make([]int, start + 1)
	queue := list.New()
	queue.PushBack(start)
	for queue.Len() > 0 {
		pop := queue.Front()
		u := pop.Value.(int)
		queue.Remove(pop)

		for _, i := range g.adj[u] {
			v := g.e[i].v
			if step[v] == 0 {
				step[v] = step[u] + 1
				queue.PushBack(v)
			}
		}
	}
}

func main() {
	fmt.Fscan(rr, &T)
	for ; T > 0; T-- {
		fmt.Fscan(rr, &n, &k)
		g = NewGraph(n + 1)

		for i := 1; i < n; i++ {
			var u, v int
			fmt.Fscan(rr, &u, &v)
			g.AddEdge(u, v)
			g.AddEdge(v, u)
		}

		for i := 1; i <= n; i++ {
			if len(g.adj[i]) <= 1 {
				g.AddEdge(n + 1, i)
			}
		}

		bfs(n + 1)

		ans := 0
		for _, i := range step {
			if i > k {
				ans++
			}
		}

		fmt.Fprintln(ww, ans)
	}
	_ = ww.Flush()
}

package main

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

const Inf = 1000

type Edge struct {
	u, v int
}

type Graph struct {
	adj [][]int
	e   []*Edge
}

func NewGraph(n int) *Graph {
	adj := make([][]int, n + 1)
	for i := range adj {
		adj[i] = make([]int, 0)
	}
	return &Graph{
		adj: adj,
		e:   make([]*Edge, 0),
	}
}

func (g *Graph) AddEdge(u, v int) {
	g.adj[u] = append(g.adj[u], len(g.e))
	g.e = append(g.e, &Edge{u, v})
}

var (
	rr = bufio.NewReader(os.Stdin)
	ww = bufio.NewWriter(os.Stdout)

	T int

	g    *Graph
	n, k int

	step []int
)

func bfs(start int) {
	step = make([]int, start + 1)
	queue := list.New()
	queue.PushBack(start)
	for queue.Len() > 0 {
		pop := queue.Front()
		u := pop.Value.(int)
		queue.Remove(pop)

		for _, i := range g.adj[u] {
			v := g.e[i].v
			if step[v] == 0 {
				step[v] = step[u] + 1
				queue.PushBack(v)
			}
		}
	}
}

func main() {
	fmt.Fscan(rr, &T)
	for ; T > 0; T-- {
		fmt.Fscan(rr, &n, &k)
		g = NewGraph(n + 1)

		for i := 1; i < n; i++ {
			var u, v int
			fmt.Fscan(rr, &u, &v)
			g.AddEdge(u, v)
			g.AddEdge(v, u)
		}

		for i := 1; i <= n; i++ {
			if len(g.adj[i]) == 1 {
				g.AddEdge(n + 1, i)
			}
		}

		bfs(n + 1)

		ans := 0
		for _, i := range step {
			if i > k {
				ans++
			}
		}

		fmt.Fprintln(ww, ans)
	}
	_ = ww.Flush()
}

package main

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

var n, k int

var gr [][]int

var mp map[int]map[int]bool

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &n, &k)

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

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

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

		ans := solve()

		fmt.Println(ans)
	}
}

type item struct {
	v int
	round int
}

var seen []bool

func solve() int {
	seen = make([]bool, n + 1)

	var queue []*item
	for i := 1; i <= n; i++ {
		if len(gr[i]) == 1 {
			seen[i] = true
			queue = append(queue, &item{i, 1})
		}
	}

	mp = make(map[int]map[int]bool)

	for i := 1; i <= n; i++ {
		chidren := gr[i]

		mp[i] = make(map[int]bool)

		for j := 0; j < len(chidren); j++ {
			mp[i][chidren[j]] = true
		}
	}

	round := 1

	removed := 0

	for ;len(queue) > 0 && round <= k; {
		it := queue[0]
		queue = queue[1:]

		if it.round > round {
			round++
			if round > k {
				break
			}
		}

		m := mp[it.v]

		var key int
		for k, _ := range m {
			key = k
			break
		}
		delete(m, key)

		m = mp[key]

		delete(m, it.v)
		removed++

		if len(m) == 1 {
			if !seen[key] {
				seen[key] = true
				queue = append(queue, &item{key, it.round + 1})
			}
		}
	}

	return  n - removed
}

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

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

func GCD(a, b int) int {
	for b != 0 {
		t := b
		b = a % b
		a = t
	}
	return a
}

package main

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

var n, k int

var gr [][]int

var mp map[int]map[int]bool

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &n, &k)

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

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

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

		ans := solve()

		fmt.Println(ans)
	}
}

type item struct {
	v int
	round int
}

var seen []bool

func solve() int {
	seen = make([]bool, n + 1)

	var queue []*item
	for i := 1; i <= n; i++ {
		if len(gr[i]) == 1 {
			seen[i] = true
			queue = append(queue, &item{i, 1})
		}
	}

	mp = make(map[int]map[int]bool)

	for i := 1; i <= n; i++ {
		chidren := gr[i]

		mp[i] = make(map[int]bool)

		for j := 0; j < len(chidren); j++ {
			mp[i][chidren[j]] = true
		}
	}

	round := 1

	removed := 0

	for ;len(queue) > 0 && round <= k; {
		it := queue[0]
		queue = queue[1:]

		if it.round > round {
			round++
			if round > k {
				break
			}
		}

		m := mp[it.v]

		var key int
		for k, _ := range m {
			key = k
			break
		}
		delete(m, key)

		m = mp[key]

		delete(m, it.v)
		removed++

		if len(m) == 1 || len(m) == 0 {
			if !seen[key] {
				seen[key] = true
				queue = append(queue, &item{key, it.round + 1})
			}
		}
	}

	return  n - removed
}

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

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

func GCD(a, b int) int {
	for b != 0 {
		t := b
		b = a % b
		a = t
	}
	return a
}

package main

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

var n, k int

var gr [][]int

var mp map[int]map[int]bool

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &n, &k)

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

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

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

		ans := solve()

		fmt.Println(ans)
	}
}

type item struct {
	v int
	round int
}

func solve() int {
	var queue []*item
	for i := 1; i <= n; i++ {
		if len(gr[i]) == 1 {
			queue = append(queue, &item{i, 1})
		}
	}

	mp = make(map[int]map[int]bool)

	for i := 1; i <= n; i++ {
		chidren := gr[i]

		mp[i] = make(map[int]bool)

		for j := 0; j < len(chidren); j++ {
			mp[i][chidren[j]] = true
		}
	}

	round := 1

	removed := 0

	for ;len(queue) > 0 && round <= k; {
		it := queue[0]
		queue = queue[1:]

		if it.round > round {
			round++
			if round > k {
				break
			}
		}

		m := mp[it.v]

		var key int
		for k, _ := range m {
			key = k
			break
		}
		delete(m, key)

		m = mp[key]

		delete(m, it.v)
		removed++

		if len(m) == 1 {
			queue = append(queue, &item{key, it.round + 1})
		}
	}

	return  n - removed
}

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

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

func GCD(a, b int) int {
	for b != 0 {
		t := b
		b = a % b
		a = t
	}
	return a
}

package main

import (
	"bufio"
	"fmt"
	"os"
)
var u []int
var v []int
var first[]int
var nex[]int
var cnt int
var d []int
func add(x, y int){
	cnt++
	u[cnt]=x
	v[cnt]=y
	nex[cnt]=first[x]
	first[x] = cnt
}
type Node struct{
	x int
	l int
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)

	for c:=1;c<=t;c++{
		var n int
		var k int
		cnt=0
		fmt.Fscan(in,&n,&k)
		u = make([]int,3*n)
		v = make([]int,3*n)
		first = make([]int,4*n)
		nex = make([]int,4*n)
		d = make([]int,4*n)
		vis := make([]int,n+5)
		mp := map[int]int{}
		for i:=1;i<=n-1;i++{
			var x , y int
			fmt.Fscan(in,&x,&y)
			add(x,y)
			add(y,x)
			d[x]++
			d[y]++
		}
		qr := []Node{}
		for i:=1;i<=n;i++{
			if d[i]==1{
				qr = append(qr,Node{i,1})
				vis[i]=1
			}
		}
		for len(qr)!=0{
			now := qr[0]
			mp[now.l]++
			qr = qr[1:]
			for k:=first[now.x];k!=0;k=nex[k]{
				if vis[v[k]]==0 {
					d[v[k]]--
					if d[v[k]] == 1 || d[v[k]] == 0{
						vis[v[k]] = 1
						qr = append(qr, Node{v[k], now.l + 1})
					}
				}
			}
		}
		ans:=n
		for i:=1;i<=k;i++{
			ans -=mp[i]
		}
		fmt.Fprintln(out,ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)
var u []int
var v []int
var first[]int
var nex[]int
var cnt int
var d []int
func add(x, y int){
	cnt++
	u[cnt]=x
	v[cnt]=y
	nex[cnt]=first[x]
	first[x] = cnt
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)

	for c:=1;c<=t;c++{
		var n int
		var k int
		cnt=0
		fmt.Fscan(in,&n,&k)
		u = make([]int,3*n)
		v = make([]int,3*n)
		first = make([]int,4*n)
		nex = make([]int,4*n)
		d = make([]int,4*n)
		mp := map[int]int{}
		for i:=1;i<=n-1;i++{
			var x , y int
			fmt.Fscan(in,&x,&y)
			add(x,y)
			add(y,x)
			d[x]++
			d[y]++
		}
		for i:=1;i<=n;i++{
			mp[d[i]]++
		}
		ans:=n
		for i:=1;i<=k;i++{
			ans-=mp[i]
		}
		fmt.Fprintln(out,ans)
	}
}