The robot is placed in the top left corner of a grid, consisting of $$$n$$$ rows and $$$m$$$ columns, in a cell $$$(1, 1)$$$.In one step, it can move into a cell, adjacent by a side to the current one:   $$$(x, y) \rightarrow (x, y + 1)$$$;  $$$(x, y) \rightarrow (x + 1, y)$$$;  $$$(x, y) \rightarrow (x, y - 1)$$$;  $$$(x, y) \rightarrow (x - 1, y)$$$. The robot can't move outside the grid.The cell $$$(s_x, s_y)$$$ contains a deadly laser. If the robot comes into some cell that has distance less than or equal to $$$d$$$ to the laser, it gets evaporated. The distance between two cells $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$.Print the smallest number of steps that the robot can take to reach the cell $$$(n, m)$$$ without getting evaporated or moving outside the grid. If it's not possible to reach the cell $$$(n, m)$$$, print -1.The laser is neither in the starting cell, nor in the ending cell. The starting cell always has distance greater than $$$d$$$ to the laser.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The only line of each testcase contains five integers $$$n, m, s_x, s_y, d$$$ ($$$2 \le n, m \le 1000$$$; $$$1 \le s_x \le n$$$; $$$1 \le s_y \le m$$$; $$$0 \le d \le n + m$$$) — the size of the grid, the cell that contains the laser and the evaporating distance of the laser. The laser is neither in the starting cell, nor in the ending cell ($$$(s_x, s_y) \neq (1, 1)$$$ and $$$(s_x, s_y) \neq (n, m)$$$). The starting cell $$$(1, 1)$$$ always has distance greater than $$$d$$$ to the laser ($$$|s_x - 1| + |s_y - 1| > d$$$).

For each testcase, print a single integer. If it's possible to reach the cell $$$(n, m)$$$ from $$$(1, 1)$$$ without getting evaporated or moving outside the grid, then print the smallest amount of steps it can take the robot to reach it. Otherwise, print -1.

package main

import (
	"fmt"
)

func main() {
	var numberOfCases int
	_, err := fmt.Scanln(&numberOfCases)
	if err != nil {
		fmt.Printf("Error DDD%s", err.Error())
		return
	}
	var i int
	for i < numberOfCases {
		var n, m, sx, sy, d int
		numberOfRead, err := fmt.Scanf("%d %d %d %d %d\n", &n, &m, &sx, &sy, &d)
		if err != nil || numberOfRead != 5 {
			fmt.Printf("Error sdadasd %x", err.Error())
			return
		}
		if (sx+d >= n && sy+d >= m) || (sx-d <= 1 && sy-d <= 1) ||
			((sx+d >= n && sy-d <= 1) && (sx-d <= 1 && sy+d >= m)) ||
			(sx+d >= n && sx-d <= 1) || (sy+d >= m && sy-d <= 1) {
			fmt.Println(-1)
		} else {
			fmt.Println(n + m - 2)
		}
		i++
	}
	return
}

package main

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

var n, m, sx, sy, d int

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &m, &sx, &sy, &d)
		if (sx-d > 1 && sy+d < m) || (sy-d > 1 && sx+d < n) {
			fmt.Println(n + m - 2)
		} else {
			fmt.Println(-1)
		}
	}
}

package main

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

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

	for ; t > 0; t-- {
		var n, m, sx, sy, d int
		fmt.Fscan(in, &n, &m, &sx, &sy, &d)
		if (sx-d > 1 && sy+d < m) || (sy-d > 1 && sx+d < n) {
			fmt.Println(n + m - 2)
		} else {
			fmt.Println(-1)
		}
	}
}

package main

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

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

	for ; t > 0; t-- {
		inputs := readArrInt(in)
		n, m, sx, sy, d := inputs[0], inputs[1], inputs[2], inputs[3], inputs[4]
		if (sx-d > 1 && sy+d < m) || (sy-d > 1 && sx+d < n) {
			fmt.Println(n + m - 2)
		} else {
			fmt.Println(-1)
		}
	}
}

///////////////////////////////////
// DO NOT EDIT
///////////////////////////////////

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 readLine(in *bufio.Reader) string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	return line
}

func readLineNumbs(in *bufio.Reader) []string {
	numbs := strings.Split(readLine(in), " ")
	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 (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	var t int
	fmt.Scanln(&t)
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	for i := 0; i < t; i++ {
		var r = getIntln(reader)
		if canCross(r) {
			writer.WriteString(strconv.Itoa(r[0]+r[1]-2) + "\n")
		} else {
			writer.WriteString("-1\n")
		}
	}
	writer.Flush()
}

func canCross(s []int) bool {
	if ((s[0]-s[2] <= s[4]) || (s[3]-1 <= s[4])) && ((s[1]-s[3] <= s[4]) || (s[2]-1 <= s[4])) {
		return false
	}

	return true
}

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

func getIntln(in *bufio.Reader) []int {
	var x = strings.Split(getString(in), " ")
	var res = make([]int, len(x))
	for k, v := range x {
		res[k], _ = strconv.Atoi(v)
	}
	return res
}

func getString(in *bufio.Reader) string {
	var s, _ = in.ReadString('\n')
	s = strings.TrimSpace(s)
	return s
}

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

		if (x-d > 1 && y+d < m) || (y-d > 1 && x+d < n) {
			fmt.Fprintln(out, n+m-2)
		} else {
			fmt.Fprintln(out, -1)
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var n, m, sx, sy, d, c, temp, temp1, ans int32
		fmt.Scan(&n, &m, &sx, &sy, &d)
		c = 0
		temp = 0
		temp1 = 0
		ans = -1
		if sx-d <= 1 {
			c++
			temp++
		}
		if sx+d >= n {
			c++
			temp1++
		}
		if sy-d <= 1 {
			c++
			temp1++
		}
		if sy+d >= m {
			c++
			temp++
		}
		// fmt.Printf(" c: %v, temp : %v \n", c, temp)
		if c == 2 && (temp == 2 || temp1 == 2) {
			ans = n + m - 2
			fmt.Println(ans)
		} else if c >= 2 {
			fmt.Println(ans)
		} else {
			ans = n + m - 2
			fmt.Println(ans)
		}
	}
}

// 00
package main

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

func write(f *bufio.Writer, a ...interface{}) {
	f.Write([]byte(fmt.Sprint(a...)))
}

type Number interface {
	int64 | float64 | int
}

func min[K Number](a K, b K) K {
	if a < b {
		return a
	}
	return b
}
func max[K Number](a K, b K) K {
	if a > b {
		return a
	}
	return b
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n, m, sx, sy, d int
		fmt.Fscan(reader, &n, &m, &sx, &sy, &d)
		if (sx-d <= 1 && sy-d <= 1) || (sx+d >= n && sy+d >= m) || (sx-d <= 1 && sx+d >= n) || (sy-d <= 1 && sy+d >= m) {
			write(f, -1, "\n")
		} else {
			write(f, n+m-2, "\n")
		}
	}
}

package main

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

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

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

func qwq() {
	var n, m, x, y, d int
	fmt.Fscan(in, &n, &m, &x, &y, &d)
	if (x-d <= 1 && x+d >= n) || (y-d <= 1 && y+d >= m) || (abs(n-x)+abs(m-y) <= d) || (x-d <= 1 && y-d <= 1) || (x+d >= n && y+d >= m) {
		fmt.Fprintln(out, -1)
		return
	}
	fmt.Fprintln(out, n+m-2)

}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

var in *bufio.Reader = bufio.NewReader(os.Stdin)
var n, m, x, y, d int
var dir [][]int = [][]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}

func main() {
	var t int
	fmt.Fscanf(in, "%d\n", &t)
	for t > 0 {
		fmt.Fscanf(in, "%d %d %d %d %d\n", &n, &m, &x, &y, &d)
		solve()
		t--
	}
}

func solve() {
	a1, a2, a3, a4 := y-1, x-1, m-y, n-x
	if (a1 <= d && a2 <= d) ||
		(a3 <= d && a4 <= d) ||
		(a1 <= d && a3 <= d) ||
		(a2 <= d && a4 <= d) {
		fmt.Println(-1)
	} else {
		fmt.Println(n + m - 2)
	}
}

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

package main

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

func main() {
	r := readerStdin()

	for cases := readInt(r); cases != 0; cases-- {
		values := readSliceInt(r)
		n, m, x, y, d := values[0], values[1], values[2], values[3], values[4]

		var result = -1
		// go to the upside
		if y+d < m && x-d > 1 || y-d > 1 && x+d < n {
			result = n + m - 2
		}

		fmt.Println(result)
	}
}

func readerStdin() *bufio.Reader {
	return bufio.NewReader(os.Stdin)
}

func readInt(r *bufio.Reader) (i int) {
	str := readString(r)
	i, _ = strconv.Atoi(str)
	return
}

func readString(r *bufio.Reader) (s string) {
	s, _ = r.ReadString('\n')
	s = strings.ReplaceAll(s, "\n", "")
	s = strings.ReplaceAll(s, "\r", "")
	return
}

func readSliceInt(r *bufio.Reader) []int {
	str := readString(r)
	slice := strings.Split(str, " ")

	var result = make([]int, len(slice))
	for i, v := range slice {
		x, _ := strconv.Atoi(v)
		result[i] = x
	}

	return result
}

package main

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

var P107 int = 1000000007

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 Abs(a int) int {
	return Max(a, -a)
}

func Gcd(a, b int) int {
	min := Min(a, b)
	max := Max(a, b)
	if min == 0 {
		return max
	}

	for max%min != 0 {
		min, max = max%min, min
	}

	return min
}

func Lcm(a, b int) int {
	return a / Gcd(a, b) * b
}

// var facs []int = make([]int, X + 1)
func FacP(n, p int) int {
	// if facs[n] != 0 {
	//   return facs[n]
	// }
	result := 1
	// facs[0] = 1
	for i := 1; i <= n; i++ {
		result = (result * i) % p
		// facs[i] = result
	}

	return result
}

