There are $$$n$$$ houses numbered from $$$1$$$ to $$$n$$$ on a circle. For each $$$1 \leq i \leq n - 1$$$, house $$$i$$$ and house $$$i + 1$$$ are neighbours; additionally, house $$$n$$$ and house $$$1$$$ are also neighbours.Initially, $$$m$$$ of these $$$n$$$ houses are infected by a deadly virus. Each morning, Cirno can choose a house which is uninfected and protect the house from being infected permanently.Every day, the following things happen in order:  Cirno chooses an uninfected house, and protect it permanently.  All uninfected, unprotected houses which have at least one infected neighbor become infected. Cirno wants to stop the virus from spreading. Find the minimum number of houses that will be infected in the end, if she optimally choose the houses to protect.Note that every day Cirno always chooses a house to protect before the virus spreads. Also, a protected house will not be infected forever.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Description of test cases follows. The first line of each test case consists of two positive integers $$$n, m$$$ ($$$5 \leq n \leq 10^9$$$, $$$1 \leq m \leq \min(n, 10^5)$$$) — the number of houses on the circle, and the number of houses that are initially infected.  The second line of each test case consists of $$$m$$$ distinct positive integers $$$a_1, a_2, \cdots , a_m$$$ ($$$1 \leq a_i \leq n$$$) — the indices of the houses infected initially. It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, output an integer on a separate line, which is the minimum number of infected houses in the end.

NoteIn the first test case:At the start of the first day, house $$$3$$$, $$$6$$$, $$$8$$$ are infected. Choose house $$$2$$$ to protect.At the start of the second day, house $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, $$$8$$$, $$$9$$$ are infected. Choose house $$$10$$$ to protect.At the start of the third day, no more houses are infected.In the second test case:At the start of the first day, house $$$2$$$, $$$5$$$ are infected. Choose house $$$1$$$ to protect.At the start of the second day, house $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$ are infected. No more available houses can be protected.

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	cases := readInt(input)
	for asdf := 0; asdf < cases; asdf++ {
		tmp := readArrInt(input)
		arr := readArrInt(input)
		fmt.Println(solve(tmp[0], tmp[1], arr))
	}
}

func solve(n int, m int, arr []int) int {
	sort.Ints(arr)
	gaps := []int{}
	for i := 1; i < m; i++ {
		gaps = append(gaps, arr[i]-arr[i-1]-1)
	}
	gaps = append(gaps, arr[0]-arr[m-1]+n-1)
	sort.Sort(sort.Reverse(sort.IntSlice(gaps)))
	neg_result := 0
	for i := 0; i < m; i++ {
		gaps[i] -= i*4 + 1
	}
	// fmt.Println(gaps)
	for i := 0; i < m; i++ {
		if gaps[i] >= 0 {
			neg_result += gaps[i]
		}
		if gaps[i] == 0 {
			neg_result += 1
		}
	}
	// fmt.Println("Saved: ", neg_result, "Infected: ", n-neg_result)
	return n - neg_result
}

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

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

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

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

func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

package main

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

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

	var buf bytes.Buffer

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n, m := readTwoNums(reader)
		infected := readNNums(reader, m)
		res := solve(n, m, infected)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}

	fmt.Print(buf.String())
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' || s[i] == '\r' {
			return s[:i]
		}
	}
	return s
}

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, infected []int) int {
	// 每个已感染的house，每天的贡献为1
	// 所以需要protect越多的house，应该是让传播的天数越低越好，而且应该是成对处理
	if m == 0 {
		return 0
	}

	if m == 1 {
		// use two days to protect
		return 2
	}

	var segs []Seg

	sort.Ints(infected)

	for i := 1; i < m; i++ {
		l := infected[i] - infected[i-1] - 1

		if l > 0 {
			segs = append(segs, Seg{infected[i-1] + 1, infected[i] - 1, l})
		}
	}

	segs = append(segs, Seg{infected[m-1] + 1, infected[0] - 1 + n, infected[0] - 1 + n - (infected[m-1] + 1) + 1})

	sort.Slice(segs, func(i, j int) bool {
		return segs[i].seg_len > segs[j].seg_len
	})

	var res int

	for i, day := 0, 0; i < len(segs); i++ {
		cur := segs[i]
		// 两头感染
		l := cur.left
		r := cur.right
		l += day
		r -= day
		if l < r {
			r--
			res += r - l + 1
			day += 2
		} else if l == r {
			res++
			day++
		} else {
			break
		}
	}

	return n - res
}

type Seg struct {
	left    int
	right   int
	seg_len int
}

package main

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

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

func CF1704C(in io.Reader, out io.Writer) {
	r := bufio.NewReader(in)
	w := bufio.NewWriter(out)
	defer w.Flush()

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {
		var n, m int64
		fmt.Fscan(r, &n, &m)
		arr, ll := make([]int, m), make([]int, 0)
		for i := int64(0); i < m; i++ {
			fmt.Fscan(r, &arr[i])
		}
		sort.Ints(arr)
		for i := int64(0); i < m; i++ {
			if i == 0 {
				ll = append(ll, arr[i]+int(n)-arr[m-1]-1)
				continue
			}
			ll = append(ll, arr[i]-arr[i-1]-1)
		}
		sort.Sort(sort.Reverse(sort.IntSlice(ll)))

		day, ans := 0, 0
		for _, v := range ll {
			if v > day*2 {
				if v-1-day*2 >= 2 {
					ans += v - 1 - day*2
					day += 2
				} else {
					ans += 1
					day += 1
				}
			}
		}
		fmt.Fprintln(w, int(n)-ans)
	}

}

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

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

package main

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

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

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

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

func do(n, m int, a []int) int {
	sort.Ints(a)
	var b []int
	for i, v := range a {
		x := 0
		if i == 0 {
			x = n - a[m-1] + v - 1
		} else {
			x = v - a[i-1] - 1
		}
		if x > 0 {
			b = append(b, x)
		}
	}
	sort.Ints(b)
	var k int
	var x int
	for i := len(b) - 1; i >= 0; i-- {
		c := b[i] - 2*k
		if c <= 0 {
			break
		}
		if c <= 2 {
			x++
			break
		}
		k += 2
		x += c - 1
	}
	return n - x
}

package main

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

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

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

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

		sort.Ints(aa)
		bb := []int{n - aa[m-1] + aa[0] - 1}
		for i := 1; i < m; i++ {
			bb = append(bb, aa[i]-aa[i-1]-1)
		}
		sort.Slice(bb, func(i, j int) bool { return bb[i] > bb[j] })

		var d int
		var x int
		for _, v := range bb {
			v -= 2 * d
			if v <= 0 {
				break
			}
			if v == 1 {
				x++
			} else {
				x += v - 1
			}
			d += 2
		}
		fmt.Fprintln(out, n-x)
	}
}

package main

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

func solve() {
	n := get()
	m := get()
	a := geta1(m)

	sort.Ints(a)
	d := ints(m, 0)
	for i := 0; i+1 < m; i++ {
		d[i] = a[i+1] - a[i] - 1
	}
	d[m-1] = n - a[m-1] + a[0] - 1
	sort.Sort(sort.Reverse(sort.IntSlice(d)))

	count := 0
	for i, v := range d {
		start := 2 * i
		end := v - 2*i - 1
		result := end - start
		if result <= 0 {
			if result == 0 {
				count++
			}
			break
		}
		count += result
	}
	putln(n - count)
}

const (
	once       = false
	maxBufSize = 0 //10_000_000
	YES        = "YES"
	NO         = "NO"
)

var (
	splitFunc = bufio.ScanWords //bufio.SplitFunc(nil)
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	if splitFunc != nil {
		in.Split(splitFunc)
	}
	if maxBufSize > 0 {
		in.Buffer(make([]byte, maxBufSize), maxBufSize)
	}

	out = bufio.NewWriter(os.Stdout)
	defer out.Flush()

	if once {
		solve()
		return
	}

	for t := get(); t > 0; t-- {
		solve()
	}
}

var (
	in  *bufio.Scanner
	out *bufio.Writer
)

func get() int {
	x, _ := strconv.Atoi(gets())
	return x
}

func geta(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get()
	}
	return a
}

func geta1(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get() - 1
	}
	return a
}

func get64() int64 {
	x, _ := strconv.ParseInt(gets(), 10, 64)
	return x
}

func get64a(n int) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = get64()
	}
	return a
}

func get64a1(n int) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = get64() - 1
	}
	return a
}

func getf() float64 {
	x, _ := strconv.ParseFloat(gets(), 64)
	return x
}

func getfa(n int) []float64 {
	a := make([]float64, n)
	for i := range a {
		a[i] = getf()
	}
	return a
}

func gets() string {
	scan()
	return in.Text()
}

func scanN(n int) {
	for n > 0 {
		scan()
		n--
	}
}

func scan() {
	in.Scan()
}

func putc(c rune) {
	out.WriteRune(c)
}

func line() {
	putc('\n')
}

func space() {
	putc(' ')
}

func put(x int) {
	out.WriteString(strconv.Itoa(x))
}

func putln(i int) {
	put(i)
	line()
}

func puta(a []int) {
	for _, v := range a {
		put(v)
		space()
	}
	line()
}

func put64(x int64) {
	out.WriteString(strconv.FormatInt(x, 10))
}

func put64ln(x int64) {
	put64(x)
	line()
}

