package main

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

func main() {
	s := NewScanner()
	t := s.NextInt()

	for i := 0; i < t; i++ {
		n, m, k := s.NextInt(), s.NextInt(), s.NextInt()

		c := n / k
		if c > m {
			fmt.Println(m)
			continue
		}

		left := m - c
		pl := k - 1

		if left%pl == 0 {
			fmt.Println(c - left/pl)
		} else {
			fmt.Println(c - left/pl - 1)
		}
	}
}

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

type scanner struct {
	*bufio.Scanner
}

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

func (s *scanner) NextInt() int {
	s.Scan()
	i, _ := strconv.Atoi(s.Text())
	return i
}

func (s *scanner) NextInt64() int64 {
	s.Scan()
	i, _ := strconv.ParseInt(s.Text(), 10, 64)
	return i
}

func (s *scanner) NextString() string {
	s.Scan()
	return s.Text()
}

package main

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

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 main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)

	for tc > 0 {
		tc--

		n, m, k := readThreeNums(reader)

		fmt.Println(solve(n, m, k))
	}
}

func solve(n, m, k int) int {
	h := n / k
	if m <= h {
		// can give the winner all m jokers
		return m
	}
	// m > h

	x := h
	y := (m - h + k - 2) / (k - 1)
	return x - y
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	fmt.Fscanf(in, "%d\n", &t)
	//fmt.Println("debug", t)
	for tt := int64(0); tt < t; tt++ {
		a, b, c := int64(0), int64(0), int64(0)
		fmt.Fscanf(in, "%d %d %d %d\n", &a, &b, &c)
		every := a / c
		max := b
		if max > every {
			max = every
		}
		remaining := b - max
		remainingMax := remaining / (c - 1)
		if remainingMax*(c-1) < remaining {
			remainingMax++
		}
		fmt.Fprintf(out, "%d\n", max-remainingMax)
	}
	return
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func same(s string, ss string) bool {
	a := make([]int, 26)
	for i := 0; i < len(s); i++ {
		b := int(s[i] - 'a')
		a[b]++
	}
	for i := 0; i < len(ss); i++ {
		b := int(ss[i] - 'a')
		a[b]--
		if a[b] == -1 {
			return false
		}
	}
	for i := 0; i < 26; i++ {
		if a[i] != 0 {
			return false
		}
	}
	return true
}

// https://codeforces.com/contest/1358/problem/A
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
var input io.Reader

func main() {
	var (
		T int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &T)
	for i := 0; i < T; i++ {
		solver()
	}
}

func solver() {
	var n, m, k int
	fmt.Fscan(input, &n, &m, &k)
	f := min(n/k, m)
	e := nn(m - f) / (k - 1)
	if nn(m - f) % (k - 1) > 0 {
	  e++
	}
	f2 := min(n/k, e)
	fmt.Println(f - f2)
}

func nn(x int) int {
  if x >= 0 {
    return x
  }
  return 0
}

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

package main

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

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

func main() {
	defer writer.Flush()
	t := reader.NextInt64()
	for ; t > 0; t-- {
		n, m, k := reader.NextInt(), reader.NextInt(), reader.NextInt()
		// aa := make([]int64, n)
		// for i := 0; i < len(aa); i++ {
		// 	aa[i] = reader.NextInt64()
		// }
		writer.Printf("%d\n", sol(n,m,k))
	}
}

func sol(n,m,k int) int {
	if m==0 {
		return 0;
	}

	cpp := n/k
	if(cpp>=m){
		return m
	}else{
		// return cpp-((m-cpp)/(k -1))-1
		m=m-cpp
		i:=1
		for ;m-(i*(k-1))>0;i++ {
		}
		// fmt.Println(i,"yo", m)
		return cpp-i
		// return cpp - arr[i]
	}
	// else{
	// 	return 0
	// }

  return -1
}

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

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

type writerImpl struct {
	*bufio.Writer
}

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

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

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

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

type wordScannerImpl struct {
	*bufio.Scanner
}

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

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

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

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

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

package main

import (
	"fmt"
)

func main() {
	var n, m, k, t, ans int
	fmt.Scanf("%d\n", &t)

	for i := 0; i < t; i++ {
		fmt.Scanf("%d %d %d\n", &n, &m, &k)
		cardNum := n / k
		if cardNum >= m {
			ans = m
		} else {
			remain := m - cardNum

			minJoker := remain / (k - 1)
			if remain%(k-1) != 0 {
				minJoker += 1
			}

			ans = cardNum - minJoker
		}

		fmt.Println(ans)
	}
}

/*
URL:
https://codeforces.com/contest/1359/problem/A
*/

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, m, k int
)

func main() {
	t = ReadInt()
	for i := 0; i < t; i++ {
		n, m, k = ReadInt3()
		solve()
	}
}

func solve() {
	h := n / k
	if m <= h {
		fmt.Println(m)
		return
	}

	r := m - h
	tmp := Kiriage(r, k-1)
	fmt.Println(h - tmp)
}

// Kiriage returns Ceil(a/b)
// a >= 0, b > 0
func Kiriage(a, b int) int {
	return (a + (b - 1)) / b
}

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