func ExpP(a, x, p int) int {
	if x == 0 {
		return 1
	}

	if x%2 == 0 {
		half := ExpP(a, x/2, p)
		return (half * half) % p
	} else {
		return ((a % p) * ExpP(a, x-1, p)) % p
	}
}

func CombP(n, c, p int) int {
	// If CombP be called multiple times, you should optimize FacP using memoization.
	return (FacP(n, p) * ((ExpP(FacP(n-c, p), p-2, p) * ExpP(FacP(c, p), p-2, p)) % p)) % p
}

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

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

	for _i := 0; _i < t; _i++ {
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		x, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		y, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		d, _ := strconv.Atoi(scanner.Text())

		if (x-d <= 1 && y-d <= 1) || (x+d >= n && y+d >= m) || (x-d <= 1 && x+d >= n) || (y-d <= 1 && y+d >= m) {
			wr.WriteString("-1\n")
		} else {
			wr.WriteString(strconv.Itoa(n+m-2) + "\n")
		}
	}
}

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

package main

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

var (
	reader           = bufio.NewReader(os.Stdin)
	t, n, m, x, y, d int
)

func solve(n, m, x, y, d int) int {
	if (n-x <= d || y-1 <= d) && (m-y <= d || x-1 <= d) {
		return -1
	}
	return n + m - 2
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n, &m, &x, &y, &d)
		fmt.Println(solve(n, m, x, y, d))
	}
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	cases := readInt(input)
	for asdf := 0; asdf < cases; asdf++ {
		tmp := readArrInt(input)
		n := tmp[0]
		m := tmp[1]
		sx := tmp[2]
		sy := tmp[3]
		d := tmp[4]
		fmt.Println(solve(n, m, sx, sy, d))
	}
}

func solve(n, m, sx, sy, d int) int {
	if sx+d >= n && sx-d <= 1 {
		return -1
	} else if sy+d >= m && sy-d <= 1 {
		return -1
	} else if n-sx+m-sy <= d {
		return -1
	} else if sx-d <= 1 && sy-d <= 1 {
		return -1
	} else if sx+d >= n && sy+d >= m {
		return -1
	}
	return n + m - 2
}

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

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

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

func readInt64(in *bufio.Reader) int64 {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return int64(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"
)

func main() {
	var numberOfCases int
	_, err := fmt.Scanln(&numberOfCases)
	if err != nil {
		fmt.Printf("Error DDD%s", err.Error())
		return
	}
	var i int
	for i < numberOfCases {
		var n, m, sx, sy, d int
		numberOfRead, err := fmt.Scanf("%d %d %d %d %d\n", &n, &m, &sx, &sy, &d)
		if err != nil || numberOfRead != 5 {
			fmt.Printf("Error sdadasd %x", err.Error())
			return
		}
		if (sx+d >= n && sy+d >= m) || (sx-d <= 1 && sy-d <= 1) ||
			((sx+d >= n && sy-d <= 1) && (sx-d <= 1 && sy+d >= m)) {
			fmt.Println(-1)
		} else {
			fmt.Println(n + m - 2)
		}
		i++
	}
	return
}

package main

import (
	"fmt"
)

func main() {
	var numberOfCases int
	_, err := fmt.Scanln(&numberOfCases)
	if err != nil {
		fmt.Printf("Error DDD%s", err.Error())
		return
	}
	var i int
	for i < numberOfCases {
		var n, m, sx, sy, d int
		numberOfRead, err := fmt.Scanf("%d %d %d %d %d\n", &n, &m, &sx, &sy, &d)
		if err != nil || numberOfRead != 5 {
			fmt.Printf("Error sdadasd %x", err.Error())
			return
		}
		if (sx+d >= n && sy+d >= m) || (sx-d <= 1 && sy-d <= 1) ||
			(sx+d >= n && sy-d <= 1) || (sx-d <= 1 && sy+d >= m) {
			fmt.Println(-1)
		} else {
			fmt.Println(n + m - 2)
		}
		i++
	}
	return
}

package main

import (
	"fmt"
)

func main() {
	var numberOfCases int
	_, err := fmt.Scanln(&numberOfCases)
	if err != nil {
		fmt.Printf("Error DDD%s", err.Error())
		return
	}
	var i int
	for i < numberOfCases {
		var n, m, sx, sy, d int
		numberOfRead, err := fmt.Scanf("%d %d %d %d %d\n", &n, &m, &sx, &sy, &d)
		if err != nil || numberOfRead != 5 {
			fmt.Printf("Error sdadasd %x", err.Error())
			return
		}
		if (sx+d >= n && sy+d >= m) || (sx-d <= 1 && sy-d <= 1) {
			fmt.Println(-1)
		} else {
			fmt.Println(n + m - 2)
		}
		i++
	}
	return
}

package main

import (
	"fmt"
)

func main() {
	var numberOfCases int
	_, err := fmt.Scanln(&numberOfCases)
	if err != nil {
		fmt.Printf("Error DDD%s", err.Error())
		return
	}
	var i int
	for i < numberOfCases {
		var n, m, sx, sy, d int
		numberOfRead, err := fmt.Scanf("%d %d %d %d %d\n", &n, &m, &sx, &sy, &d)
		if err != nil || numberOfRead != 5 {
			fmt.Printf("Error sdadasd %x", err.Error())
			return
		}
		if ((sx-1)+(sy-1) <= d) || ((n-sx)+(m-sy) <= d) || (n-1) <= d || (m-1) <= d {
			fmt.Println(-1)
		} else {
			fmt.Println(n + m - 2)
		}
		i++
	}
	return
}

package main

import (
	"fmt"
)

func main() {
	var numberOfCases int
	_, err := fmt.Scanln(&numberOfCases)
	if err != nil {
		fmt.Printf("Error DDD%s", err.Error())
		return
	}
	var i int
	for i < numberOfCases {
		var n, m, sx, sy, d int
		numberOfRead, err := fmt.Scanf("%d %d %d %d %d\n", &n, &m, &sx, &sy, &d)
		if err != nil || numberOfRead != 5 {
			fmt.Printf("Error sdadasd %x", err.Error())
			return
		}
		if ((sx-1)+(sy-1) <= d) || ((n-sx)+(m-sy) <= d) {
			fmt.Println(-1)
		} else {
			fmt.Println(n + m - 2)
		}
		i++
	}
	return
}

package main

import (
	"fmt"
)

func main() {
	var numberOfCases int
	_, err := fmt.Scanln(&numberOfCases)
	if err != nil {
		fmt.Printf("Error DDD%s", err.Error())
		return
	}
	var i int
	for i < numberOfCases {
		var n, m, sx, sy, d int
		numberOfRead, err := fmt.Scanf("%d %d %d %d %d", &n, &m, &sx, &sy, &d)
		if err != nil || numberOfRead != 5 {
			fmt.Printf("Error sdadasd %x", err.Error())
			return
		}
		if ((sx-1)+(sy-1) <= d) || ((n-sx)+(m-sy) <= d) {
			fmt.Println(-1)
		} else {
			fmt.Println(n + m - 2)
		}
		i++
	}
	return
}

package main

import (
	"fmt"
)

func main() {
	var numberOfCases int
	_, err := fmt.Scanln(&numberOfCases)
	if err != nil {
		fmt.Printf("Error DDD%s", err.Error())
		return
	}
	var i int
	for i <= numberOfCases {
		var n, m, sx, sy, d int
		numberOfRead, err := fmt.Scanf("%d %d %d %d %d", &n, &m, &sx, &sy, &d)
		if err != nil || numberOfRead != 5 {
			fmt.Printf("Error sdadasd %x", err.Error())
			return
		}
		if ((sx-1)+(sy-1) <= d) || ((n-sx)+(m-sy) <= d) {
			fmt.Println(-1)
		} else {
			fmt.Println(n + m - 2)
		}
		i++
	}
	return
}

package main

import (
	"fmt"
)

func main() {
	var numberOfCases int
	_, err := fmt.Scanf("%d\n", &numberOfCases)
	if err != nil {
		fmt.Printf("Error %s", err.Error())
		return
	}
	var i int
	for i <= numberOfCases {
		var n, m, sx, sy, d int
		numberOfRead, err := fmt.Scanf("%d %d %d %d %d", &n, &m, &sx, &sy, &d)
		if err != nil || numberOfRead != 5 {
			fmt.Printf("Error %x", err.Error())
			return
		}
		if ((sx-1)+(sy-1) <= d) || ((n-sx)+(m-sy) <= d) {
			fmt.Println(-1)
		} else {
			fmt.Println(n + m - 2)
		}
	}
	return
}

package main

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

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

	for ; t > 0; t-- {
		inputs := readArrInt(in)
		n, m, sx, sy, d := inputs[0], inputs[1], inputs[2], inputs[3], inputs[4]
		/*
			sx - d > 1
			sy - d > 1
			sx + d < n -1
			sy + d < m - 1

		*/
		if sx-d >= 1 && sy-d >= 1 && sx+d < n || sy+d < m {
			fmt.Println(n + m - 2)
		} else {
			fmt.Println(-1)
		}
	}
}

///////////////////////////////////
// DO NOT EDIT
///////////////////////////////////

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 readLine(in *bufio.Reader) string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	return line
}

func readLineNumbs(in *bufio.Reader) []string {
	numbs := strings.Split(readLine(in), " ")
	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 (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

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

	var collision = func(x, y, rx, ry, d int) bool {
		_rx := rx + d
		_ry := ry - d
		if x >= _rx-2*d && x <= _rx && y >= _ry-2*d && y <= _ry {
			return true
		}
		return false
	}

	for ; t > 0; t-- {
		inputs := readArrInt(in)
		n, m, sx, sy, d := inputs[0], inputs[1], inputs[2], inputs[3], inputs[4]
		if !collision(1, 1, sx, sy, d) && !collision(n, m, sx, sy, d) {
			fmt.Println(n + m - 2)
		} else {
			fmt.Println(-1)
		}
	}
}

///////////////////////////////////
// DO NOT EDIT
///////////////////////////////////

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 readLine(in *bufio.Reader) string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	return line
}