func put64a(a []int64) {
	for _, v := range a {
		put64(v)
		space()
	}
	line()
}

func puts(s string) {
	out.WriteString(s)
}

func putsln(s string) {
	puts(s)
	line()
}

func yn(y bool) {
	if y {
		yes()
		return
	}
	no()
}

func yes() {
	puts(YES)
	line()
}

func no() {
	puts(NO)
	line()
}

func putf(x float64) {
	out.WriteString(strconv.FormatFloat(x, 'f', -1, 64))
}

func putfln(x float64) {
	putf(x)
	line()
}

func putfa(a []float64) {
	for _, v := range a {
		putf(v)
		space()
	}
	line()
}

type IntTp interface {
	int | int64
}

type Tp interface {
	IntTp | float64
}

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

func fabs(x float64) float64 {
	return math.Abs(x)
}

func max1[T Tp](x T, y T) T {
	if x > y {
		return x
	}
	return y
}

func max[T Tp](xs ...T) T {
	r := xs[0]
	for _, x := range xs[1:] {
		r = max1(r, x)
	}
	return r
}

func maxa[T Tp](a []T) (i int, val T) {
	i, val = 0, a[0]
	for j := 1; j < len(a); j++ {
		if a[j] <= val {
			continue
		}
		i = j
		val = a[j]
	}
	return
}

func min1[T Tp](x T, y T) T {
	if x < y {
		return x
	}
	return y
}

func min[T Tp](xs ...T) T {
	r := xs[0]
	for _, x := range xs[1:] {
		r = min1(r, x)
	}
	return r
}

func mina[T Tp](a []T) (i int, val T) {
	i, val = 0, a[0]
	for j := 1; j < len(a); j++ {
		if a[j] >= val {
			continue
		}
		i = j
		val = a[j]
	}
	return
}

func L(i int) int64 {
	return int64(i)
}

func F(i int) float64 {
	return float64(i)
}

func ints(n int, val int) (a []int) {
	a = make([]int, n)
	if val == 0 {
		return
	}
	for i := range a {
		a[i] = val
	}
	return
}

func int64s(n int, val int64) (a []int64) {
	a = make([]int64, n)
	if val == 0 {
		return
	}
	for i := range a {
		a[i] = val
	}
	return
}

func floats(n int, val float64) (a []float64) {
	a = make([]float64, n)
	if val == 0 {
		return
	}
	for i := range a {
		a[i] = val
	}
	return
}

const heapBase = 4

func children(i int) (int, int) {
	start := heapBase * i
	return start + 1, start + (1 + heapBase)
}

func parent(i int) int {
	return (i - 1) / heapBase
}

package main

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

func main() {
	var n, m int
	f := bufio.NewReader(os.Stdin)
	s, _ := f.ReadString('\n')
	s = strings.ReplaceAll(s, "\n", "")
	s = strings.ReplaceAll(s, "\r", "")
	t, _ := strconv.ParseInt(s, 10, 32)
	for i := 0; i < int(t); i++ {
		s, _ = f.ReadString('\n')
		s = strings.ReplaceAll(s, "\n", "")
		s = strings.ReplaceAll(s, "\r", "")
		_, _ = fmt.Sscanf(s, "%d %d", &n, &m)
		s, _ = f.ReadString('\n')
		s = strings.ReplaceAll(s, "\r", "")
		s = strings.ReplaceAll(s, "\n", "")
		as := strings.SplitN(s, " ", m)
		arr := make([]int, len(as))
		if len(as) != m {
			panic("kek")
		}
		for k, a := range as {
			x, _ := strconv.ParseInt(a, 10, 64)
			arr[k] = int(x)
		}
		sort.Ints(arr)
		ranges := make([]int, 0, len(as))
		safe := 0
		for j := 0; j < m-1; j++ {
			ranges = append(ranges, arr[j+1]-arr[j]-1)
		}
		ranges = append(ranges, arr[0]-1+n-arr[len(arr)-1])
		sort.Ints(ranges)
		time := 0
		k := len(ranges) - 1
		for k >= 0 {
			if ranges[k] > time*2+1 {
				safe += ranges[k] - time*2 - 1
				time += 2
				k--
			} else if ranges[k] == time*2+1 {
				safe++
				k--
				time++
			} else {
				break
			}
		}
		fmt.Println(n - safe)
	}
}

package main

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

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

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

func main() {
	scanner := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var niter int
	fmt.Fscan(scanner, &niter)
	for iiter := 0; iiter < niter; iiter++ {
		var n int
		var m int
		fmt.Fscan(scanner, &n, &m)
		res := int64(0)
		cor := make([]int, m)
		intervals := make([]int, 0)
		for i := 0; i < m; i++ {
			fmt.Fscan(scanner, &cor[i])
		}
		sort.Slice(cor, func(i, j int) bool { return cor[i] < cor[j] })
		for i := 1; i < m; i++ {
			intervals = append(intervals, cor[i]-cor[i-1]-1)
		}
		intervals = append(intervals, cor[0]+n-cor[len(cor)-1]-1)
		sort.Slice(intervals, func(i, j int) bool { return intervals[i] > intervals[j] })
		//fmt.Println("!!!", intervals, cor)
		day := 0
		for i := 0; i < len(intervals); i++ {
			tmp := 0
			if intervals[i]-day*2 != 1 {
				tmp = max(intervals[i]-1-day*2, 0)
				day += 2
			} else {
				tmp = 1
				day += 2
			}
			res += int64(tmp)
			if tmp <= 0 {
				break
			}
			//fmt.Println("!!!", intervals, day, res, "!!!", i)
		}
		fmt.Fprintln(writer, int64(n)-res)
	}
}

package main

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

const cf = true

func solve() {
	var n, m int
	fmt.Fscan(in, &n, &m)
	arr := make([]int, m)
	for i := range arr {
		fmt.Fscan(in, &arr[i])
	}
	sort.Slice(arr, func(i, j int) bool {
		return arr[i] < arr[j]
	})
	packs := make([]int, 0, m+2)
	packs = append(packs, arr[0]-1+(n-arr[m-1]))
	for i := 1; i < m; i++ {
		packs = append(packs, arr[i]-arr[i-1]-1)
	}
	sort.Slice(packs, func(i, j int) bool {
		return packs[i] > packs[j]
	})
	sub, ans := 0, 0
	for i := range packs {
		toAdd := packs[i] - sub
		if toAdd != 1 {
			toAdd -= 1
		}
		sub += 4
		if toAdd <= 0 {
			break
		}
		ans += toAdd
	}
	fmt.Fprintln(out, n-ans)
}

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

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

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

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

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

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

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

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

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

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

var exists = struct{}{}

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

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

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

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

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

type Ordered interface {
	Integer | Float | ~string
}

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

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

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

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

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

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

package main

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

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

func slove() {
	var n, m int
	fmt.Fscan(rr, &n, &m)
	nums := make([]int, m)
	for i:=0;i<len(nums);i++ {
		fmt.Fscan(rr, &nums[i])
		nums[i]--
	}
	sort.Ints(nums)
	intervals := make([]int, 0)
	for i:=1;i<len(nums);i++ {
		if nums[i] - nums[i-1] > 1 {
			intervals = append(intervals, nums[i] - nums[i-1] - 1)
		}
	}
	if n - nums[m-1] - 1 + nums[0] >= 1 {
		intervals = append(intervals, n - nums[m-1] - 1 + nums[0])
	}
	sort.Slice(intervals, func(i, j int) bool {
		return intervals[j] < intervals[i]
	})
	cnt := 0
	ans := 0
	for i := range intervals {
		if intervals[i] - cnt * 2 <= 0 {
			fmt.Fprintln(ww, n - ans)
			return
		} else if intervals[i] - cnt * 2 == 1 {
			fmt.Fprintln(ww, n - ans - 1)
			return
		}
		ans += intervals[i] - cnt * 2 - 1
		cnt += 2
	}
	fmt.Fprintln(ww, n - ans)
}

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

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	cases := readInt(input)
	for asdf := 0; asdf < cases; asdf++ {
		tmp := readArrInt(input)
		arr := readArrInt(input)
		fmt.Println(solve(tmp[0], tmp[1], arr))
	}
}

func solve(n int, m int, arr []int) int {
	sort.Ints(arr)
	gaps := []int{}
	for i := 1; i < m; i++ {
		gaps = append(gaps, arr[i]-arr[i-1]-1)
	}
	gaps = append(gaps, arr[0]-arr[m-1]+n-1)
	sort.Sort(sort.Reverse(sort.IntSlice(gaps)))
	neg_result := 0
	cheat := 0
	// fmt.Println(gaps)
	for i := 0; i < m; i++ {
		if cheat >= gaps[i] {
			if cheat-gaps[i] == 3 && gaps[i-1] < cheat-2 {
				neg_result++
			}
			break
		}
		neg_result += gaps[i] - 1 - cheat
		cheat += 4
	}
	// fmt.Println("Saved: ", neg_result, "Infected: ", n-neg_result)
	return n - neg_result
}

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

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

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

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

func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	cases := readInt(input)
	for asdf := 0; asdf < cases; asdf++ {
		tmp := readArrInt(input)
		arr := readArrInt(input)
		fmt.Println(solve(tmp[0], tmp[1], arr))
	}
}

