You are given an array $$$a$$$ of length $$$n$$$, which initially is a permutation of numbers from $$$1$$$ to $$$n$$$. In one operation, you can choose an index $$$i$$$ ($$$1 \leq i < n$$$) such that $$$a_i < a_{i + 1}$$$, and remove either $$$a_i$$$ or $$$a_{i + 1}$$$ from the array (after the removal, the remaining parts are concatenated). For example, if you have the array $$$[1, 3, 2]$$$, you can choose $$$i = 1$$$ (since $$$a_1 = 1 < a_2 = 3$$$), then either remove $$$a_1$$$ which gives the new array $$$[3, 2]$$$, or remove $$$a_2$$$ which gives the new array $$$[1, 2]$$$.Is it possible to make the length of this array equal to $$$1$$$ with these operations?

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 2 \cdot 10^4$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 3 \cdot 10^5$$$)  — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \leq a_i \leq n$$$, $$$a_i$$$ are pairwise distinct) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$3 \cdot 10^5$$$.

For each test case, output on a single line the word "YES" if it is possible to reduce the array to a single element using the aforementioned operation, or "NO" if it is impossible to do so.

NoteFor the first two test cases and the fourth test case, we can operate as follow (the bolded elements are the pair chosen for that operation):$$$[\text{1}, \textbf{2}, \textbf{3}] \rightarrow [\textbf{1}, \textbf{2}] \rightarrow [\text{1}]$$$$$$[\text{3}, \textbf{1}, \textbf{2}, \text{4}] \rightarrow [\text{3}, \textbf{1}, \textbf{4}] \rightarrow [\textbf{3}, \textbf{4}] \rightarrow [\text{4}]$$$$$$[\textbf{2}, \textbf{4}, \text{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\textbf{4}, \textbf{6}, \text{1}, \text{3}, \text{5}] \rightarrow [\text{4}, \text{1}, \textbf{3}, \textbf{5}] \rightarrow [\text{4}, \textbf{1}, \textbf{5}] \rightarrow [\textbf{4}, \textbf{5}] \rightarrow [\text{4}]$$$

package main

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

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

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

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

var err error

func nextInt() (n int) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

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

func add(v []int, ind, x int) {
	for ; ind < len(v); ind += (-ind) & ind {
		v[ind] += x
	}
}

func read(v []int, ind int) (res int) {
	for ; ind > 0; ind -= (-ind) & ind {
		res += v[ind]
	}
	return res
}

func main() {
	out = bufio.NewWriterSize(os.Stdout, 8192)
	defer out.Flush()
	if false {
		file, err := os.Open("b.in")
		if err != nil {
			log.Fatal(err)
		}
		in = bufio.NewReaderSize(file, 8192)
	} else {
		in = bufio.NewReaderSize(os.Stdin, 8192)
	}

	t := nextInt()
	for ; t > 0; t-- {
		n := nextInt()
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			arr[i] = nextInt()
		}
		if arr[0] < arr[n-1] {
			printf("YES\n")
		} else {
			printf("NO\n")
		}
	}
}

package main

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

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

	tc := readNum(reader)

	var buf bytes.Buffer

	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		res := solve(n, A)
		if res {
			buf.WriteString("YES\n")
		} else {
			buf.WriteString("NO\n")
		}
	}
	fmt.Print(buf.String())
}

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

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

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

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

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

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

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

	return i
}

func solve(n int, A []int) bool {
	if n == 1 {
		return true
	}
	return A[0] < A[n-1]
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(in, &n)
		first := 0
		last := 0
		for j := 0; j < n; j++ {
			var a int
			fmt.Fscan(in, &a)
			if j == 0 {
				first = a
			}
			if j+1 == n {
				last = a
			}
		}
		if first < last {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

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

	var t int
	var n int
	var a [300005]int

	fmt.Fscan(input, &t)

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

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

		if a[0] < a[n-1] {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

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

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

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

var err error

func nextInt() (n int) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

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

func add(v []int, ind, x int) {
	for ; ind < len(v); ind += (-ind) & ind {
		v[ind] += x
	}
}

func read(v []int, ind int) (res int) {
	for ; ind > 0; ind -= (-ind) & ind {
		res += v[ind]
	}
	return res
}

func main() {
	out = bufio.NewWriterSize(os.Stdout, 8192)
	defer out.Flush()
	if false {
		file, err := os.Open("b.in")
		if err != nil {
			log.Fatal(err)
		}
		in = bufio.NewReaderSize(file, 8192)
	} else {
		in = bufio.NewReaderSize(os.Stdin, 8192)
	}

	t := nextInt()
outer:
	for ; t > 0; t-- {
		n := nextInt()
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			arr[i] = nextInt()
		}

		bit := make([]int, n+1)

		for i := n - 1; i > 0; i-- {
			add(bit, arr[i], 1)
			if read(bit, n-i) == n-i {
				printf("NO\n")
				continue outer
			}
		}
		printf("YES\n")
	}
}


Petya is the most responsible worker in the Research Institute. So he was asked to make a very important experiment: to melt the chocolate bar with a new laser device. The device consists of a rectangular field of n × m cells and a robotic arm. Each cell of the field is a 1 × 1 square. The robotic arm has two lasers pointed at the field perpendicularly to its surface. At any one time lasers are pointed at the centres of some two cells. Since the lasers are on the robotic hand, their movements are synchronized — if you move one of the lasers by a vector, another one moves by the same vector.The following facts about the experiment are known:   initially the whole field is covered with a chocolate bar of the size n × m, both lasers are located above the field and are active;  the chocolate melts within one cell of the field at which the laser is pointed;  all moves of the robotic arm should be parallel to the sides of the field, after each move the lasers should be pointed at the centres of some two cells;  at any one time both lasers should be pointed at the field. Petya doesn't want to become a second Gordon Freeman. You are given n, m and the cells (x1, y1) and (x2, y2), where the lasers are initially pointed at (xi is a column number, yi is a row number). Rows are numbered from 1 to m from top to bottom and columns are numbered from 1 to n from left to right. You are to find the amount of cells of the field on which the chocolate can't be melted in the given conditions.

The first line contains one integer number t (1 ≤ t ≤ 10000) — the number of test sets. Each of the following t lines describes one test set. Each line contains integer numbers n, m, x1, y1, x2, y2, separated by a space (2 ≤ n, m ≤ 109, 1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m). Cells (x1, y1) and (x2, y2) are distinct.

Each of the t lines of the output should contain the answer to the corresponding input test set.

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

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

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  t := getI()
  for ti := 0; ti < t; ti++ {
    w, h := getI64(), getI64()
    x1, y1, x2, y2 := getI64(), getI64(), getI64(), getI64()
    dx, dy := abs(x2-x1), abs(y2-y1)
    area := 2*dx*dy
    sx, sy := w - 2*dx, h - 2*dy
    if sx < 0 || sy < 0 {
      area -= sx*sy
    }
    writer.WriteString(fmt.Sprintf("%d\n", area))
  }
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

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

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  t := getI()
  for ti := 0; ti < t; ti++ {
    w, h, x1, y1, x2, y2 := getI(), getI(), getI(), getI(), getI(), getI()
    dx, dy := abs(x2-x1), abs(y2-y1)
    area := 2*dx*dy
    sx, sy := w - 2*dx, h - 2*dy
    if sx < 0 || sy < 0 {
      area -= sx*sy
    }
    writer.WriteString(fmt.Sprintf("%d\n", area))
  }
}


You a captain of a ship. Initially you are standing in a point $$$(x_1, y_1)$$$ (obviously, all positions in the sea can be described by cartesian plane) and you want to travel to a point $$$(x_2, y_2)$$$. You know the weather forecast — the string $$$s$$$ of length $$$n$$$, consisting only of letters U, D, L and R. The letter corresponds to a direction of wind. Moreover, the forecast is periodic, e.g. the first day wind blows to the side $$$s_1$$$, the second day — $$$s_2$$$, the $$$n$$$-th day — $$$s_n$$$ and $$$(n+1)$$$-th day — $$$s_1$$$ again and so on. Ship coordinates change the following way:  if wind blows the direction U, then the ship moves from $$$(x, y)$$$ to $$$(x, y + 1)$$$;  if wind blows the direction D, then the ship moves from $$$(x, y)$$$ to $$$(x, y - 1)$$$;  if wind blows the direction L, then the ship moves from $$$(x, y)$$$ to $$$(x - 1, y)$$$;  if wind blows the direction R, then the ship moves from $$$(x, y)$$$ to $$$(x + 1, y)$$$. The ship can also either go one of the four directions or stay in place each day. If it goes then it's exactly 1 unit of distance. Transpositions of the ship and the wind add up. If the ship stays in place, then only the direction of wind counts. For example, if wind blows the direction U and the ship moves the direction L, then from point $$$(x, y)$$$ it will move to the point $$$(x - 1, y + 1)$$$, and if it goes the direction U, then it will move to the point $$$(x, y + 2)$$$.You task is to determine the minimal number of days required for the ship to reach the point $$$(x_2, y_2)$$$.

The first line contains two integers $$$x_1, y_1$$$ ($$$0 \le x_1, y_1 \le 10^9$$$) — the initial coordinates of the ship. The second line contains two integers $$$x_2, y_2$$$ ($$$0 \le x_2, y_2 \le 10^9$$$) — the coordinates of the destination point. It is guaranteed that the initial coordinates and destination point coordinates are different. The third line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the string $$$s$$$. The fourth line contains the string $$$s$$$ itself, consisting only of letters U, D, L and R.

The only line should contain the minimal number of days required for the ship to reach the point $$$(x_2, y_2)$$$. If it's impossible then print "-1".

NoteIn the first example the ship should perform the following sequence of moves: "RRRRU". Then its coordinates will change accordingly: $$$(0, 0)$$$ $$$\rightarrow$$$ $$$(1, 1)$$$ $$$\rightarrow$$$ $$$(2, 2)$$$ $$$\rightarrow$$$ $$$(3, 3)$$$ $$$\rightarrow$$$ $$$(4, 4)$$$ $$$\rightarrow$$$ $$$(4, 6)$$$.In the second example the ship should perform the following sequence of moves: "DD" (the third day it should stay in place). Then its coordinates will change accordingly: $$$(0, 3)$$$ $$$\rightarrow$$$ $$$(0, 3)$$$ $$$\rightarrow$$$ $$$(0, 1)$$$ $$$\rightarrow$$$ $$$(0, 0)$$$.In the third example the ship can never reach the point $$$(0, 1)$$$.

package main

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

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

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

type p struct {
	x int64
	y int64
}

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

func (p *p) String() string {
	return fmt.Sprintf("x: %v; y: %v\n", p.x, p.y)
}

func main() {
	//STDOUT MUST BE FLUSHED MANUALLY
	defer writer.Flush()
	var n int64
	start := p{0, 0}
	end := p{0, 0}
	scanf("%d %d\n", &start.x, &start.y)
	scanf("%d %d\n", &end.x, &end.y)
	scanf("%d\n", &n)
	var forecast string
	scanf("%s\n", &forecast)
	move := make([]p, n+1)
	cur := p{0, 0} // cur is endpoint where ship is going to be if it stays still
	move[0] = cur
	for i, c := range forecast {
		switch c {
		case 'U':
			cur.y++
		case 'D':
			cur.y--
		case 'L':
			cur.x--
		case 'R':
			cur.x++
		}
		move[i+1] = cur
	}
	// printf("move: %+v\n", move)
	// printf("end: %+v\n", end)
	// Let's use binary search. K is amount of days used for journey
	// To get to finish point we need to move for atleas 1 time close
	// to endpoint. Max manchaten distance is 2 * 10*9, max weather forecast
	// length is 10^5, hence upper bound is 2 * 10 ^ 14.
	// lower bound is 0
	// To check what (x2, y2) is accesible, lets calculate (x3, y3)
	// k value for (x3,y3) found with binary search, and ship stays still
	// if |x2 - x3| + |y2 - y3| <= k then (x2,y2) is accesible
	// To calculate (x3,y3) we know what k/n cycles past and k % n days of new cicle
	// Whats O(1) using move array above

	var x, y, dist int64
	l, r := int64(0), int64(1e18)
	for (r - l) > 1 {
		mid := (int64(l+r) / 2)
		cnt, rem := mid/n, mid%n
		x = start.x + move[rem].x + cnt*move[n].x
		y = start.y + move[rem].y + cnt*move[n].y
		dist = abs(x-end.x) + abs(y-end.y)
		if dist <= mid {
			r = mid
		} else {
			l = mid
		}
	}

	if r > 5e17 {
		r = int64(-1)
	}

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

package main

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

func abs(x int64) int64{
	if x < 0 {
		return -x
	} else {
		return x
	}
}
var dx, dy int64
var x1, x2, y1, y2, n int64
var px, py [100001]int64

func search(day, left, right int) int {
	if left > right {
		return -1
	}
	mid := (left + right) / 2
	x, y := int64(mid) * dx + px[day], int64(mid) * dy + py[day]
	if abs(x - x2) + abs(y - y2) <= int64(day) + int64(mid) * n {
		ans := search(day, left, mid - 1)
		if ans == -1 {
			return mid
		} else {
			return ans
		}
	} else {
		return search(day, mid + 1, right)
	}
}

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

	var s string
	fmt.Fscanf(reader, "%d %d\n", &x1, &y1)
	fmt.Fscanf(reader, "%d %d\n", &x2, &y2)
	fmt.Fscanf(reader, "%d\n", &n)
	fmt.Fscanf(reader, "%s", &s)
	px[0], py[0] = x1, y1
	for i := 0;i < int(n);i ++ {
		switch s[i] {
		case 'U':
			px[i + 1], py[i + 1] = px[i], py[i] + 1
		case 'D':
			px[i + 1], py[i + 1] = px[i], py[i] - 1
		case 'L':
			px[i + 1], py[i + 1] = px[i] - 1, py[i]
		case 'R':
			px[i + 1], py[i + 1] = px[i] + 1, py[i]
		}
	}
	dx, dy = px[n] - px[0], py[n] - py[0]
	var ans int64 = 1e16
	for i := 0;i < int(n);i ++ {
		cycle := search(i, 0, 1000000000)
		if cycle == -1 {
			continue
		}
		newAns := int64(i) + int64(cycle) * n
		if ans > newAns {
			ans = newAns
		}
	}
	if ans == 1e16 {
		fmt.Printf("-1")
	} else {
		fmt.Printf("%d", ans)
	}
}

package main

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

var (
	sumx   []int64
	sumy   []int64
	x0, y0 int64
	x1, y1 int64
)

func getD(day int64, n int64) (int64, int64) {
	if day <= n {
		return sumx[day-1], sumy[day-1]
	}
	d := day / n
	l := day - d*n
	if l > 0 {
		return sumx[n-1]*d + sumx[l-1], sumy[n-1]*d + sumy[l-1]
	} else {
		return sumx[n-1] * d, sumy[n-1] * d
	}

}
func cabs(a, b int64) int64 {
	a -= b
	if a < 0 {
		a = 0 - a
	}
	return a
}
func solveC(day, n int64) bool {
	dx, dy := getD(day, n)
	xmid := x0 + dx
	ymid := y0 + dy

	if day >= cabs(xmid, x1)+cabs(ymid, y1) {
		return true
	} else {
		return false
	}
}
func bsearchC(l, r, n int64) int64 {
	var ans int64 = -1
	for l <= r {
		mid := ((l + r) >> 1)
		if solveC(mid, n) {
			ans = mid
			r = mid - 1
		} else {
			l = mid + 1
		}
	}
	return ans
}
func main() {
	buf := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanf(buf, "%d %d\n", &x0, &y0)
	fmt.Fscanf(buf, "%d %d\n", &x1, &y1)
	fmt.Fscanf(buf, "%d\n", &n)
	str := make([]byte, n+1)
	fmt.Fscanf(buf, "%s\n", &str)
//	fmt.Printf("s==%s\n",str)
	sumx = make([]int64,n)
	sumy = make([]int64,n)
	var prex, prey int64
	for i := 0; i < n; i++ {
		if str[i] == 'U' {
			sumx[i] = prex
			sumy[i] = prey + 1
		} else if str[i] == 'D' {
			sumx[i] = prex
			sumy[i] = prey - 1

		} else if str[i] == 'L' {
			sumx[i] = prex - 1
			sumy[i] = prey
		} else if str[i] == 'R' {
			sumx[i] = prex + 1
			sumy[i] = prey
		}
		prex = sumx[i]
		prey = sumy[i]
	}
	var maxx int64 = 1000000000
	fmt.Printf("%d\n", bsearchC(int64(1), int64(100000)*maxx, int64(n)))
}
/**
0 0
0 1
3
UDD
 */

package main

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

func abs(x int64) int64{
	if x < 0 {
		return -x
	} else {
		return x
	}
}
var dx, dy int64
var x1, x2, y1, y2, n int64
var px, py [100001]int64

func search(day, left, right int) int {
	if left > right {
		return -1
	}
	mid := (left + right) / 2
	x, y := int64(mid) * dx + px[day], int64(mid) * dy + py[day]
	if abs(x - x2) + abs(y - y2) <= int64(day) + int64(mid) * n {
		ans := search(day, left, mid - 1)
		if ans == -1 {
			return mid
		} else {
			return ans
		}
	} else {
		return search(day, mid + 1, right)
	}
}

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

	var s string
	fmt.Fscanf(reader, "%d %d\n", &x1, &y1)
	fmt.Fscanf(reader, "%d %d\n", &x2, &y2)
	fmt.Fscanf(reader, "%d\n", &n)
	fmt.Fscanf(reader, "%s", &s)
	px[0], py[0] = x1, y1
	for i := 0;i < int(n);i ++ {
		switch s[i] {
		case 'U':
			px[i + 1], py[i + 1] = px[i], py[i] + 1
		case 'D':
			px[i + 1], py[i + 1] = px[i], py[i] - 1
		case 'L':
			px[i + 1], py[i + 1] = px[i] - 1, py[i]
		case 'R':
			px[i + 1], py[i + 1] = px[i] + 1, py[i]
		}
	}
	dx, dy = px[n] - px[0], py[n] - py[0]
	if (abs(x1 + dx - x2) + abs(y1 + dy - y2)) - (abs(x1 - x2) + abs(y1 - y2)) == n {
		fmt.Printf("-1")
		return
	}
	var ans int64 = 1e16
	for i := 0;i < int(n);i ++ {
		cycle := search(i, 0, 1000000000)
		newAns := int64(i) + int64(cycle) * n
		if ans > newAns {
			ans = newAns
		}
	}
	fmt.Printf("%d", ans)
}