func readLineNumbs(in *bufio.Reader) []string {
	numbs := strings.Split(readLine(in), " ")
	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 (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

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

	var collision = func(x, y, rx, ry, d int) bool {
		_rx := rx + d
		_ry := ry - d
		if x >= _rx && x <= _rx+d && y >= _ry && y <= _ry+d {
			return true
		}
		return false
	}

	for ; t > 0; t-- {
		inputs := readArrInt(in)
		n, m, sx, sy, d := inputs[0], inputs[1], inputs[2], inputs[3], inputs[4]
		if !collision(1, 1, sx, sy, d) && !collision(n, m, sx, sy, d) {
			fmt.Println(n + m - 2)
		} else {
			fmt.Println(-1)
		}
	}
}

///////////////////////////////////
// DO NOT EDIT
///////////////////////////////////

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 readLine(in *bufio.Reader) string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	return line
}

func readLineNumbs(in *bufio.Reader) []string {
	numbs := strings.Split(readLine(in), " ")
	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 (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

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

	for ; t > 0; t-- {
		inputs := readArrInt(in)
		n, m, sx, sy, d := inputs[0], inputs[1], inputs[2], inputs[3], inputs[4]
		if (sx+d < n || sx-d > 1) && (sy+d < m || sy-d > 1) {
			fmt.Println(n + m - 2)
		} else {
			fmt.Println(-1)
		}
	}
}

///////////////////////////////////
// DO NOT EDIT
///////////////////////////////////

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 readLine(in *bufio.Reader) string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	return line
}

func readLineNumbs(in *bufio.Reader) []string {
	numbs := strings.Split(readLine(in), " ")
	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 (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

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

	for ; t > 0; t-- {
		inputs := readArrInt(in)
		n, m, sx, sy, d := inputs[0], inputs[1], inputs[2], inputs[3], inputs[4]
		if (sx+d < n || sx-d >= 1) && (sy+d < m || sy-d >= 1) {
			fmt.Println(n + m - 2)
		} else {
			fmt.Println(-1)
		}
	}
}

///////////////////////////////////
// DO NOT EDIT
///////////////////////////////////

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 readLine(in *bufio.Reader) string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	return line
}

func readLineNumbs(in *bufio.Reader) []string {
	numbs := strings.Split(readLine(in), " ")
	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 (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	var t int
	fmt.Scanln(&t)
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	for i := 0; i < t; i++ {
		var r = getIntln(reader)
		if canCross(r) {
			writer.WriteString(strconv.Itoa(r[0]+r[1]-2) + "\n")
		} else {
			writer.WriteString("-1\n")
		}
	}
	writer.Flush()
}

func canCross(s []int) bool {
	if ((s[0]-s[2] <= s[4]) || (s[1]-s[3] <= s[4])) && ((s[2]-1 <= s[4]) || (s[3]-1 <= s[4])) {
		return false
	}

	return true
}

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

func getIntln(in *bufio.Reader) []int {
	var x = strings.Split(getString(in), " ")
	var res = make([]int, len(x))
	for k, v := range x {
		res[k], _ = strconv.Atoi(v)
	}
	return res
}

func getString(in *bufio.Reader) string {
	var s, _ = in.ReadString('\n')
	s = strings.TrimSpace(s)
	return s
}

package main

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

func main() {
	var t int
	fmt.Scanln(&t)
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	for i := 0; i < t; i++ {
		var r = getIntln(reader)
		if canCross(r) {
			writer.WriteString(strconv.Itoa(r[0]+r[1]-2) + "\n")
		} else {
			writer.WriteString("-1\n")
		}
	}
	writer.Flush()
}

func canCross(s []int) bool {
	if ((s[0]-s[2] <= s[4]) || (s[1]-s[3] <= s[4])) && ((s[2]-1 <= s[4]) || (s[3]-1 <= s[4])) {
		return false
	}

	if s[0]-s[2]+s[1]-s[3] <= s[4] {
		return false
	}

	return true
}

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

func getIntln(in *bufio.Reader) []int {
	var x = strings.Split(getString(in), " ")
	var res = make([]int, len(x))
	for k, v := range x {
		res[k], _ = strconv.Atoi(v)
	}
	return res
}

func getString(in *bufio.Reader) string {
	var s, _ = in.ReadString('\n')
	s = strings.TrimSpace(s)
	return s
}

package main

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

func main() {
	var t int
	fmt.Scanln(&t)
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	for i := 0; i < t; i++ {
		var r = getIntln(reader)
		if canCross(r) {
			writer.WriteString(strconv.Itoa(r[0]+r[1]-2) + "\n")
		} else {
			writer.WriteString("-1\n")
		}
	}
	writer.Flush()
}

func canCross(s []int) bool {
	if (s[0]-s[2] <= s[4]) && (s[1]-s[3] <= s[4]) {
		return false
	}
	if (s[2]-1 <= s[4]) && (s[3]-1 <= s[4]) {
		return false
	}

	if s[0]-s[2]+s[1]-s[3] <= s[4] {
		return false
	}

	return true
}

func getIntln(in *bufio.Reader) []int {
	var x = strings.Split(getString(in), " ")
	var res = make([]int, len(x))
	for k, v := range x {
		res[k], _ = strconv.Atoi(v)
	}
	return res
}

func getString(in *bufio.Reader) string {
	var s, _ = in.ReadString('\n')
	s = strings.TrimSpace(s)
	return s
}

package main

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

func main() {
	var t int
	fmt.Scanln(&t)
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	for i := 0; i < t; i++ {
		var r = getIntln(reader)
		if canCross(r) {
			writer.WriteString(strconv.Itoa(r[0]+r[1]-2) + "\n")
		} else {
			writer.WriteString("-1\n")
		}
	}
	writer.Flush()
}

func canCross(s []int) bool {
	if (s[0]-s[2] <= s[4]) && (s[1]-s[3] <= s[4]) {
		return false
	}
	if s[0]-s[2]+s[1]-s[3] <= s[4] {
		return false
	}

	return true
}

func getIntln(in *bufio.Reader) []int {
	var x = strings.Split(getString(in), " ")
	var res = make([]int, len(x))
	for k, v := range x {
		res[k], _ = strconv.Atoi(v)
	}
	return res
}

func getString(in *bufio.Reader) string {
	var s, _ = in.ReadString('\n')
	s = strings.TrimSpace(s)
	return s
}

package main

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

func main() {
	var N, D int
	fmt.Scanln(&N, &D)
	var reader = bufio.NewReader(os.Stdin)
	var p = getIntln(reader)
	sort.Slice(p, func(i, j int) bool {
		return p[i] < p[j]
	})
	var x = sort.Search(N, func(i int) bool {
		return p[i] > D
	})
	var result = N - x
	var first, last = 0, x - 1
	for first < last {
		var x = p[last]
		for x <= D && first < last {
			x += p[last]
			first++
		}
		if x > D {
			result++
		}
		last--
	}
	fmt.Println(result)
}

func getIntln(in *bufio.Reader) []int {
	var x = strings.Split(getString(in), " ")
	var res = make([]int, len(x))
	for k, v := range x {
		res[k], _ = strconv.Atoi(v)
	}
	return res
}

func getString(in *bufio.Reader) string {
	var s, _ = in.ReadString('\n')
	s = strings.TrimSpace(s)
	return s
}

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

		if (x+d >= n && y+d >= m) || (x-d <= 1 && y-d <= 1) {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, n+m-2)
		}
	}
}

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

		if x+d >= n && y+d >= m {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, n+m-2)
		}
	}
}

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

		if x+d >= n && y+d >= m {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, (n-1)+(m-1))
		}
	}
}

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

		if x+d >= n && y+d >= m {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, n-1+m-1)
		}
	}
}

package main

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

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

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n, m, x, y, d int
		fmt.Fscan(in, &n, &m, &x, &y, &d)

		if x-d <= 1 && x+d >= n || y-d <= 1 && y+d >= m {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, n-1+m-1)
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var n, m, sx, sy, d, c, temp, ans int32
		fmt.Scan(&n, &m, &sx, &sy, &d)
		c = 0
		temp = 0
		ans = -1
		if sx-d <= 1 {
			c++
			temp++
		}
		if sx+d >= n {
			c++
		}
		if sy-d <= 1 {
			c++
		}
		if sy+d >= m {
			c++
			temp++
		}
		// fmt.Printf(" c: %v, temp : %v \n", c, temp)
		if c == 2 && temp == 2 {
			ans = n + m - 2
			fmt.Println(ans)
		} else if c >= 2 {
			fmt.Println(ans)
		} else {
			ans = n + m - 2
			fmt.Println(ans)
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var n, m, sx, sy, d, c, temp, ans int32
		fmt.Scan(&n, &m, &sx, &sy, &d)
		c = 0
		temp = 0
		ans = -1
		if sx-d <= 1 {
			c++
			temp++
		}
		if sx+d >= n {
			c++
		}
		if sy-d <= 1 {
			c++
		}
		if sy+d >= m {
			c++
			temp++
		}
		// fmt.Printf(" c: %v, temp : %v \n", c, temp)
		if c == 2 && temp == 2 {
			ans = n + m - 2
			fmt.Println(ans)
		} else if c > 2 {
			fmt.Println(ans)
		} else {
			ans = n + m - 2
			fmt.Println(ans)
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var n, m, sx, sy, d int
		fmt.Scan(&n, &m, &sx, &sy, &d)
		c := 0
		temp := 0
		ans := -1
		if sx-d <= 1 {
			c++
			temp++
		}
		if sx+d >= n {
			c++
		}
		if sy-d <= 1 {
			c++
		}
		if sy+d >= m {
			c++
			temp++
		}
		// fmt.Printf(" c: %v, temp : %v \n", c, temp)
		if c == 2 && temp == 2 {
			ans = n + m - 2
			fmt.Println(ans)
		} else if c > 2 {
			fmt.Println(ans)
		} else {
			ans = n + m - 2
			fmt.Println(ans)
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var n, m, sx, sy, d int
		fmt.Scan(&n, &m, &sx, &sy, &d)
		if d == 0 {
			ans := n + m - 2
			fmt.Println(ans)
			continue
		}
		c := 0
		ans := -1
		if sx-d <= 1 {
			c++
		}
		if sx+d >= n {
			c++
		}
		if sy-d <= 1 {
			c++
		}
		if sy+d >= m {
			c++
		}
		if c >= 2 {
			fmt.Println(ans)
		} else {
			ans = n + m - 2
			fmt.Println(ans)
		}
	}
}

// 00
package main

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

func write(f *bufio.Writer, a ...interface{}) {
	f.Write([]byte(fmt.Sprint(a...)))
}

type Number interface {
	int64 | float64 | int
}

func min[K Number](a K, b K) K {
	if a < b {
		return a
	}
	return b
}
func max[K Number](a K, b K) K {
	if a > b {
		return a
	}
	return b
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n, m, sx, sy, d int
		fmt.Fscan(reader, &n, &m, &sx, &sy, &d)
		if (sx-d <= 1 && sy-d <= 1) || (sx+d >= n && sy+d >= m) {
			write(f, -1, "\n")
		} else {
			write(f, n+m-2, "\n")
		}
	}
}