func solve(n int, m int, arr []int) int {
	sort.Ints(arr)
	gaps := []int{}
	for i := 1; i < m; i++ {
		gaps = append(gaps, arr[i]-arr[i-1]-1)
	}
	gaps = append(gaps, arr[0]-arr[m-1]+n-1)
	sort.Sort(sort.Reverse(sort.IntSlice(gaps)))
	neg_result := 0
	cheat := 0
	// fmt.Println(gaps)
	for i := 0; i < m; i++ {
		if cheat >= gaps[i] {
			if cheat-gaps[i] == 3 && gaps[i-1] < cheat {
				neg_result++
			}
			break
		}
		neg_result += gaps[i] - 1 - cheat
		cheat += 4
	}
	// fmt.Println("Saved: ", neg_result, "Infected: ", n-neg_result)
	return n - neg_result
}

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

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

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

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

func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}


This is a hard version of the problem. In this version, the given array can contain equal elements and the constraints on $$$n$$$ are greater than in the easy version of the problem.You are given an array $$$a$$$ of $$$n$$$ integers (the given array can contain equal elements). You can perform the following operations on array elements:  choose any index $$$i$$$ ($$$1 \le i \le n$$$) and move the element $$$a[i]$$$ to the begin of the array;  choose any index $$$i$$$ ($$$1 \le i \le n$$$) and move the element $$$a[i]$$$ to the end of the array. For example, if $$$n = 5$$$, $$$a = [4, 7, 2, 2, 9]$$$, then the following sequence of operations can be performed:   after performing the operation of the first type to the second element, the array $$$a$$$ will become $$$[7, 4, 2, 2, 9]$$$;  after performing the operation of the second type to the second element, the array $$$a$$$ will become $$$[7, 2, 2, 9, 4]$$$. You can perform operations of any type any number of times in any order.Find the minimum total number of operations of the first and second type that will make the $$$a$$$ array sorted in non-decreasing order. In other words, what is the minimum number of operations must be performed so the array satisfies the inequalities $$$a[1] \le a[2] \le \ldots \le a[n]$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array $$$a$$$. Then follow $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) — an array that needs to be sorted by the given operations. The given array can contain equal elements. The sum of $$$n$$$ for all test cases in one test does not exceed $$$2 \cdot 10^5$$$.

For each test case output one integer — the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.

NoteIn the first test case, you first need to move two 2, to the beginning of the array. Therefore, the desired sequence of operations: $$$[4, 7, 2, 2, 9] \rightarrow [2, 4, 7, 2, 9] \rightarrow [2, 2, 4, 7, 9]$$$.In the second test case, you need to move the 1 to the beginning of the array, and the 8 — to the end. Therefore, the desired sequence of operations: $$$[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$$$.In the third test case, the array is already sorted.

package main

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

type Node struct {
	i, v, l, f, ll int
	h              bool
}

type dataSlice []Node

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

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

	var t, n int

	fmt.Fscanf(reader, "%d\n", &t)
	for ; t > 0; t-- {

		fmt.Fscanf(reader, "%d\n", &n)
		datas := make(dataSlice, n)
		for i := 0; i < n; i++ {
			if i+1 == n {
				fmt.Fscanf(reader, "%d\n", &datas[i].v)
			} else {
				fmt.Fscanf(reader, "%d ", &datas[i].v)
			}
			datas[i].i = i
		}

		sort.Sort(datas)

		for i := 0; i < n; i++ {
			if i > 0 && datas[i].v == datas[i-1].v {
				datas[i].f = datas[i-1].f
			} else {
				datas[i].f = i
			}
		}

		ans := 1

		bsearch := func(l, r, i int) int {
			//fmt.Println("search", l, r, i)
			var mid int
			left := l

			for l < r-1 {
				mid = l + (r-l)/2
				if datas[mid].i > i {
					r = mid
				} else {
					l = mid
				}
			}

			for ; l <= r; l++ {
				if datas[l].i > i {
					return l - left
				}
			}

			if datas[r].h {
				return datas[r].l
			} else {
				return datas[r].ll
			}
		}

		datas[0].l = 1
		datas[0].ll = 1

		for i := 1; i < n; i++ {
			if datas[i].i > datas[i-1].i {
				datas[i].l = datas[i-1].l + 1
			} else {
				datas[i].l = 1
			}

			if datas[i].v == datas[i-1].v {
				datas[i].ll = datas[i-1].ll + 1
				datas[i].h = datas[i-1].h
			} else {
				datas[i].ll = datas[i].l
				datas[i].h = false
			}

			// fmt.Println("before", i, datas[i].l, datas[i].ll, datas[i].h)
			r := max(datas[i].f-1, 0)
			l := datas[r].f
			ll := bsearch(l, r, datas[i].i) + 1
			if datas[i].ll < ll {
				if datas[i].v == datas[i-1].v {
					datas[i].h = true
				}

				datas[i].ll = ll
			}

			if !datas[i].h {
				datas[i].l = datas[i].ll
			}

			// fmt.Println(ll, "after", i, datas[i].l, datas[i].ll, datas[i].h)
			// fmt.Println()

			if datas[i].ll > ans {
				ans = datas[i].ll
			}

		}

		fmt.Println(n - ans)
	}
}

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

	return b
}

package main

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

type Node struct {
	i, v, l, f, ll int
}

type dataSlice []Node

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

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

	var t, n int

	fmt.Fscanf(reader, "%d\n", &t)
	for ; t > 0; t-- {
		fmt.Fscanf(reader, "%d\n", &n)
		datas := make(dataSlice, n)
		for i := 0; i < n; i++ {
			if i+1 == n {
				fmt.Fscanf(reader, "%d\n", &datas[i].v)
			} else {
				fmt.Fscanf(reader, "%d ", &datas[i].v)
			}
			datas[i].i = i
			datas[i].l = 1
			datas[i].ll = 1
		}

		sort.Sort(datas)

		for i := 0; i < n; i++ {
			if i > 0 && datas[i].v == datas[i-1].v {
				datas[i].f = datas[i-1].f
			} else {
				datas[i].f = i
			}
		}

		ans := 1
		// fmt.Println(datas)

		for i := 1; i < n; i++ {
			if datas[i].i > datas[i-1].i {
				datas[i].l = datas[i-1].l + 1
				datas[i].ll = datas[i].l
			}
		}

		for i := 1; i < n; i++ {
			if datas[i].i > datas[i-1].i {
				datas[i].ll = datas[i-1].ll + 1
			}

			if datas[i].v == datas[i-1].v && datas[i].f > 0 {
				if datas[i].i > datas[datas[i].f-1].i {
					datas[i].ll = max(datas[i].ll, datas[datas[i].f-1].l+1)
				}
			}

			if datas[i].v != datas[i-1].v && datas[i].i < datas[i-1].i {
				for k := i - 2; k >= 0; k-- {
					if datas[k].v != datas[i-1].v {
						break
					}

					if datas[i].i > datas[k].i {
						datas[i].ll = max(datas[i].ll, datas[k].l+1)
						break
					}
				}
			}

			if datas[i].ll > ans {
				ans = datas[i].ll
			}
		}

		fmt.Println(n - ans)
	}
}

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

	return b
}

package main

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

type Node struct {
	i, v, l, f, ll int
	h              bool
}

type dataSlice []Node

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

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

	var t, n int

	fmt.Fscanf(reader, "%d\n", &t)
	for ; t > 0; t-- {
		fmt.Fscanf(reader, "%d\n", &n)
		datas := make(dataSlice, n)
		for i := 0; i < n; i++ {
			if i+1 == n {
				fmt.Fscanf(reader, "%d\n", &datas[i].v)
			} else {
				fmt.Fscanf(reader, "%d ", &datas[i].v)
			}
			datas[i].i = i
			datas[i].l = 1
		}

		sort.Sort(datas)

		for i := 0; i < n; i++ {
			if i > 0 && datas[i].v == datas[i-1].v {
				datas[i].f = datas[i-1].f
			} else {
				datas[i].f = i
			}
		}

		ans := 1
		// fmt.Println(datas)
		bsearch := func(l, r, i int) int {
			var mid int
			left := l

			for l < r-1 {
				mid = l + (r-l)/2
				if datas[mid].i > i {
					r = mid
				} else {
					l = mid
				}
			}

			for ; l <= r; l++ {
				if datas[l].i > i {
					return l - left
				}
			}

			if datas[r].h {
				return datas[r].l
			} else {
				return datas[r].ll
			}
		}

		datas[0].ll = datas[0].l
		for i := 1; i < n; i++ {
			if datas[i].i > datas[i-1].i {
				datas[i].l = datas[i-1].l + 1
			}

			datas[i].ll = datas[i].l
		}

		for i := 1; i < n; i++ {
			if datas[i].i > datas[i-1].i && datas[i].v == datas[i-1].v {
				datas[i].ll = datas[i-1].ll + 1
				datas[i].h = datas[i-1].h
			}

			if datas[i].i > datas[i-1].i && datas[i].v != datas[i-1].v && datas[i-1].h {
				datas[i].ll = datas[i-1].l + 1
			}

			if datas[i].i > datas[i-1].i && datas[i].v != datas[i-1].v && !datas[i-1].h {
				datas[i].ll = datas[i-1].ll + 1
			}

			r := max(datas[i].f-1, 0)
			l := datas[r].f
			ll := bsearch(l, r, datas[i].i) + 1
			if datas[i].ll < ll {
				if datas[i].v == datas[i-1].v {
					datas[i].h = true
				}

				datas[i].ll = ll

			}

			if datas[i].ll > ans {
				ans = datas[i].ll
			}

			// fmt.Println(i, datas[i].l)
		}

		fmt.Println(n - ans)
	}
}

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

	return b
}

