The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples:  $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points;  $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points;  $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point;  $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$).

For each test case, print one integer — the maximum number of points a player can get for winning the game.

NoteTest cases of the example are described in the statement.

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
}

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

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


Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad:  Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good).  Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good).  Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good).  Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$.

The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy.

Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days.

NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$.

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

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

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] = 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"
	"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
		}
	}
}


Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya.$$$n$$$ athletes participate in the tournament, numbered from $$$1$$$ to $$$n$$$. Burenka determined the strength of the $$$i$$$-th athlete as an integer $$$a_i$$$, where $$$1 \leq a_i \leq n$$$. All the strength values are different, that is, the array $$$a$$$ is a permutation of length $$$n$$$. We know that in a fight, if $$$a_i > a_j$$$, then the $$$i$$$-th participant always wins the $$$j$$$-th.The tournament goes like this: initially, all $$$n$$$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back.Burenka decided to ask Tonya $$$q$$$ questions. In each question, Burenka asks how many victories the $$$i$$$-th participant gets in the first $$$k$$$ rounds of the competition for some given numbers $$$i$$$ and $$$k$$$. Tonya is not very good at analytics, so he asks you to help him answer all the questions.

The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$2 \leq n \leq 10^5$$$, $$$1 \leq q \leq 10^5$$$) — the number of tournament participants and the number of questions. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$) — the array $$$a$$$, which is a permutation. The next $$$q$$$ lines of a test case contain questions. Each line contains two integers $$$i$$$ and $$$k$$$ ($$$1 \leq i \leq n$$$, $$$1 \leq k \leq 10^9$$$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases do not exceed $$$10^5$$$.

For each Burenka's question, print a single line containing one integer — the answer to the question.

NoteIn the first test case, the first numbered athlete has the strength of $$$3$$$, in the first round he will defeat the athlete with the number $$$2$$$ and the strength of $$$1$$$, and in the second round, the athlete with the number $$$3$$$ and the strength of $$$2$$$.In the second test case, we list the strengths of the athletes fighting in the first $$$5$$$ fights: $$$1$$$ and $$$3$$$, $$$3$$$ and $$$4$$$, $$$4$$$ and $$$2$$$, $$$4$$$ and $$$1$$$, $$$4$$$ and $$$3$$$. The participant with the number $$$4$$$ in the first $$$5$$$ rounds won $$$0$$$ times (his strength is $$$2$$$). The participant with the number $$$3$$$ has a strength of $$$4$$$ and won $$$1$$$ time in the first two fights by fighting $$$1$$$ time.

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
}

// 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"
	"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])
			}
		}
	}
}

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
}