// 00
package main

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

func write(f *bufio.Writer, a ...interface{}) {
	f.Write([]byte(fmt.Sprint(a...)))
}

type Number interface {
	int64 | float64 | int
}

func min[K Number](a K, b K) K {
	if a < b {
		return a
	}
	return b
}
func max[K Number](a K, b K) K {
	if a > b {
		return a
	}
	return b
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n, m, sx, sy, d int
		fmt.Fscan(reader, &n, &m, &sx, &sy, &d)
		if (sx-d <= 0 && sy-d <= 0) || (sx+d >= n && sy+d >= m) {
			write(f, -1, "\n")
		} else {
			write(f, n+m-2, "\n")
		}
	}
}

// 00
package main

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

func write(f *bufio.Writer, a ...interface{}) {
	f.Write([]byte(fmt.Sprint(a...)))
}

type Number interface {
	int64 | float64 | int
}

func min[K Number](a K, b K) K {
	if a < b {
		return a
	}
	return b
}
func max[K Number](a K, b K) K {
	if a > b {
		return a
	}
	return b
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n, m, sx, sy, d int
		fmt.Fscan(reader, &n, &m, &sx, &sy, &d)
		if sx-d <= 0 && sy-d <= 0 || sx+d >= n && sy+d >= m {
			write(f, -1, "\n")
		} else {
			write(f, n+m-2, "\n")
		}
	}
}

package main

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

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

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

func qwq() {
	var n, m, x, y, d int
	fmt.Fscan(in, &n, &m, &x, &y, &d)
	if (x-d <= 1 && x+d >= n) || (y-d <= 1 && y+d >= m) || (abs(n-x)+abs(m-y) <= d) || (abs(1-x)+abs(1-y) <= d) {
		fmt.Fprintln(out, -1)
		return
	}
	fmt.Fprintln(out, n+m-2)

}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

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

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

func qwq() {
	var n, m, x, y, d int
	fmt.Fscan(in, &n, &m, &x, &y, &d)
	if (x-d <= 1 && x+d >= n) || (y-d <= 1 && y+d >= m) || (abs(n-x)+abs(m-y) <= d) {
		fmt.Fprintln(out, -1)
		return
	}
	fmt.Fprintln(out, n+m-2)

}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

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

func qwq() {
	var n, m, x, y, d int
	fmt.Fscan(in, &n, &m, &x, &y, &d)
	if (x-d <= 1 && x+d >= n) || (y-d <= 1 && y+d >= m) {
		fmt.Fprintln(out, -1)
		return
	}
	fmt.Fprintln(out, n+m-2)

}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

var in *bufio.Reader = bufio.NewReader(os.Stdin)
var n, m, x, y, d int
var dir [][]int = [][]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}

func main() {
	var t int
	fmt.Fscanf(in, "%d\n", &t)
	for t > 0 {
		fmt.Fscanf(in, "%d %d %d %d %d\n", &n, &m, &x, &y, &d)
		solve()
		t--
	}
}

func solve() {
	a1, a2, a3, a4 := y-1, x-1, m-y, n-x
	if (a1 <= d && a2 <= d) ||
		(a3 <= 3 && a4 <= d) ||
		(a1 <= d && a3 <= d) ||
		(a2 <= 3 && a4 <= d) {
		fmt.Println(-1)
	} else {
		fmt.Println(n + m - 2)
	}
}

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

package main

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

var in *bufio.Reader = bufio.NewReader(os.Stdin)
var n, m, x, y, d int
var dir [][]int = [][]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}

func main() {
	var t int
	fmt.Fscanf(in, "%d\n", &t)
	for t > 0 {
		fmt.Fscanf(in, "%d %d %d %d %d\n", &n, &m, &x, &y, &d)
		solve()
		t--
	}
}

func solve() {
	if (x-1 <= d && y-1 <= d) || (n-x <= d && m-y <= d) {
		fmt.Println(-1)
	} else {
		fmt.Println(n + m - 2)
	}
}

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

package main

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

func main() {
	r := readerStdin()

	for cases := readInt(r); cases != 0; cases-- {
		values := readSliceInt(r)
		n, m, x, y, d := values[0], values[1], values[2], values[3], values[4]

		var result = -1
		// go to the upside
		if y+d < m && x-d > 0 || y-d > 0 && x+d < n {
			result = n + m - 2
		}

		fmt.Println(result)
	}
}

func readerStdin() *bufio.Reader {
	return bufio.NewReader(os.Stdin)
}

func readInt(r *bufio.Reader) (i int) {
	str := readString(r)
	i, _ = strconv.Atoi(str)
	return
}

func readString(r *bufio.Reader) (s string) {
	s, _ = r.ReadString('\n')
	s = strings.ReplaceAll(s, "\n", "")
	s = strings.ReplaceAll(s, "\r", "")
	return
}

func readSliceInt(r *bufio.Reader) []int {
	str := readString(r)
	slice := strings.Split(str, " ")

	var result = make([]int, len(slice))
	for i, v := range slice {
		x, _ := strconv.Atoi(v)
		result[i] = x
	}

	return result
}

package main

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

var P107 int = 1000000007

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 Abs(a int) int {
	return Max(a, -a)
}

func Gcd(a, b int) int {
	min := Min(a, b)
	max := Max(a, b)
	if min == 0 {
		return max
	}

	for max%min != 0 {
		min, max = max%min, min
	}

	return min
}

func Lcm(a, b int) int {
	return a / Gcd(a, b) * b
}

// var facs []int = make([]int, X + 1)
func FacP(n, p int) int {
	// if facs[n] != 0 {
	//   return facs[n]
	// }
	result := 1
	// facs[0] = 1
	for i := 1; i <= n; i++ {
		result = (result * i) % p
		// facs[i] = result
	}

	return result
}

func ExpP(a, x, p int) int {
	if x == 0 {
		return 1
	}

	if x%2 == 0 {
		half := ExpP(a, x/2, p)
		return (half * half) % p
	} else {
		return ((a % p) * ExpP(a, x-1, p)) % p
	}
}

func CombP(n, c, p int) int {
	// If CombP be called multiple times, you should optimize FacP using memoization.
	return (FacP(n, p) * ((ExpP(FacP(n-c, p), p-2, p) * ExpP(FacP(c, p), p-2, p)) % p)) % p
}

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

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

	for _i := 0; _i < t; _i++ {
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		x, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		y, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		d, _ := strconv.Atoi(scanner.Text())

		if (x-d <= 1 && y-d <= 1) || (x+d >= n && y+d >= m) {
			wr.WriteString("-1\n")
		} else {
			wr.WriteString(strconv.Itoa(n+m-2) + "\n")
		}
	}
}

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

package main

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

var (
	reader           = bufio.NewReader(os.Stdin)
	t, n, m, x, y, d int
)

func solve(n, m, x, y, d int) int {
	if n-x <= d && m-y <= d {
		return -1
	}
	return n + m - 2
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n, &m, &x, &y, &d)
		fmt.Println(solve(n, m, x, y, d))
	}
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	cases := readInt(input)
	for asdf := 0; asdf < cases; asdf++ {
		tmp := readArrInt(input)
		n := tmp[0]
		m := tmp[1]
		sx := tmp[2]
		sy := tmp[3]
		d := tmp[4]
		fmt.Println(solve(n, m, sx, sy, d))
	}
}

func solve(n, m, sx, sy, d int) int {
	if sx+d >= n && sx-d <= 1 {
		return -1
	} else if sy+d >= m && sy-d <= 1 {
		return -1
	} else if n-sx+m-sy <= d {
		return -1
	}
	return n + m - 2
}

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

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

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

func readInt64(in *bufio.Reader) int64 {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return int64(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
}


Evlampiy was gifted a rooted tree. The vertices of the tree are numbered from $$$1$$$ to $$$n$$$. Each of its vertices also has an integer $$$a_i$$$ written on it. For each vertex $$$i$$$, Evlampiy calculated $$$c_i$$$ — the number of vertices $$$j$$$ in the subtree of vertex $$$i$$$, such that $$$a_j < a_i$$$. Illustration for the second example, the first integer is $$$a_i$$$ and the integer in parentheses is $$$c_i$$$After the new year, Evlampiy could not remember what his gift was! He remembers the tree and the values of $$$c_i$$$, but he completely forgot which integers $$$a_i$$$ were written on the vertices.Help him to restore initial integers!

The first line contains an integer $$$n$$$ $$$(1 \leq n \leq 2000)$$$ — the number of vertices in the tree. The next $$$n$$$ lines contain descriptions of vertices: the $$$i$$$-th line contains two integers $$$p_i$$$ and $$$c_i$$$ ($$$0 \leq p_i \leq n$$$; $$$0 \leq c_i \leq n-1$$$), where $$$p_i$$$ is the parent of vertex $$$i$$$ or $$$0$$$ if vertex $$$i$$$ is root, and $$$c_i$$$ is the number of vertices $$$j$$$ in the subtree of vertex $$$i$$$, such that $$$a_j < a_i$$$. It is guaranteed that the values of $$$p_i$$$ describe a rooted tree with $$$n$$$ vertices.

If a solution exists, in the first line print "YES", and in the second line output $$$n$$$ integers $$$a_i$$$ $$$(1 \leq a_i \leq {10}^{9})$$$. If there are several solutions, output any of them. One can prove that if there is a solution, then there is also a solution in which all $$$a_i$$$ are between $$$1$$$ and $$$10^9$$$. If there are no solutions, print "NO".

package main

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

type node struct {
	diff int
	time int
}

type ByTime []node

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

var p []int
var c []int
var children [][]int
var invalid bool

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	//fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		children = make([][]int, n)
		for i := 0; i < n; i++ {
			children[i] = make([]int, 0)
		}
		p, c = make([]int, n), make([]int, n)
		root := -1
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d %d\n", &p[i], &c[i])
			p[i]--
			if p[i] == -1 {
				root = i
			} else {
				children[p[i]] = append(children[p[i]], i)
			}
		}
		invalid = false
		ans := dfs(root)
		a := make([]int, n)
		for i := 0; i < len(ans); i++ {
			a[ans[i]] = i + 1
		}
		if invalid {
			fmt.Fprintf(out, "NO\n")
		} else {
			fmt.Fprintf(out, "YES\n")
			for i := 0; i < n-1; i++ {
				fmt.Fprintf(out, "%d ", a[i])
			}
			fmt.Fprintf(out, "%d\n", a[n-1])
		}
	}
	return
}