package main

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

type Node struct {
	i, v, l, f, ll int
	h              bool
}

type dataSlice []Node

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

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

	var t, n, num int

	fmt.Fscanf(reader, "%d\n", &t)
	for ; t > 0; t-- {
		num++
		fmt.Fscanf(reader, "%d\n", &n)
		datas := make(dataSlice, n)
		for i := 0; i < n; i++ {
			if i+1 == n {
				fmt.Fscanf(reader, "%d\n", &datas[i].v)
			} else {
				fmt.Fscanf(reader, "%d ", &datas[i].v)
			}
			datas[i].i = i
			datas[i].l = 1
		}

		if t+num >= 10000 {
			if num == 1732 {
				for i := 0; i < n; i++ {
					fmt.Printf("%d ", datas[i].v)
				}
			} else {
				continue
			}
		}

		sort.Sort(datas)

		for i := 0; i < n; i++ {
			if i > 0 && datas[i].v == datas[i-1].v {
				datas[i].f = datas[i-1].f
			} else {
				datas[i].f = i
			}
		}

		ans := 1
		// fmt.Println(datas)
		bsearch := func(l, r, i int) int {
			var mid int
			left := l

			for l < r-1 {
				mid = l + (r-l)/2
				if datas[mid].i > i {
					r = mid
				} else {
					l = mid
				}
			}

			for ; l <= r; l++ {
				if datas[l].i > i {
					return l - left
				}
			}

			if datas[r].h {
				return datas[r].l
			} else {
				return datas[r].ll
			}
		}

		datas[0].ll = datas[0].l
		for i := 1; i < n; i++ {
			if datas[i].i > datas[i-1].i {
				datas[i].l = datas[i-1].l + 1
			}

			datas[i].ll = datas[i].l
		}

		for i := 1; i < n; i++ {
			if datas[i].i > datas[i-1].i && datas[i].v == datas[i-1].v {
				datas[i].ll = datas[i-1].ll + 1
				datas[i].h = datas[i-1].h
			}

			if datas[i].i > datas[i-1].i && datas[i].v != datas[i-1].v && datas[i-1].h {
				datas[i].ll = datas[i-1].l + 1
			}

			if datas[i].i > datas[i-1].i && datas[i].v != datas[i-1].v && !datas[i-1].h {
				datas[i].ll = datas[i-1].ll + 1
			}

			r := max(datas[i].f-1, 0)
			l := datas[r].f
			ll := bsearch(l, r, datas[i].i) + 1
			if datas[i].ll < ll {
				if datas[i].v == datas[i-1].v {
					datas[i].h = true
				}

				datas[i].ll = ll
			}

			if datas[i].ll > ans {
				ans = datas[i].ll
			}

			// fmt.Println(i, datas[i].l)
		}

		fmt.Println(n - ans)
	}
}

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

	return b
}


Ayush and Ashish play a game on an unrooted tree consisting of $$$n$$$ nodes numbered $$$1$$$ to $$$n$$$. Players make the following move in turns:   Select any leaf node in the tree and remove it together with any edge which has this node as one of its endpoints. A leaf node is a node with degree less than or equal to $$$1$$$. A tree is a connected undirected graph without cycles.There is a special node numbered $$$x$$$. The player who removes this node wins the game. Ayush moves first. Determine the winner of the game if each player plays optimally.

The first line of the input contains a single integer $$$t$$$ $$$(1 \leq t \leq 10)$$$ — the number of testcases. The description of the test cases follows. The first line of each testcase contains two integers $$$n$$$ and $$$x$$$ $$$(1\leq n \leq 1000, 1 \leq x \leq n)$$$ — the number of nodes in the tree and the special node respectively. Each of the next $$$n-1$$$ lines contain two integers $$$u$$$, $$$v$$$ $$$(1 \leq u, v \leq n, \text{ } u \ne v)$$$, meaning that there is an edge between nodes $$$u$$$ and $$$v$$$ in the tree.

For every test case, if Ayush wins the game, print "Ayush", otherwise print "Ashish" (without quotes).

NoteFor the $$$1$$$st test case, Ayush can only remove node $$$2$$$ or $$$3$$$, after which node $$$1$$$ becomes a leaf node and Ashish can remove it in his turn.For the $$$2$$$nd test case, Ayush can remove node $$$2$$$ in the first move itself.

package main

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

func readline(r *bufio.Reader) []int {
	l, _ , err := r.ReadLine()
	if err != nil {
		panic(err)
	}
	var res []int
	for _, s := range strings.Split(string(l), " ") {
		n, err := strconv.ParseInt(s, 10, 64)
		if err != nil {
			panic(err)
		}
		res = append(res, int(n))
	}
	return res
}

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

	r := bufio.NewReader(os.Stdin)
	for i := 0; i < t; i++ {
		nx := readline(r)
		n, x := nx[0], nx[1]

		var degree int
		for j := 0; j < n-1; j++ {
			edge := readline(r)
			if edge[0] == x || edge[1] == x {
				degree++
			}
		}

		if degree <= 1 {
			fmt.Printf("Ayush\n")
		} else {
			if n % 2 == 0 {
				fmt.Printf("Ayush\n")
			} else {
				fmt.Printf("Ashish\n")
			}
		}
	}
}

package main

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

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

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n, x := readTwoNums(reader)
		E := make([][]int, n-1)
		for i := 0; i < n-1; i++ {
			E[i] = readNNums(reader, 2)
		}
		fmt.Println(solve(n, x, E))
	}
}

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
}

const A = "Ayush"
const B = "Ashish"

func solve(n int, x int, E [][]int) string {
	if n == 1 {
		return A
	}
	degree := make([]int, n+1)
	for _, e := range E {
		u, v := e[0], e[1]
		degree[u]++
		degree[v]++
	}

	if degree[x] == 1 {
		return A
	}
	if n&1 == 1 {
		return B
	}
	return A
}

package main

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

var checked []bool
var edges [][]int
var cnt int

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		n, x := 0, 0
		fmt.Fscanf(in, "%d %d\n", &n, &x)
		x--
		edges = make([][]int, n)
		checked = make([]bool, n)
		for i := 0; i < n-1; i++ {
			o, p := 0, 0
			fmt.Fscanf(in, "%d %d\n", &o, &p)
			o--
			p--
			//fmt.Println(o, p)
			edges[o] = append(edges[o], p)
			edges[p] = append(edges[p], o)
		}
		ds := []int{}
		checked[x] = true
		for _, q := range edges[x] {
			cnt = 0
			dfs(q)
			ds = append(ds, cnt)
		}
		if canWin(ds) {
			fmt.Fprintf(out, "Ayush\n")
		} else {
			fmt.Fprintf(out, "Ashish\n")
		}
	}
	return
}

func canWin(a []int) bool {
	//fmt.Println(a)
	if len(a) <= 1 {
		return true
	}
	sum := 0
	for i := range a {
		sum += a[i]
	}
	if (sum-2)%2 == 0 {
		return false
	}
	return true
}

func dfs(cur int) {
	cnt++
	checked[cur] = true
	for _, next := range edges[cur] {
		if !checked[next] {
			dfs(next)
		}
	}
}

package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

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

	for t := in(); t > 0; t-- {
		n, x := in(), in()
		x--

		g := make([][]int, n)

		for i := range g {
			g[i] = make([]int, 0)
		}

		for i := 0; i < n-1; i++ {
			u, v := in(), in()
			g[u-1] = append(g[u-1], v-1)
			g[v-1] = append(g[v-1], u-1)
		}

		if len(g[x]) <= 1 {
			fmt.Println("Ayush")
		} else {
			z := n - 2

			if z%2 == 0 {
				fmt.Println("Ayush")
			} else {
				fmt.Println("Ashish")
			}
		}
	}
}

/*
URL:
https://codeforces.com/problemset/problem/1363/C
*/

package main

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

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const (
	// General purpose
	MOD          = 1000000000 + 7
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	ReadString = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

var (
	t    int
	n, x int
	D    []int
)

func main() {
	t = ReadInt()
	for i := 0; i < t; i++ {
		n, x = ReadInt2()
		x--
		D = make([]int, n)
		for j := 0; j < n-1; j++ {
			a, b := ReadInt2()
			a--
			b--

			D[a]++
			D[b]++
		}

		solve()
	}
}

func solve() {
	if D[x] == 1 || n == 1 {
		fmt.Println("Ayush")
		return
	}

	if n%2 == 0 {
		fmt.Println("Ayush")
	} else {
		fmt.Println("Ashish")
	}
}

/*******************************************************************/

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintfDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintfDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1363C(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var t, n, x, v, w int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &x)
		x--
		g := make([][]int, n)
		for i := 1; i < n; i++ {
			Fscan(in, &v, &w)
			v--
			w--
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}
		if len(g[x]) <= 1 || n&1 == 0 {
			Fprintln(out, "Ayush")
		} else {
			Fprintln(out, "Ashish")
		}
	}
}

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