package main

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

var (
	sumx   []int64
	sumy   []int64
	x0, y0 int64
	x1, y1 int64
)

func getD(day int64, n int64) (int64, int64) {
	if day <= n {
		return sumx[day-1], sumy[day-1]
	}
	d := day / n
	l := day - d*n
	if l > 0 {
		return sumx[n-1]*d + sumx[l-1], sumy[n-1]*d + sumy[l-1]
	} else {
		return sumx[n-1] * d, sumy[n-1] * d
	}

}
func cabs(a, b int64) int64 {
	a -= b
	if a < 0 {
		a = 0 - a
	}
	return a
}
func solveC(day, n int64) bool {
	dx, dy := getD(day, n)
	xmid := x0 + dx
	ymid := y0 + dy
	if day >= cabs(xmid, x1)+cabs(ymid, y1) {
		return true
	} else {
		return false
	}
}
func bsearchC(l, r, n int64) int64 {
	var ans int64 = -1
	for l < r {
		mid := ((l + r) >> 1)
		if solveC(mid, n) {
			ans = mid
			r = mid - 1
		} else {
			l = mid + 1
		}
	}
	return ans
}
func main() {
	buf := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanf(buf, "%d %d\n", &x0, &y0)
	fmt.Fscanf(buf, "%d %d\n", &x1, &y1)
	fmt.Fscanf(buf, "%d\n", &n)
	str := make([]byte, n+1)
	fmt.Fscanf(buf, "%s\n", &str)
//	fmt.Printf("s==%s\n",str)
	sumx = make([]int64,n)
	sumy = make([]int64,n)
	var prex, prey int64
	for i := 0; i < n; i++ {
		if str[i] == 'U' {
			sumx[i] = prex
			sumy[i] = prey + 1
		} else if str[i] == 'D' {
			sumx[i] = prex
			sumy[i] = prey - 1

		} else if str[i] == 'L' {
			sumx[i] = prex - 1
			sumy[i] = prey
		} else if str[i] == 'R' {
			sumx[i] = prex + 1
			sumy[i] = prey
		}
		prex = sumx[i]
		prey = sumy[i]
	}
	var maxx int64 = 1000000000
	fmt.Printf("%d\n", bsearchC(int64(1), int64(100000)*maxx, int64(n)))
}


A robot cleaner is placed on the floor of a rectangle room, surrounded by walls. The floor consists of $$$n$$$ rows and $$$m$$$ columns. The rows of the floor are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and columns of the floor are numbered from $$$1$$$ to $$$m$$$ from left to right. The cell on the intersection of the $$$r$$$-th row and the $$$c$$$-th column is denoted as $$$(r,c)$$$. The initial position of the robot is $$$(r_b, c_b)$$$.In one second, the robot moves by $$$dr$$$ rows and $$$dc$$$ columns, that is, after one second, the robot moves from the cell $$$(r, c)$$$ to $$$(r + dr, c + dc)$$$. Initially $$$dr = 1$$$, $$$dc = 1$$$. If there is a vertical wall (the left or the right walls) in the movement direction, $$$dc$$$ is reflected before the movement, so the new value of $$$dc$$$ is $$$-dc$$$. And if there is a horizontal wall (the upper or lower walls), $$$dr$$$ is reflected before the movement, so the new value of $$$dr$$$ is $$$-dr$$$.Each second (including the moment before the robot starts moving), the robot cleans every cell lying in the same row or the same column as its position. There is only one dirty cell at $$$(r_d, c_d)$$$. The job of the robot is to clean that dirty cell.  Illustration for the first example. The blue arc is the robot. The red star is the target dirty cell. Each second the robot cleans a row and a column, denoted by yellow stripes. Given the floor size $$$n$$$ and $$$m$$$, the robot's initial position $$$(r_b, c_b)$$$ and the dirty cell's position $$$(r_d, c_d)$$$, find the time for the robot to do its job.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. A test case consists of only one line, containing six integers $$$n$$$, $$$m$$$, $$$r_b$$$, $$$c_b$$$, $$$r_d$$$, and $$$c_d$$$ ($$$1 \le n, m \le 100$$$, $$$1 \le r_b, r_d \le n$$$, $$$1 \le c_b, c_d \le m$$$) — the sizes of the room, the initial position of the robot and the position of the dirt cell.

For each test case, print an integer — the time for the robot to clean the dirty cell. We can show that the robot always cleans the dirty cell eventually.