func dfs(i int) []int {
	if len(children[i]) == 0 {
		if c[i] > 0 {
			invalid = true
			return nil
		}
		return []int{i}
	}
	tmp := []int{}
	for j := 0; j < len(children[i]); j++ {
		tmp = append(tmp, dfs(children[i][j])...)
		if invalid {
			return nil
		}
	}
	if c[i] > len(tmp) {
		invalid = true
		return nil
	}
	ans := []int{}
	for j := 0; j < c[i]; j++ {
		ans = append(ans, tmp[j])
	}
	ans = append(ans, i)
	ans = append(ans, tmp[c[i]:]...)
	return ans
}

package main

import (
	"fmt"
)

type node struct {
	s []int
	c int
	i int
	a int
}

func main() {
	var n, p, c int
	fmt.Scan(&n)
	t := make([]node, n+1)
	for i := 1; i <= n; i++ {
		fmt.Scan(&p, &c)
		t[i].i = i
		t[i].c = c
		t[p].s = append(t[p].s, i)
	}
	// fmt.Println(t)
	a, ok := dfs(t, t[0].s[0])
	if !ok {
		fmt.Println("NO")
		return
	}
	fmt.Println("YES")
	for a, i := range a {
		t[i].a = a+1
	}
	for i := 1; i <= n; i++ {
		fmt.Printf("%d ", t[i].a)
	}
	fmt.Println()
}

func dfs(t []node, x int) ([]int, bool) {
	r := &t[x]
	a := []int{r.i}
	if len(r.s) == 0 {
		if r.c != 0 {
			return nil, false
		}
	}
	for _, j := range r.s {
		b, ok := dfs(t, j)
		if !ok {
			return nil ,false
		}
		if r.c == 0 {
			a = append(a, b...)
		} else if r.c >= len(b) {
			a = append(a[:len(a)-1], b...)
			a = append(a, r.i)
			r.c -= len(b)
		} else {//r.c < len(b)
			a = append(a[:len(a)-1], b[:r.c]...)
			a = append(a, r.i)
			a = append(a, b[r.c:]...)
			r.c = 0
		}
	}
	if r.c > 0 {
		return nil, false
	}
	return a, true
}

package main

import (
	"fmt"
	"os"
)

const N = 2010
const M = N << 1
var tot = 0
var st [N]int
var nex, b [M]int
func addEdge(x, y int) {
	tot++
	b[tot] = y
	nex[tot] = st[x]
	st[x] = tot
}

var n int
var c, v, sz [N]int
var used [N]bool
func fill(x int) {
	rest := c[x] + 1
	for i := 1; i <= n; i++ {
		if used[i] {
			continue
		}
		rest--
		if rest == 0 {
			v[x] = i
			used[i] = true
			break
		}
	}
	if rest > 0 {
		fmt.Println("NO")
		os.Exit(0)
	}
	sz[x] = 1
	for j := st[x]; j != 0; j = nex[j] {
		fill(b[j])
		sz[x] += sz[b[j]]
	}
	if sz[x] < c[x] + 1 {
		fmt.Println("NO")
		os.Exit(0)
	}
}

func main() {
	var root int
	fmt.Scan(&n)
	for i := 1; i <= n; i++ {
		var tmp int
		fmt.Scan(&tmp, &c[i])
		if tmp == 0 {
			root = i
			continue
		}
		addEdge(tmp, i)
	}

	fill(root)
	fmt.Println("YES")
	for i := 1; i <= n; i++ {
		fmt.Printf("%d", v[i])
		if i != n {
			fmt.Print(" ")
		}
	}
	fmt.Print("\n")
}

package main

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

type node struct {
	diff int
	time int
}

type ByTime []node

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

var p []int
var c []int
var children [][]int
var invalid bool

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	//fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		children = make([][]int, n)
		for i := 0; i < n; i++ {
			children[i] = make([]int, 0)
		}
		p, c = make([]int, n), make([]int, n)
		root := -1
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d %d\n", &p[i], &c[i])
			p[i]--
			if p[i] == -1 {
				root = i
			} else {
				children[p[i]] = append(children[p[i]], i)
			}
		}
		invalid = false
		ans := dfs(root)
		a := make([]int, n)
		for i := 0; i < len(ans); i++ {
			a[ans[i]] = i + 1
		}
		if invalid {
			fmt.Fprintf(out, "NO\n")
		} else {
			fmt.Fprintf(out, "YES\n")
			for i := 0; i < n-1; i++ {
				fmt.Fprintf(out, "%d ", a[i])
			}
			fmt.Fprintf(out, "%d\n", a[n-1])
		}
	}
	return
}

func dfs(i int) []int {
	if len(children[i]) == 0 {
		return []int{i}
	}
	tmp := []int{}
	for j := 0; j < len(children[i]); j++ {
		tmp = append(tmp, dfs(children[i][j])...)
		if invalid {
			return nil
		}
	}
	if c[i] > len(tmp) {
		invalid = true
		return nil
	}
	ans := []int{}
	for j := 0; j < c[i]; j++ {
		ans = append(ans, tmp[j])
	}
	ans = append(ans, i)
	ans = append(ans, tmp[c[i]:]...)
	return ans
}

package main

import (
	"fmt"
)

type node struct {
	s []int
	c int
	i int
	a int
}

func main() {
	var n, p, c int
	fmt.Scan(&n)
	t := make([]node, n+1)
	for i := 1; i <= n; i++ {
		fmt.Scan(&p, &c)
		t[i].i = i
		t[i].c = c
		t[p].s = append(t[p].s, i)
	}
	// fmt.Println(t)
	a, ok := dfs(t, t[0].s[0])
	if !ok {
		fmt.Println("NO")
		return
	}
	fmt.Println("YES")
	for a, i := range a {
		t[i].a = a+1
	}
	for i := 1; i <= n; i++ {
		fmt.Printf("%d ", t[i].a)
	}
	fmt.Println()
}

func dfs(t []node, x int) ([]int, bool) {
	r := &t[x]
	a := []int{r.i}
	if len(r.s) == 0 {
		if r.c != 0 {
			return nil, false
		}
	}
	for _, j := range r.s {
		b, ok := dfs(t, j)
		if !ok {
			return nil ,false
		}
		if r.c == 0 {
			a = append(a, b...)
		} else if r.c >= len(b) {
			a = append(a[:len(a)-1], b...)
			a = append(a, r.i)
			r.c -= len(b)
		} else {//r.c < len(b)
			a = append(a[:len(a)-1], b[:r.c]...)
			a = append(a, r.i)
			a = append(a, b[r.c:]...)
			r.c = 0
		}
	}
	return a, true
}

package main

import (
	"fmt"
)

func main() {
	var n, p, c int
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&p, &c)
	}
	fmt.Println("NO")
}

package main

import (
	"fmt"
	"os"
)

const N = 2010
const M = N << 1
var tot = 0
var st [N]int
var nex, b [M]int
func addEdge(x, y int) {
	tot++
	b[tot] = y
	nex[tot] = st[x]
	st[x] = tot
}

var n int
var c, v [N]int
var used [N]bool
func fill(x int) {
	rest := c[x] + 1
	for i := 1; i <= n; i++ {
		if used[i] {
			continue
		}
		rest--
		if rest == 0 {
			v[x] = i
			used[i] = true
			break
		}
	}
	if rest > 0 {
		fmt.Println("NO")
		os.Exit(0)
	}
	for j := st[x]; j != 0; j = nex[j] {
		fill(b[j])
	}
}

func main() {
	var root int
	fmt.Scan(&n)
	for i := 1; i <= n; i++ {
		var tmp int
		fmt.Scan(&tmp, &c[i])
		if tmp == 0 {
			root = i
			continue
		}
		addEdge(tmp, i)
	}

	fill(root)
	fmt.Println("YES")
	for i := 1; i <= n; i++ {
		fmt.Printf("%d", v[i])
		if i != n {
			fmt.Print(" ")
		}
	}
	fmt.Print("\n")
}