package main

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

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var t int
	fmt.Fscan(r, &t)
	for ; t > 0; t-- {
		var n, x int
		fmt.Fscan(r, &n, &x)
		x--
		e := make([]int, n)
		for i := 0; i < n-1; i++ {
			var u, v int
			fmt.Fscan(r, &u, &v)
			u--
			v--
			e[u]++
			e[v]++
		}
		if e[x] <= 1 {
			fmt.Fprintln(w, "Ayush")
			continue
		}
		if (n-1)%2 == 1 {
			fmt.Fprintln(w, "Ayush")
		} else {
			fmt.Fprintln(w, "Ashish")
		}
	}
}

package main

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

func readline(r *bufio.Reader) []int {
	l, _ , err := r.ReadLine()
	if err != nil {
		panic(err)
	}
	var res []int
	for _, s := range strings.Split(string(l), " ") {
		n, err := strconv.ParseInt(s, 10, 64)
		if err != nil {
			panic(err)
		}
		res = append(res, int(n))
	}
	return res
}

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

	r := bufio.NewReader(os.Stdin)
	for i := 0; i < t; i++ {
		nx := readline(r)
		n, x := nx[0], nx[1]

		var degree int
		for j := 0; j < n-1; j++ {
			edge := readline(r)
			if edge[0] == x || edge[1] == x {
				degree++
			}
		}

		if degree == 1 {
			fmt.Printf("Ayush\n")
		} else {
			if n % 2 == 0 {
				fmt.Printf("Ayush\n")
			} else {
				fmt.Printf("Ashish\n")
			}
		}
	}
}

package main

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

var checked []bool
var edges [][]int
var cnt int

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		n, x := 0, 0
		fmt.Fscanf(in, "%d %d\n", &n, &x)
		x--
		edges = make([][]int, n)
		checked = make([]bool, n)
		for i := 0; i < n-1; i++ {
			o, p := 0, 0
			fmt.Fscanf(in, "%d %d\n", &o, &p)
			o--
			p--
			//fmt.Println(o, p)
			edges[o] = append(edges[o], p)
			edges[p] = append(edges[p], o)
		}
		ds := []int{}
		checked[x] = true
		for _, q := range edges[x] {
			cnt = 0
			dfs(q)
			ds = append(ds, cnt)
		}
		if canWin(ds) {
			fmt.Fprintf(out, "Ayush\n")
		} else {
			fmt.Fprintf(out, "Ashish\n")
		}
	}
	return
}

func canWin(a []int) bool {
	//fmt.Println(a)
	if len(a) <= 1 {
		return true
	}
	sum := 0
	for i := range a {
		sum += a[i]
	}
	if (sum-len(a))%2 == 0 {
		return false
	}
	return true
}

func dfs(cur int) {
	cnt++
	checked[cur] = true
	for _, next := range edges[cur] {
		if !checked[next] {
			dfs(next)
		}
	}
}

/*
URL:
https://codeforces.com/problemset/problem/1363/C
*/

package main

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

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const (
	// General purpose
	MOD          = 1000000000 + 7
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	ReadString = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

var (
	t    int
	n, x int
	D    []int
)

func main() {
	t = ReadInt()
	for i := 0; i < t; i++ {
		n, x = ReadInt2()
		x--
		D = make([]int, n)
		for j := 0; j < n-1; j++ {
			a, b := ReadInt2()
			a--
			b--

			D[a]++
			D[b]++
		}

		solve()
	}
}

func solve() {
	if D[x] == 1 {
		fmt.Println("Ayush")
		return
	}

	if n%2 == 0 {
		fmt.Println("Ayush")
	} else {
		fmt.Println("Ashish")
	}
}

/*******************************************************************/

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintfDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintfDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1363C(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var t, n, x, v, w int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &x)
		x--
		g := make([][]int, n)
		for i := 1; i < n; i++ {
			Fscan(in, &v, &w)
			v--
			w--
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}
		if len(g[x]) == 1 || (n-3)&1 == 1 {
			Fprintln(out, "Ayush")
		} else {
			Fprintln(out, "Ashish")
		}
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1363C(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var t, n, x, v, w int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &x)
		x--
		g := make([][]int, n)
		for i := 1; i < n; i++ {
			Fscan(in, &v, &w)
			v--
			w--
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}
		if len(g[x]) == 1 || (n-1-len(g[x]))&1 == 1 {
			Fprintln(out, "Ayush")
		} else {
			Fprintln(out, "Ashish")
		}
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1363C(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var t, n, x, v, w int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &x)
		x--
		g := make([][]int, n)
		for i := 1; i < n; i++ {
			Fscan(in, &v, &w)
			v--
			w--
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}
		if len(g[x]) == 1 || n&1 == 0 {
			Fprintln(out, "Ayush")
		} else {
			Fprintln(out, "Ashish")
		}
	}
}

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

package main

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

var (
	e [][]int
	n int
)

func dfs(u, p int) int {
	var ret int
	for _, v := range e[u] {
		if v == p {
			continue
		}
		ret += dfs(v, u)
	}
	return ret + 1
}

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var t int
	fmt.Fscan(r, &t)
	for ; t > 0; t-- {
		var x int
		fmt.Fscan(r, &n, &x)
		x--
		e = make([][]int, n)
		for i := 0; i < n-1; i++ {
			var u, v int
			fmt.Fscan(r, &u, &v)
			u--
			v--
			e[u] = append(e[u], v)
			e[v] = append(e[v], u)
		}
		if len(e[x]) == 1 {
			fmt.Fprintln(w, "Ayush")
			continue
		}
		ret := dfs(x, x) - 1
		if ret%2 == 0 {
			fmt.Fprintln(w, "Ashish")
		} else {
			fmt.Fprintln(w, "Ayush")
		}
	}
}

package main

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

var (
	e [][]int
	n int
)

func dfs(u, p int) int {
	var ret int
	for _, v := range e[u] {
		if v == p {
			continue
		}
		ret += dfs(v, u)
	}
	return ret
}

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var t int
	fmt.Fscan(r, &t)
	for ; t > 0; t-- {
		var x int
		fmt.Fscan(r, &n, &x)
		x--
		e = make([][]int, n)
		for i := 0; i < n-1; i++ {
			var u, v int
			fmt.Fscan(r, &u, &v)
			u--
			v--
			e[u] = append(e[u], v)
			e[v] = append(e[v], u)
		}
		if len(e[x]) == 1 {
			fmt.Fprintln(w, "Ayush")
			continue
		}
		ret := dfs(x, x)
		if ret%2 == 0 {
			fmt.Fprintln(w, "Ashish")
		} else {
			fmt.Fprintln(w, "Ayush")
		}
	}
}


Petya has n integers: 1, 2, 3, ..., n. He wants to split these integers in two non-empty groups in such a way that the absolute difference of sums of integers in each group is as small as possible. Help Petya to split the integers. Each of n integers should be exactly in one group.

The first line contains a single integer n (2 ≤ n ≤ 60 000) — the number of integers Petya has.

Print the smallest possible absolute difference in the first line. In the second line print the size of the first group, followed by the integers in that group. You can print these integers in arbitrary order. If there are multiple answers, print any of them.

NoteIn the first example you have to put integers 1 and 4 in the first group, and 2 and 3 in the second. This way the sum in each group is 5, and the absolute difference is 0.In the second example there are only two integers, and since both groups should be non-empty, you have to put one integer in the first group and one in the second. This way the absolute difference of sums of integers in each group is 1.

package main

import (
	"fmt"
)

func main() {
	var n int32
	fmt.Scan(&n)
	if n%2 == 0 {
		fmt.Println(n % 4 / 2)
		fmt.Print(n / 2)
		for i := int32(1); i < n/2; i++ {
			if i%2 == 0 {
				continue
			}
			fmt.Printf(" %d %d", i, n-i+1)
		}
		if n%4 == 2 {
			fmt.Printf(" %d", n/2)
		}
		fmt.Println()
		return
	}
	fmt.Println((n + 1) % 4 / 2)
	fmt.Print(n / 2)
	for i := int32(2); i <= n/2; i++ {
		if i%2 == 1 {
			continue
		}
		fmt.Printf(" %d %d", i, n-i+2)
	}
	if (n-1)%4 != 0 {
		fmt.Printf(" %d", n/2+2)
	}
	fmt.Println()
}

package main

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

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

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

func toInt(buf []byte) (o int) {
	for _, v := range buf {
		o = o*10 + int(v-'0')
	}
	return
}

type pair struct {
	first int
	second int
}

type pairs []pair

func (a pairs) Len() int {return len(a)}
func (a pairs) Swap(i,j int) {a[i], a[j] = a[j], a[i]}
func (a pairs) Less(i,j int) bool {
	return a[i].first < a[j].first
	if a[i].first > a[j].first {
		return false
	}
	return a[i].second <= a[j].second
}