NoteIn the first example, the floor has the size of $$$10\times 10$$$. The initial position of the robot is $$$(6, 1)$$$ and the position of the dirty cell is $$$(2, 8)$$$. See the illustration of this example in the problem statement.In the second example, the floor is the same, but the initial position of the robot is now $$$(9, 9)$$$, and the position of the dirty cell is $$$(1, 1)$$$. In this example, the robot went straight to the dirty cell and clean it.   In the third example, the floor has the size $$$9 \times 8$$$. The initial position of the robot is $$$(5, 6)$$$, and the position of the dirty cell is $$$(2, 1)$$$.   In the fourth example, the floor has the size $$$6 \times 9$$$. The initial position of the robot is $$$(2, 2)$$$ and the position of the dirty cell is $$$(5, 8)$$$.   In the last example, the robot was already standing in the same column as the dirty cell, so it can clean the cell right away.

package main

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

type IO interface {
	GetInt() int
	GetChar() byte
	GetString() string
}

type FastIO struct {
	IO
	in *bufio.Reader
}

func (t *FastIO) GetInt() (res int) {
	p := 1
	c, _ := t.in.ReadByte()

	for ; c < '0' || c > '9'; c, _ = t.in.ReadByte() {
	}
	// No comment...
	res = 0
	if c == '-' {
		p = -1
	} else {
		res = int(c - '0')
	}

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

	res *= p
	return
}

func solve(fast FastIO) {
	var n, m, i1, j1, i2, j2 int
	n = fast.GetInt()
	m = fast.GetInt()
	i1 = fast.GetInt()
	j1 = fast.GetInt()
	i2 = fast.GetInt()
	j2 = fast.GetInt()

	var (
		a int
		b int
	)

	if i2 >= i1 {
		a = i2 - i1
	} else {
		a = (n-i1)*2 + (i1 - i2)
	}

	if j2 >= j1 {
		b = (j2 - j1)
	} else {
		b = (m-j1)*2 + (j1 - j2)
	}

	fmt.Print(math.Min(float64(a), float64(b)), " ")
}

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

	var t int

	fmt.Scan(&t)
	for ; t > 0; t-- {
		solve(fast)
	}
}

package main

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

type IO interface {
	GetInt() int
	GetChar() byte
	GetString() string
}

type FastIO struct {
	IO
	in *bufio.Reader
}

func (t *FastIO) GetInt() (res int) {
	p := 1
	c, _ := t.in.ReadByte()

	for ; c < '0' || c > '9'; c, _ = t.in.ReadByte() {
	}

	res = 0
	if c == '-' {
		p = -1
	} else {
		res = int(c - '0')
	}

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

	res *= p
	return
}

func solve(fast FastIO) {
	var n, m, i1, j1, i2, j2 int
	n = fast.GetInt()
	m = fast.GetInt()
	i1 = fast.GetInt()
	j1 = fast.GetInt()
	i2 = fast.GetInt()
	j2 = fast.GetInt()

	var (
		a int
		b int
	)

	if i2 >= i1 {
		a = i2 - i1
	} else {
		a = (n-i1)*2 + (i1 - i2)
	}

	if j2 >= j1 {
		b = (j2 - j1)
	} else {
		b = (m-j1)*2 + (j1 - j2)
	}

	fmt.Println(math.Min(float64(a), float64(b)))
}

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

	var t int

	fmt.Scan(&t)
	for ; t > 0; t-- {
		solve(fast)
	}
}

package main

import (
	"fmt"
	"math"
)

func solve() {
	var n, m, i1, j1, i2, j2 int
	fmt.Scan(&n)
	fmt.Scan(&m)
	fmt.Scan(&i1)
	fmt.Scan(&j1)
	fmt.Scan(&i2)
	fmt.Scan(&j2)

	var (
		a int
		b int
	)

	if i2 >= i1 {
		a = i2 - i1
	} else {
		a = (n-i1)*2 + (i1 - i2)
	}

	if j2 >= j1 {
		b = (j2 - j1)
	} else {
		b = (m-j1)*2 + (j1 - j2)
	}

	fmt.Println(math.Min(float64(a), float64(b)))
}

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

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

package main

import (
	"fmt"
	"math"
)

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for ; t > 0; t-- {
		var n, m, i1, j1, i2, j2 int
		fmt.Scanf("%d %d %d %d %d %d\n", &n, &m, &i1, &j1, &i2, &j2)

		var (
			a int
			b int
		)

		if i2 >= i1 {
			a = i2 - i1
		} else {
			a = (n-i1)*2 + (i1 - i2)
		}

		if j2 >= j1 {
			b = (j2 - j1)
		} else {
			b = (m-j1)*2 + (j1 - j2)
		}

		fmt.Print(math.Min(float64(a), float64(b)), " ")
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var t int
	fmt.Scan(&t)
	for ; t > 0; t-- {
		var n, m, i1, j1, i2, j2 int
		fmt.Scan(&n, &m, &i1, &j1, &i2, &j2)

		var (
			a int
			b int
		)

		if i2 >= i1 {
			a = i2 - i1
		} else {
			a = (n-i1)*2 + (i1 - i2)
		}

		if j2 >= j1 {
			b = (j2 - j1)
		} else {
			b = (m-j1)*2 + (j1 - j2)
		}

		fmt.Print(math.Min(float64(a), float64(b)), " ")
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var t int
	fmt.Scan(&t)
	for ; t > 0; t-- {
		var n, m, i1, j1, i2, j2 int
		fmt.Scan(&n, &m, &i1, &j1, &i2, &j2)

		var (
			a int
			b int
		)

		if i2 >= i1 {
			a = i2 - i1
		} else {
			a = (n-i1)*2 + (i1 - i2)
		}

		if j2 >= j1 {
			b = (j2 - j1)
		} else {
			b = (m-j1)*2 + (j1 - j2)
		}

		fmt.Println(math.Min(float64(a), float64(b)))
	}
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n, m, rb, cb, rd, cd := 0, 0, 0, 0, 0, 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n, &m, &rb, &cb, &rd, &cd)
		tot, dr, dc := 0, 1, 1
		for {
			if rb == rd || cb == cd {
				break
			}
			if !(1 <= rb+dr && rb+dr <= n) {
				dr = -dr
			}
			if !(1 <= cb+dc && cb+dc <= m) {
				dc = -dc
			}
			rb, cb, tot = rb+dr, cb+dc, tot+1
		}
		fmt.Fprintln(w, tot)
	}
	w.Flush()
}

func main() {
	solve()
}

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var y,x,ys,xs,yf,xf int
		fmt.Fscan(in,&y,&x,&ys,&xs,&yf,&xf,)
		if yf == ys || xf == xs {
			fmt.Fprintln(out,0)
			continue
		}
		var r,c int
		if yf > ys {
			r = yf-ys
		} else {
			r = 2*y-yf-ys
		}
		if xf > xs {
			c = xf-xs
		} else {
			c = 2*x-xs-xf
		}
		if r > c {
			fmt.Fprintln(out,c)
		} else {
			fmt.Fprintln(out,r)
		}
	}
}

package main

import (
	"fmt"
)

func oneDim(s, e, w int) int {
	if e >= s {
		return e - s
	} else {
		return w - s + w - e
	}
}
func min(a, b int) int {
	if a < b {
		return a
	} else {
		return b
	}
}
func Solve(n, m, rb, cb, rd, cd int) int {
	r := oneDim(rb, rd, n)
	c := oneDim(cb, cd, m)
	return min(r, c)
}
func main() {
	n := 0
	_, err := fmt.Scanf("%d\n", &n)
	if err != nil {
		fmt.Println("error:", err)
	}
	for i := 0; i < n; i++ {
		var n, m, rb, rd, cd, cb int
		_, err = fmt.Scanf("%d %d %d %d %d %d\n", &n, &m, &rb, &cb, &rd, &cd)
		if err != nil {
			fmt.Println(err)
		}
		fmt.Println(Solve(n, m, rb, cb, rd, cd))
	}
}

package main

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

func CF1623A(rawIn io.Reader, rawOut io.Writer) {
	in := bufio.NewReader(rawIn)
	out := bufio.NewWriter(rawOut)
	defer out.Flush()

	var cases int
	fmt.Fscan(in, &cases)
	for cc := 0; cc < cases; cc++ {
		var n, m, rb, cb, rd, cd int
		fmt.Fscan(in, &n, &m, &rb, &cb, &rd, &cd)

		rowD := calculateLineDist(rd, rb, n)
		colD := calculateLineDist(cd, cb, m)

		if rowD < colD {
			fmt.Fprintf(out, "%d\n", rowD)
		} else {
			fmt.Fprintf(out, "%d\n", colD)
		}
	}
}

func calculateLineDist(target int, init int, limit int) int {
	if target >= init {
		return target - init
	} else {
		return (limit - init) + (limit - target)
	}
}

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

package main

import (
	"fmt"
)

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

func main() {
	var t, n, m, rb, cb, rd, cd int
	fmt.Scanln(&t)
	for i := 0; i < t; i++ {
		fmt.Scanln(&n, &m, &rb, &cb, &rd, &cd)
		ans := 0
		if rd < rb {
			ans = n - rb + n - rd
		} else {
			ans = rd - rb
		}

		if cd < cb {
			ans = min(ans, m-cb+m-cd)
		} else {
			ans = min(ans, cd-cb)
		}
		fmt.Println(ans)
	}

}

package main

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

func main() {
	//file, _ := os.Open("input.txt")
	//in := bufio.NewReader(file)
	in := bufio.NewReader(os.Stdin)

	input1 := readArrInt(in)
	t := input1[0]
	for ; t != 0; t-- {
		input := readArrInt(in)
		n, m, y1, x1, y2, x2 := input[0], input[1], input[2], input[3], input[4], input[5]
		x, y := true, true
		ans := 0
		for {
			if y1 == y2 || x1 == x2 {
				break
			}
			ans++
			if m != 1 {
				if x && x1 == m {
					x = false
				} else if !x && x1 == 1 {
					x = true
				}
				if x {
					x1++
				} else {
					x1--
				}
			}
			if n != 1 {
				if y && y1 == n {
					y = false
				} else if !y && y1 == 1 {
					y = true
				}
				if y {
					y1++
				} else {
					y1--
				}
			}
		}
		fmt.Println(ans)
	}
}

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

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

func readFloat(in *bufio.Reader) float64 {
	nStr := readStr(in)
	n, _ := strconv.ParseFloat(nStr, 64)
	return n
}

func readInt(in *bufio.Reader) int {
	nStr := readStr(in)
	n, _ := strconv.Atoi(nStr)
	return n
}

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

func readLineNumbs(in *bufio.Reader) []string {
	line := readStr(in)
	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 (
	"bufio"
	"fmt"
	"os"
)

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

var h int
var w int
var targetx int
var targety int

func solve(x, y int) int {
	var dx = 1
	var dy = 1
	var ans = 0

	for x != targetx && y != targety {
		if x+dx < 1 || x+dx > h {
			dx = -dx
		}
		if y+dy < 1 || y+dy > w {
			dy = -dy
		}
		x += dx
		y += dy
		ans++
	}
	return ans
}

func main() {
	var ntc int
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t++ {
		var initx int
		var inity int
		fmt.Fscan(in, &h, &w, &initx, &inity, &targetx, &targety)
		fmt.Fprintf(out, "%d\n", solve(initx, inity))
	}
	out.Flush()
}

package main

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

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

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

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

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

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

/**
https://codeforces.com/problemset/problem/1623/A
Date: 22/03/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1623/A
Problem: CF1623A
**/
func (in *CF1623A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, m, ix, iy, fx, fy := in.NextInt(), in.NextInt(), in.NextInt(), in.NextInt(), in.NextInt(), in.NextInt()
		ans := 0
		dx, dy := 1, 1
		for ix != fx && iy != fy {

			if ix+dx > n {
				dx *= -1
			}

			if ix+dx < 1 {
				dx *= -1
			}

			if iy+dy > m {
				dy *= -1
			}

			if iy+dy < 1 {
				dy *= -1
			}

			ix += dx
			iy += dy
			ans++

			if ix == fx {
				break
			}
			if iy == fy {
				break
			}

		}

		fmt.Println(ans)
	}
}

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

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