Barney lives in country USC (United States of Charzeh). USC has n cities numbered from 1 through n and n - 1 roads between them. Cities and roads of USC form a rooted tree (Barney's not sure why it is rooted). Root of the tree is the city number 1. Thus if one will start his journey from city 1, he can visit any city he wants by following roads.  Some girl has stolen Barney's heart, and Barney wants to find her. He starts looking for in the root of the tree and (since he is Barney Stinson not a random guy), he uses a random DFS to search in the cities. A pseudo code of this algorithm is as follows:let starting_time be an array of length ncurrent_time = 0dfs(v):	current_time = current_time + 1	starting_time[v] = current_time	shuffle children[v] randomly (each permutation with equal possibility)	// children[v] is vector of children cities of city v	for u in children[v]:		dfs(u)As told before, Barney will start his journey in the root of the tree (equivalent to call dfs(1)).Now Barney needs to pack a backpack and so he wants to know more about his upcoming journey: for every city i, Barney wants to know the expected value of starting_time[i]. He's a friend of Jon Snow and knows nothing, that's why he asked for your help.

The first line of input contains a single integer n (1 ≤ n ≤ 105) — the number of cities in USC. The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi is the number of the parent city of city number i in the tree, meaning there is a road between cities numbered pi and i in USC.

In the first and only line of output print n numbers, where i-th number is the expected value of starting_time[i]. Your answer for each city will be considered correct if its absolute or relative error does not exceed 10 - 6.

package main
import (
	"fmt"
	"bufio"
	"os"
	)

var n int
var a [100005][]int
var b [100005]int
var A [100005]float64

func pre_compute(x int) {
    b[x] = 1
    for _,ch := range(a[x]) {
        pre_compute(ch)
        b[x] += b[ch]
    }
}

func dfs(x int, pp float64) {
    mn := pp+1
    mx := mn
    for _,ch := range(a[x]) {
        mx += float64(b[ch])
    }
    for _,ch := range(a[x]) {
        A[ch] = (mn+mx-float64(b[ch]))/2.0
        dfs(ch,A[ch])
    }
}

func main() {
	in := bufio.NewReader(os.Stdin)
    fmt.Fscan(in,&n);
    for i:=2;i<=n;i++ {
        x := 0
        fmt.Fscan(in,&x)
        a[x] = append(a[x],i)
    }
    pre_compute(1)
    A[1] = 1.0
    dfs(1,1.0)
    for i:=1;i<=n;i++ {
        fmt.Printf("%.1f ",A[i])
    }
}

package main

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

func main() {
	n := getInt()
	C := make([][]int, n+1)
	for i := 2; i <= n; i++ {
		p := getInt()
		C[p] = append(C[p], i)
	}
	S := make([]int, n+1)
	var dfsSize func(x int) int
	dfsSize = func(x int) int {
		S[x] = 1
		for _, v := range C[x] {
			S[x] += dfsSize(v)
		}
		return S[x]
	}
	dfsSize(1)
	T := make([]int64, n+1)
	var dfsTime func(x int, tp int64, sp int)
	dfsTime = func(x int, tp int64, sp int) {
		T[x] = tp + 2 + int64(sp-S[x]-1)
		for _, v := range C[x] {
			dfsTime(v, T[x], S[x])
		}
	}
	dfsTime(1, 0, S[1]+1)
	buf := &bytes.Buffer{}
	for i := 1; i <= n; i++ {
		if t := T[i]; t%2 == 0 {
			fmt.Fprintf(buf, "%v.0\n", t/2)
		} else {
			fmt.Fprintf(buf, "%v.5\n", t/2)
		}
	}
	io.Copy(os.Stdout, buf)
}

type Input struct {
	*bufio.Scanner
}

var globalInput = wrapStdin()

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func getInt() int {
	globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x
}
func getIntOk() (int, bool) {
	ok := globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x, ok
}
func getNint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = getInt()
	}
	return v
}
func getVint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = getIntOk()
	}
	return ok
}
func getWord() string {
	globalInput.Scan()
	return globalInput.Text()
}
func getWordOk() (string, bool) {
	ok := globalInput.Scan()
	return globalInput.Text(), ok
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}
func minStr(a, b string) string {
	if a < b {
		return a
	}
	return b
}
func maxStr(a, b string) string {
	if a < b {
		return b
	}
	return a
}
func remin(a *int, b int) bool {
	if b < *a {
		*a = b
		return true
	}
	return false
}
func remax(a *int, b int) bool {
	if b > *a {
		*a = b
		return true
	}
	return false
}
func remin64(a *int64, b int64) bool {
	if b < *a {
		*a = b
		return true
	}
	return false
}
func remax64(a *int64, b int64) bool {
	if b > *a {
		*a = b
		return true
	}
	return false
}
func reminStr(a *string, b string) bool {
	if b < *a {
		*a = b
		return true
	}
	return false
}
func remaxStr(a *string, b string) bool {
	if b > *a {
		*a = b
		return true
	}
	return false
}

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

type (
	FLess    func(i, j int) bool
	FSwap    func(i, j int)
	wrapSort struct {
		Length int
		FLess
		FSwap
	}
)

func (w *wrapSort) Len() int           { return w.Length }
func (w *wrapSort) Less(i, j int) bool { return w.FLess(i, j) }
func (w *wrapSort) Swap(i, j int)      { w.FSwap(i, j) }
func wsort(length int, fless func(i, j int) bool, fswap func(i, j int)) {
	sort.Sort(&wrapSort{length, fless, fswap})
}


The hero is addicted to glory, and is fighting against a monster. The hero has $$$n$$$ skills. The $$$i$$$-th skill is of type $$$a_i$$$ (either fire or frost) and has initial damage $$$b_i$$$. The hero can perform all of the $$$n$$$ skills in any order (with each skill performed exactly once). When performing each skill, the hero can play a magic as follows:   If the current skill immediately follows another skill of a different type, then its damage is doubled.  In other words,   If a skill of type fire and with initial damage $$$c$$$ is performed immediately after a skill of type fire, then it will deal $$$c$$$ damage;  If a skill of type fire and with initial damage $$$c$$$ is performed immediately after a skill of type frost, then it will deal $$$2c$$$ damage;  If a skill of type frost and with initial damage $$$c$$$ is performed immediately after a skill of type fire, then it will deal $$$2c$$$ damage;  If a skill of type frost and with initial damage $$$c$$$ is performed immediately after a skill of type frost , then it will deal $$$c$$$ damage. Your task is to find the maximum damage the hero can deal.

Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$) — the number of test cases. The following lines contain the description of each test case. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$), indicating the number of skills.  The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 1$$$), where $$$a_i$$$ indicates the type of the $$$i$$$-th skill. Specifically, the $$$i$$$-th skill is of type fire if $$$a_i = 0$$$, and of type frost if $$$a_i = 1$$$.  The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \leq b_i \leq 10^9$$$), where $$$b_i$$$ indicates the initial damage of the $$$i$$$-th skill.  It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, output the maximum damage the hero can deal.

NoteIn the first test case, we can order the skills by $$$[3, 1, 4, 2]$$$, and the total damage is $$$100 + 2 \times 1 + 2 \times 1000 + 10 = 2112$$$.In the second test case, we can order the skills by $$$[1, 4, 2, 5, 3, 6]$$$, and the total damage is $$$3 + 2 \times 6 + 2 \times 4 + 2 \times 7 + 2 \times 5 + 2 \times 8 = 63$$$.In the third test case, we can order the skills by $$$[1, 2, 3]$$$, and the total damage is $$$1000000000 + 1000000000 + 1000000000 = 3000000000$$$.In the fourth test case, there is only one skill with initial damage $$$1$$$, so the total damage is $$$1$$$.

package main

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

func gena(x, n int) (int, int) {
	if x == 0 || x == n {
		return 0, 0
	}
	y := n - x
	if x > y {
		return y, y
	}
	if x == y {
		return x - 1, y
	}
	return x - 1, x
}

func headsum(a []int64, n int) int64 {
	if n == 0 {
		return 0
	}
	return a[n-1]
}

func calheadsum(a []int64, n int) {
	for i := 1; i < n; i++ {
		a[i] += a[i-1]
	}
}

func main() {
	Fire := make([]int64, 1e5+50)
	Frost := make([]int64, 1e5+50)
	var t int
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	fmt.Fscan(in, &t)
	for ; t != 0; t-- {
		var n int
		fmt.Fscan(in, &n)
		a := make([]int, n)
		fireCount := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			if a[i] == 0 {
				fireCount++
			}
		}
		var fireIndex, frostIndex int
		var sum int64
		for i := 0; i < n; i++ {
			var tmp int64
			fmt.Fscan(in, &tmp)
			if a[i] == 0 {
				Fire[fireIndex] = tmp
				fireIndex++
			} else {
				Frost[frostIndex] = tmp
				frostIndex++
			}
			sum += tmp
		}
		fire := Fire[0:fireIndex]
		frost := Frost[0:frostIndex]
		sort.Slice(fire, func(i, j int) bool {
			return fire[i] > fire[j]
		})
		sort.Slice(frost, func(i, j int) bool {
			return frost[i] > frost[j]
		})
		calheadsum(fire, fireIndex)
		calheadsum(frost, frostIndex)
		x, y := gena(fireCount, n)
		res1 := headsum(fire, x) + headsum(frost, y)
		x, y = gena(n-fireCount, n)
		res2 := headsum(frost, x) + headsum(fire, y)
		if res1 > res2 {
			fmt.Fprintln(out, sum+res1)
		} else {
			fmt.Fprintln(out, sum+res2)
		}
	}
	out.Flush()
}

package main

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

const cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	fire := make([]int, 0, n)
	ice := make([]int, 0, n)
	a := make([]int, n)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	mn := int(1e10)
	var num int
	sum := 0
	for i := range a {
		fmt.Fscan(in, &num)
		if a[i] == 0 {
			fire = append(fire, num)
		} else {
			ice = append(ice, num)
		}
		sum += num
		mn = min(mn, num)
	}
	sort.Ints(fire)
	sort.Ints(ice)

	ans := sum

	if len(fire) > len(ice) {
		fire, ice = ice, fire
	}

	if len(fire) < len(ice) {
		for _, p := range fire {
			ans += p
		}
		start := len(ice) - 1
		for i := start; i >= start-len(fire)+1; i-- {
			ans += ice[i]
		}
	} else if len(fire) == len(ice) {
		ans += sum
		ans -= mn
	}

	fmt.Fprintln(out, ans)
}

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

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

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

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

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

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

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

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

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

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

var exists = struct{}{}

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

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

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

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

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

type Ordered interface {
	Integer | Float | ~string
}

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

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

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

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

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

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