func main() {
	in := bufio.NewReader(os.Stdin)
	//in, _ := os.Open("a.txt")
	var n int
	fmt.Fscanf(in, "%d", &n)
	sum := (int64(n) * int64(n + 1)) / 2
	fmt.Println(sum % 2)
	if n % 2 == 0 {
		fmt.Print(n / 2," ")
		for i := 1; i < n / 2; i +=2 {
			fmt.Print(i, " ", n - i + 1, " ")
		}
		if sum % 2 == 1 {
			fmt.Println(n / 2)
		}
	} else {
		idx := int64(n) - 1
		var ans []int64
		//ans = append(ans, int64(n))
		var t int64
		for t = int64(n); t <= sum / 2 + sum % 2; t,idx = t + idx, idx - 1 {
			if t <= sum / 2 + sum % 2 {
				ans = append(ans, idx + 1)
			}
		}
		if sum / 2 + sum % 2 - (t - idx - 1) != 0 {
			ans = append(ans, sum / 2 + sum % 2 - (t - idx - 1))
		}
		fmt.Print(len(ans)," ")
		for i := range ans {
			fmt.Print(ans[i]," ")
		}
	}
}

package main

import "fmt"

func main(){
	var n int
	fmt.Scan(&n)
	tmp := n
	var result[] int
	for ;; {
		if tmp < 4{
			break
		}
		result = append(result, tmp)
		result = append(result, tmp - 3)
		tmp -= 4
	}
	if tmp == 0{
		fmt.Println(0)
	}else  if tmp == 1{
		fmt.Println(1)
		result = append(result, 1)
	}else if tmp == 2{
		fmt.Println(1)
		result = append(result, 1)
	}else if tmp == 3{
		fmt.Println(0)
		result = append(result, 3)
	}
	fmt.Print(len(result))
	for _, each := range result{
		fmt.Printf(" %d", each)
	}
	fmt.Println()
}


Nian is a monster which lives deep in the oceans. Once a year, it shows up on the land, devouring livestock and even people. In order to keep the monster away, people fill their villages with red colour, light, and cracking noise, all of which frighten the monster out of coming.Little Tommy has n lanterns and Big Banban has m lanterns. Tommy's lanterns have brightness a1, a2, ..., an, and Banban's have brightness b1, b2, ..., bm respectively.Tommy intends to hide one of his lanterns, then Banban picks one of Tommy's non-hidden lanterns and one of his own lanterns to form a pair. The pair's brightness will be the product of the brightness of two lanterns.Tommy wants to make the product as small as possible, while Banban tries to make it as large as possible.You are asked to find the brightness of the chosen pair if both of them choose optimally.

The first line contains two space-separated integers n and m (2 ≤ n, m ≤ 50). The second line contains n space-separated integers a1, a2, ..., an. The third line contains m space-separated integers b1, b2, ..., bm. All the integers range from  - 109 to 109.

Print a single integer — the brightness of the chosen pair.

NoteIn the first example, Tommy will hide 20 and Banban will choose 18 from Tommy and 14 from himself.In the second example, Tommy will hide 3 and Banban will choose 2 from Tommy and 1 from himself.

package main

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

func main() {
	sc := NewScanner()
	n, m := sc.NextInt(), sc.NextInt()
	a := sc.NextInt64Array()
	b := sc.NextInt64Array()

	mx := int64(-1e18)
	p := 0
	for i := 0; i < n; i++ {
		for k := 0; k < m; k++ {
			if mx < a[i]*b[k] {
				mx = a[i] * b[k]
				p = i
			}
		}
	}

	a = append(a[:p], a[p+1:]...)

	mx = int64(-1e18)
	for i := 0; i < n-1; i++ {
		for k := 0; k < m; k++ {
			if mx < a[i]*b[k] {
				mx = a[i] * b[k]
			}
		}
	}

	fmt.Println(mx)
}

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"
	"strconv"
)

func main() {
	sc := NewScanner()
	n, m := sc.NextInt(), sc.NextInt()
	a := sc.NextInt64Array()
	b := sc.NextInt64Array()

	mx := int64(-1e9)
	p := 0
	for i := 0; i < n; i++ {
		for k := 0; k < m; k++ {
			if mx < a[i]*b[k] {
				mx = a[i] * b[k]
				p = i
			}
		}
	}

	a = append(a[:p], a[p+1:]...)

	mx = int64(-1e9)
	for i := 0; i < n-1; i++ {
		for k := 0; k < m; k++ {
			if mx < a[i]*b[k] {
				mx = a[i] * b[k]
			}
		}
	}

	fmt.Println(mx)
}

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

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

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

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

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

	return mp
}

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

package main

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

func main() {
	sc := NewScanner()
	_, _ = sc.NextInt(), sc.NextInt()
	a := sc.NextIntArray()
	b := sc.NextIntArray()

	sort.Ints(a)
	sort.Ints(b)
	a = a[:len(a)-1]
	ans := a[len(a)-1] * b[len(b)-1]
	fmt.Println(ans)
}

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

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

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

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

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

	return mp
}

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

package main

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

func main() {
	sc := NewScanner()
	n, m := sc.NextInt(), sc.NextInt()
	a := sc.NextIntArray()
	b := sc.NextIntArray()

	list := []int{0, 0}
	sort.Ints(a)
	sort.Ints(b)
	if a[0]*b[0] < a[n-1]*b[m-1] {
		a = a[:len(a)-1]
	} else {
		a = a[1:]
	}
	for i := 0; i < n-1; i++ {
		for k := 0; k < m; k++ {
			d := a[i] * b[k]
			list = append(list, d)
			sort.Ints(list)
			list = list[1:]
		}
	}

	fmt.Println(list[1])
}

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"
	"strconv"
)

func main() {
	sc := NewScanner()
	n, m := sc.NextInt(), sc.NextInt()
	a := sc.NextIntArray()
	b := sc.NextIntArray()

	mx := int(-1e9)
	p := 0
	for i := 0; i < n; i++ {
		for k := 0; k < m; k++ {
			if mx < a[i]*b[k] {
				mx = a[i] * b[k]
				p = i
			}
		}
	}

	a = append(a[:p], a[p+1:]...)

	mx = int(-1e9)
	for i := 0; i < n-1; i++ {
		for k := 0; k < m; k++ {
			if mx < a[i]*b[k] {
				mx = a[i] * b[k]
			}
		}
	}

	fmt.Println(mx)
}

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


In this task you need to process a set of stock exchange orders and use them to create order book.An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di — buy or sell, and integer qi. This means that the participant is ready to buy or sell qi stocks at price pi for one stock. A value qi is also known as a volume of an order.All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders.An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order.An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book.You are given n stock exhange orders. Your task is to print order book of depth s for these orders.

The input starts with two positive integers n and s (1 ≤ n ≤ 1000, 1 ≤ s ≤ 50), the number of orders and the book depth. Next n lines contains a letter di (either 'B' or 'S'), an integer pi (0 ≤ pi ≤ 105) and an integer qi (1 ≤ qi ≤ 104) — direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order.

Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input.

NoteDenote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample.You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders.

package main

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

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

type stock struct {
	p, q int
}

type ByPrice []stock

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

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

	n, s := nextInt(), nextInt()
	sells := map[int]int{}
	buys := map[int]int{}
	stocks := make([]stock, 0, 1000)
	p := 0

	for i := 0; i < n; i++ {
		if next() == "B" {
			p = nextInt()

			if _, ok := buys[p]; ok {
				buys[p] += nextInt()
			} else {
				buys[p] = nextInt()
			}
		} else {
			p = nextInt()

			if _, ok := sells[p]; ok {
				sells[p] += nextInt()
			} else {
				sells[p] = nextInt()
			}
		}
	}

	for k, v := range sells {
		stocks = append(stocks, stock{k, v})
	}

	sort.Sort(ByPrice(stocks))

	if len(stocks) < s {
		n = len(stocks)
	} else {
		n = s
	}

	for i := n; i > 0; i-- {
		println("S", stocks[i-1].p, stocks[i-1].q)
	}

	stocks = stocks[:0]

	for k, v := range buys {
		stocks = append(stocks, stock{k, v})
	}

	sort.Sort(sort.Reverse(ByPrice(stocks)))

	if len(stocks) < s {
		n = len(stocks)
	} else {
		n = s
	}

	for i := 0; i < n; i++ {
		println("B", stocks[i].p, stocks[i].q)
	}
}

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

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

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

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

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

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

package main

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

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

type stock struct {
	p, q int
}

type ByPrice []stock

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

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

	n, s := nextInt(), nextInt()
	sells := map[int]int{}
	buys := map[int]int{}
	stocks := make([]stock, 0, 1000)
	p := 0

	for i := 0; i < n; i++ {
		if next() == "B" {
			p = nextInt()

			if _, ok := buys[p]; ok {
				buys[p] += nextInt()
			} else {
				buys[p] = nextInt()
			}
		} else {
			p = nextInt()

			if _, ok := sells[p]; ok {
				sells[p] += nextInt()
			} else {
				sells[p] = nextInt()
			}
		}
	}

	for k, v := range sells {
		stocks = append(stocks, stock{k, v})
	}

	sort.Sort(sort.Reverse(ByPrice(stocks)))

	if len(stocks) < s {
		n = len(stocks)
	} else {
		n = s
	}

	for i := 0; i < n; i++ {
		println("S", stocks[i].p, stocks[i].q)
	}

	stocks = stocks[:0]

	for k, v := range buys {
		stocks = append(stocks, stock{k, v})
	}

	sort.Sort(sort.Reverse(ByPrice(stocks)))

	if len(stocks) < s {
		n = len(stocks)
	} else {
		n = s
	}

	for i := 0; i < n; i++ {
		println("B", stocks[i].p, stocks[i].q)
	}
}

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

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

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

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

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

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