package main

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

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

	tc := readNum(reader)
	var buf bytes.Buffer

	for tc > 0 {
		tc--
		in := readNNums(reader, 6)
		res := solve(in[0], in[1], in[2], in[3], in[4], in[5])
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}

	fmt.Print(buf.String())
}

func normalize(s string) string {

	for i := len(s); i > 0; i-- {
		if s[i-1] >= 'a' && s[i-1] <= 'z' {
			return s[:i]
		}
	}
	return ""
}

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

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

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

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

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

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

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

	return i
}

func solve(n int, m int, rb int, cb int, rd int, cd int) int {

	var res int
	dr, dc := 1, 1

	for rb != rd && cb != cd {
		res++

		if rb == n && dr == 1 {
			dr = -1
		}
		if rb == 1 && dr == -1 {
			dr = 1
		}

		if cb == m && dc == 1 {
			dc = -1
		}
		if cb == 1 && dc == -1 {
			dc = 1
		}

		rb += dr
		cb += dc
	}

	return res
}

package main

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

const (
	Inf = 0x3f3f3f3f
)

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

var (
	T                    int
	n, m, x1, y1, x2, y2 int
)

func solveOne(a, b, n int) int {
	if a <= b {
		return b - a
	}
	return a - b + 2*(n-a)
}

func solve() int {
	a, b := solveOne(x1, x2, n), solveOne(y1, y2, m)
	if a > b {
		return b
	}
	return a
}

func Init() {
}

func main() {
	_, _ = fmt.Fscan(rr, &T)
	for iCase := 1; iCase <= T; iCase++ {
		_, _ = fmt.Fscan(rr, &n, &m, &x1, &y1, &x2, &y2)

		Init()
		_, _ = fmt.Fprintln(ww, solve())
	}
	_ = ww.Flush()
}

package main

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n, m, rRow, rCol, dRow, dCol int
		fmt.Fscan(in, &n, &m, &rRow, &rCol, &dRow, &dCol)
		dr, dc := 1, 1
		var secs int
		for rRow != dRow && rCol != dCol {
			if rRow == n {
				dr = -1
			}

			if rRow == 1 {
				dr = 1
			}

			if rCol == m {
				dc = -1
			}

			if rCol == 1 {
				dc = 1
			}

			rRow += dr
			rCol += dc
			secs++
		}
		fmt.Fprintln(out, secs)
	}
	out.Flush()
}

package main

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

var (
	reader                  = bufio.NewReader(os.Stdin)
	t, n, m, rb, cb, rd, cd int
)

func solve(n, m, rb, cb, rd, cd int) int {
	var c, r int
	if rd >= rb {
		r = rd - rb
	} else {
		r = 2*n - rd - rb
	}
	if cd >= cb {
		c = cd - cb
	} else {
		c = 2*m - cb - cd
	}
	if r > c {
		return c
	}
	return r
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n, &m, &rb, &cb, &rd, &cd)
		fmt.Println(solve(n, m, rb, cb, rd, cd))
	}
}

package main

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

func main() {
	reader := bReader(os.Stdin)
	writer := bWriter(os.Stdout)

	t := readInt(reader)

	for t > 0 {
		a := readArrayInt(reader)
		n, m, r_b, c_b, r_d, c_d := a[0], a[1], a[2], a[3], a[4], a[5]

		x, y := r_d - r_b, c_d - c_b
		if x < 0 {
			x = 2*n - r_d - r_b
		}

		if y < 0 {
			y = 2*m - c_d - c_b
		}

		if x < y {
			fmt.Fprintln(writer, x)
		} else {
			fmt.Fprintln(writer, y)
		}

		t--
	}

	writer.Flush()
}

func bReader(stdin *os.File) *bufio.Reader {
	return bufio.NewReaderSize(stdin, 16*1024*1024)
}

func bWriter(stdout *os.File) *bufio.Writer {
	return bufio.NewWriterSize(stdout, 16*1024*1024)
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func readInt(reader *bufio.Reader) int {
	return int(readInt64(reader))
}

func readInt64(reader *bufio.Reader) int64 {
	value, err := strconv.ParseInt(readLine(reader), 10, 64)
	checkError(err)

	return value
}

func readString(reader *bufio.Reader) string {
	return readLine(reader)
}

func readArrayString(reader *bufio.Reader) []string {
	return strings.Split(strings.TrimSpace(readLine(reader)), " ")
}

func readArrayInt(reader *bufio.Reader) []int {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := int(arItemTemp)
		ar = append(ar, arItem)
	}

	return ar
}

func readArrayInt64(reader *bufio.Reader) []int64 {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int64

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := arItemTemp
		ar = append(ar, arItem)
	}

	return ar
}

func readTwoInt(reader *bufio.Reader) (int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1]
}

func readTwoInt64(reader *bufio.Reader) (int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1]
}

func readThreeInt(reader *bufio.Reader) (int, int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1], ar[2]
}

func readThreeInt64(reader *bufio.Reader) (int64, int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1], ar[2]
}

package main

import (
	"fmt"
)

func main() {

	t := 0
	fmt.Scan(&t)

	for t > 0 {

		m, n, im, in, dm, dn := 0, 0, 0, 0, 0, 0

		fmt.Scan(&m, &n, &im, &in, &dm, &dn)
		fmt.Println(solve(m, n, im, in, dm, dn))

		t--
	}

}

func solve(m, n, im, in, dm, dn int) int {

	t := 0
	direction := 0

	for im != dm && in != dn {

		switch direction {
		case 0:
			if im+1 > m && in+1 > n {
				im--
				in--
				direction = 2
			} else if im+1 > m {
				im--
				in++
				direction = 1
			} else if in+1 > n {
				im++
				in--
				direction = 3
			} else {
				im++
				in++
			}
		case 1:
			if im-1 < 0 && in+1 > n {
				im++
				in--
				direction = 3
			} else if im-1 < 0 {
				im++
				in++
				direction = 0
			} else if in+1 > n {
				im--
				in--
				direction = 2
			} else {
				im--
				in++
			}
		case 2:
			if im-1 < 0 && in-1 < 0 {
				im++
				in++
				direction = 0
			} else if im-1 < 0 {
				im++
				in--
				direction = 3
			} else if in-1 < 0 {
				im--
				in++
				direction = 1
			} else {
				im--
				in--
			}
		default:
			if im+1 > m && in-1 < 0 {
				im--
				in++
				direction = 1
			} else if im+1 > m {
				im--
				in--
				direction = 2
			} else if in-1 < 0 {
				im++
				in++
				direction = 0
			} else {
				im++
				in--
			}
		}

		//fmt.Println(im, in)
		t++
	}

	return t
}

func printMatrix(matrix [][]int) {
	for i := range matrix {

		for j := range matrix[i] {
			fmt.Printf("%d ", matrix[i][j])
		}

		fmt.Println()

	}
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	testCount, _ := strconv.Atoi(scanner.Text())

	for i := 0; i < testCount; i++ {
		scanner.Scan()
		parameters := strings.Split(scanner.Text(), " ")
		n, _ := strconv.Atoi(parameters[0])
		m, _ := strconv.Atoi(parameters[1])
		rb, _ := strconv.Atoi(parameters[2])
		cb, _ := strconv.Atoi(parameters[3])
		rd, _ := strconv.Atoi(parameters[4])
		cd, _ := strconv.Atoi(parameters[5])

		// dirty is a flag that represents whether the dirty cell is
		// present.
		dirty := true
		// dr is the vertical movement delta.
		dr := 1
		// dc is the horizontal movement delta.
		dc := 1
		// steps represents the number of steps taken
		// by the robot to clean the dirty cell.
		steps := 0

		for dirty {
			// Clear the dirty flag if the column or row matches.
			if rb == rd || cb == cd {
				dirty = false
			} else {
				// Reverse the vertical movement if a
				// collision with the horizontal walls is determined.
				if rb+dr > n {
					dr *= -1
				}

				// Reverse the horizontal movement if a
				// collision with the vertical walls is detected.
				if cb+dc > m {
					dc *= -1
				}

				// Update the position of the robot.
				rb += dr
				cb += dc

				steps++
			}
		}

		fmt.Println(steps)
	}
}

package main

import "fmt"

func main() {
	var caseNum int
	fmt.Scanf("%d\n", &caseNum)
	for i := 0; i < caseNum; i++ {
		dr := 1
		dc := 1
		cnt := 0
		var n, m, rb, cb, rd, cd int
		fmt.Scanf("%d %d %d %d %d %d\n", &n, &m, &rb, &cb, &rd, &cd)
		for {
			//fmt.Printf("%d %d\n", rb, cb)
			if rb == rd || cb == cd {
				fmt.Printf("%d\n", cnt)
				break
			}
			if (rb == 1 && dr < 0) || (rb == n && dr > 0) {
				dr *= -1
			}
			if (cb == 1 && dc < 0) || (cb == m && dc > 0) {
				dc *= -1
			}
			rb += dr
			cb += dc
			cnt++
		}
	}
}

package main

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

const (
	NL string = "\n"
	CR string = "\r"
)

type gridPoint struct {
	r, c int
}

type Actor struct {
	pos gridPoint
}

func isNotResult(r Actor, d Actor) bool {
	return r.pos.c != d.pos.c && r.pos.r != d.pos.r
}

func Solve(nums []int) {
	grid := Actor{}
	robot := Actor{}
	dirt := Actor{}
	var dr,dc int = 1, 1

	for i := 0; i < len(nums); i+=2 {
		switch {
		case i == 0:
			grid.pos.r = nums[0]
			grid.pos.c = nums[1]

		case i == 2:
			robot.pos.r = nums[2]
			robot.pos.c = nums[3]

		case i == 4:
			dirt.pos.r = nums[4]
			dirt.pos.c = nums[5]
		}
	}

	var ss int = 0
	for isNotResult(robot, dirt) {
		if robot.pos.r==grid.pos.r { dr=-1 }
		if robot.pos.c==grid.pos.c { dc=-1 }
		if robot.pos.r==1 { dr=1 }
		if robot.pos.c==1 { dc=1 }
		robot.pos.r+=dr
		robot.pos.c+=dc
		ss+=1
	}
	fmt.Println(ss)
}

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

func ReadArrInt(in *bufio.Reader) []int {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, CR, "")
	line = strings.ReplaceAll(line, NL, "")
	numbs := strings.Split(line, " ")
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func main() {
	bufIn := bufio.NewReader(os.Stdin)
	N := ReadInt(bufIn)
	for i := 0; i < N; i++ {
		numbers := ReadArrInt(bufIn)
		Solve(numbers)
	}
}

package main

import "fmt"

var (
	T, n, m, rb, cb, rd, cd int
)

func main() {
	fmt.Scanf("%d\n", &T)
	for T > 0 {
		fmt.Scanf("%d %d %d %d %d %d\n", &n, &m, &rb, &cb, &rd, &cd)
		x, y, ans := rb, cb, 0
		dx, dy := 1, 1
		for x != rd && y != cd {
			tx, ty := x+dx, y+dy
			if tx > n {
				dx = -dx
				tx = x + dx
			}
			if ty > m {
				dy = -dy
				ty = y + dy
			}
			x, y = tx, ty
			ans++
		}
		fmt.Printf("%d\n", ans)
		T--
	}
}

package main

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

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