package main

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

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

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

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

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

		for i := 0; i < n; i++ {
			var v int64
			fmt.Fscan(in, &v)

			if aa[i] == 0 {
				bb = append(bb, v)
			} else {
				cc = append(cc, v)
			}
		}

		sort.Slice(bb, func(i, j int) bool { return bb[i] < bb[j] })
		sort.Slice(cc, func(i, j int) bool { return cc[i] < cc[j] })

		d := min(len(bb), len(cc))
		for i := 0; i < d; i++ {
			bb[len(bb)-i-1] *= 2
			cc[len(cc)-i-1] *= 2
		}
		if d == len(bb) && d == len(cc) {
			if bb[0] <= cc[0] {
				bb[0] /= 2
			} else {
				cc[0] /= 2
			}
		}

		var tot int64
		for _, v := range bb {
			tot += v
		}
		for _, v := range cc {
			tot += v
		}
		fmt.Fprintln(out, tot)
	}
}

package main

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

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

func sum(a []int) int {
	res := 0
	for _, v := range a { res += v }
	return res
}

func solve(d1, d2 []int) int {
	if len(d1) == len(d2) {
		return 2 * (sum(d1) + sum(d2)) - min(d1[0], d2[0])
	} else {
		l := min(len(d1), len(d2))
		return sum(d1[:len(d1)-l]) + sum(d2[:len(d2)-l]) + 2 * (sum(d1[len(d1)-l:]) + sum(d2[len(d2)-l:]))
	}
}

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

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

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

		var dmg1, dmg2 [] int
		for _, i := range a {
			var dmg int
			fmt.Fscan(r, &dmg)
			if i == 1 {
				dmg2 = append(dmg2, dmg)
			} else {
				dmg1 = append(dmg1, dmg)
			}
		}

		sort.Ints(dmg1)
		sort.Ints(dmg2)

		fmt.Fprintln(w, solve(dmg1, dmg2))
	}

}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1738A
Date: 10/4/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1738/A
Problem: CF1738A
**/
func (in *CF1738A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		a := make([][]int, n)
		for i := 0; i < n; i++ {
			a[i] = make([]int, 2)
			a[i][0] = in.NextInt()
		}
		for i := 0; i < n; i++ {
			a[i][1] = in.NextInt()
		}

		sort.Slice(a, func(i, j int) bool {
			if a[i][1] < a[j][1] {
				return true
			}
			return false
		})
		zeros := make([][]int, 0)
		ones := make([][]int, 0)
		for i := 0; i < n; i++ {
			if a[i][0] == 0 {
				zeros = append(zeros, a[i])
			} else {
				ones = append(ones, a[i])
			}
		}

		sum := 0
		if len(zeros) == len(ones) {
			if zeros[0][1] < ones[0][1] {
				sum = zeros[0][1]
				for i := 1; i < len(zeros); i++ {
					sum += zeros[i][1] * 2
				}
				for i := 0; i < len(ones); i++ {
					sum += ones[i][1] * 2
				}
			} else {
				sum = ones[0][1]
				for i := 0; i < len(zeros); i++ {
					sum += zeros[i][1] * 2
				}
				for i := 1; i < len(ones); i++ {
					sum += ones[i][1] * 2
				}
			}
		} else if len(zeros) > len(ones) {
			for i := 0; i < len(zeros)-len(ones); i++ {
				sum += zeros[i][1]
			}
			for i := len(zeros) - len(ones); i < len(zeros); i++ {
				sum += zeros[i][1] * 2
			}
			for i := 0; i < len(ones); i++ {
				sum += ones[i][1] * 2
			}
		} else {
			for i := 0; i < len(ones)-len(zeros); i++ {
				sum += ones[i][1]
			}
			for i := len(ones) - len(zeros); i < len(ones); i++ {
				sum += ones[i][1] * 2
			}
			for i := 0; i < len(zeros); i++ {
				sum += zeros[i][1] * 2
			}
		}
		fmt.Println(sum)
	}
}

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

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

package main

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

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)
	fi := make([]int64, 0)
	fr := make([]int64, 0)
	a := make([]int, n)
	b := make([]int64, n)
	var s int64 = 0
	for i := 0; i < n; i++ {
		scan(&a[i])
	}
	for i := 0; i < n; i++ {
		scan(&b[i])
		s += b[i]
	}
	if n == 1 {
		printf("%d\n", b[0])
		return
	}
	for i := 0; i < n; i++ {
		if a[i] == 0 {
			fi = append(fi, b[i])
		} else {
			fr = append(fr, b[i])
		}
	}
	sort.Slice(fi, func(i, j int) bool {
		return fi[i] < fi[j]
	})
	sort.Slice(fr, func(i, j int) bool {
		return fr[i] < fr[j]
	})
	if len(fi) == 0 || len(fr) == 0 {
		printf("%d\n", s)
		return
	}
	m1 := len(fi)
	m2 := len(fr)
	var r int64 = 0
	i, j := len(fi)-1, len(fr)-1
	if (m1 > m2) || (m1 == m2 && fi[0] < fr[0]) {
		r += fi[0]
		// i++
		turn := 1
		for i > 0 || j >= 0 {
			// printf("%d %d %d\n", i, j, turn)
			if turn == 1 {
				if j >= 0 {
					r += 2 * fr[j]
					j--
					turn = 0
				} else {
					if i > 0 {
						r += fi[i]
						i--
					}
				}
			} else {
				if i > 0 {
					r += 2 * fi[i]
					turn = 1
					i--
				} else {
					if j >= 0 {
						r += fr[j]
						j--
					}
				}
			}
		}
	} else {
		r += fr[0]
		turn := 0
		for i >= 0 || j > 0 {
			// printf("%d %d %d\n", i, j, turn)
			if turn == 1 {
				if j > 0 {
					r += 2 * fr[j]
					j--
					turn = 0
				} else {
					if i >= 0 {
						r += fi[i]
						i--
					}
				}
			} else {
				if i >= 0 {
					r += 2 * fi[i]
					turn = 1
					i--
				} else {
					if j > 0 {
						r += fr[j]
						j--
					}
				}
			}
		}
	}
	printf("%d\n", r)
}
func main() {
	defer out.Flush()
	n := 0
	scan(&n)
	for i := 0; i < n; i++ {
		solve()
	}
}

package main

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

func gena(x, n int) (int, int) {
	if x == 0 || x == n {
		return 0, 0
	}
	y := n - x
	if x > y {
		return y, y
	}
	if x == y {
		return x - 1, y
	}
	return x - 1, x
}

func headsum(a []int64, n int) int64 {
	if n == 0 {
		return 0
	}
	return a[n-1]
}

func calheadsum(a []int64, n int) {
	for i := 1; i < n; i++ {
		a[i] += a[i-1]
	}
}

func main() {
	Fire := make([]int64, 1e5+50)
	Frost := make([]int64, 1e5+50)
	var t int
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	fmt.Fscan(in, &t)
	for ; t != 0; t-- {
		var n int
		fmt.Fscan(in, &n)
		a := make([]int, n)
		fireCount := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			if a[i] == 0 {
				fireCount++
			}
		}
		var fireIndex, frostIndex int
		var sum int64
		for i := 0; i < n; i++ {
			var tmp int64
			fmt.Fscan(in, &tmp)
			if a[i] == 0 {
				Fire[fireIndex] = tmp
				fireIndex++
			} else {
				Frost[frostIndex] = tmp
				frostIndex++
			}
			sum += tmp
		}
		fire := Fire[0:fireIndex]
		frost := Frost[0:frostIndex]
		sort.Slice(fire, func(i, j int) bool {
			return fire[i] > fire[j]
		})
		sort.Slice(frost, func(i, j int) bool {
			return frost[i] > frost[j]
		})
		calheadsum(fire, fireIndex)
		calheadsum(frost, frostIndex)
		x, y := gena(fireCount, n)
		res1 := headsum(fire, x) + headsum(frost, y)
		x, y = gena(n-fireCount, n)
		res2 := headsum(frost, x) + headsum(fire, y)
		if res1 > res2 {
			fmt.Fprintln(out, sum+res1)
		} else {
			fmt.Fprintln(out, sum+res2)
		}
	}
}

package main

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

const cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	fire := make([]int, 0, n)
	ice := make([]int, 0, n)
	a := make([]int, n)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	var num int
	for i := range a {
		fmt.Fscan(in, &num)
		if a[i] == 0 {
			fire = append(fire, num)
		} else {
			ice = append(ice, num)
		}
	}
	sort.Ints(fire)
	sort.Ints(ice)

	ans := 0

	check := func(curIsFire bool) {
		start := curIsFire

		prev := curIsFire
		curAns := 0
		if curIsFire {
			curAns += fire[0]
		} else {
			curAns += ice[0]
		}
		curIsFire = !curIsFire

		curFire, curIce := len(fire)-1, len(ice)-1
		for i := 0; i < n-1; i++ {
			if curIsFire {
				if (start == curIsFire && curFire == 0) || curFire < 0 {
					curIsFire = !curIsFire
					i--
					continue
				}
				if prev != curIsFire {
					curAns += fire[curFire]
				}
				curAns += fire[curFire]
				curFire--
				prev = curIsFire
			} else {
				if (start == curIsFire && curFire == 0) || curIce < 0 {
					curIsFire = !curIsFire
					i--
					continue
				}
				if prev != curIsFire {
					curAns += ice[curIce]
				}
				curAns += ice[curIce]
				curIce--
				prev = curIsFire
			}
			curIsFire = !curIsFire
		}

		ans = max(ans, curAns)
	}

	if len(fire) > 0 {
		check(true)
	}
	if len(ice) > 0 {
		check(false)
	}

	fmt.Fprintln(out, ans)
}

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

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

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

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

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

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

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

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

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

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

var exists = struct{}{}

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

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

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

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

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

type Ordered interface {
	Integer | Float | ~string
}

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

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

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

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

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

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

package main

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

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

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

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

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

		for i := 0; i < n; i++ {
			var v int64
			fmt.Fscan(in, &v)

			if aa[i] == 0 {
				bb = append(bb, v)
			} else {
				cc = append(cc, v)
			}
		}

		sort.Slice(bb, func(i, j int) bool { return bb[i] < bb[j] })
		sort.Slice(cc, func(i, j int) bool { return cc[i] < cc[j] })

		d := min(len(bb), len(cc))
		for i := 0; i < d; i++ {
			bb[len(bb)-i-1] *= 2
			cc[len(cc)-i-1] *= 2
		}
		if d == len(bb) && d == len(cc) {
			bb[0] /= 2
		}

		var tot int64
		for _, v := range bb {
			tot += v
		}
		for _, v := range cc {
			tot += v
		}
		fmt.Fprintln(out, tot)
	}
}