There are famous Russian nesting dolls named matryoshkas sold in one of the souvenir stores nearby, and you'd like to buy several of them. The store has $$$n$$$ different matryoshkas. Any matryoshka is a figure of volume $$$out_i$$$ with an empty space inside of volume $$$in_i$$$ (of course, $$$out_i > in_i$$$).You don't have much free space inside your bag, but, fortunately, you know that matryoshkas can be nested one inside another. Formally, let's call a set of matryoshkas nested if we can rearrange dolls in such a way, that the first doll can be nested inside the second one, the second doll — inside the third one and so on. Matryoshka $$$i$$$ can be nested inside matryoshka $$$j$$$ if $$$out_i \le in_j$$$. So only the last doll will take space inside your bag.Let's call extra space of a nested set of dolls as a total volume of empty space inside this structure. Obviously, it's equal to $$$in_{i_1} + (in_{i_2} - out_{i_1}) + (in_{i_3} - out_{i_2}) + \dots + (in_{i_k} - out_{i_{k-1}})$$$, where $$$i_1$$$, $$$i_2$$$, ..., $$$i_k$$$ are the indices of the chosen dolls in the order they are nested in each other.Finally, let's call a nested subset of the given sequence as big enough if there isn't any doll from the sequence that can be added to the nested subset without breaking its nested property.You want to buy many matryoshkas, so you should choose a big enough nested subset to buy it. But you will be disappointed if too much space in your bag will be wasted, so you want to choose a big enough subset so that its extra space is minimum possible among all big enough subsets. Now you wonder, how many different nested subsets meet these conditions (they are big enough, and there is no big enough subset such that its extra space is less than the extra space of the chosen subset). Two subsets are considered different if there exists at least one index $$$i$$$ such that one of the subsets contains the $$$i$$$-th doll, and another subset doesn't.Since the answer can be large, print it modulo $$$10^9 + 7$$$.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of matryoshkas. The next $$$n$$$ lines contain a description of each doll: two integers $$$out_i$$$ and $$$in_i$$$ ($$$1 \le in_i < out_i \le 10^9$$$) — the outer and inners volumes of the $$$i$$$-th matryoshka.

Print one integer — the number of big enough nested subsets such that extra space of each of these subsets is minimum possible. Since the answer can be large, print it modulo $$$10^9 + 7$$$.

NoteThere are $$$6$$$ big enough nested subsets with minimum possible extra space in the example:   $$$\{1, 5\}$$$: we can't add any other matryoshka and keep it nested; it's extra space is $$$1$$$;  $$$\{1, 6\}$$$;  $$$\{2, 4, 5\}$$$;  $$$\{2, 4, 6\}$$$;  $$$\{3, 4, 5\}$$$;  $$$\{3, 4, 6\}$$$. There are no more "good" subsets because, for example, subset $$$\{6, 7\}$$$ is not big enough (we can add the $$$4$$$-th matryoshka to it) or subset $$$\{4, 6, 7\}$$$ has extra space equal to $$$2$$$.

package main

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

var P int64 = 1000000007

type Dolls []*Doll

func (this *Dolls) Len() int {
	return len(*this)
}

func (this *Dolls) Swap(i, j int) {
	(*this)[i], (*this)[j] = (*this)[j], (*this)[i]
}

func (this *Dolls) Less(i, j int) bool {
	return (*this)[i].outer < (*this)[j].outer
}

type Doll struct {
	inner, outer int
	cover int
	ans int64
	sumAns int64
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	dolls := Dolls{}
	maxInner := 0
	for i := 0;i < n;i ++ {
		var inner, outer int
		fmt.Fscanf(reader, "%d %d\n", &outer, &inner)
		dolls = append(dolls, &Doll{inner: inner, outer: outer})
		if inner > maxInner {
			maxInner = inner
		}
	}
	sort.Sort(&dolls)
	stack := []Doll{Doll{outer: 0, ans: 1, sumAns: 1}}
	for i := 0;i <len(dolls);i ++ {
		cover := dolls[i].outer - dolls[i].inner
		// find index which outer <= current inner
		index, left, right := -1, 0, len(stack) - 1
		for left <= right {
			mid := (left + right) / 2
			if stack[mid].outer <= dolls[i].inner {
				index = mid
				left = mid + 1
			} else {
				right = mid - 1
			}
		}
		maxCover := stack[index].cover
		bestCover := maxCover + cover
		if bestCover < stack[len(stack) - 1].cover {
			continue
		}
		// find lower_bound which has cover = maxCover
		lb_index, left, right := -1, 0, len(stack) - 1
		for left <= right {
			mid := (left + right) / 2
			if stack[mid].cover > maxCover {
				right = mid - 1
			} else if stack[mid].cover < maxCover {
				left = mid + 1
			} else {
				lb_index = mid
				right = mid - 1
			}
		}
		var ans int64
		if lb_index == 0 {
			ans = stack[index].sumAns
		} else {
			ans = (stack[index].sumAns - stack[lb_index - 1].sumAns + P) % P
		}
		if dolls[i].outer == stack[len(stack) - 1].outer {
			if bestCover > stack[len(stack) - 1].cover {
				stack[len(stack) - 1].cover = bestCover
				stack[len(stack) - 1].ans = ans
				stack[len(stack) - 1].sumAns = (stack[len(stack) - 2].sumAns + ans) % P
			} else {
				stack[len(stack) - 1].ans += ans
				stack[len(stack) - 1].ans %= P
				stack[len(stack) - 1].sumAns += ans
				stack[len(stack) - 1].sumAns %= P
			}
		} else {
			stack = append(stack, Doll {
				outer: dolls[i].outer,
				ans: ans,
				sumAns: (stack[len(stack) - 1].sumAns + ans) % P,
				cover: bestCover,
			})
		}
	}
	totalAns := int64(0)
	minExtraSpace := -1
	for i := 0;i < len(stack);i ++ {
		if stack[i].outer > maxInner && (minExtraSpace == -1 || minExtraSpace > stack[i].outer - stack[i].cover){
			minExtraSpace = stack[i].outer - stack[i].cover
		}
	}
	for i := 0;i < len(stack);i ++ {
		if stack[i].outer > maxInner && minExtraSpace == stack[i].outer - stack[i].cover {
			totalAns = (totalAns + stack[i].ans) % P
		}
	}
	fmt.Printf("%d", totalAns)
}

package main

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

var P int64 = 1000000007

type Dolls []*Doll

func (this *Dolls) Len() int {
	return len(*this)
}

func (this *Dolls) Swap(i, j int) {
	(*this)[i], (*this)[j] = (*this)[j], (*this)[i]
}

func (this *Dolls) Less(i, j int) bool {
	return (*this)[i].outer < (*this)[j].outer
}

type Doll struct {
	inner, outer int
	cover int
	ans int64
	sumAns int64
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	dolls := Dolls{}
	maxInner := 0
	for i := 0;i < n;i ++ {
		var inner, outer int
		fmt.Fscanf(reader, "%d %d\n", &outer, &inner)
		dolls = append(dolls, &Doll{inner: inner, outer: outer})
		if inner > maxInner {
			maxInner = inner
		}
	}
	sort.Sort(&dolls)
	stack := []Doll{Doll{outer: 0, ans: 1, sumAns: 1}}
	for i := 0;i <len(dolls);i ++ {
		cover := dolls[i].outer - dolls[i].inner
		// find index which outer <= current inner
		index, left, right := -1, 0, len(stack) - 1
		for left <= right {
			mid := (left + right) / 2
			if stack[mid].outer <= dolls[i].inner {
				index = mid
				left = mid + 1
			} else {
				right = mid - 1
			}
		}
		maxCover := stack[index].cover
		bestCover := maxCover + cover
		if bestCover < stack[len(stack) - 1].cover {
			continue
		}
		// find lower_bound which has cover = maxCover
		lb_index, left, right := -1, 0, len(stack) - 1
		for left <= right {
			mid := (left + right) / 2
			if stack[mid].cover > maxCover {
				right = mid - 1
			} else if stack[mid].cover < maxCover {
				left = mid + 1
			} else {
				lb_index = mid
				right = mid - 1
			}
		}
		// find upper_bound which has cover = maxCover
		ub_index, left, right := -1, 0, len(stack) - 1
		for left <= right {
			mid := (left + right) / 2
			if stack[mid].cover > maxCover {
				right = mid - 1
			} else if stack[mid].cover < maxCover {
				left = mid + 1
			} else {
				ub_index = mid
				left = mid + 1
			}
		}
		var ans int64
		if lb_index == 0 {
			ans = stack[ub_index].sumAns
		} else {
			ans = (stack[ub_index].sumAns - stack[lb_index - 1].sumAns + P) % P
		}
		if dolls[i].outer == stack[len(stack) - 1].outer {
			if bestCover > stack[len(stack) - 1].cover {
				stack[len(stack) - 1].cover = bestCover
				stack[len(stack) - 1].ans = ans
				stack[len(stack) - 1].sumAns = (stack[len(stack) - 2].sumAns + ans) % P
			} else {
				stack[len(stack) - 1].ans += ans
				stack[len(stack) - 1].ans %= P
				stack[len(stack) - 1].sumAns += ans
				stack[len(stack) - 1].sumAns %= P
			}
		} else {
			stack = append(stack, Doll {
				outer: dolls[i].outer,
				ans: ans,
				sumAns: (stack[len(stack) - 1].sumAns + ans) % P,
				cover: bestCover,
			})
		}
	}
	totalAns := int64(0)
	minExtraSpace := -1
	for i := 0;i < len(stack);i ++ {
		if stack[i].outer > maxInner && (minExtraSpace == -1 || minExtraSpace > stack[i].outer - stack[i].cover){
			minExtraSpace = stack[i].outer - stack[i].cover
		}
	}
	for i := 0;i < len(stack);i ++ {
		if stack[i].outer > maxInner && minExtraSpace == stack[i].outer - stack[i].cover {
			totalAns = (totalAns + stack[i].ans) % P
		}
	}
	fmt.Printf("%d", totalAns)
}