func solve(t int) {
	n, m, x, y, x1, y1 := readInt(), readInt(), readInt(), readInt(), readInt(), readInt()
	result := 0
	dx, dy := 1, 1
	for x != x1 && y != y1 {
		if x+dx < 1 || x+dx > n {
			dx *= -1
		}
		if y+dy < 1 || y+dy > m {
			dy *= -1
		}
		result += 1
		x += dx
		y += dy
	}
	println(result)
}

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

	for i := 0; i < t; i++ {
		solve(i)
	}
}

// READ
func readInt() int {
	var res int
	fmt.Fscan(in, &res)
	return res
}

func readInt64() int64 {
	var res int64
	fmt.Fscan(in, &res)
	return res
}

func readString() string {
	var res string
	fmt.Fscan(in, &res)
	return res
}

// PRINT
func print(args ...interface{}) {
	fmt.Fprint(out, args...)
	out.Flush()
}

func println(args ...interface{}) {
	fmt.Fprintln(out, args...)
	out.Flush()
}

func printErr(args ...interface{}) {
	fmt.Fprint(err, args...)
	err.Flush()
}

func printlnErr(args ...interface{}) {
	fmt.Fprintln(err, args...)
	err.Flush()
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"
)

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

	solve := func(Case int) {
		var n,m,x,y,tx,ty int
		Fscan(in, &n, &m, &x, &y, &tx, &ty)
		ans := 0
		dx,dy := 1,1
		for x!= tx && y != ty {

			if x+dx < 1 || x+dx > n  {
				dx=-dx
			}
			if y+dy< 1 || y+dy>m {
				dy=-dy
			}
			x+=dx
			y+=dy
			ans++
		}

		Fprintln(out, ans)
	}

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

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}

}

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

package main

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

type IO interface {
	GetInt() int
	GetChar() byte
	GetString() string
}

type FastIO struct {
	IO
	in *bufio.Reader
}

func (t *FastIO) GetInt() (res int) {
	p := 1
	c, _ := t.in.ReadByte()

	for ; c < '0' || c > '9'; c, _ = t.in.ReadByte() {
	}
	// No comment...
	res = 0
	if c == '-' {
		p = -1
	} else {
		res = int(c - '0')
	}

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

	res *= p
	return
}

func solve(fast FastIO) {
	var n, m, i1, j1, i2, j2 int
	n = fast.GetInt()
	m = fast.GetInt()
	i1 = fast.GetInt()
	j1 = fast.GetInt()
	i2 = fast.GetInt()
	j2 = fast.GetInt()

	var (
		a int
		b int
	)

	if i2 >= i1 {
		a = i2 - i1
	} else {
		a = (n-i1)*2 + (i1 - i2)
	}

	if j2 >= j1 {
		b = (j2 - j1)
	} else {
		b = (m-j1)*2 + (j1 - j2)
	}

	fmt.Print(math.Min(float64(a), float64(b)), ' ')
}

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

	var t int

	fmt.Scan(&t)
	for ; t > 0; t-- {
		solve(fast)
	}
}

package main

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

type IO interface {
	GetInt() int
	GetChar() byte
	GetString() string
}

type FastIO struct {
	IO
	in *bufio.Reader
}

func (t *FastIO) GetInt() (res int) {
	p := 1
	c, _ := t.in.ReadByte()

	for ; c < '0' || c > '9'; c, _ = t.in.ReadByte() {
	}
	// No comment...
	res = 0
	if c == '-' {
		p = -1
	} else {
		res = int(c - '0')
	}

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

	res *= p
	return
}

func solve(fast FastIO) {
	var n, m, i1, j1, i2, j2 int
	n = fast.GetInt()
	m = fast.GetInt()
	i1 = fast.GetInt()
	j1 = fast.GetInt()
	i2 = fast.GetInt()
	j2 = fast.GetInt()

	var (
		a int
		b int
	)

	if i2 >= i1 {
		a = i2 - i1
	} else {
		a = (n-i1)*2 + (i1 - i2)
	}

	if j2 >= j1 {
		b = (j2 - j1)
	} else {
		b = (m-j1)*2 + (j1 - j2)
	}

	fmt.Print(math.Min(float64(a), float64(b)))
}

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

	var t int

	fmt.Scan(&t)
	for ; t > 0; t-- {
		solve(fast)
	}
}

package main

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

type IO interface {
	GetInt() int
	GetChar() byte
	GetString() string
}

type FastIO struct {
	IO
	in *bufio.Reader
}

func (t *FastIO) GetInt() (res int) {
	p := 1
	c, _ := t.in.ReadByte()
	res = 0
	if c == '-' {
		p = -1
	} else {
		res = int(c - '0')
	}

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

	res *= p
	return
}

func solve(fast FastIO) {
	var n, m, i1, j1, i2, j2 int
	n = fast.GetInt()
	m = fast.GetInt()
	i1 = fast.GetInt()
	j1 = fast.GetInt()
	i2 = fast.GetInt()
	j2 = fast.GetInt()

	var (
		a int
		b int
	)

	if i2 >= i1 {
		a = i2 - i1
	} else {
		a = (n-i1)*2 + (i1 - i2)
	}

	if j2 >= j1 {
		b = (j2 - j1)
	} else {
		b = (m-j1)*2 + (j1 - j2)
	}

	fmt.Println(math.Min(float64(a), float64(b)))
}

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

	var t int

	fmt.Scan(&t)
	for ; t > 0; t-- {
		solve(fast)
	}
}

package main

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

type IO interface {
	GetInt() int
	GetChar() byte
	GetString() string
}

type FastIO struct {
	IO
	in *bufio.Reader
}

func (t *FastIO) GetInt() (res int) {
	p := 1
	c, _ := t.in.ReadByte()
	res = 0
	if c == '-' {
		p = -1
	} else {
		res = int(c - '0')
	}

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

	res *= p
	return
}

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

	t := fast.GetInt()

	for ; t > 0; t-- {
		var n, m, i1, j1, i2, j2 int
		n = fast.GetInt()
		m = fast.GetInt()
		i1 = fast.GetInt()
		j1 = fast.GetInt()
		i2 = fast.GetInt()
		j2 = fast.GetInt()

		var (
			a int
			b int
		)

		if i2 >= i1 {
			a = i2 - i1
		} else {
			a = (n-i1)*2 + (i1 - i2)
		}

		if j2 >= j1 {
			b = (j2 - j1)
		} else {
			b = (m-j1)*2 + (j1 - j2)
		}

		fmt.Print(math.Min(float64(a), float64(b)), " ")
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for ; t > 0; t-- {
		var n, m, i1, j1, i2, j2 int
		fmt.Scanf("%d %d %d %d %d %d\n", &n, &m, &i1, &j1, &i2, &j2)

		fmt.Println(n, m, i1, j1, i2, j2)

		var (
			a int
			b int
		)

		if i2 >= i1 {
			a = i2 - i1
		} else {
			a = (n-i1)*2 + (i1 - i2)
		}

		if j2 >= j1 {
			b = (j2 - j1)
		} else {
			b = (m-j1)*2 + (j1 - j2)
		}

		fmt.Print(math.Min(float64(a), float64(b)), " ")
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var t int
	fmt.Scanf("%d", &t)
	for ; t > 0; t-- {
		var n, m, i1, j1, i2, j2 int
		fmt.Scanf("%d %d %d %d %d %d", &n, &m, &i1, &j1, &i2, &j2)

		var (
			a int
			b int
		)

		if i2 >= i1 {
			a = i2 - i1
		} else {
			a = (n-i1)*2 + (i1 - i2)
		}

		if j2 >= j1 {
			b = (j2 - j1)
		} else {
			b = (m-j1)*2 + (j1 - j2)
		}

		fmt.Print(math.Min(float64(a), float64(b)), " ")
	}
}

package main

import (
	"fmt"
	"math"
)

func solve() {
	var n, m, i1, j1, i2, j2 int
	fmt.Scanf("%d", &n)
	fmt.Scanf("%d", &m)
	fmt.Scanf("%d", &i1)
	fmt.Scanf("%d", &j1)
	fmt.Scanf("%d", &i2)
	fmt.Scanf("%d", &j2)

	var (
		a int
		b int
	)

	if i2 >= i1 {
		a = i2 - i1
	} else {
		a = (n-i1)*2 + (i1 - i2)
	}

	if j2 >= j1 {
		b = (j2 - j1)
	} else {
		b = (m-j1)*2 + (j1 - j2)
	}

	fmt.Println(math.Min(float64(a), float64(b)))
}

func main() {
	var t int
	fmt.Scanf("%d", &t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var t int
	fmt.Scan(&t)
	for ; t > 0; t-- {
		var n, m, i1, j1, i2, j2 int
		fmt.Scan(&n, &m, &i1, &j1, &i2, &j2)

		var (
			a int
			b int
		)

		if i2 >= i1 {
			a = i2 - i1
		} else {
			a = (n-i1)*2 + (i1 - i2)
		}

		if j2 >= j1 {
			b = (j2 - j1)
		} else {
			b = (m-j1)*2 + (j1 - j2)
		}

		fmt.Print(math.Min(float64(a), float64(b)), ' ')
	}
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N := 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		n, m, rb, cb, rd, cd, dr, dc, tot := 0, 0, 0, 0, 0, 0, 1, 1, 0
		fmt.Fscan(f, &n, &m, &rb, &cb, &rd, &cd)
		revr, revc := false, false
		if rb != rd && cb != cd {
			for rb+dr != rd && cb+dc != cd {
				if rb+dr == n || rb+dr == 1 {
					revr = !revr
				}
				if cb+dc == m || cb+dc == 1 {
					revc = !revc
				}
				if revr {
					dr--
				} else {
					dr++
				}
				if revc {
					dc--
				} else {
					dc++
				}
				fmt.Fprintln(w, rb+dr, cb+dc)
				tot++
			}
		}
		fmt.Fprintln(w, tot)
	}
	w.Flush()
}

func main() {
	solve()
}

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var y,x,ys,xs,yf,xf int
		fmt.Fscan(in,&y,&x,&ys,&xs,&yf,&xf,)
		if yf == ys || xf == xs {
			fmt.Fprintln(out,0)
			continue
		}
		if yf>ys && xf>xs {
			fmt.Fprintln(out,min(yf-ys,xf-xs))
			continue
		}
		if yf > ys {
			fmt.Fprintln(out,yf-ys)
			continue
		}
		if xf > xs {
			fmt.Fprintln(out,xf-xs)
			continue
		}
		fmt.Fprintln(out,min(2*x-xs-xf,2*y-yf-ys))
	}
}
func min(a,b int) int {
	if a<=b{return a}
	return b
}

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var y,x,ys,xs,yf,xf int
		fmt.Fscan(in,&y,&x,&ys,&xs,&yf,&xf,)
		if yf == ys || xf == xs {
			fmt.Fprintln(out,0)
			continue
		}
		if yf>ys && xf>xs {
			fmt.Fprintln(out,min(yf-ys,xf-xs))
			continue
		}
		if yf > ys {
			fmt.Fprintln(out,yf-ys)
			continue
		}
		if xf > xs {
			fmt.Fprintln(out,xf-xs)
			continue
		}
		fmt.Fprintln(out,min((x-xs)+(x-xf),(y-ys)+(y-yf)))
	}
}
func min(a,b int) int {
	if a<=b{return a}
	return b
}

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var y,x,ys,xs,yf,xf int
		fmt.Fscan(in,&y,&x,&ys,&xs,&yf,&xf,)
		if yf == ys || xf == xs {
			fmt.Fprintln(out,0)
			continue
		}
		if yf>ys && xf>xs {
			fmt.Fprintln(out,min(yf-ys,xf-xs))
			continue
		}
		if yf > ys {
			fmt.Fprintln(out,yf-ys)
			continue
		}
		if xf > xs {
			fmt.Fprintln(out,xf-xs)
			continue
		}
		fmt.Fprintln(out,min(abs(xs-x)+abs(xf-x),abs(ys-y)+abs(yf-y)))
	}
}
func min(a,b int) int {
	if a<=b{return a}
	return b
}
func abs(a int) int {
	if a<0{return a*-1}
	return a
}

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var y,x,ys,xs,yf,xf int
		fmt.Fscan(in,&y,&x,&ys,&xs,&yf,&xf,)
		if yf == ys || xf == xs {
			fmt.Fprintln(out,0)
			continue
		}
		if yf > ys {
			fmt.Fprintln(out,yf-ys)
			continue
		}
		if xf > xs {
			fmt.Fprintln(out,xf-xs)
			continue
		}
		fmt.Fprintln(out,min(abs(xs-x)+abs(xf-x),abs(ys-y)+abs(yf-y)))
	}
}
func min(a,b int) int {
	if a<=b{return a}
	return b
}
func abs(a int) int {
	if a<0{return a*-1}
	return a
}