You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.Every element of the sequence must appear in exactly one subsequence.

The first line of input data contains integer n (1 ≤ n ≤ 105) — the length of the sequence. The second line of input data contains n different integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.

In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements. In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci ≤ n), then ci integers l1, l2, ..., lci (1 ≤ lj ≤ n) — indices of these elements in the original sequence.  Indices could be printed in any order. Every index from 1 to n must appear in output exactly once. If there are several possible answers, print any of them.

NoteIn the first sample output:After sorting the first subsequence we will get sequence 1 2 3 6 5 4.Sorting the second subsequence changes nothing.After sorting the third subsequence we will get sequence 1 2 3 4 5 6.Sorting the last subsequence changes nothing.

package main

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

type A struct {
	value int
	index int
}

func solve() {
	n := readInt()
	a := make([]A, n)
	for i := 0; i < n; i++ {
		a[i] = A{readInt(), i}
	}
	sort.Sort(ByValue(a))
	djset := NewDJSet(n)
	for i := 0; i < n; i++ {
		cur := a[i]
		with := a[cur.index]
		if with.index == i {
			djset.Union(with.index, cur.index)
		} else {
			djset.Union(i, cur.index)
		}
	}
	byroot := make([][]string, n)
	for i := 0; i < n; i++ {
		byroot[i] = make([]string, 0)
	}
	for i := 0; i < n; i++ {
		root := djset.Root(i)
		byroot[root] = append(byroot[root], strconv.Itoa(i+1))
	}
	fmt.Println(djset.Count())
	for i := 0; i < n; i++ {
		if len(byroot[i]) > 0 {
			fmt.Printf("%d %s\n", len(byroot[i]), strings.Join(byroot[i], " "))
		}
	}
}

type ByValue []A

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

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

type DJSet struct {
	upper []int
}

func NewDJSet(n int) *DJSet {
	s := DJSet{upper: make([]int, n)}
	for i := 0; i < n; i++ {
		s.upper[i] = -1
	}
	return &s
}

func (s *DJSet) Root(x int) int {
	if s.upper[x] < 0 {
		return x
	} else {
		s.upper[x] = s.Root(s.upper[x])
	}
	return s.upper[x]
}

func (s *DJSet) Union(x, y int) bool {
	x = s.Root(x)
	y = s.Root(y)
	if x != y {
		if s.upper[y] < s.upper[x] {
			x, y = y, x
		}
		s.upper[x] += s.upper[y]
		s.upper[y] = x
	}
	return x == y
}

func (s *DJSet) Equiv(x, y int) bool {
	return s.Root(x) == s.Root(y)
}

func (s *DJSet) Upper(y int) int {
	return s.upper[y]
}

func (s *DJSet) Count() int {
	ct := 0
	for _, u := range s.upper {
		if u < 0 {
			ct++
		}
	}
	return ct
}

// 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 int) int {
	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"
	"sort"
	"strconv"
	"strings"
)

type A struct {
	value int
	index int
}

func solve() {
	n := readInt()
	a := make([]A, n)
	for i := 0; i < n; i++ {
		a[i] = A{readInt(), i}
	}
	sort.Sort(ByValue(a))
	visited := make([]bool, n)

	res := make([]string, 0)
	djset := NewDJSet(n)
	for i := 0; i < n; i++ {
		cur := a[i]
		if cur.index == i {
			res = append(res, fmt.Sprintf("1 %d", cur.index+1))
			visited[i] = true
		}
		if visited[i] {
			continue
		}
		with := a[cur.index]
		if with.index == i {
			visited[i] = true
			visited[cur.index] = true
			res = append(res, fmt.Sprintf("2 %d %d", with.index+1, cur.index+1))
			djset.Union(with.index, cur.index)
		} else {
			djset.Union(i, cur.index)
		}
	}
	byroot := make([][]string, n)
	for i := 0; i < n; i++ {
		byroot[i] = make([]string, 0)
	}
	for i := 0; i < n; i++ {
		root := djset.Root(i)
		byroot[root] = append(byroot[root], strconv.Itoa(i+1))
	}
	fmt.Println(djset.Count())
	for i := 0; i < n; i++ {
		if len(byroot[i]) > 0 {
			fmt.Printf("%d %s\n", len(byroot[i]), strings.Join(byroot[i], " "))
		}
	}
}

type ByValue []A

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

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

type DJSet struct {
	upper []int
}

func NewDJSet(n int) *DJSet {
	s := DJSet{upper: make([]int, n)}
	for i := 0; i < n; i++ {
		s.upper[i] = -1
	}
	return &s
}

func (s *DJSet) Root(x int) int {
	if s.upper[x] < 0 {
		return x
	} else {
		s.upper[x] = s.Root(s.upper[x])
	}
	return s.upper[x]
}

func (s *DJSet) Union(x, y int) bool {
	x = s.Root(x)
	y = s.Root(y)
	if x != y {
		if s.upper[y] < s.upper[x] {
			x, y = y, x
		}
		s.upper[x] += s.upper[y]
		s.upper[y] = x
	}
	return x == y
}

func (s *DJSet) Equiv(x, y int) bool {
	return s.Root(x) == s.Root(y)
}

func (s *DJSet) Upper(y int) int {
	return s.upper[y]
}

func (s *DJSet) Count() int {
	ct := 0
	for _, u := range s.upper {
		if u < 0 {
			ct++
		}
	}
	return ct
}

// 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 int) int {
	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"
	"sort"
	"strconv"
	"strings"
)

type A struct {
	value int
	index int
}

func solve() {
	n := readInt()
	a := make([]A, n)
	for i := 0; i < n; i++ {
		a[i] = A{readInt(), i}
	}
	sort.Sort(ByValue(a))
	visited := make([]bool, n)

	res := make([]string, 0)
	for i := 0; i < n; i++ {
		cur := a[i]
		if cur.index == i {
			res = append(res, fmt.Sprintf("1 %d", cur.index+1))
			visited[i] = true
			continue
		}
		if visited[i] {
			continue
		}
		with := a[cur.index]
		if with.index == i {
			visited[i] = true
			visited[cur.index] = true
			res = append(res, fmt.Sprintf("2 %d %d", cur.index+1, with.index+1))
		}
	}
	indexes := make([]string, 0)
	for i := 0; i < n; i++ {
		if !visited[i] {
			indexes = append(indexes, fmt.Sprintf("%d", i+1))
		} else if len(indexes) > 0 {
			res = append(res, fmt.Sprintf("%d %s", len(indexes), strings.Join(indexes, " ")))
			indexes = make([]string, 0)
		}
	}
	if len(indexes) > 0 {
		res = append(res, fmt.Sprintf("%d %s", len(indexes), strings.Join(indexes, " ")))
	}
	fmt.Println(len(res))
	for i := 0; i < len(res); i++ {
		fmt.Println(res[i])
	}
}

type ByValue []A

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

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 int) int {
	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"
	"sort"
	"strconv"
	"strings"
)

type A struct {
	value int
	index int
}

func solve() {
	n := readInt()
	a := make([]A, n)
	for i := 0; i < n; i++ {
		a[i] = A{readInt(), i}
	}
	sort.Sort(ByValue(a))
	visited := make([]bool, n)

	res := make([]string, 0)
	for i := 0; i < n; i++ {
		cur := a[i]
		if cur.index == i {
			res = append(res, fmt.Sprintf("1 %d", cur.index+1))
			visited[i] = true
			continue
		}
		if visited[i] {
			continue
		}
		with := a[cur.index]
		if with.index == i {
			visited[i] = true
			visited[cur.index] = true
			res = append(res, fmt.Sprintf("2 %d %d", cur.index+1, with.index+1))
		}
	}
	indexes := make([]string, 0)
	for i := 0; i < n; i++ {
		if !visited[i] {
			indexes = append(indexes, fmt.Sprintf("%d", i+1))
		}
	}
	if len(indexes) > 0 {
		res = append(res, fmt.Sprintf("%d %s", len(indexes), strings.Join(indexes, " ")))
	}
	fmt.Println(len(res))
	for i := 0; i < len(res); i++ {
		fmt.Println(res[i])
	}
}

type ByValue []A

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

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 int) int {
	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"
	"sort"
	"strconv"
	"strings"
)

type A struct {
	value int
	index int
}

func solve() {
	n := readInt()
	a := make([]A, n)
	for i := 0; i < n; i++ {
		a[i] = A{readInt(), i}
	}
	sort.Sort(ByValue(a))
	visited := make([]bool, n)

	res := make([]string, 0)
	for i := 0; i < n; i++ {
		cur := a[i]
		if cur.index == i {
			res = append(res, fmt.Sprintf("1 %d", cur.index+1))
			visited[i] = true
			continue
		}
		if visited[i] {
			continue
		}
		with := a[cur.index]
		if with.index == i {
			visited[i] = true
			visited[cur.index] = true
			res = append(res, fmt.Sprintf("2 %d %d", with.index+1, cur.index+1))
		}
	}
	indexes := make([]string, 0)
	for i := 0; i < n; i++ {
		if !visited[i] {
			indexes = append(indexes, fmt.Sprintf("%d", i+1))
		} else if len(indexes) > 0 {
			res = append(res, fmt.Sprintf("%d %s", len(indexes), strings.Join(indexes, " ")))
			indexes = make([]string, 0)
		}
	}
	if len(indexes) > 0 {
		res = append(res, fmt.Sprintf("%d %s", len(indexes), strings.Join(indexes, " ")))
	}
	fmt.Println(len(res))
	for i := 0; i < len(res); i++ {
		fmt.Println(res[i])
	}
}

type ByValue []A

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

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 int) int {
	if a > b {
		return a
	}
	return b
}

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