/*********** 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"
	"os"
)

var nTest, n, m, k int

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	fmt.Fscan(in, &nTest)

	for i := 0; i < nTest; i++ {
		fmt.Fscan(in, &n, &m, &k)

		maxJokers := n / k
		if maxJokers > m {
			maxJokers = m
		}

		secondMaxJoker := (m - maxJokers + k - 2) / (k - 1)

		fmt.Fprintln(out, maxJokers-secondMaxJoker)
	}
	out.Flush()
}

package main

import "fmt"

func main() {
	var testCases int
	fmt.Scanf("%d\n", &testCases)

	var n, m, k, x, y, cardsEach int

	for i := 0; i < testCases; i++ {
		fmt.Scanf("%d%d%d\n", &n, &m, &k)
		cardsEach = n / k
		if m <= cardsEach {
			x = m
			y = 0
		} else {
			x = cardsEach
			if m - cardsEach > k-1 {
				y = (m - cardsEach) / (k - 1)
				if (m - cardsEach) % (k - 1) != 0 {
					y++
				}
			} else {
				y = 1
			}
		}
		fmt.Printf("%d\n", x-y)
	}
}

package main

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

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

	var T, c, j, p int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &c, &j, &p)
		c /= p
		if j <= c {
			Fprintln(out, j)
		} else {
			Fprintln(out, c-(j-c-1)/(p-1)-1)
		}
	}
}

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

package main

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

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

	scanner.Scan()
	c, _ := strconv.Atoi(scanner.Text())
	var inputList = make([]int, 3)
	for i := 0; i < c; i++ {
		scanner.Scan()
		for ii, d := range strings.Split(scanner.Text(), " ") {
			inputList[ii], _ = strconv.Atoi(d)
		}

		n := inputList[0]
		m := inputList[1]
		k := inputList[2]

		d := n - m
		c := d / (k - 1)
		if c < m {
			fmt.Println(c)
		} else {
			fmt.Println(m)
		}
	}
}

package main

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

func min(a, b int) int {
	return int(math.Min(float64(a), float64(b)))
}

func max(a, b int) int {
	return int(math.Max(float64(a), float64(b)))
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscanf(in, "%v\n", &t)
	for now := 0; now < t; now++ {
		var n, m, k int
		fmt.Fscanf(in, "%v %v %v\n", &n, &m, &k)
		fmt.Fprintln(out, min(n / k, m) - ((m - min(n / k, m) + k - 2) / (k - 1)))
	}
	return
}

package main

import "fmt"
import "bufio"
import "os"

func main() {
	buf := bufio.NewReader(os.Stdin)
	var t int

	fmt.Fscanf(buf, "%d\n", &t)

	var n, m, k []int

	n = make([]int, t)
	m = make([]int, t)
	k = make([]int, t)

	for i:=0; i< t; i++ {
		fmt.Fscanf(buf, "%d", &n[i])
		fmt.Fscanf(buf, "%d", &m[i])
		fmt.Fscanf(buf, "%d\n", &k[i])
	}

	for i:=0; i< t; i++ {
		fmt.Println(getMaxPoint(n[i], m[i], k[i]))
	}

}

func getMaxPoint(n, m, k int) int {
	if m == 0 {
		return 0
	}

	maxJ := min(n/k, m)

	var minJ int
	if (m-maxJ)%(k-1) == 0 {
		minJ = (m-maxJ)/(k-1)
	} else {
		minJ = (m-maxJ)/(k-1) +1
	}

	return maxJ - minJ
}

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

	return y
}

package main

import (
	"fmt"
	"bufio"
	"os"
)
var fin *bufio.Reader = bufio.NewReader(os.Stdin)
var fout *bufio.Writer = bufio.NewWriter(os.Stdout)
func max(a int, b int) int { if(a > b) { return a } else { return b }}
func main() {
	defer fout.Flush()
	var asdf int
	fmt.Fscan(fin, &asdf)
	for ; asdf > 0; asdf-- {
		var n, m, k int
		fmt.Fscan(fin, &n, &m, &k)
		if m <= n/k {
			fmt.Fprintln(fout, m)
		} else {
			fmt.Fprintln(fout, n/k-(m-n/k+k-2)/(k-1))
		}
	}
}

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, m, k int
		fmt.Fscan(r, &n, &m, &k)
		var winner, second int
		winner = n / k
		if winner > m {
			winner = m
		}
		second = (m - winner + k - 2) / (k - 1)
		fmt.Fprintln(w, winner-second)
	}
}

package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}
func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0
	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}
	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	solve(scanner, writer)
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	t := getNextInt(scanner)
	for tt := 0; tt < t; tt++ {
		n := getNextInt(scanner)
		m := getNextInt(scanner)
		k := getNextInt(scanner)
		d := n / k
		if d >= m {
			fmt.Fprintln(writer, m)
			continue
		}
		fmt.Fprintln(writer, d-(m-d+k-2)/(k-1))
	}
}

package main

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

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

type writerImpl struct {
	*bufio.Writer
}

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

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

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

type WordScanner interface {
	Int64() int64
	UInt64() uint64
	String() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

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

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

func (impl *wordScannerImpl) UInt64() uint64 {
	impl.Scan()
	bb := impl.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

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

func gcd(a, b int64) int64 {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func lcm(a, b int64) int64 {
	return a * b / gcd(a, b)
}

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

func min(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

const (
	INF = math.MaxInt64
	MOD = 1e9 + 7
)

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

func main() {
	defer writer.Flush()
	t := reader.Int64()
	for ; t > 0; t-- {
		n := reader.Int64()
		m := reader.Int64()
		k := reader.Int64()
		writer.Printf("%d\n", sol(n, m, k))
	}
}

func sol(n, m, k int64) int64 {
	per := n / k
	if per >= m {
		return m
	}
	remainJ := m - per
	remainK := k - 1
	maxRemainJ := ceilDiv(remainJ, remainK)
	return per - maxRemainJ
}

func ceilDiv(a, b int64) int64 {
	if a % b == 0 {
		return a / b
	}
	return a / b + 1
}

package main

import "fmt"

func main() {
	var T int
	fmt.Scan(&T)
	for i := 0; i < T; i++ {
		var a,b,c int
		fmt.Scan(&a,&b,&c)
		var k int= a/c
    	if (k >= b){
			fmt.Print("\n",b)
		} else {
			fmt.Print("\n",k-((b-k-1)/(c-1)+1))
		}
	}

}

// https://codeforces.com/contest/1358/problem/A
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
var input io.Reader

func main() {
	var (
		T int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &T)
	for i := 0; i < T; i++ {
		solver()
	}
}

func solver() {
	var n, m, k int
	fmt.Fscan(input, &n, &m, &k)
	f := min(n/k, m)
	e := nn(m - f) / (k - 1)
	e += nn(m - f) - e*(k - 1)
	fmt.Println(f - e)
}

func nn(x int) int {
  if x >= 0 {
    return x
  }
  return 0
}

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

package main

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

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

func main() {
	defer writer.Flush()
	t := reader.NextInt64()
	for ; t > 0; t-- {
		n, m, k := reader.NextInt(), reader.NextInt(), reader.NextInt()
		// aa := make([]int64, n)
		// for i := 0; i < len(aa); i++ {
		// 	aa[i] = reader.NextInt64()
		// }
		writer.Printf("%d\n", sol(n,m,k))
	}
}

func sol(n,m,k int) int {
	if m==0 {
		return 0;
	}

	cpp := n/k
	if(cpp>=m){
		return m
	}else{
		// return cpp-((m-cpp)/(k -1))-1
		m=m-cpp
		i:=1
		for ;m>0;i++ {
			m=m-(i*(k-1))
		}
		// fmt.Println(i,"yo", m)
		return cpp-i+1
		// return cpp - arr[i]
	}
	// else{
	// 	return 0
	// }

  return -1
}

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

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

type writerImpl struct {
	*bufio.Writer
}

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

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

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

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

type wordScannerImpl struct {
	*bufio.Scanner
}

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

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

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

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

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

package main

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

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

func main() {
	defer writer.Flush()
	t := reader.NextInt64()
	for ; t > 0; t-- {
		n, m, k := reader.NextInt(), reader.NextInt(), reader.NextInt()
		// aa := make([]int64, n)
		// for i := 0; i < len(aa); i++ {
		// 	aa[i] = reader.NextInt64()
		// }
		writer.Printf("%d\n", sol(n,m,k))
	}
}

func sol(n,m,k int) int {
	if m==0 {
		return 0;
	}

	cpp := n/k
	if(cpp>=m){
		return m
	}else{
		// return cpp-((m-cpp)/(k -1))-1
		m=m-cpp
		i:=1
		for ;m>0;i++ {
			m=m-(i*(k-1))
		}
		if m<0{
			i--
		}
		// fmt.Println(i,"yo", m)
		return cpp-i
		// return cpp - arr[i]
	}
	// else{
	// 	return 0
	// }

  return -1
}

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

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

type writerImpl struct {
	*bufio.Writer
}

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

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

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

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

type wordScannerImpl struct {
	*bufio.Scanner
}

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

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

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

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

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

package main

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

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

func main() {
	defer writer.Flush()
	t := reader.NextInt64()
	for ; t > 0; t-- {
		n, m, k := reader.NextInt(), reader.NextInt(), reader.NextInt()
		// aa := make([]int64, n)
		// for i := 0; i < len(aa); i++ {
		// 	aa[i] = reader.NextInt64()
		// }
		writer.Printf("%d\n", sol(n,m,k))
	}
}

func sol(n,m,k int) int {
	if m==0 {
		return 0;
	}

	cpp := n/k
	if(cpp>=m){
		return m
	}else{
		return cpp-((m-cpp)/(k -1))-1

		// return cpp - arr[i]
	}
	// else{
	// 	return 0
	// }

  return -1
}

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

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

type writerImpl struct {
	*bufio.Writer
}

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

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

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

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

type wordScannerImpl struct {
	*bufio.Scanner
}

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

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

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

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

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

package main

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

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

	scanner.Scan()
	c, _ := strconv.Atoi(scanner.Text())
	var inputList = make([]int, 3)
	for i := 0; i < c; i++ {
		scanner.Scan()
		for ii, d := range strings.Split(scanner.Text(), " ") {
			inputList[ii], _ = strconv.Atoi(d)
		}

		n := inputList[0]
		m := inputList[1]
		k := inputList[2]

		cartCount := n / k
		if cartCount == 0 || m == 0 {
			fmt.Println(0)
			continue
		}

		if cartCount == 1 {
			fmt.Println(0)
			continue
		}

		if cartCount == m {
			fmt.Println(m)
			continue
		}

		if cartCount > m {
			fmt.Println(m)
			continue
		}

		if cartCount < m {
			secMax := cartCount - (m-1)/cartCount

			a := cartCount - secMax
			if a < 0 {
				a = 0
			}

			fmt.Println(a)
		}
	}
}

package main

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

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

	scanner.Scan()
	c, _ := strconv.Atoi(scanner.Text())
	var inputList = make([]int, 3)
	for i := 0; i < c; i++ {
		scanner.Scan()
		for ii, d := range strings.Split(scanner.Text(), " ") {
			inputList[ii], _ = strconv.Atoi(d)
		}

		n := inputList[0]
		m := inputList[1]
		k := inputList[2]

		cartCount := n / k
		if cartCount == 0 || m == 0 {
			fmt.Println(0)
			continue
		}

		if cartCount == m {
			fmt.Println(m)
			continue
		}

		if cartCount > m {
			fmt.Println(m)
			continue
		}

		if cartCount < m {
			y := 1 + (m-cartCount)%(k-1)
			a := cartCount - y
			if a < 0 {
				a = 0
			}

			fmt.Println(a)
		}
	}
}

package main

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

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

	scanner.Scan()
	c, _ := strconv.Atoi(scanner.Text())
	var inputList = make([]int, 3)
	for i := 0; i < c; i++ {
		scanner.Scan()
		for ii, d := range strings.Split(scanner.Text(), " ") {
			inputList[ii], _ = strconv.Atoi(d)
		}

		n := inputList[0]
		m := inputList[1]
		k := inputList[2]

		cartCount := n / k
		if cartCount == 0 || m == 0 {
			fmt.Println(0)
			continue
		}

		if cartCount == m {
			fmt.Println(m)
			continue
		}

		if cartCount > m {
			fmt.Println(m)
			continue
		}

		if cartCount < m {
			y := 1 + (m-cartCount)%(k-1)

			fmt.Println(cartCount - y)
		}
	}
}

package main

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

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

	scanner.Scan()
	c, _ := strconv.Atoi(scanner.Text())
	var inputList = make([]int, 3)
	for i := 0; i < c; i++ {
		scanner.Scan()
		for ii, d := range strings.Split(scanner.Text(), " ") {
			inputList[ii], _ = strconv.Atoi(d)
		}

		n := inputList[0]
		m := inputList[1]
		k := inputList[2]

		cartCount := n / k
		if cartCount == 0 || m == 0 {
			fmt.Println(0)
			continue
		}

		if cartCount == m {
			fmt.Println(m)
			continue
		}

		if cartCount > m {
			fmt.Println(m)
			continue
		}

		if cartCount < m {
			var y int
			if (k - 1) == 1 {
				y = m / 2
			} else {
				y = 1 + (m-cartCount)%(k-1)
			}

			a := cartCount - y
			if a < 0 {
				a = 0
			}

			fmt.Println(a)
		}
	}
}

package main

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

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

	scanner.Scan()
	c, _ := strconv.Atoi(scanner.Text())
	var inputList = make([]int, 3)
	for i := 0; i < c; i++ {
		scanner.Scan()
		for ii, d := range strings.Split(scanner.Text(), " ") {
			inputList[ii], _ = strconv.Atoi(d)
		}

		n := inputList[0]
		m := inputList[1]
		k := inputList[2]

		cartCount := n / k
		if cartCount == 0 || m == 0 {
			fmt.Println(0)
			continue
		}

		if cartCount == m {
			fmt.Println(m)
			continue
		}

		if cartCount > m {
			fmt.Println(m)
			continue
		}

		if cartCount < m {
			a := cartCount - (m+cartCount-1)/cartCount
			if a < 0 {
				a = 0
			}

			fmt.Println(a)
			continue
		}
	}
}

package main

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

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

	scanner.Scan()
	c, _ := strconv.Atoi(scanner.Text())
	var inputList = make([]int, 3)
	for i := 0; i < c; i++ {
		scanner.Scan()
		for ii, d := range strings.Split(scanner.Text(), " ") {
			inputList[ii], _ = strconv.Atoi(d)
		}

		n := inputList[0]
		m := inputList[1]
		k := inputList[2]

		cartCount := n / k
		if cartCount == 0 || m == 0 {
			fmt.Println(0)
			continue
		}

		if cartCount > m {
			fmt.Println(m)
			continue
		}

		if cartCount < m {
			a := cartCount - (m+cartCount-1)/cartCount
			if a < 0 {
				a = 0
			}

			fmt.Println(a)
			continue
		}
	}
}


// ProblemURL : https://codeforces.com/problemset/problem/1118/B
// ---------------------------------------------
package main

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

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}
func diff(a, b int) int {
	if a > b {
		return a - b
	}
	return b - a
}
func larger(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func smaller(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func largest(a, b, c int) (lgst int) {
	if a > b {
		lgst = a
	} else {
		lgst = b
	}
	if c > lgst {
		lgst = c
	}
	return
}
func smallest(a, b, c int) (slst int) {
	if a < b {
		slst = a
	} else {
		slst = b
	}
	if c < slst {
		slst = c
	}
	return
}
func intsMax(a []int) (val int) {
	if len(a) == 0 {
		panic("func intsMax: zero length slice")
	}
	val = a[0]
	for _, aa := range a {
		if aa > val {
			val = aa
		}
	}
	return
}
func intsMaxIdx(a []int) (idx, val int) {
	if len(a) == 0 {
		panic("func intsMaxIdx: zero length slice")
	}
	val = a[0]
	for i, aa := range a {
		if aa > val {
			idx, val = i, aa
		}
	}
	return
}
func intsMin(a []int) (val int) {
	if len(a) == 0 {
		panic("func intsMin: zero length slice")
	}
	val = a[0]
	for _, aa := range a {
		if aa < val {
			val = aa
		}
	}
	return
}
func intsMinIdx(a []int) (idx, val int) {
	if len(a) == 0 {
		panic("func intsMinIdx: zero length slice")
	}
	val = a[0]
	for i, aa := range a {
		if aa < val {
			idx, val = i, aa
		}
	}
	return
}
func intsSum(a []int) int {
	res := 0
	for _, v := range a {
		res += v
	}
	return res
}
func intsAve(s []int) float64 {
	return float64(intsSum(s)) / float64(len(s))
}
func intsAdd(a *[]int, x int)    { *a = append(*a, x) }
func intsDelete(a *[]int, i int) { *a = append((*a)[:i], (*a)[i+1:]...) }
func intsJoin(a, b []int) []int  { return append(a, b...) }
func intsClear(a []int) []int    { return a[:0] }
func intsCopy(a []int) []int     { return append([]int(nil), a...) }
func intsCopySortAsc(a []int) []int {
	s := intsCopy(a)
	sort.Ints(s)
	return s
}
func intsCopySortDesc(a []int) []int {
	s := intsCopy(a)
	sort.Sort(sort.Reverse(sort.IntSlice(s)))
	return s
}
func intsReverse(a []int) {
	for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
		a[i], a[j] = a[j], a[i]
	}
}
func intsFill(a []int, val int) {
	for i := 0; i < len(a); i++ {
		a[i] = val
	}
}
func intsPeekBack(a []int) int {
	if len(a) == 0 {
		panic("func peekBack: zero length slice")
	}
	return a[len(a)-1]
}
func intsPeekFront(a []int) int {
	if len(a) == 0 {
		panic("func peekFront: zero length slice")
	}
	return a[0]
}
func intsPopBack(a *[]int) int {
	if len(*a) == 0 {
		panic("func popBack: zero length slice")
	}
	t := (*a)[len(*a)-1]
	*a = (*a)[:len(*a)-1]
	return t
}
func intsPopFront(a *[]int) int {
	if len(*a) == 0 {
		panic("func popFront: zero length slice")
	}
	h := (*a)[0]
	*a = (*a)[1:]
	return h
}
func intsPushBack(a *[]int, x int) { *a = append(*a, x) }
func intsPushFront(a *[]int, x int) {
	*a = append(*a, 0)
	copy((*a)[1:], (*a)[0:])
	(*a)[0] = x
}
func intsAppendSentinelHead(a *[]int, sentinel int) { intsPushFront(a, sentinel) }
func intsAppendSentinelTail(a *[]int, sentinel int) { intsPushBack(a, sentinel) }
func intsAppendSentinels(a *[]int, sentinel int) {
	intsPushFront(a, sentinel)
	intsPushBack(a, sentinel)
}
func intsIota(n int) []int {
	as := make([]int, n)
	for i := range as {
		as[i] = i
	}
	return as
}
func intsHaveDuplicateElm(a []int) bool {
	m := make(map[int]bool, len(a))
	for _, aa := range a {
		if _, exists := m[aa]; exists {
			return true
		} else {
			m[aa] = true
		}
	}
	return false
}
func isEven(n int) bool     { return n&1 == 0 }
func isOdd(n int) bool      { return n&1 == 1 }
func floor(x float64) int   { return int(x) }
func ceil(x float64) int    { return int(math.Ceil(x)) }
func ceilDiv(x, y int) int  { return ((x) + (y) - 1) / (y) }
func floorDiv(x, y int) int { return (x) / (y) }
func pow(x, y int) int {
	if y < 0 {
		panic("Exponent must be a natural number")
	}
	z := 1
	for ; y != 0; y >>= 1 {
		if y&1 == 1 {
			z *= x
		}
		x *= x
	}
	return z
}
func fact(x int) int {
	fact := 1
	for i := 1; i <= x; i++ {
		fact *= i
	}
	return fact
}
func sigmaK(n int) int  { return n * (n + 1) / 2 }
func sigmaKK(n int) int { return (n * (n + 1) / 2) * (2*n + 1) / 3 }
func euclideanDistance(a, b point) float64 {
	s := diff(a.x, b.x)
	s *= s
	t := diff(a.y, b.y)
	t *= t
	return math.Sqrt(float64(s) + float64(t))
}
func manhattanDistance(a, b point) int {
	s := diff(a.x, b.x)
	t := diff(a.y, b.y)
	return s + t
}
func swap(a, b *int) { *a, *b = *b, *a }
func chmax(a *int, b int) bool {
	if *a < b {
		*a = b
		return true
	}
	return false
}
func chmin(a *int, b int) bool {
	if *a > b {
		*a = b
		return true
	}
	return false
}
func cumsumBuild(a []int) []int {
	b := append([]int{0}, a...)
	for i := 1; i < len(b); i++ {
		b[i] += b[i-1]
	}
	return b
}
func bytesReverse(a []byte) {
	for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
		a[i], a[j] = a[j], a[i]
	}
}
func strIsUpperAlphabet(s string) bool {
	if len(s) != 1 {
		panic("wrong arg")
	}
	t := s[0]
	return 'A' <= t && t <= 'Z'
}
func strIsLowerAlphabet(s string) bool {
	if len(s) != 1 {
		panic("wrong arg")
	}
	t := s[0]
	return 'a' <= t && t <= 'z'
}
func strToUpperCase(s string) string { return strings.ToUpper(s) }
func strToLowerCase(s string) string { return strings.ToLower(s) }
func strAlphaUpperCaseToInt(s string) int {
	if len(s) != 1 {
		panic("wrong arg")
	}
	t := s[0]
	if !('A' <= t && t <= 'Z') {
		panic("arg must be upper case alphabet")
	}
	return int(t - 'A')
}
func strAlphaLowerCaseToInt(s string) int {
	if len(s) != 1 {
		panic("wrong arg")
	}
	t := s[0]
	if !('a' <= t && t <= 'z') {
		panic("arg must be lower case alphabet")
	}
	return int(t - 'a')
}
func strCountAlphabetsLower(alphaLower string) (ctAlphas [26]int) {
	ct := [26]int{}
	for _, ss := range alphaLower {
		if !('a' <= ss && ss <= 'z') {
			panic("func strCountAlphabetsLower: argument must be lowercase string")
		}
		ct[ss-'a']++
	}
	return ct
}
func strCountAlphabetsUpper(alphaUpper string) (ctAlphas [26]int) {
	ct := [26]int{}
	for _, ss := range alphaUpper {
		if !('A' <= ss && ss <= 'Z') {
			panic("func strCountAlphabetsUpper: argument must be uppercase string")
		}
		ct[ss-'A']++
	}
	return ct
}
func strReverse(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}
func runeIsUpperAlphabet(r rune) bool { return 'A' <= r && r <= 'Z' }
func runeIsLowerAlphabet(r rune) bool { return 'a' <= r && r <= 'z' }
func runesReverse(a []rune) {
	for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
		a[i], a[j] = a[j], a[i]
	}
}
func strToInt(s string) int {
	i, err := strconv.Atoi(s)
	if err != nil {
		panic(err)
	}
	return i
}
func intToStr(i int) string     { return strconv.Itoa(i) }
func CalcMod(a, mod int) int    { return (a%mod + mod) % mod }
func ModSum(a *int, b, mod int) { *a = (((*a) % mod) + mod + (b % mod)) % mod }
func ModMul(a *int, b, mod int) { *a = (((*a) % mod) * (b % mod)) % mod }
func ModPow(x, y, mod int) int {
	if y < 0 {
		panic("Exponent must be a natural number")
	}
	z := 1
	for ; y != 0; y >>= 1 {
		if y&1 == 1 {
			z = (z * x) % mod
		}
		x = (x * x) % mod
	}
	return z
}
func ModFact(x, mod int) int {
	fact := 1
	for i := 1; i <= x; i++ {
		fact = fact * i % mod
	}
	return fact
}
func ModInv(x, mod int) int { return ModPow(x, mod-2, mod) }
func initMatrixBool(parentSize, childSize int, initialValue bool) *[][]bool {
	res := make([][]bool, parentSize)
	for i := range res {
		res[i] = make([]bool, childSize)
		for j := range res[i] {
			res[i][j] = initialValue
		}
	}
	return &res
}
func initMatrixInt(parentSize, childSize int, initialValue int) *[][]int {
	res := make([][]int, parentSize)
	for i := range res {
		res[i] = make([]int, childSize)
		for j := range res[i] {
			res[i][j] = initialValue
		}
	}
	return &res
}

var (
	sc = bufio.NewScanner(os.Stdin)
	bw = bufio.NewWriterSize(os.Stdout, bwBufSize)
)

func init() {
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1e7), 1e7)
}
func ru() (n int) {
	sc.Scan()
	if err := sc.Err(); err != nil {
		panic(err)
	}
	for _, v := range sc.Bytes() {
		n = n*10 + int(v-48)
	}
	return
}
func ri() (n int) {
	sc.Scan()
	if err := sc.Err(); err != nil {
		panic(err)
	}
	b := sc.Bytes()
	neg := false
	if b[0] == 45 {
		neg = true
		b = b[1:]
	}
	for _, v := range b {
		n = n*10 + int(v-48)
	}
	if neg {
		n = -n
	}
	return
}
func ri64() (n int64) {
	sc.Scan()
	if err := sc.Err(); err != nil {
		panic(err)
	}
	b := sc.Bytes()
	neg := false
	if b[0] == 45 {
		neg = true
		b = b[1:]
	}
	for _, v := range b {
		n = n*10 + int64(v-48)
	}
	if neg {
		n = -n
	}
	return
}
func rf64() float64 {
	sc.Scan()
	if err := sc.Err(); err != nil {
		panic(err)
	}
	f, err := strconv.ParseFloat(sc.Text(), 64)
	if err != nil {
		panic(err)
	}
	return f
}
func rs() string {
	sc.Scan()
	if err := sc.Err(); err != nil {
		panic(err)
	}
	return sc.Text()
}
func rb() []byte {
	sc.Scan()
	if err := sc.Err(); err != nil {
		panic(err)
	}
	return sc.Bytes()
}
func rr() []rune {
	sc.Scan()
	if err := sc.Err(); err != nil {
		panic(err)
	}
	return []rune(sc.Text())
}
func ris(n int) []int {
	s := make([]int, n)
	for i := range s {
		s[i] = ri()
	}
	return s
}
func risZeroBased(n int) []int {
	s := make([]int, n)
	for i := range s {
		s[i] = ri() - 1
	}
	return s
}
func ri64s(n int) []int64 {
	s := make([]int64, n)
	for i := range s {
		s[i] = ri64()
	}
	return s
}
func riis(n int) ([]int, []int) {
	s := make([]int, n)
	t := make([]int, n)
	for i := range s {
		s[i] = ri()
		t[i] = ri()
	}
	return s, t
}
func rss(n int) []string {
	s := make([]string, n)
	for i := range s {
		s[i] = rs()
	}
	return s
}
func pf(format string, a ...interface{}) {
	if _, err := fmt.Fprintf(bw, format, a...); err != nil {
		panic(err)
	}
}
func pln(a ...interface{}) {
	if _, err := fmt.Fprintln(bw, a...); err != nil {
		panic(err)
	}
}
func pints(a []int) {
	for _, v := range a {
		fmt.Fprintln(bw, v)
	}
}
func pintsol(a []int) {
	s := fmt.Sprint(a)
	if _, err := fmt.Fprintln(bw, s[1:len(s)-1]); err != nil {
		panic(err)
	}
}
func dbg(a ...interface{}) {
	if _, err := fmt.Fprintln(os.Stderr, a...); err != nil {
		panic(err)
	}
}
func dbgMatrix(a [][]int) {
	for i := 0; i < len(a); i++ {
		dbg(a[i])
	}
}
func flush() {
	if err := bw.Flush(); err != nil {
		panic(err)
	}
}
func YESorNO(b bool) string {
	if b {
		return "YES"
	} else {
		return "NO"
	}
}
func YesOrNo(b bool) string {
	if b {
		return "Yes"
	} else {
		return "No"
	}
}
func main() {
	solve()
	flush()
}

const (
	bwBufSize = 1e6

	alphabetLower = "abcdefghijklmnopqrstuvwxyz"
	alphabetUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

	// maxInt64  = 9223372036854775807  // 9e18
	// maxInt32  = 2147483647           // 2e9
	// maxUint64 = 18446744073709551615 // 1e19
	inf = 1 << 60
	mod = 1e9 + 7
)

type pair struct{ fi, se int }
type byFiSe []pair

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

type point struct{ x, y int }

func solve() {
	n := ri()
	a := ris(n)

	cumSumOdd := make([]int, n+1)
	cumSumEvn := make([]int, n+1)
	for i := 0; i < n; i++ {
		cumSumOdd[i+1] += cumSumOdd[i]
		cumSumEvn[i+1] += cumSumEvn[i]
		if isOdd(i) {
			cumSumOdd[i+1] += a[i]
		} else {
			cumSumEvn[i+1] += a[i]
		}
	}

	ct := 0
	for i := 0; i < n; i++ {
		sumOdd := (cumSumOdd[i] - cumSumOdd[0]) + (cumSumEvn[n] - cumSumEvn[i+1])
		sumEvn := (cumSumEvn[i] - cumSumEvn[0]) + (cumSumOdd[n] - cumSumOdd[i+1])
		if sumOdd == sumEvn {
			ct++
		}
	}
	pln(ct)
}

package main

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

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

	M := N
	if N%2 == 1 {
		M++
	}
	A = append(A, 0)

	even := make([]int, M/2+1)
	odd := make([]int, M/2+1)

	for i := 0; i < M; i++ {
		if i%2 == 0 {
			even[i/2] = A[i]
			if i > 1 {
				even[i/2] += even[i/2-1]
			}
		} else {
			odd[i/2] = A[i]
			if i > 1 {
				odd[i/2] += odd[i/2-1]
			}
		}
	}
	count := 0
	for k := 0; k < N; k++ {
		prevOddSum := 0
		prevEvenSum := 0
		nextOddSum := 0
		nextEvenSum := 0

		if k%2 == 0 {
			if k > 0 {
				prevOddSum = odd[k/2-1]
			}
			if k > 1 {
				prevEvenSum = even[k/2-1]
			}
			nextOddSum = odd[M/2-1] - prevOddSum
			nextEvenSum = even[M/2-1] - even[k/2]
		} else {
			if k > 1 {
				prevOddSum = odd[k/2-1]
			}
			if k > 0 {
				prevEvenSum = even[k/2]
			}
			nextOddSum = odd[M/2-1] - odd[k/2]
			nextEvenSum = even[M/2-1] - prevEvenSum
		}
		A := prevEvenSum + nextOddSum
		B := prevOddSum + nextEvenSum
		//fmt.Println(A, prevEvenSum, nextOddSum, B, prevOddSum, nextEvenSum)

		if A == B {
			count++
		}
	}
	fmt.Println(count)
}

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

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

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

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

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

	return mp
}

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

package main

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

func main() {
	n := readInts()[0]
	a := readInts()
	r1, r2 := 0, 0
	for i := 1; i < n; i += 2 {
		r1 += a[i]
	}
	for i := 2; i < n; i += 2 {
		r2 += a[i]
	}
	r := 0
	if r1 == r2 {
		r = 1
	}
	l1, l2 := 0, 0
	for i := 1; i < n; i++ {
		if i % 2 == 1 {
			r1 -= a[i]
			l1 += a[i-1]
		} else {
			r2 -= a[i]
			l2 += a[i-1]
		}
		if l1 + r1 == l2 + r2 {
			r += 1
		}
	}
	fmt.Println(r)
}

var (
	stdin = bufio.NewReader(os.Stdin)
)

func readLine() string {
	s, _ := stdin.ReadString('\n')
	return s
}

func readInts() (r []int) {
	reader := strings.NewReader(readLine())
	for {
		var x int
		_, err := fmt.Fscan(reader, &x)
		if err != nil {
			break
		}
		r = append(r, x)
	}
	return
}

package main

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

func solveB(mat, sum, even []int, n, index int) bool {
	if (sum[n-1]-mat[index])%2 != 0 {
		return false
	}
	p := (sum[n-1] - mat[index]) / 2
	esum := even[index]
	if index%2 == 0 {
		esum -= mat[index]
	}

	esum += sum[n-1]-sum[index]-(even[n-1]-even[index])

	if p == esum {
		return true
	}
	return false
}
func main() {
	buf := bufio.NewReader(os.Stdin)

	var n int
	fmt.Fscanf(buf, "%d\n", &n)

	mat := make([]int, n)
	sum := make([]int, n)
	even := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &mat[i])
		if i == 0 {
			sum[i] = mat[i]
			even[i] = mat[i]
		} else {
			sum[i] = mat[i] + sum[i-1]
			if (i & 1) == 0 {
				even[i] = even[i-1] + mat[i]
			} else {
				even[i] = even[i-1]
			}
		}
	}
	ans := 0
	for i := 0; i < n; i++ {
		if solveB(mat,sum,even,n,i) {
			ans++
		}
	}
	fmt.Printf("%d\n", ans)
}

package main

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

func main()  {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n,x  int
	fmt.Fscan(in,&n)
	a := make([]int,n + 1)
	var oddsuf,evensuf,evepref,oddpref int64;
	for i := 1 ; i <= n ;i++{
		fmt.Fscan(in,&x)
		a[i] = x;
		if(i % 2 == 1){
			oddsuf += int64(x);
		}else{
			evensuf += int64(x)
		}
	}
	ans := 0;
	for i := 1 ;i  <= n;i ++{
		if(i % 2 == 0){
			evensuf -= int64(a[i])
		}else{
			oddsuf -= int64(a[i])
		}
		if(evensuf + oddpref == oddsuf + evepref){
			ans ++
		}
		if(i % 2 == 0){
			evepref += int64(a[i])
		}else{
			oddpref += int64(a[i])
		}
	}
	fmt.Fprint(out,ans)

}

package main

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

var sc = bufio.NewScanner(os.Stdin)

func init() { sc.Split(bufio.ScanWords) }
func ri() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}
func isOdd(n int) bool { return n%2 != 0 }

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

	cumSumOdd := make([]int, n+1)
	cumSumEvn := make([]int, n+1)
	for i := 0; i < n; i++ {
		cumSumOdd[i+1] += cumSumOdd[i]
		cumSumEvn[i+1] += cumSumEvn[i]
		if isOdd(i) {
			cumSumOdd[i+1] += a[i]
		} else {
			cumSumEvn[i+1] += a[i]
		}
	}

	ct := 0
	for i := 0; i < n; i++ {
		sumOdd := (cumSumOdd[i] - cumSumOdd[0]) + (cumSumEvn[n] - cumSumEvn[i+1])
		sumEvn := (cumSumEvn[i] - cumSumEvn[0]) + (cumSumOdd[n] - cumSumOdd[i+1])
		if sumOdd == sumEvn {
			ct++
		}
	}
	fmt.Println(ct)
}

package main

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

func solve(a []int) int {
	res := (0)

	a = append([]int{0}, a...)
	n := len(a)
	oddSum := make([]int, n)
	evenSum := make([]int, n)

	oddTotal := 0
	evenTotal := 0

	for i, w := range a {
		if i%2 == 0 {
			evenTotal += w
		} else {
			oddTotal += w
		}
		oddSum[i] = oddTotal
		evenSum[i] = evenTotal
	}

	for i := 1; i < n; i++ {
		even := evenSum[i-1] + oddTotal - oddSum[i]
		odd := oddSum[i-1] + evenTotal - evenSum[i]
		if even == odd {
			res++
		}
	}

	return res
}

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

func main() {
	n := 0

	fmt.Scan(&n)
	a := make([]int, n)

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := range a {
		scanner.Scan()
		a[i] = toInt(scanner.Bytes())
	}

	fmt.Println(solve(a))
}

package main

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

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

	M := N
	if N%2 == 1 {
		M++
	}
	A = append(A, 0)

	even := make([]int, M/2+1)
	odd := make([]int, M/2+1)

	for i := 0; i < M; i++ {
		if i%2 == 0 {
			even[i/2+1] = A[i]
			if i > 1 {
				even[i/2+1] += even[i/2]
			}
		} else {
			odd[i/2+1] = A[i]
			if i > 1 {
				odd[i/2+1] += odd[i/2]
			}
		}
	}
	count := 0
	for k := 0; k < N; k++ {
		A := odd[M/2] - odd[k/2]
		if k > 1 {
			A += even[k/2]
		}
		B := even[M/2] - even[k/2+1]
		if k > 1 {
			B += odd[k/2]
		}
		if A == B {
			count++
		}
	}
	fmt.Println(count)
}

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

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

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

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

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

	return mp
}

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


package main

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

const N int = 1e5 + 7
const INF int = math.MaxInt

var a []int = make([]int, N)
var st, ed []int = make([]int, N), make([]int, N)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, n, k, u, v int

	fmt.Fscan(in, &t)
	for ; t != 0; t-- {
		fmt.Fscan(in, &n, &k)
		for i := 1; i <= n; i++ {
			fmt.Fscan(in, &a[i])
			st[a[i]], ed[a[i]] = i-1, INF
			if st[a[i]] == 0 {
				st[a[i]] = 1
			}
		}
		currentBest := a[1]
		for i := 1; i < n; i++ {
			if currentBest > a[i+1] {
				ed[a[i+1]] = i
			} else {
				ed[currentBest] = i
				currentBest = a[i+1]
			}
		}
		for ; k != 0; k-- {
			fmt.Fscan(in, &u, &v)
			u = a[u]
			if st[u] == ed[u] || v < st[u] {
				fmt.Fprintln(out, 0)
			} else {
				if ed[u] <= v {
					v = ed[u]
				} else {
					v = v + 1
				}
				fmt.Fprintln(out, v-st[u])
			}
		}
	}
}

// 00
package main

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

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

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	total := 0
	for t := 1; t <= T; t++ {
		var n, q int
		fmt.Fscan(reader, &n, &q)
		a := make([]int, n)
		mx := make([]int, n)
		max_i := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
			if a[i] == n {
				max_i = i
			}
			if i == 0 {
				mx[i] = a[i]
			} else {
				mx[i] = max(a[i], mx[i-1])
			}
		}
		maxed := make([]int, 0)
		for i := 0; i < n; i++ {
			if a[i] == mx[i] {
				maxed = append(maxed, i)
			}
		}
		maxed_wins := make(map[int]int)
		for i := 0; i < len(maxed)-1; i++ {
			maxed_wins[maxed[i]] = maxed[i+1] - maxed[i] - 1
		}
		for i := 0; i < q; i++ {
			total++
			var j, k int
			fmt.Fscan(reader, &j, &k)
			if a[j-1] == n {
				if k <= max_i-1 {
					fmt.Println(0)
				} else if max_i-1 > 0 {
					fmt.Println(k - (max_i - 1))
				} else {
					fmt.Println(k)
				}
			} else {
				if a[j-1] == mx[j-1] {
					wins := maxed_wins[j-1]
					if k <= j-2 {
						fmt.Println(0)
					} else {
						if j > 1 {
							wins++
							fmt.Println(min(k-(j-2), wins))
						} else {
							if k <= wins {
								fmt.Println(k)
							} else {
								fmt.Println(wins)
							}
						}

					}
				} else {
					fmt.Println(0)
				}
			}

		}
	}
}

package main

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

const MOD int64 = 1000000007

var n, q int

var a[]int

var qu [][2]int

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

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

func main() {

	read := bufio.NewReader(os.Stdin)

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

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

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

		a = make([]int, n)

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

		qu = make([][2]int, q)

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

		solve()
	}
}

type pair struct {
	v int
	idx []int
}

func solve() {

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

	cycle := 0

	var queue []int

	for i := 0; i < n; i++ {
		queue = append(queue, a[i])
	}

	for ;; {
		f := queue[0]
		s := queue[1]

		if f == n {
			break
		}

		cycle++

		//queue = queue[2 : ]

		if f > s {
			if wins[f][1] == 0 {
				wins[f][1] = cycle
			}
			wins[f][0]++

			wins[s][2] = cycle

			queue[1] = f
			queue = queue[1 : ]
			queue = append(queue, s)

		} else {
			if wins[s][1] == 0 {
				wins[s][1] = cycle
			}

			wins[s][0]++

			wins[f][2] = cycle

			queue = queue[1 : ]
			queue = append(queue, f)
		}

	}

	for j := 0; j < q; j++ {
		i, k := qu[j][0], qu[j][1]
		i--

		i = a[i]

		if i == n {

			if k >= cycle {
				fmt.Println(wins[i][0] + k - cycle)
			} else {
				startWin := wins[i][1]

				if k < startWin {
					fmt.Println(0)
				} else {
					fmt.Println(k - startWin + 1)
				}
			}

		} else {

			if k >= cycle {
				fmt.Println(wins[i][0])

			} else {
				startWin := wins[i][1]
				lose := wins[i][2]

				if startWin == 0 || k < startWin {
					fmt.Println(0)
				} else {
					if k >= lose {
						fmt.Println(wins[i][0])
					} else {
						fmt.Println(k - startWin + 1)
					}
				}
			}
		}
	}
}

//IQ = 87

//........105....99...87..87..87..87.87.......87....

//            86

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

package main

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

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, n)
		for i := range a {
			fmt.Fscan(r, &a[i])
		}
		b := make([][2]int, m)
		for i := range b {
			fmt.Fscan(r, &b[i][0], &b[i][1])
		}
		w1 := do(n, m, a, b)
		for _, v := range w1 {
			fmt.Fprintln(w, v)
		}
	}
}

func do(n, m int, a []int, b [][2]int) []int {
	c := make([]int, n)
	for i := range c {
		c[i] = n
	}
	x := 0
	for i := 1; i < n; i++ {
		if a[i] > a[x] {
			c[x] = i
			x = i
		} else {
			c[i] = x
		}
	}
	d := make([]int, m)
	for i, b := range b {
		x, k := b[0]-1, b[1]
		if x > c[x] {
			continue
		}
		if k < x {
			continue
		}
		if x > 0 {
			d[i] = 1
		}
		k -= x
		if c[x] < n {
			v := c[x] - x - 1
			if k > v {
				k = v
			}
		}
		d[i] += k
	}
	return d
}

package main

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

var (
	in      *bufio.Reader = bufio.NewReader(os.Stdin)
	t, n, q int
	a       []int  = make([]int, 1000000)
	pos     []int  = make([]int, 1000000)
	k       []int  = make([]int, 1000000)
	left    []bool = make([]bool, 1000000) //记录左侧是否有更大的元素，如果有，该元素不可能成为赢家
	right   []int  = make([]int, 1000000)  //记录右侧第一个更大的元素，再此之前，该元素才有可能成为赢家
)

func main() {
	fmt.Fscan(in, &t)
	for t > 0 {
		fmt.Fscan(in, &n, &q)
		for i := 1; i <= n; i++ {
			fmt.Fscan(in, &a[i])
		}
		for i := 1; i <= q; i++ {
			fmt.Fscan(in, &pos[i], &k[i])
		}
		// fmt.Printf("n=%d, q=%d, a=%v, pos=%v, k=%v\n", n, q, a[1:n+1], pos[1:q+1], k[1:q+1])
		solve()
		t--
	}
}

func solve() {
	//初始化left
	for i := 1; i <= n; i++ {
		left[i] = false
	}
	for i, max := 2, a[1]; i <= n; i++ {
		if max > a[i] {
			left[i] = true
		} else {
			max = a[i]
		}
	}
	//初始化right
	que := []int{}
	for i := n; i >= 1; i-- {
		for len(que) != 0 && a[que[len(que)-1]] < a[i] {
			que = que[:len(que)-1]
		}
		if len(que) == 0 {
			right[i] = n + 1
		} else {
			right[i] = que[len(que)-1]
		}
		que = append(que, i)
	}
	// fmt.Printf("left=%v,right=%v\n", left[1:n+1], right[1:n+1])
	// fmt.Printf("a=%v\n", a[1:n+1])
	for qi := 1; qi <= q; qi++ {
		ans := 0
		if left[pos[qi]] || k[qi] < pos[qi]-1 {
			//查询的数字左侧有比他大或者前k元素个没有当前数字
			// print(ans)
			fmt.Println(ans)
		} else {
			if right[pos[qi]] == n+1 {
				ans = k[qi] - val(pos[qi]) + 1
			} else {
				ans = min(val(right[pos[qi]])-1, k[qi]) - val(pos[qi]) + 1
			}
			fmt.Println(ans)
		}
	}
}

// 第一次轮到a比赛需要比赛多少轮
func val(a int) int {
	if a == 0 || a == 1 {
		return 1
	}
	return a - 1
}
func print(ans int) {
	fmt.Printf("ans=%d\n", ans)
}
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
}

package main

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

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

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

	var T int
	var n int
	var m int
	var x int
	var y int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		Fscan(in, &m)

		a := make([]int, n)
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			a[i] = x
		}

		w := make([][]int, n)
		mi := 0
		for i := 1; i < n; i++ {
			if a[i] > a[mi] {
				mi = i
				w[i] = append(w[i], i)
			} else {
				w[mi] = append(w[mi], i)
			}
		}

		for i := 0; i < m; i++ {
			Fscan(in, &x)
			x--
			Fscan(in, &y)

			t := y
			if t > n-1 {
				t = n - 1
			}
			ans := sort.SearchInts(w[x], t+1)
			if y > n-1 && mi == x {
				ans += y - n + 1
			}
			Fprintln(out, ans)
		}
	}
}

package main

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

func remove(s []int, i int) []int {
	s[i] = s[len(s)-1]
	return s[:len(s)-1]
}

func main() {
	var t int

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	t = scanAtoi(scanner)
	for ; t > 0; t-- {
		var n, q int
		n, q = scanAtoi(scanner), scanAtoi(scanner)
		mas := make([]int, n)
		start := make([]int, n)
		end := make([]int, n)

		for i := 0; i < n; i++ {
			var b int
			b = scanAtoi(scanner)
			mas[i] = b
			start[i] = 2000000000
			end[i] = 2000000000
		}
		ch1 := 0
		ch2 := 1
		start[0] = 1
		start[1] = 1
		for i := 1; i < n; i++ {
			if mas[ch2] > mas[ch1] {
				end[ch1] = i
				start[ch2] = i
				ch1, ch2 = ch2, ch2+1
			} else {
				end[ch2] = i
				ch2++
			}
		}

		for i := 0; i < q; i++ {
			var r, k int
			r, k = scanAtoi(scanner), scanAtoi(scanner)
			r--
			ans := 0
			k++
			curEnd := k
			if end[r] < curEnd {
				curEnd = end[r]
			}
			ans = curEnd - start[r]
			if ans < 0 {
				ans = 0
			}
			fmt.Println(ans)
		}
	}
}

func scanAtoi(scanner *bufio.Scanner) (n int) {
	scanner.Scan()

	txt := scanner.Text()
	n, _ = strconv.Atoi(txt)

	return
}

package main

import (
	"fmt"
	"os"
)

func remove(s []int, i int) []int {
	s[i] = s[len(s)-1]
	return s[:len(s)-1]
}

func main() {
	var t int
	f, _ := os.Open("input.txt")

	fmt.Fscanf(f, "%d\n", &t)
	for ; t > 0; t-- {
		var n, q int
		fmt.Fscanf(f, "%d %d\n", &n, &q)
		mas := make([]int, n)
		start := make([]int, n)
		end := make([]int, n)
		for i := 0; i < n; i++ {
			var b int
			if i == n-1 {
				fmt.Fscanf(f, "%d\n", &b)
			} else {
				fmt.Fscanf(f, "%d", &b)
			}
			mas[i] = b
			start[i] = 2000000000
			end[i] = 2000000000
		}
		ch1 := 0
		ch2 := 1
		start[0] = 1
		start[1] = 1
		for i := 1; i < n; i++ {
			if mas[ch2] > mas[ch1] {
				end[ch1] = i
				start[ch2] = i
				ch1, ch2 = ch2, ch2+1
			} else {
				end[ch2] = i
				ch2++
			}
		}

		for i := 0; i < q; i++ {
			var r, k int
			fmt.Fscanf(f, "%d %d\n", &r, &k)
			r--
			ans := 0
			k++
			curEnd := k
			if end[r] < curEnd {
				curEnd = end[r]
			}
			ans = curEnd - start[r]
			if ans < 0 {
				ans = 0
			}
			fmt.Println(ans)
		}
	}
}

// 00
package main

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

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

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	total := 0
	for t := 1; t <= T; t++ {
		var n, q int
		fmt.Fscan(reader, &n, &q)
		a := make([]int, n)
		mx := make([]int, n)
		max_i := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
			if a[i] == n {
				max_i = i
			}
			if i == 0 {
				mx[i] = a[i]
			} else {
				mx[i] = max(a[i], mx[i-1])
			}
		}
		for i := 0; i < q; i++ {
			total++
			var j, k int
			fmt.Fscan(reader, &j, &k)
			if total == 132 {
				fmt.Println(a, j, k)
			}
			if a[j-1] == n {
				if k <= max_i-1 {
					fmt.Println(0)
				} else if max_i-1 > 0 {
					fmt.Println(k - (max_i - 1))
				} else {
					fmt.Println(k)
				}
			} else {
				if a[j-1] == mx[j-1] {
					start := j
					wins := 0
					for start < n && a[start] < a[j-1] {
						start++
						wins++
					}
					if k <= j-1 {
						fmt.Println(0)
					} else {
						if j-1 > 0 {
							wins++
							fmt.Println(min(k-(j-1), wins))
						} else {
							if k <= wins {
								fmt.Println(k)
							} else {
								fmt.Println(wins)
							}
						}

					}
				} else {
					fmt.Println(0)
				}
			}

		}
	}
}

// 00
package main

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

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

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n, q int
		fmt.Fscan(reader, &n, &q)
		a := make([]int, n)
		mx := make([]int, n)
		max_i := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
			if a[i] == n {
				max_i = i
			}
			if i == 0 {
				mx[i] = a[i]
			} else {
				mx[i] = max(a[i], mx[i-1])
			}
		}
		for i := 0; i < q; i++ {
			var j, k int
			fmt.Fscan(reader, &j, &k)
			if a[j-1] == n {
				if k <= max_i-1 {
					fmt.Println(0)
				} else if max_i-1 > 0 {
					fmt.Println(k - (max_i - 1))
				} else {
					fmt.Println(k)
				}
			} else {
				if a[j-1] == mx[j-1] {
					start := j
					wins := 0
					for start < n && a[start] < a[j-1] {
						start++
						wins++
					}
					if k <= j-1 {
						fmt.Println(0)
					} else {
						if j-1 > 0 {
							wins++
							fmt.Println(min(k-(j-1), wins))
						} else {
							if k <= wins {
								fmt.Println(k)
							} else {
								fmt.Println(wins)
							}
						}

					}
				} else {
					fmt.Println(0)
				}
			}

		}
	}
}

// 00
package main

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

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

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n, q int
		fmt.Fscan(reader, &n, &q)
		a := make([]int, n)
		mx := make([]int, n)
		max_i := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
			if a[i] == n {
				max_i = i
			}
			if i == 0 {
				mx[i] = a[i]
			} else {
				mx[i] = max(a[i], mx[i-1])
			}
		}
		for i := 0; i < q; i++ {
			var j, k int
			fmt.Fscan(reader, &j, &k)
			if a[j-1] == n {
				if k <= max_i-1 {
					fmt.Println(0)
				} else if max_i-1 > 0 {
					fmt.Println(k - (max_i - 1))
				} else {
					fmt.Println(k)
				}
			} else {
				if a[j-1] == mx[j-1] {
					start := j
					wins := 1
					for start < n && a[start] < a[j-1] {
						start++
						wins++
					}
					if k < j-1 {
						fmt.Println(0)
					} else if k < start {
						fmt.Println(start - k - (j - 2))
					} else {
						fmt.Println(wins)
					}
				} else {
					fmt.Println(0)
				}
			}

		}
	}
}

// 00
package main

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

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

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n, q int
		fmt.Fscan(reader, &n, &q)
		a := make([]int, n)
		mx := make([]int, n)
		max_i := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
			if a[i] == n {
				max_i = i
			}
			if i == 0 {
				mx[i] = a[i]
			} else {
				mx[i] = max(a[i], mx[i-1])
			}
		}
		for i := 0; i < q; i++ {
			var j, k int
			fmt.Fscan(reader, &j, &k)
			if a[j-1] == n {
				if k <= max_i-1 {
					fmt.Println(0)
				} else if max_i-1 > 0 {
					fmt.Println(k - (max_i - 1))
				} else {
					fmt.Println(k)
				}
			} else {
				if a[j-1] == mx[j-1] {
					start := j
					wins := 0
					for start < n && a[start] < a[j-1] {
						start++
						wins++
					}
					if k < wins {
						fmt.Println(k)
					} else {
						fmt.Println(wins)
					}
				} else {
					fmt.Println(0)
				}
			}

		}
	}
}

package main

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

const MOD int64 = 1000000007

var n, q int

var a[]int

var qu [][2]int

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

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

func main() {

	read := bufio.NewReader(os.Stdin)

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

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

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

		a = make([]int, n)

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

		qu = make([][2]int, q)

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

		solve()
	}
}

type pair struct {
	v int
	idx []int
}

func solve() {

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

	cycle := 0

	var queue []int

	for i := 0; i < n; i++ {
		queue = append(queue, a[i])
	}

	for ;; {
		f := queue[0]
		s := queue[1]

		if f == n {
			break
		}

		cycle++

		//queue = queue[2 : ]

		if f > s {
			if wins[f][1] == 0 {
				wins[f][1] = cycle
			}
			wins[f][0]++

			wins[s][2] = cycle

			queue[1] = f
			queue = queue[1 : ]
			queue = append(queue, s)

		} else {
			if wins[s][1] == 0 {
				wins[s][1] = cycle
			}

			wins[s][0]++

			wins[f][2] = cycle

			queue = queue[1 : ]
			queue = append(queue, f)
		}

	}

	for j := 0; j < q; j++ {
		i, k := qu[j][0], qu[j][1]
		i--

		i = a[i]

		if i == n {

			if k >= cycle {
				fmt.Println(wins[i][0] + k - cycle)
			} else {
				startWin := wins[i][1]

				if k < startWin {
					fmt.Println(0)
				} else {
					fmt.Println(k - startWin + 1)
				}
			}

		} else {

			if k >= cycle {
				fmt.Println(wins[i][0])

			} else {
				startWin := wins[i][1]
				lose := wins[i][2]

				if startWin == 0 || k < startWin {
					fmt.Println(0)
				} else {
					if k >= lose {
						fmt.Println(wins[i])
					} else {
						fmt.Println(k - startWin + 1)
					}
				}
			}
		}
	}
}

//IQ = 87

//........105....99...87..87..87..87.87.......87....

//            86

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

package main

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

const MOD int64 = 1000000007

var n, q int

var a[]int

var qu [][2]int

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

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

func main() {

	read := bufio.NewReader(os.Stdin)

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

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

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

		a = make([]int, n)

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

		qu = make([][2]int, q)

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

		solve()
	}
}

type pair struct {
	v int
	idx []int
}

func solve() {

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

	cycle := 0

	var queue []int

	for i := 0; i < n; i++ {
		queue = append(queue, a[i])
	}

	for ;; {
		cycle++
		f := queue[0]
		s := queue[1]
		//queue = queue[2 : ]

		if f == n {
			break
		}

		if f > s {
			if wins[f][1] == 0 {
				wins[f][1] = cycle
			}
			wins[f][0]++

			wins[s][2] = cycle

			queue[1] = f
			queue = queue[1 : ]
			queue = append(queue, s)

		} else {
			if wins[s][1] == 0 {
				wins[s][1] = cycle
			}

			wins[s][0]++

			wins[f][2] = cycle

			queue = queue[1 : ]
			queue = append(queue, f)
		}

	}

	for j := 0; j < q; j++ {
		i, k := qu[j][0], qu[j][1]
		i--

		i = a[i]

		if i == n {

			if k >= cycle {
				fmt.Println(wins[i][0] + k - cycle + 1)
			} else {
				startWin := wins[i][1]

				if k < startWin {
					fmt.Println(0)
				} else {
					fmt.Println(k - startWin + 1)
				}
			}

		} else {

			if k >= cycle {
				fmt.Println(wins[i][0])

			} else {
				startWin := wins[i][1]
				lose := wins[i][2]

				if startWin == 0 || k < startWin {
					fmt.Println(0)
				} else {
					if k >= lose {
						fmt.Println(wins[i])
					} else {
						fmt.Println(k - startWin + 1)
					}
				}
			}
		}
	}
}

//IQ = 87

//........105....99...87..87..87..87.87.......87....

//            86

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

package main

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

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, n)
		for i := range a {
			fmt.Fscan(r, &a[i])
		}
		b := make([][2]int, m)
		for i := range b {
			fmt.Fscan(r, &b[i][0], &b[i][1])
		}
		w1 := do(n, m, a, b)
		for _, v := range w1 {
			fmt.Fprintln(w, v)
		}
	}
}

func do(n, m int, a []int, b [][2]int) []int {
	c := make([]int, n)
	for i := range c {
		c[i] = n
	}
	x := 0
	for i := 1; i < n; i++ {
		if a[i] > a[x] {
			c[x] = i
			x = i
		} else {
			c[i] = x
		}
	}
	d := make([]int, m)
	for i, b := range b {
		x, k := b[0]-1, b[1]
		if x > c[x] {
			d[i] = 0
			continue
		}
		if k < x {
			d[i] = k
			continue
		}
		if x > 0 {
			d[i] = 1
		}
		k -= x
		if c[x] < n {
			v := c[x] - x - 1
			if k > v {
				k = v
			}
		}
		d[i] += k
	}
	return d
}


package main

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

var in = bufio.NewReader(os.Stdin)

func solve() {
	var n, k int
	fmt.Fscan(in, &n)
	fmt.Fscan(in, &k)

	pairs := make(map[int]int)
	for i := 0; i < n/2; i++ {
		pairs[i] = (n - 1) ^ i
	}

	if 0 < k && k < n-1 {
		ck := (n - 1) ^ k
		if k < n/2 {
			delete(pairs, k)
		} else {
			delete(pairs, ck)
		}
		pairs[0] = ck
		pairs[k] = n - 1
	} else if k == n-1 {
		if n == 4 {
			fmt.Println(-1)
			return
		}
		delete(pairs, 0)
		delete(pairs, 1)
		delete(pairs, 2)
		pairs[0] = 2
		pairs[1] = n - 3
		pairs[n-2] = n - 1
	}

	for a, b := range pairs {
		fmt.Println(a, b)
	}
}

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

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

package main

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

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n, k int
	Fscan(in, &n, &k)
	a := make([]int, n/2)
	b := make([]int, n/2)
	if k == 0 {
		for i := 0; i < n/2; i++ {
			a[i] = i
			b[i] = c(i, n)
		}
	} else if k < n-1 {
		m := min32(c(k, n), k)
		a[m] = k
		b[m] = n - 1
		a[0] = c(k, n)
		b[0] = 0
		for i := 1; i < n/2; i++ {
			if i == m {
				continue
			}
			a[i] = i
			b[i] = c(i, n)
		}
	} else {
		if n == 4 && k == 3 {
			Fprintln(out, -1)
			return
		}
		a[1] = 1
		b[1] = n - 3
		a[0] = n - 1
		b[0] = n - 2
		a[2] = 2
		b[2] = 0
		for i := 3; i < n/2; i++ {
			a[i] = i
			b[i] = c(i, n)
		}
	}
	for i := 0; i < n/2; i++ {
		Fprintf(out, "%d %d\n", a[i], b[i])
	}

}

func c(i, n int) int {
	return i ^ (n - 1)
}

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

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

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var n, k, m int64
		fmt.Fscan(in, &n, &k)
		m = n - 1

		if n == 4 && k == 3 {
			fmt.Fprintln(out, -1)
		} else if m == k {
			p := m ^ 3
			fmt.Fprintln(out, m-1, m)
			fmt.Fprintln(out, 1, 3)
			fmt.Fprintln(out, 0, p)
			for i := int64(2); i < n/2; i++ {
				if i == p || i == 3 {
					continue
				}
				fmt.Fprintln(out, i, m-i)
			}
		} else {
			p := m ^ k
			fmt.Fprintln(out, k, m)
			if k != 0 {
				fmt.Fprintln(out, 0, p)
			}

			for i := int64(1); i < n/2; i++ {
				if i == p || i == k {
					continue
				}
				fmt.Fprintln(out, i, m-i)
			}
		}
	}
}

package main

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

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

	out = bufio.NewWriter(os.Stdout)
)

var complementFactor int = (1 << 16) - 1

func inv(n int) int {
	return n ^ complementFactor
}

func main() {
	var t int
	fmt.Fscanf(in, "%d\n", &t)
	for ;t > 0; t-- {
		var n, k int
		fmt.Fscanf(in, "%d %d\n", &n, &k)
		// 0..n-1 find pairs that sum of AND is k
		//
		complementFactor = n-1
		if k == 0 {
			for i := 0; i < n/2; i++ {
				fmt.Fprintf(out, "%d %d\n", i, inv(i))
			}
		} else if k < n-1 {
			for i := 0; i < n/2; i++ {
				if i != 0 && i != k && i != n-1 && i != inv(k) {
					fmt.Fprintf(out, "%d %d\n", i, inv(i))
				}
			}
			fmt.Fprintf(out, "%d %d\n", k, n-1)
			fmt.Fprintf(out, "%d %d\n", 0, inv(k))
		} else {
			if n <= 4 {
				fmt.Fprintf(out, "-1\n")
				continue
			}
			for i := 0; i < n/2; i++ {
				if i != n-1 && i != n-2 && i != n-3 && i != 1 && i != 0 && i != 2 {
					fmt.Fprintf(out, "%d %d\n", i, inv(i))
				}
			}
			fmt.Fprintf(out, "%d %d\n", n-1, n-2)
			fmt.Fprintf(out, "%d %d\n", 1, n-3)
			fmt.Fprintf(out, "%d %d\n", 0, 2)
		}
		out.Flush()
	}

	out.Flush()
}

package main

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

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

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

func solve(n, k int) []int {
	var ans = make([]int, n)
	if k+1 == n && n <= 4 {
		return nil
	}
	if k+1 == n {
		var ones = n - 1
		ans[0] = 1
		ans[1] = ones - 2 // ans[0] & ans[1] = 000...01
		ans[2] = ones
		ans[3] = ones - 1 // ans[2] & ans[3] = 1111..10
		ans[4] = 3
		ans[5] = ones - 3 // ans[4] & ans[5] = 0
		ans[6] = 0
		ans[7] = 2 // ans[6] & ans[7] = 0

		for i := 4; i < (n / 2); i++ {
			ans[2*i] = i
			ans[2*i+1] = n - i - 1
		}

		return ans
	}
	var indexOf = make([]int, n)
	for i := 0; i < (n / 2); i++ {
		ans[2*i] = i
		ans[2*i+1] = n - 1 - i
	}
	for i, v := range ans {
		indexOf[v] = i
	}
	if k == 0 {
		return ans
	}
	if k+1 == n && n <= 4 {
		return nil
	}
	var i = indexOf[k]
	var j = indexOf[0]
	ans[i], ans[j] = ans[j], ans[i]
	return ans
}

func main() {
	defer flush()

	var ntc int
	scan(&ntc)
	for t := 0; t < ntc; t++ {
		var n, k int
		scan(&n, &k)
		ans := solve(n, k)
		if ans == nil {
			printf("-1\n")
		} else {
			var check = 0
			for i := 0; i < len(ans); i += 2 {
				printf("%d %d\n", ans[i], ans[i+1])
				check += ans[i] & ans[i+1]
			}
			assert(check == k)
		}

	}
}

package main

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

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

	solve := func(Case int) {
		var n,k int
		Fscan(in, &n,&k)
		if n == 4 && k == 3 {
			Fprintln(out, -1)
			return
		}
		a := make([]int, n)
		for _i := range a {
			a[_i] = _i
		}
		if k == n-1 {
			a[2], a[1] = a[1], a[2]
			Fprintln(out, a[0],a[1])
			Fprintln(out, a[n-1],a[n-2])
			for i := 2; i < n/2; i++ {
				Fprintln(out, a[i],a[n-1-i])
			}
		} else {
			a[0], a[k] = a[k], a[0]
			for i := 0; i < n/2; i++ {
				Fprintln(out, a[i],a[n-1-i])
			}
		}
	}

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

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

}

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

package main

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

var in = bufio.NewReader(os.Stdin)

func solve() {
	var n, k int
	fmt.Fscan(in, &n)
	fmt.Fscan(in, &k)

	pairs := make(map[int]int)
	for i := 0; i < n/2; i++ {
		pairs[i] = (n - 1) ^ i
	}

	if 0 < k && k < n-1 {
		ck := (n - 1) ^ k
		if k < n/2 {
			delete(pairs, k)
		} else {
			delete(pairs, ck)
		}
		pairs[0] = ck
		pairs[k] = n - 1
	} else if k == n-1 {
		if n == 4 {
			fmt.Println(-1)
			return
		}
		delete(pairs, 0)
		delete(pairs, 1)
		delete(pairs, 2)
		pairs[0] = 1
		pairs[2] = n - 3
		pairs[n-2] = n - 1
	}

	for a, b := range pairs {
		fmt.Println(a, b)
	}
}

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

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


package main

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

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

	m := map[[2]int]byte{}
	var q, v, w, same, bi int
	var s string
	for Fscan(in, &q, &q); q > 0; q-- {
		if Fscan(in, &s, &v); s[0] == '+' {
			Fscan(in, &w, &s)
			m[[2]int{v, w}] = s[0]
			if c := m[[2]int{w, v}]; c > 0 {
				if c == s[0] {
					same++
				}
				bi++
			}
		} else if s[0] == '-' {
			Fscan(in, &w)
			x := m[[2]int{v, w}]
			delete(m, [2]int{v, w})
			if y := m[[2]int{w, v}]; y > 0 {
				if y == x {
					same--
				}
				bi--
			}
		} else {
			if v&1 > 0 && bi > 0 || v&1 == 0 && same > 0 {
				Fprintln(out, "YES")
			} else {
				Fprintln(out, "NO")
			}
		}
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	m := map[[2]int]string{}
	var q, v, w, eq, bi int
	var s string
	for Fscan(in, &q, &q); q > 0; q-- {
		if Fscan(in, &s, &v); s == "+" {
			Fscan(in, &w, &s)
			if t := m[[2]int{w, v}]; t != "" {
				if t == s {
					eq++
				}
				bi++
			}
			m[[2]int{v, w}] = s
		} else if s == "-" {
			Fscan(in, &w)
			if t := m[[2]int{w, v}]; t != "" {
				if t == m[[2]int{v, w}] {
					eq--
				}
				bi--
			}
			delete(m, [2]int{v, w})
		} else if eq+v&1*bi > 0 {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	m := map[[2]int]string{}
	var q, v, w, eq, bi int
	var s string
	for Fscan(in, &q, &q); q > 0; q-- {
		if Fscan(in, &s, &v); s == "+" {
			Fscan(in, &w, &s)
			if t := m[[2]int{w, v}]; t != "" {
				if t == s {
					eq++
				}
				bi++
			}
			m[[2]int{v, w}] = s
		} else if s == "-" {
			Fscan(in, &w)
			if t := m[[2]int{w, v}]; t != "" {
				if t == m[[2]int{v, w}] {
					eq--
				}
				bi--
			}
			delete(m, [2]int{v, w})
		} else if v&1 > 0 && bi > 0 || v&1 == 0 && eq > 0 {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}