package main

import (
	"fmt"
)

func oneDim(s, e, w int) int {
	if e >= s {
		return e - s
	} else {
		return w - s + w - e
	}
}
func min(a, b int) int {
	if a < b {
		return a
	} else {
		return b
	}
}
func Solve(n, m, rb, cb, rd, cd int) int {
	r := oneDim(rb, rd, n)
	c := oneDim(cb, cd, m)
	return min(r, c)
}
func main() {
	n := 0
	_, err := fmt.Scanf("%d", &n)
	if err != nil {
		fmt.Println("error:", err)
	}
	for i := 0; i < n; i++ {
		var n, m, rb, rd, cd, cb int
		_, err = fmt.Scanf("%d %d %d %d %d %d\n", &n, &m, &rb, &cb, &rd, &cd)
		if err != nil {
			fmt.Println(err)
		}
		fmt.Println(Solve(n, m, rb, cb, rd, cd))
	}
}

package main

import (
	"fmt"
)

func oneDim(s, e, w int) int {
	if e >= s {
		return e - s
	} else {
		return w - s + w - e
	}
}
func min(a, b int) int {
	if a < b {
		return a
	} else {
		return b
	}
}
func Solve(n, m, rb, cb, rd, cd int) int {
	r := oneDim(rb, rd, n)
	c := oneDim(cb, cd, m)
	return min(r, c)
}
func main() {
	n := 0
	_, err := fmt.Scanf("%d", &n)
	if err != nil {
		fmt.Println("error:", err)
	}
	for i := 0; i < n; i++ {
		var n, m, rb, rd, cd, cb int
		_, err = fmt.Scanf("%d %d %d %d %d %d", &n, &m, &rb, &cb, &rd, &cd)
		if err != nil {
			fmt.Println(err)
		}
		fmt.Println(Solve(n, m, rb, cb, rd, cd))
	}
}

package main

import (
	"fmt"
)

func oneDim(s, e, w int) int {
	if e >= s {
		return e - s
	} else {
		return w - s + w - e
	}
}
func min(a, b int) int {
	if a < b {
		return a
	} else {
		return b
	}
}
func Solve(n, m, rb, cb, rd, cd int) int {
	r := oneDim(rb, rd, n)
	c := oneDim(cb, cd, m)
	return min(r, c)
}
func main() {
	n := 0
	fmt.Scanf("%d", &n)
	for i := 0; i < n; i++ {
		var n, m, rb, rd, cd, cb int
		fmt.Scanf("%d %d %d %d %d %d", n, m, rb, cb, rd, cd)
		fmt.Println(Solve(n, m, rb, cb, rd, cd))
	}
}

package main

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

func CF1623A(rawIn io.Reader, rawOut io.Writer) {
	in := bufio.NewReader(rawIn)
	out := bufio.NewWriter(rawOut)
	defer out.Flush()

	var cases int
	fmt.Fscan(in, &cases)
	for cc := 0; cc < cases; cc++ {
		var n, m, rb, cb, rd, cd int
		fmt.Fscan(in, &n, &m, &rb, &cb, &rd, &cd)

		rowD := calculateLineDist(rd, rb, m)
		colD := calculateLineDist(cd, cb, m)

		if rowD < colD {
			fmt.Fprintf(out, "%d\n", rowD)
		} else {
			fmt.Fprintf(out, "%d\n", colD)
		}
	}
}

func calculateLineDist(target int, init int, limit int) int {
	if target >= init {
		return target - init
	} else {
		return (limit - init) + (limit - target)
	}
}

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

package main

import (
	"fmt"
)

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

func main() {
	var t, n, m, rb, cb, rd, cd int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scanln(&n, &m, &rb, &cb, &rd, &cd)
		ans := 0
		if rd < rb {
			ans = n - rb + n - rd
		} else {
			ans = rd - rb
		}

		if cd < cb {
			ans = min(ans, m-cb+m-cd)
		} else {
			ans = min(ans, cd-cb)
		}
		fmt.Println(ans)
	}

}


There is a colony of villains with several holes aligned in a row, where each hole contains exactly one villain.Each colony arrangement can be expressed as a string of even length, where the $$$i$$$-th character of the string represents the type of villain in the $$$i$$$-th hole. Iron Man can destroy a colony only if the colony arrangement is such that all villains of a certain type either live in the first half of the colony or in the second half of the colony.His assistant Jarvis has a special power. It can swap villains of any two holes, i.e. swap any two characters in the string; he can do this operation any number of times.Now Iron Man asks Jarvis $$$q$$$ questions. In each question, he gives Jarvis two numbers $$$x$$$ and $$$y$$$. Jarvis has to tell Iron Man the number of distinct colony arrangements he can create from the original one using his powers such that all villains having the same type as those originally living in $$$x$$$-th hole or $$$y$$$-th hole live in the same half and the Iron Man can destroy that colony arrangement.Two colony arrangements are considered to be different if there exists a hole such that different types of villains are present in that hole in the arrangements.

The first line contains a string $$$s$$$ ($$$2 \le |s| \le 10^{5}$$$), representing the initial colony arrangement. String $$$s$$$ can have both lowercase and uppercase English letters and its length is even. The second line contains a single integer $$$q$$$ ($$$1 \le q \le 10^{5}$$$) — the number of questions. The $$$i$$$-th of the next $$$q$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i, y_i \le |s|$$$, $$$x_i \ne y_i$$$) — the two numbers given to the Jarvis for the $$$i$$$-th question.

For each question output the number of arrangements possible modulo $$$10^9+7$$$.

NoteConsider the first example. For the first question, the possible arrangements are "aabb" and "bbaa", and for the second question, index $$$1$$$ contains 'a' and index $$$2$$$ contains 'b' and there is no valid arrangement in which all 'a' and 'b' are in the same half.

package main

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

var reader *bufio.Reader
var writer *bufio.Writer

var inputFile = flag.String("input", "", "")

func init() {
	flag.Parse()
	var i io.Reader
	if *inputFile != "" {
		i, _ = os.Open(*inputFile)
	} else {
		i = os.Stdin
	}
	reader = bufio.NewReaderSize(i, 1<<20)
	writer = bufio.NewWriterSize(os.Stdout, 1<<20)
}

func println(a ...interface{}) {
	_, _ = fmt.Fprintln(writer, a...)
}

func printf(format string, a ...interface{}) {
	_, _ = fmt.Fprintf(writer, format, a...)
}

func scanf(format string, a ...interface{}) {
	_, _ = fmt.Fscanf(reader, format, a...)
}

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

func NextInt64() int64 {
	var res int64
	scan(&res)
	return res
}

func NextInt() int {
	var res int
	scan(&res)
	return res
}

func NextString() string {
	var res string
	scan(&res)
	return res
}

func NextBigInt() *big.Int {
	n := new(big.Int)
	n, _ = n.SetString(NextString(), 10)
	return n
}

const mod = 1000000007

func GCD(a, b int64, x, y *int64) int64 {
	if b == 0 {
		*x, *y = 1, 0
		return a
	}
	//a * x + b * y == g

	//a == b * k + a'

	g := GCD(b, a%b, x, y)

	// b * x + (a - k * b) * y == g

	*x, *y = *y, *x-a/b**y

	return g
}

func polyDiv(data []int32, a int) {
	for i := a; i < len(data); i++ {
		data[i] -= data[i-a]
		if data[i] < 0 {
			data[i] += mod
		}
	}
}

func polyMul(data []int32, a int) {
	for i := len(data) - 1; i >= a; i-- {
		data[i] += data[i-a]
		if data[i] >= mod {
			data[i] -= mod
		}
	}
}

func main() {
	defer writer.Flush()

	s := []byte(NextString())

	types := make([]int, len(s))

	counts := make([]int, 52)

	for i, c := range s {
		if c >= 'a' && c <= 'z' {
			types[i] = int(c - 'a')
		} else {
			types[i] = 26 + int(c-'A')
		}
		counts[types[i]]++
	}

	n := len(s)

	r := make([]int64, n+1)
	r[1] = 1
	for i := 2; i <= n; i++ {
		r[i] = int64(mod-(mod/i)) * r[mod%i] % mod
		if r[i]*int64(i)%mod != 1 {
			panic("oops")
		}
	}

	f := make([]int64, n+1)
	rf := make([]int64, n+1)

	f[0], rf[0] = 1, 1

	for i := 1; i <= n; i++ {
		f[i] = f[i-1] * int64(i) % mod
		rf[i] = rf[i-1] * r[i] % mod
	}

	total := make([]int32, n+1)
	total[0] = 1

	for _, c := range counts {
		if c > 0 {
			polyMul(total, c)
		}
	}

	answer := make([][]int32, len(counts))

	for i := range counts {
		if counts[i] == 0 {
			continue
		}

		answer[i] = make([]int32, len(counts))
		cur := total

		polyDiv(cur, counts[i])

		for j := 0; j < i; j++ {
			if counts[j] == 0 {
				continue
			}

			polyDiv(cur, counts[j])

			if counts[i]+counts[j] <= n/2 {
				answer[i][j] = cur[n/2-(counts[i]+counts[j])] + cur[n/2]

				if answer[i][j] >= mod {
					answer[i][j] -= mod
				}
			}

			polyMul(cur, counts[j])
		}

		polyMul(cur, counts[i])

	}

	var mul int64 = 1

	for _, c := range counts {
		mul = mul * rf[c] % mod
	}
	mul = mul * f[n/2] % mod
	mul = mul * f[n/2] % mod

	q := NextInt()

	for i := 0; i < q; i++ {
		x := NextInt() - 1
		y := NextInt() - 1

		t1, t2 := types[x], types[y]
		if t1 < t2 {
			t1, t2 = t2, t1
		}

		if t1 == t2 {
			println(int64(total[n/2]) * mul % mod)
		} else {
			println(int64(answer[t1][t2]) * mul % mod)
		}
	}
}


You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it so that as many as possible integers will become on a place where a smaller integer used to stand. Help Vasya find the maximal number of such integers.For instance, if we are given an array $$$[10, 20, 30, 40]$$$, we can permute it so that it becomes $$$[20, 40, 10, 30]$$$. Then on the first and the second positions the integers became larger ($$$20>10$$$, $$$40>20$$$) and did not on the third and the fourth, so for this permutation, the number that Vasya wants to maximize equals $$$2$$$. Read the note for the first example, there is one more demonstrative test case.Help Vasya to permute integers in such way that the number of positions in a new array, where integers are greater than in the original one, is maximal.

The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the length of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the elements of the array.

Print a single integer — the maximal number of the array's elements which after a permutation will stand on the position where a smaller element stood in the initial array.

NoteIn the first sample, one of the best permutations is $$$[1, 5, 5, 3, 10, 1, 1]$$$. On the positions from second to fifth the elements became larger, so the answer for this permutation is 4.In the second sample, there is no way to increase any element with a permutation, so the answer is 0.