You are given a set of $$$n$$$ ($$$n$$$ is always a power of $$$2$$$) elements containing all integers $$$0, 1, 2, \ldots, n-1$$$ exactly once.Find $$$\frac{n}{2}$$$ pairs of elements such that:  Each element in the set is in exactly one pair.  The sum over all pairs of the bitwise AND of its elements must be exactly equal to $$$k$$$. Formally, if $$$a_i$$$ and $$$b_i$$$ are the elements of the $$$i$$$-th pair, then the following must hold: $$$$$$\sum_{i=1}^{n/2}{a_i \& b_i} = k,$$$$$$ where $$$\&$$$ denotes the bitwise AND operation. If there are many solutions, print any of them, if there is no solution, print $$$-1$$$ instead.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$) — the number of test cases. Description of the test cases follows. Each test case consists of a single line with two integers $$$n$$$ and $$$k$$$ ($$$4 \leq n \leq 2^{16}$$$, $$$n$$$ is a power of $$$2$$$, $$$0 \leq k \leq n-1$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2^{16}$$$. All test cases in each individual input will be pairwise different.

For each test case, if there is no solution, print a single line with the integer $$$-1$$$. Otherwise, print $$$\frac{n}{2}$$$ lines, the $$$i$$$-th of them must contain $$$a_i$$$ and $$$b_i$$$, the elements in the $$$i$$$-th pair.  If there are many solutions, print any of them. Print the pairs and the elements in the pairs in any order.

NoteIn the first test, $$$(0\&3)+(1\&2) = 0$$$.In the second test, $$$(0\&2)+(1\&3) = 1$$$.In the third test, $$$(0\&1)+(2\&3) = 2$$$.In the fourth test, there is no solution.

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

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


Vanya decided to walk in the field of size n × n cells. The field contains m apple trees, the i-th apple tree is at the cell with coordinates (xi, yi). Vanya moves towards vector (dx, dy). That means that if Vanya is now at the cell (x, y), then in a second he will be at cell . The following condition is satisfied for the vector: , where  is the largest integer that divides both a and b. Vanya ends his path when he reaches the square he has already visited. Vanya wonders, from what square of the field he should start his path to see as many apple trees as possible.

The first line contains integers n, m, dx, dy(1 ≤ n ≤ 106, 1 ≤ m ≤ 105, 1 ≤ dx, dy ≤ n) — the size of the field, the number of apple trees and the vector of Vanya's movement. Next m lines contain integers xi, yi (0 ≤ xi, yi ≤ n - 1) — the coordinates of apples. One cell may contain multiple apple trees.

Print two space-separated numbers — the coordinates of the cell from which you should start your path. If there are several answers you are allowed to print any of them.

NoteIn the first sample Vanya's path will look like: (1, 3) - (3, 1) - (0, 4) - (2, 2) - (4, 0) - (1, 3)In the second sample: (0, 0) - (1, 1) - (0, 0)

package main

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

var (
	n, m, dx, dy int
	x            []int
	y            []int
	result       int
)

func in() {
	read(&n, &m, &dx, &dy)
	x = make([]int, m)
	y = make([]int, m)
	for i := 0; i < m; i++ {
		read(&x[i], &y[i])
	}
}

func out() {
	println(0, result)
	defer output.Flush()
}

func run() {
	shift_y := make([]int, n)
	for i, x0, y0 := 0, 0, 0; i < n; i++ {
		shift_y[x0] = y0
		x0, y0 = (x0+dx)%n, (y0+dy)%n
	}

	count := make([]int, n)
	for i := 0; i < m; i++ {
		y0 := (y[i] - shift_y[x[i]] + n) % n
		count[y0]++
	}

	max := count[0]
	result = 0
	for i, v := range count {
		if v > max {
			max = v
			result = i
		}
	}
}

/** main **/
func main() {
	in()
	run()
	out()
}

// Input & Output
var (
	input  = bufio.NewReader(os.Stdin)
	output = bufio.NewWriter(os.Stdout)
)

func read(a ...interface{}) {
	_, err := fmt.Fscan(input, a...)
	if err != nil {
		println(err)
	}
}

func println(a ...interface{}) {
	fmt.Fprintln(output, a...)
}

func printf(format string, a ...interface{}) {
	fmt.Fprintf(output, format, a...)
}

package main

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

var (
	n, m, dx, dy int
	x            []int
	y            []int
	result       int
)

func in() {
	read(&n, &m, &dx, &dy)
	x = make([]int, m)
	y = make([]int, m)
	for i := 0; i < m; i++ {
		read(&x[i], &y[i])
	}
}

func out() {
	println(0, result)
	defer output.Flush()
}

func run() {
	cd := gcd(dx, dy)
	dx, dy = dx/cd, dy/cd

	if dx != 1 {
		for x0, y0 := 0, 0; ; x0, y0 = (x0+dx)%n, (y0+dy)%n {
			if x0 == 1 {
				dx, dy = x0, y0
				break
			}
		}
	}

	count := make([]int, n)
	for i := 0; i < m; i++ {
		y0 := (y[i] - x[i]*dy) % n
		if y0 < 0 {
			y0 += n
		}
		count[y0]++
	}

	max := count[0]
	result = 0
	for i, v := range count {
		if v > max {
			max = v
			result = i
		}
	}
}

/** main **/
func main() {
	in()
	run()
	out()
}

// Input & Output
var (
	input  = bufio.NewReader(os.Stdin)
	output = bufio.NewWriter(os.Stdout)
)

func read(a ...interface{}) {
	_, err := fmt.Fscan(input, a...)
	if err != nil {
		println(err)
	}
}

func println(a ...interface{}) {
	fmt.Fprintln(output, a...)
}

func printf(format string, a ...interface{}) {
	fmt.Fprintf(output, format, a...)
}

// Greatest common divisor
func gcd(a, b int) int {
	if a < b {
		a, b = b, a
	}

	for b != 0 {
		a, b = b, a%b
	}
	return a
}

package main

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

var (
	n, m, dx, dy int
	x            []int
	y            []int
	result       int
)

func in() {
	read(&n, &m, &dx, &dy)
	x = make([]int, m)
	y = make([]int, m)
	for i := 0; i < m; i++ {
		read(&x[i], &y[i])
	}
}

func out() {
	println(0, result)
	defer output.Flush()
}

func run() {
	cd := gcd(dx, dy)
	dx, dy = dx/cd, dy/cd
	dx, dy = 1, (dx + dy - 1)
	count := make([]int, n)
	for i := 0; i < m; i++ {
		y0 := (y[i] - x[i]*dy) % n
		if y0 < 0 {
			y0 += n
		}
		count[y0]++
	}
	max := count[0]
	result = 0
	for i, v := range count {
		if v > max {
			max = v
			result = i
		}
	}
}

/** main **/
func main() {
	in()
	run()
	out()
}

// Input & Output
var (
	input  = bufio.NewReader(os.Stdin)
	output = bufio.NewWriter(os.Stdout)
)

func read(a ...interface{}) {
	_, err := fmt.Fscan(input, a...)
	if err != nil {
		println(err)
	}
}

func println(a ...interface{}) {
	fmt.Fprintln(output, a...)
}

func printf(format string, a ...interface{}) {
	fmt.Fprintf(output, format, a...)
}

// Greatest common divisor
func gcd(a, b int) int {
	if a < b {
		a, b = b, a
	}

	for b != 0 {
		a, b = b, a%b
	}
	return a
}