You have got a shelf and want to put some books on it.You are given $$$q$$$ queries of three types:  L $$$id$$$ — put a book having index $$$id$$$ on the shelf to the left from the leftmost existing book;  R $$$id$$$ — put a book having index $$$id$$$ on the shelf to the right from the rightmost existing book;  ? $$$id$$$ — calculate the minimum number of books you need to pop from the left or from the right in such a way that the book with index $$$id$$$ will be leftmost or rightmost. You can assume that the first book you will put can have any position (it does not matter) and queries of type $$$3$$$ are always valid (it is guaranteed that the book in each such query is already placed). You can also assume that you don't put the same book on the shelf twice, so $$$id$$$s don't repeat in queries of first two types.Your problem is to answer all the queries of type $$$3$$$ in order they appear in the input.Note that after answering the query of type $$$3$$$ all the books remain on the shelf and the relative order of books does not change.If you are Python programmer, consider using PyPy instead of Python when you submit your code.

The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. Then $$$q$$$ lines follow. The $$$i$$$-th line contains the $$$i$$$-th query in format as in the problem statement. It is guaranteed that queries are always valid (for query type $$$3$$$, it is guaranteed that the book in each such query is already placed, and for other types, it is guaranteed that the book was not placed before). It is guaranteed that there is at least one query of type $$$3$$$ in the input. In each query the constraint $$$1 \le id \le 2 \cdot 10^5$$$ is met.

Print answers to queries of the type $$$3$$$ in order they appear in the input.

NoteLet's take a look at the first example and let's consider queries:   The shelf will look like $$$[1]$$$;  The shelf will look like $$$[1, 2]$$$;  The shelf will look like $$$[1, 2, 3]$$$;  The shelf looks like $$$[1, \textbf{2}, 3]$$$ so the answer is $$$1$$$;  The shelf will look like $$$[4, 1, 2, 3]$$$;  The shelf looks like $$$[4, \textbf{1}, 2, 3]$$$ so the answer is $$$1$$$;  The shelf will look like $$$[5, 4, 1, 2, 3]$$$;  The shelf looks like $$$[5, 4, \textbf{1}, 2, 3]$$$ so the answer is $$$2$$$. Let's take a look at the second example and let's consider queries:   The shelf will look like $$$[100]$$$;  The shelf will look like $$$[100, 100000]$$$;  The shelf will look like $$$[100, 100000, 123]$$$;  The shelf will look like $$$[101, 100, 100000, 123]$$$;  The shelf looks like $$$[101, 100, 100000, \textbf{123}]$$$ so the answer is $$$0$$$;  The shelf will look like $$$[10, 101, 100, 100000, 123]$$$;  The shelf will look like $$$[10, 101, 100, 100000, 123, 115]$$$;  The shelf looks like $$$[10, 101, \textbf{100}, 100000, 123, 115]$$$ so the answer is $$$2$$$;  The shelf will look like $$$[10, 101, 100, 100000, 123, 115, 110]$$$;  The shelf looks like $$$[10, 101, 100, 100000, 123, \textbf{115}, 110]$$$ so the answer is $$$1$$$.

package main

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

var qnum int
var lnum, rnum int = 0, 0

var qtype map[int]string
var qlnum, qrnum map[int]int
var qtotal map[int]int

var total int = 0

func getAns(id int) int {
  var l, r = lnum - qlnum[id], rnum - qrnum[id]
  if qtype[id] == "L" {
    r += qtotal[id] - 1
  } else {
    l += qtotal[id] - 1
  }
  if l < r {
    return l
  } else {
    return r
  }
}

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

  qtype = make(map[int]string)
  qlnum = make(map[int]int)
  qrnum = make(map[int]int)
  qtotal = make(map[int]int)

  fmt.Fscanf(reader, "%d\n", &qnum)
  for i := 0; i < qnum; i++ {
    var q string
    var id int
    fmt.Fscanf(reader, "%s %d\n", &q, &id)
    if q == "L" {
      lnum++
    } else if q == "R" {
      rnum++
    }
    if q == "?" {
      fmt.Fprintf(writer, "%d\n", getAns(id))
    } else {
      total++
      qtype[id] = q
      qlnum[id] = lnum
      qrnum[id] = rnum
      qtotal[id] = total
    }
  }
}

package main

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

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

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

	var n, id int
	l, r := 0, 0
	mp := make(map[int]int)
	dir := make(map[int]int)
	var str string

	fmt.Fscan(reader, &n)
	for i := 0; i < n; i ++{
		//fmt.Fscanf(reader,"%s %d", &str, &id)
		fmt.Fscan(reader, &str, &id)
		//fmt.Println(n, str, id)
		if str == "L"{
			l ++
			mp[id] = l
			dir[id] = -1
		} else if str == "R" {
			r ++
			mp[id] = r
			dir[id] = 1
		}else {
			if dir[id] == -1 {
				fmt.Fprintln(writer, min(l-mp[id], mp[id]-1+r))
				//fmt.Println(min(l-mp[id], mp[id]-1+r))
			}else{
				fmt.Fprintln(writer, min(r-mp[id], mp[id]-1+l))
				//fmt.Println(min(r-mp[id], mp[id]-1+l))
			}
			//fmt.Printf("l=%d, mp[id]=%d, r=%d\n", l, mp[id], r)
		}
	}

}

package main

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

var scanner = bufio.NewScanner(os.Stdin)

func scanNum() int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func main() {
	scanner.Split(bufio.ScanWords)
	n := scanNum()
	idMap, L, R := map[int]int{}, -1, -1
	for i := 0; i < n; i++ {
		scanner.Scan()
		mode := scanner.Text()
		num := scanNum()
		if R == -1 {
			idMap[num], L, R = 0, 0, 0
			continue
		}
		if mode == "R" {
			idMap[num], R = R+1, R+1
		}
		if mode == "L" {
			idMap[num], L = L-1, L-1
		}
		if mode == "?" {
			c1, c2 := idMap[num]-L, R-idMap[num]
			if c1 < c2 {
				fmt.Println(c1)
			} else {
				fmt.Println(c2)
			}
		}
	}
}

package main

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

const (
	MaxN = 200005
)

var n int
var p1, p2 int
var data [MaxN]int

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

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d\n", &n)
	p1, p2 = 0, 1
	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()
	for i := 0; i < n; i++ {
		var q byte
		var val int
		fmt.Fscanf(inBuf, "%c %d\n", &q, &val)
		if q == 'L' {
			data[val] = p1
			p1--
		}
		if q == 'R' {
			data[val] = p2
			p2++
		}
		if q == '?' {
			pos := data[val]
			fmt.Println(min(pos - p1, p2 - pos) - 1)
		}
	}
}

package main

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

var qnum int
var lnum, rnum = 0, 0

var qtype map[int]string
var qlnum, qrnum map[int]int
var qtotal map[int]int

var total = 0

func getAns(id int) int {
  var l, r = lnum - qlnum[id], rnum - qrnum[id]
  if qtype[id] == "L" {
    r += qtotal[id] - 1
  } else {
    l += qtotal[id] - 1
  }
  if l < r {
    return l
  } else {
    return r
  }
}

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

  qtype = make(map[int]string)
  qlnum = make(map[int]int)
  qrnum = make(map[int]int)
  qtotal = make(map[int]int)

  fmt.Fscanf(reader, "%d\n", &qnum)
  for i := 0; i < qnum; i++ {
    var q string
    var id int
    fmt.Fscanf(reader, "%s %d\n", &q, &id)
    qtype[id] = q
    if q == "L" {
      lnum++
    } else if q == "R" {
      rnum++
    }
    if q == "?" {
      fmt.Fprintf(writer, "%d\n", getAns(id))
    } else {
      total++
      qlnum[id] = lnum
      qrnum[id] = rnum
      qtotal[id] = total
    }
  }
}

package main

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

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

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

	var n, id int
	l, r := 0, 0
	mp := make(map[int]int)
	dir := make(map[int]int)
	var str string
	fmt.Scan(&n)
	for i := 0; i < n; i ++{
		fmt.Fscanf(reader,"%s %d", &str, &id)
		//fmt.Println(n, str, id)
		if str == "L"{
			l ++
			mp[id] = l
			dir[id] = -1
		} else if str == "R" {
			r ++
			mp[id] = r
			dir[id] = 1
		}else {
			if dir[id] == -1 {
				fmt.Fprintln(writer, min(l-mp[id], mp[id]-1+r))
				//fmt.Println(min(l-mp[id], mp[id]-1+r))
			}else{
				fmt.Fprintln(writer, min(r-mp[id], mp[id]-1+l))
				//fmt.Println(min(r-mp[id], mp[id]-1+l))
			}
		}
	}

}