package main

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

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

	i := 0
	j := 1
	count := 0
	for i < N && j < N {
		if A[i] >= A[j] {
			j++
		} else {
			i++
			j++
			count++
		}
	}
	fmt.Println(count)

}

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

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

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

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

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

	return mp
}

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	for {
		var n int
		_, err := fmt.Scanln(&n)
		if err != nil {
			break
		}

		cnt_map := make(map[int]int)
		remain_cnt_map := make(map[int]int)
		for i := 0; i < n; i++ {
			var a int
			fmt.Fscan(in, &a)
			cnt_map[a]++
			remain_cnt_map[a]++
		}
		var unique_list sort.IntSlice
		for k, _ := range cnt_map {
			unique_list = append(unique_list, k)
		}
		unique_list.Sort()
		//fmt.Println(unique_list)
		res := 0
		for l, r := 0, 1; l < len(unique_list) && r < len(unique_list); {
			cnt_l := cnt_map[unique_list[l]]
			cnt_r := remain_cnt_map[unique_list[r]]
			if cnt_l <= cnt_r {
				res += cnt_l
				l++
				remain_cnt_map[unique_list[r]] -= cnt_l
				if l == r || remain_cnt_map[unique_list[r]] <= 0 {
					r++
				}

			} else {
				res += cnt_r
				cnt_map[unique_list[l]] -= cnt_r
				r++
			}
		}

		fmt.Println(res)
	}
}

package main

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

type NumCnt struct {
	num   int
	cnt_l int
	cnt_r int
}

type NumCntList []NumCnt

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

func main() {
	in := bufio.NewReader(os.Stdin)
	for {
		var n int
		_, err := fmt.Scanln(&n)
		if err != nil {
			break
		}

		cnt_map := make(map[int]int)
		for i := 0; i < n; i++ {
			var a int
			fmt.Fscan(in, &a)
			cnt_map[a]++
		}
		var unique_list NumCntList
		for k, v := range cnt_map {
			unique_list = append(unique_list, NumCnt{k, v, v})
		}
		sort.Sort(unique_list)
		//fmt.Println(unique_list)
		res := 0
		for l, r := 0, 1; l < len(unique_list) && r < len(unique_list); {
			cnt_l := &unique_list[l].cnt_l
			cnt_r := &unique_list[r].cnt_r
			if *cnt_l <= *cnt_r {
				res += *cnt_l
				l++
				*cnt_r -= *cnt_l
				if l == r || *cnt_r <= 0 {
					r++
				}
			} else {
				res += *cnt_r
				*cnt_l -= *cnt_r
				r++
			}
		}

		fmt.Println(res)
	}
}

package main
import (
	"bufio"
	"fmt"
	"sort"
	"os"
)
func main(){
	in := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(in, &n)
	a:=make([]int,n)
	for i := 0; i<n; i++ {
		fmt.Fscan(in,&a[i])
	}
	sort.Ints(a)
	b:=make([]int,n)
	copy(b, a)
	ans:=0
	for i := 0; i<n; i++ {
		for ;len(b)!=0&&b[0]<=a[i]; {
			b=b[1:]
		}
		if len(b)!=0{
			ans+=1
			b=b[1:]
		}
	}
	fmt.Printf("%d\n",ans)
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	for {
		var n int
		_, err := fmt.Scanln(&n)
		if err != nil {
			break
		}

		cnt_map := make(map[int]int)
		for i := 0; i < n; i++ {
			var a int
			fmt.Fscan(in, &a)
			cnt_map[a]++
		}
		var unique_list sort.IntSlice
		for k, _ := range cnt_map {
			unique_list = append(unique_list, k)
		}
		unique_list.Sort()
		//fmt.Println(unique_list)
		res := 0
		for l, r := 0, 1; l < len(unique_list) && r < len(unique_list); {
			cnt_l := cnt_map[unique_list[l]]
			cnt_r := cnt_map[unique_list[r]]
			if cnt_l <= cnt_r {
				res += cnt_l
				l++
				r++
			} else {
				res += cnt_r
				cnt_map[unique_list[l]] -= cnt_r
				r++
			}
		}

		fmt.Println(res)
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	for {
		var n int
		_, err := fmt.Scanln(&n)
		if err != nil {
			break
		}

		cnt_map := make(map[int]int)
		remain_cnt_map := make(map[int]int)
		for i := 0; i < n; i++ {
			var a int
			fmt.Fscan(in, &a)
			cnt_map[a]++
			remain_cnt_map[a]++
		}
		var unique_list sort.IntSlice
		for k, _ := range cnt_map {
			unique_list = append(unique_list, k)
		}
		unique_list.Sort()
		//fmt.Println(unique_list)
		res := 0
		for l, r := 0, 1; l < len(unique_list) && r < len(unique_list); {
			cnt_l := cnt_map[unique_list[l]]
			cnt_r := remain_cnt_map[unique_list[r]]
			if cnt_l <= cnt_r {
				res += cnt_l
				l++
				remain_cnt_map[unique_list[r]] -= cnt_l
				if remain_cnt_map[unique_list[r]] <= 0 {
					r++
				}

			} else {
				res += cnt_r
				cnt_map[unique_list[l]] -= cnt_r
				r++
			}
		}

		fmt.Println(res)
	}
}


Kana was just an ordinary high school girl before a talent scout discovered her. Then, she became an idol. But different from the stereotype, she is also a gameholic. One day Kana gets interested in a new adventure game called Dragon Quest. In this game, her quest is to beat a dragon. The dragon has a hit point of $$$x$$$ initially. When its hit point goes to $$$0$$$ or under $$$0$$$, it will be defeated. In order to defeat the dragon, Kana can cast the two following types of spells.  Void Absorption Assume that the dragon's current hit point is $$$h$$$, after casting this spell its hit point will become $$$\left\lfloor \frac{h}{2} \right\rfloor + 10$$$. Here $$$\left\lfloor \frac{h}{2} \right\rfloor$$$ denotes $$$h$$$ divided by two, rounded down. Lightning Strike This spell will decrease the dragon's hit point by $$$10$$$. Assume that the dragon's current hit point is $$$h$$$, after casting this spell its hit point will be lowered to $$$h-10$$$.Due to some reasons Kana can only cast no more than $$$n$$$ Void Absorptions and $$$m$$$ Lightning Strikes. She can cast the spells in any order and doesn't have to cast all the spells. Kana isn't good at math, so you are going to help her to find out whether it is possible to defeat the dragon.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. The next $$$t$$$ lines describe test cases. For each test case the only line contains three integers $$$x$$$, $$$n$$$, $$$m$$$ ($$$1\le x \le 10^5$$$, $$$0\le n,m\le30$$$)  — the dragon's intitial hit point, the maximum number of Void Absorptions and Lightning Strikes Kana can cast respectively.

If it is possible to defeat the dragon, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).

NoteOne possible casting sequence of the first test case is shown below: Void Absorption $$$\left\lfloor \frac{100}{2} \right\rfloor + 10=60$$$. Lightning Strike $$$60-10=50$$$. Void Absorption $$$\left\lfloor \frac{50}{2} \right\rfloor + 10=35$$$. Void Absorption $$$\left\lfloor \frac{35}{2} \right\rfloor + 10=27$$$. Lightning Strike $$$27-10=17$$$. Lightning Strike $$$17-10=7$$$. Lightning Strike $$$7-10=-3$$$.

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-- {
		var x, n, m int64
		fmt.Fscan(in, &x, &n, &m, )
		for ; x > 20 && n > 0; n-- {
			x = x/2 + 10
		}
		if x > m*10 {
			fmt.Fprintln(out, "NO")
		} else {
			fmt.Fprintln(out, "YES")
		}
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var x, n, m int64
		fmt.Fscan(in, &x, &n, &m, )
		for ; x > 20 && n > 0; n-- {
			x = x/2 + 10
		}
		for ; m > 0; m-- {
			x -= 10
		}
		if x > 0 {
			fmt.Fprintln(out, "NO")
		} else {
			fmt.Fprintln(out, "YES")
		}
	}
}

package main

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

var reader = bufio.NewReader(os.Stdin)
var 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 scanIntArr() ([]int, error) {
	str, err := reader.ReadString('\n')
	if err != nil {
		return nil, err
	}
	str = strings.TrimRight(str, "\n\r")
	strArr := strings.Split(str, " ")

	arr := make([]int, len(strArr))
	for i, strA := range strArr {
		arr[i], err = strconv.Atoi(strA)
		if err != nil {
			return nil, err
		}
	}

	return arr, nil
}

func main() {
	defer writer.Flush()

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

	for i := 0; i < t; i++ {
		exec()
	}
}

func exec() {
	var x, n, m int64
	scanf("%d %d %d\n", &x, &n, &m)

	for i := n; i > 0 && x > 20; i-- {
		x = x/2 + 10
	}
	x -= 10 * m

	if x <= 0 {
		printf("YES\n")
		return
	}
	printf("NO\n")
}

// @problemURL url.com
package main

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

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

func ni(s *bufio.Scanner) int {
	return atoi(nt(s))
}

func atoi(s string) int {
	i, err := strconv.Atoi(s)
	if err != nil {
		panic(err)
	}
	return i
}

func main() {
	var in = bufio.NewReader(os.Stdin)
	s := bufio.NewScanner(in)
	s.Split(bufio.ScanWords)

	// Code
	for T := ni(s); T > 0; T-- {
		x, n, m := ni(s), ni(s), ni(s)

		for x > 0 && (m > 0 || n > 0) {
			if x > 20 && n > 0 {
				x /= 2
				x += 10
				n--
			} else if m > 0 {
				x -= 10
				m--
			} else {
				break
			}
		}

		if x > 0 {
			fmt.Println("NO")
		} else {
			fmt.Println("YES")
		}
	}

}

package main

import (
	"fmt"
)

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

	for t := 0; t < T; t++ {
		var x, a, b int
		fmt.Scan(&x, &a, &b)

		for ; a > 0; a--{
			if x / 2 + 10 < x {
				x = x / 2 + 10
			}
		}
		for ; b > 0; b-- {
			x -= 10
		}

		if x <= 0 {
			fmt.Println("YES")
		}else {
			fmt.Println("NO")
		}
	}
}

// http://acm.timus.ru/problem.aspx?space=1&num=1741
package main

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

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

func main () {
	var (
		T int
	)

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

	for i := 0; i < T; i++ {
		var x, n, m int
		fmt.Fscan(input, &x, &n, &m)
		fmt.Println(solve(x, n, m))
	}
}

func solve(x, n, m int) string {
	for n > 0 && x > 10 {
		x = (x >> 1) + 10
		n--
	}

	if x <= 10 * m {
		return "YES"
	} else {
		return "NO"
	}
}

package main

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

func solve(x int, n int, m int) string {
	for x > 0 && (n > 0 || m > 0) {
		for x > 20 && n > 0 {
			x = voidAbsorptions(x)
			n--
		}

		if x <= 20 || m > 0 {
			if m > 0 {
				x = lightningStrikes(x)
				m--
				continue
			}
		}

		if x <= 20 && m == 0 {
			return "NO"
		}
	}

	if x > 0 {
		return "NO"
	}

	return "YES"
}

func voidAbsorptions(x int) int {
	return x/2 + 10
}

func lightningStrikes(x int) int {
	return x - 10
}

func main() {
	r := bufio.NewReader(os.Stdin)
	var t, x, n, m int
	fmt.Fscanf(r, "%d\n", &t)

	for tc := 0; tc < t; tc++ {
		fmt.Fscanf(r, "%d %d %d\n", &x, &n, &m)
		fmt.Fprintf(os.Stdout, "%s\n", solve(x, n, m))
	}
}

package main

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

func main() {
	rd := bufio.NewReader(os.Stdin)
	wr := bufio.NewWriter(os.Stdout)

	var t, x, n, m, i int
	fmt.Fscanf(rd, "%d\n", &t)

	for i = 0; i < t; i++ {
		fmt.Fscanf(rd, "%d %d %d\n", &x, &n, &m)
		for x > 20 {
			if n == 0 {
				break
			}
			x /= 2
			x += 10
			n--
		}

		if x > m*10 {
			fmt.Fprintf(wr, "NO\n")
		} else {
			fmt.Fprintf(wr, "YES\n")
		}
	}
	wr.Flush()
}

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

	solve := func(_case int) bool {
		//if _case == 7 {
		//	println()
		//}
		var x, n, m int
		Fscan(in, &x, &n, &m)
		for x > 0 {
			if n == 0 && m == 0 {
				return false
			}

			if m == 0 {
				x = x/2 + 10
				n--
			} else if n == 0 {
				x -= 10
				m--
			} else {
				x1 := x/2 + 10
				x2 := x - 10
				if x1 < x {
					x=x1
					n--
				} else {
					x=x2
					m--
				}
			}
		}
		return true
	}

	var t int
	Fscan(in, &t)
	for _case := 1; _case <= t; _case++ {
		Fprintln(out, map[bool]string{true: "YES", false: "NO"} [solve(_case)])
	}
}

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

package main

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

func main() {
	SolveKanaAndDragon()
}

func SolveKanaAndDragon() {
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		text := scanner.Text()

		inputs := strings.Split(text, " ")

		if len(inputs) > 1 {
			life, _ := strconv.Atoi(inputs[0])
			voidout, _ := strconv.Atoi(inputs[1])
			lightning, _ := strconv.Atoi(inputs[2])
			fmt.Println(KanaAndDragon(float64(life), float64(voidout), float64(lightning)))
		}
		//fmt.Println(text)
	}
}

func KanaAndDragon(life, voidout, lightning float64) string {
	for life > 20 && voidout > 0 {
		life = math.Floor(life / 2) + 10
		voidout--
	}

	if life - lightning*10 <= 0 {
		return "YES"
	}
	return "NO"
}

package main

import "fmt"

func main() {
	var testNum int
	fmt.Scanln(&testNum)
	for i := 0; i<testNum; i++{
		fight()
	}

}

func fight(){
	var x,n,m int
	fmt.Scanln(&x,&n,&m)
	for n>0 && x>20{
		void(&x)
		n--
	}
	for m>0{
		x -= 10
		m--
		if x<=0 {
			fmt.Println("YES")
			return
		}
	}
	fmt.Println("NO")

}

func void(x *int){
	*x = *x/2 + 10
}

package main

import (
	"fmt"
	"math"
)

func main () {
	var t int
	fmt.Scan(&t)
	for i:=0; i < t; i++ {
		var x, n, m int
		fmt.Scan(&x, &n, &m)
		var cur_x int
		cur_x = x
		if (cur_x > 20 && n > 0) {
			for ok := true; ok; ok = (cur_x > 20 && n > 0) {
				cur_x = int(math.Floor(float64(cur_x)/2.)) + 10
				n -= 1
			}
		}
		cur_x -= m * 10
		if cur_x <= 0 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import "fmt"

func main() {
	var t, x, m, n int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&x, &n, &m)
		for x > 0 && (n > 0 || m > 0) {
			if x > 20 && n > 0 {
				n -= 1
				x = x/2 + 10
			} else if m > 0 {
				m -= 1
				x -= 10
			} else {
				break
			}
		}
		if x > 0 {
			fmt.Println("NO")
		} else {
			fmt.Println("YES")
		}
	}
}

package main

import (
	"fmt"
)

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

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

func max(arr []int) (max int) {
	max = arr[0]
	for _, val := range arr {
		if val > max {
			max = val
		}
	}
	return max
}

func main() {
	var t int
	fmt.Scan(&t)
	for qt := 0; qt < t; qt++ {
		var x, n, m int
		fmt.Scan(&x, &n, &m)
		for x > 0 && n > 0 && x/2 + 10 < x {
			n--
			x = x/2 + 10
		}
		if x <= m * 10 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

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

func newScanner() *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	return scanner
}

func scanInt() int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func scanInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = scanInt()
	}
	return a
}

func scanString() string {
	if sc.Scan() {
		return sc.Text()
	}
	panic(sc.Err())
}

var sc = newScanner()

func main() {
	for t:=scanInt(); t > 0; t-- {
		x, n, m := scanInt(), scanInt(), scanInt()
		for i := 0; i < n; i++ {
			if x <= 20 {
				break
			}
			x = x / 2 + 10
		}

		if x <= m * 10 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	. "fmt"
)

var In = Scan

func main() {
	var t int; In(&t)
	for i := 0; i < t; i++ {
		var x, n, m int64; In(&x, &n, &m)
		for ; x > 20 && n > 0; {
			x /= 2
			x += 10
			n -= 1
		}
		if m * 10 >= x {
			Println("YES")
		} else {
			Println("NO")
		}
	}
}

package main

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

type BufferedWriter interface {
    Printf(format string, a ...interface{})
    Flush()
}

type writerImpl struct {
    *bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
    return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
    fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
    impl.Writer.Flush()
}

type WordScanner interface {
    NextInt() int
    NextInt64() int64
    NextString() string
}

type wordScannerImpl struct {
    *bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
    s := bufio.NewScanner(reader)
    s.Split(bufio.ScanWords)
    // adjust the following size as needed
    // 2 << 17 is good enough to scan strings of len 2*10^5
    // it could go as high as 2 << 26 before cf judge breaks
    size := 2 << 20
    buf := make([]byte, size)
    s.Buffer(buf, size)
    return &wordScannerImpl{Scanner: s}
}

func (impl *wordScannerImpl) NextInt() int {
    impl.Scan()
    bb := impl.Bytes()
    i := 0
    if bb[0] == '-' {
        for _, b := range bb[1:] {
            i *= 10
            i -= int(b - '0')
        }
        return i
    }
    for _, b := range bb {
        i *= 10
        i += int(b - '0')
    }
    return i
}

func (impl *wordScannerImpl) NextInt64() int64 {
    impl.Scan()
    bb := impl.Bytes()
    i := int64(0)
    if bb[0] == '-' {
        for _, b := range bb[1:] {
            i *= 10
            i -= int64(b - '0')
        }
        return i
    }
    for _, b := range bb {
        i *= 10
        i += int64(b - '0')
    }
    return i
}

func (impl *wordScannerImpl) NextString() string {
    impl.Scan()
    return impl.Text()
}

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
    defer writer.Flush()
    t := reader.NextInt64()
    for ; t > 0; t-- {
        x, n, m := reader.NextInt64(), reader.NextInt64(), reader.NextInt64()
        for ; x > 20 && n > 0; n-- {
            x /= 2
            x += 10
        }
        x -= m*10
        if x <= 0 {
            writer.Printf("YES\n")
        } else {
            writer.Printf("NO\n")
        }
    }
}

// @problemURL url.com
package main

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

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

func ni(s *bufio.Scanner) int {
	return atoi(nt(s))
}

func atoi(s string) int {
	i, err := strconv.Atoi(s)
	if err != nil {
		panic(err)
	}
	return i
}

func main() {
	var in = bufio.NewReader(os.Stdin)
	s := bufio.NewScanner(in)
	s.Split(bufio.ScanWords)

	// Code
	for T := ni(s); T > 0; T-- {
		x, n, m := ni(s), ni(s), ni(s)

		for x > 0 && (m > 0 || n > 0) {
			if x >= 41 && n > 0 {
				x /= 2
				x += 10
				n--
			} else if m > 0 {
				x -= 10
				m--
			} else {
				break
			}
		}

		if x > 0 {
			fmt.Println("NO")
		} else {
			fmt.Println("YES")
		}
	}

}

package main

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

func main() {
	rd := bufio.NewReader(os.Stdin)
	wr := bufio.NewWriter(os.Stdout)

	var t, x, n, m, i int
	fmt.Fscanf(rd, "%d\n", &t)

	for i = 0; i < t; i++ {
		fmt.Fscanf(rd, "%d %d %d\n", &x, &n, &m)
		for x > 20 {
			x /= 2
			x += 10
			n--
			if n == 0 {
				break
			}
		}

		if x > m*10 {
			fmt.Fprintf(wr, "NO\n")
		} else {
			fmt.Fprintf(wr, "YES\n")
		}
	}
	wr.Flush()
}

package main

import "fmt"

func main() {
	var t, x, m, n int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&x, &n, &m)
		for x > 0 && (n > 0 || m > 0) {
			if x > 20 && n > 0 {
				n -= 1
				x = x/2 + 10
			} else {
				m -= 1
				x -= 10
			}
		}
		if x > 0 {
			fmt.Println("NO")
		} else {
			fmt.Println("YES")
		}
	}
}


As we all know, Max is the best video game player among her friends. Her friends were so jealous of hers, that they created an actual game just to prove that she's not the best at games. The game is played on a directed acyclic graph (a DAG) with n vertices and m edges. There's a character written on each edge, a lowercase English letter.  Max and Lucas are playing the game. Max goes first, then Lucas, then Max again and so on. Each player has a marble, initially located at some vertex. Each player in his/her turn should move his/her marble along some edge (a player can move the marble from vertex v to vertex u if there's an outgoing edge from v to u). If the player moves his/her marble from vertex v to vertex u, the "character" of that round is the character written on the edge from v to u. There's one additional rule; the ASCII code of character of round i should be greater than or equal to the ASCII code of character of round i - 1 (for i > 1). The rounds are numbered for both players together, i. e. Max goes in odd numbers, Lucas goes in even numbers. The player that can't make a move loses the game. The marbles may be at the same vertex at the same time.Since the game could take a while and Lucas and Max have to focus on finding Dart, they don't have time to play. So they asked you, if they both play optimally, who wins the game?You have to determine the winner of the game for all initial positions of the marbles.

The first line of input contains two integers n and m (2 ≤ n ≤ 100, ). The next m lines contain the edges. Each line contains two integers v, u and a lowercase English letter c, meaning there's an edge from v to u written c on it (1 ≤ v, u ≤ n, v ≠ u). There's at most one edge between any pair of vertices. It is guaranteed that the graph is acyclic.

Print n lines, a string of length n in each one. The j-th character in i-th line should be 'A' if Max will win the game in case her marble is initially at vertex i and Lucas's marble is initially at vertex j, and 'B' otherwise.

NoteHere's the graph in the first sample test case:  Here's the graph in the second sample test case:

package main

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

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

	var n, v, w byte
	var m int
	var s []byte
	Fscan(in, &n, &m)
	g := make([][]byte, n)
	for i := range g {
		g[i] = make([]byte, n)
	}
	for ; m > 0; m-- {
		Fscan(in, &v, &w, &s)
		g[v-1][w-1] = s[0] & 31
	}

	dp := make([][][27]byte, n)
	for i := range dp {
		dp[i] = make([][27]byte, n)
	}
	var f func(x, y, c byte) byte
	f = func(x, y, c byte) (r byte) {
		d := &dp[x][y][c]
		if *d > 0 {
			return *d
		}
		defer func() { *d = r }()
		for i, b := range g[x] {
			if b > 0 && b >= c && f(y, byte(i), b) == 'B' {
				return 'A'
			}
		}
		return 'B'
	}
	for i := byte(0); i < n; i++ {
		for j := byte(0); j < n; j++ {
			Fprintf(out, "%c", f(i, j, 0))
		}
		Fprintln(out)
	}
}

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