package main

import (
	"bufio"
	"fmt"
	"os"
	"unicode/utf8"
)

func isPal(s string) bool {
	for len(s) > 0 {
		if len(s) == 1 {
			return true
		}
		r1, sz1 := utf8.DecodeRuneInString(s)
		r2, sz2 := utf8.DecodeLastRuneInString(s)
		if r1 != r2 {
			return false
		}
		s = s[sz1 : len(s)-sz2]
	}
	return true
}

func main() {
	in := bufio.NewReader(os.Stdin)
	var tests int
	fmt.Fscan(in, &tests)
	for i := 0; i < tests; i++ {
		var n, k int
		var s string
		fmt.Fscan(in, &n, &k, &s)
		if k == 0 || isPal(s) {
			fmt.Println(1)
		} else {
			fmt.Println(2)
		}
	}
}

package main

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

const MOD int64 = 1000000007

var n, k int

var s string

// 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, &k, &s)

		fmt.Println(solve())
	}
}

type pair struct {
	v int
	idx []int
}

func solve() int {

	if k == 0 {
		return 1
	}

	isPal := true

	for i, j := 0, n - 1; i <= j; i, j = i + 1, j - 1 {
		if s[i] == s[j] {
			continue
		}
		isPal = false
		break
	}

	if isPal {
		return 1
	}

	return 2
}

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

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 scanf(f string, a ...interface{})  { fmt.Fscanf(in, f, a...) }
func print(a ...interface{})			{ fmt.Fprintln(out, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) { if !f { panic("gg") } }

func slove(cases int) {
	n, k := 0, 0
	str := ""
	scan(&n, &k, &str)
	flag := false
	for i := range str {
		if str[i] != str[n-i-1] {
			flag = true
			break
		}
	}

	if k > 0 {
		if flag {
			print(2)
		} else {
			print(1)
		}
	} else {
		print(1)
	}
}

func main() {
	defer flush()

	cases := 0
	scan(&cases)
	for i := 0; i < cases; i++ {
		slove(i)
	}
}

package main

import "fmt"

func main() {
	var _t int

	fmt.Scan(&_t)

	for _t > 0 {
		_t--

		var n int
		var k int
		var s string

		fmt.Scan(&n, &k)
		fmt.Scan(&s)

		pali := true

		for i, _ := range s {
			if s[i] != s[n - i - 1] {
				pali = false
			}
		}

		var res int

		if pali || k == 0 {
			res = 1
		} else {
			res = 2
		}

		fmt.Println(res)
	}
}

package main

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

const (
	MOD int64 = 1e9+7
	NL byte = byte('\n')
	SP byte = byte(' ')
)

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

	n, k int
	s string
	res int
)

func main() {
	initBuffer()
	var t int = 1
	t = getInt()
	for i := 0; i < t; i++ {
		solve()
	}
	// test()
	out.Flush()
}

func solve() {
	initialize()
	input()
	count()
	output()
}

func initialize() {

}

func input() {
	n = getInt()
	k = getInt()
	s = getString()
}

func count() {
	if k == 0 || isPalind(s) {
		res = 1
	} else {
		res = 2
	}
}

func isPalind(s string) bool {
	for i := 0; i < len(s) / 2; i++ {
		if s[i] != s[len(s)-1-i] {
			return false
		}
	}
	return true
}

func output() {
	out.Write(intToBytes(res))
	out.WriteByte(NL)
}

func test() {
	fmt.Print()
}

func initBuffer() {
	in = bufio.NewScanner(os.Stdin)
	in.Buffer([]byte{}, 4 * 1024 * 1024)
	in.Split(scanWords)
	out = bufio.NewWriter(os.Stdout)
}

func scanWords(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	i := bytes.IndexByte(data, '\n')
	j := bytes.IndexByte(data, ' ')
	ind := min(i, j)
	if ind < 0 {
		ind = max(i, j)
	}
	if ind >= 0 {
		return ind + 1, dropCR(data[0:ind]), nil
	}
	if atEOF {
		return len(data), dropCR(data), nil
	}
	return 0, nil, nil
}

func dropCR(data []byte) []byte {
	if len(data) > 0 && data[len(data)-1] == '\r' {
		return data[:len(data)-1]
	}
	return data
}

func getFloat() float64 {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res1 int64
	for i := 0; i < len(b); i++ {
		if b[i] == '.' {
			b = b[i+1:]
			break
		}
		res1 *= 10
		res1 += int64(b[i]) - 48
		if i == len(b) - 1 {
			b = b[:0]
		}
	}
	var res2 float64
	for i := len(b); i > 0; i-- {
		res2 += float64(b[i-1]) - 48
		res2 /= 10
	}
	if isNeg {
		res1 *= -1
		res2 *= -1
	}
	return float64(res1) + res2
}

func getInt() int {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res int
	for i := 0; i < len(b); i++ {
		res *= 10
		res += int(b[i]) - 48
	}
	if isNeg {
		res *= -1
	}
	return res
}

func getInt64() int64 {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res int64
	for i := 0; i < len(b); i++ {
		res *= 10
		res += int64(b[i]) - 48
	}
	if isNeg {
		res *= -1
	}
	return res
}

func getString() string {
	in.Scan()
	return in.Text()
}

func intToBytes(n int) []byte {
	var res [33]byte
	var isNeg bool
	if n < 0 {
		isNeg = true
		n = -n
	}
	var l int = 33
	for n > 0 {
		l--
		res[l] = byte(n % 10 + 48)
		n /= 10
	}
	if l == 33 {
		l--
		res[l] = byte(48)
	}
	if isNeg {
		l--
		res[l] = byte(45)
	}
	return res[l:]
}

func int64ToBytes(n int64) []byte {
	var res [65]byte
	var isNeg bool
	if n < 0 {
		isNeg = true
		n = -n
	}
	var l int = 65
	for n > 0 {
		l--
		res[l] = byte(n % 10 + 48)
		n /= 10
	}
	if l == 65 {
		l--
		res[l] = byte(48)
	}
	if isNeg {
		l--
		res[l] = byte(45)
	}
	return res[l:]
}

func bsLower(arr []int, n int) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] < n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] >= n {
		return minN
	}
	return maxN
}

func bsLower64(arr []int64, n int64) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] < n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] >= n {
		return minN
	}
	return maxN
}

func bsUpper(arr []int, n int) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] <= n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] > n {
		return minN
	}
	return maxN
}

func bsUpper64(arr []int64, n int64) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] <= n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] > n {
		return minN
	}
	return maxN
}

func max(n1, n2 int) int {
	if n1 > n2 {
		return n1
	}
	return n2
}

func max64(n1, n2 int64) int64 {
	if n1 > n2 {
		return n1
	}
	return n2
}

func min(n1, n2 int) int {
	if n1 < n2 {
		return n1
	}
	return n2
}

func min64(n1, n2 int64) int64 {
	if n1 < n2 {
		return n1
	}
	return n2
}

func yes(res, isUpperCase bool) bool {
	var arr [4]string = [4]string{"No", "Yes", "NO", "YES"}
	var ind int
	if res == true {
		ind = 1
	}
	if isUpperCase == true {
		ind += 2
	}
	out.WriteString(arr[ind])
	out.WriteByte(NL)
	return res
}

package main
import "fmt"

func isPalin(s string) bool {
	for i:= 0; i < len(s); i++ {
		if s[i] != s[len(s) - i - 1] {
			return false
		}
	}
	return true
}

func solve() int {
	var (
		n int
		k int
		s string
	)
	fmt.Scanf("%v %v\n%v\n", &n, &k, &s)
	if k == 0 || isPalin(s) {
		return 1
	}
	return 2
}

func main() {
	var ntests int;
	fmt.Scanf("%v\n", &ntests)
	for i:= 0; i < ntests; i++ {
		fmt.Println(solve())
	}
}

package main

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

func parseCase(r *bufio.Reader) (int, int, string) {
	var n, k int
	{
		nkStr, _ := r.ReadString('\n')
		_, _ = fmt.Sscanf(nkStr, "%d %d", &n, &k)
	}
	word, _ := r.ReadString('\n')
	return n, k, strings.TrimSpace(word)
}

func parseHeader(r *bufio.Reader) int {
	tStr, _ := r.ReadString('\n')
	t, _ := strconv.Atoi(strings.TrimSpace(tStr))
	return t
}

func naiveTraverse(k int, s string, result map[string]interface{}) {
	if k == 0 {
		//fmt.Println(s)
		result[s] = struct{}{}
		return
	}
	runes := []rune(s)
	rs := make([]rune, 2 * len(s))
	sr := make([]rune, 2 * len(s))
	for i := 0; i < len(runes); i += 1 {
		rs[i] = runes[i]
		rs[len(runes) + i] = runes[len(runes) - 1 - i]
		sr[i] = runes[len(runes) - 1 - i]
		sr[len(runes) + i] = runes[i]
	}
	naiveTraverse(k - 1, string(rs), result)
	naiveTraverse(k - 1, string(sr), result)
}

func combine(s string) (string, string) {
	runes := []rune(s)
	rs := make([]rune, 2 * len(s))
	sr := make([]rune, 2 * len(s))
	for i := 0; i < len(runes); i += 1 {
		rs[i] = runes[i]
		rs[len(runes) + i] = runes[len(runes) - 1 - i]
		sr[i] = runes[len(runes) - 1 - i]
		sr[len(runes) + i] = runes[i]
	}
	return string(rs), string(sr)
}

func traverse(k int, s string, result map[string]interface{}, cache map[string]interface{}) {
	fmt.Println(k)
	if k == 0 {
		//fmt.Println(s)
		result[s] = struct{}{}
		return
	}
	cache[s] = struct{}{}
	_, rs := combine(s)
	traverse(k - 1, rs, result, cache)
	//if _, ok := cache[sr]; !ok {
	//	traverse(k - 1, sr, result, cache)
	//}
}

func main() {
	r := bufio.NewReader(os.Stdin)
	t := parseHeader(r)
	for i := 0; i < t; i += 1 {
		result := map[string]interface{}{}
		//cache := map[string]interface{}{}
		//fmt.Println(fmt.Sprintf("Case %d", i))
		_, k, word := parseCase(r)
		//fmt.Println(fmt.Sprintf("n = %d, k = %d", n, k))
		//fmt.Println(fmt.Sprintf("word = %s", word))
		if k > 3 {
			k = 3
		}
		naiveTraverse(k, word, result)
		fmt.Println(len(result))
	}
}

package main

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

/*
4
3 0
123
3 1
123
3 2
123
4 3
1111
---
1
2
2
1
*/

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		var n, k int
		var s string
		scanf("%d %d\n%s\n", &n, &k, &s)

		if k == 0 {
			fmt.Fprintln(writer, 1)
		} else {
			revS := reverse(s)
			if s+revS == revS+s {
				fmt.Fprintln(writer, 1)
			} else {
				fmt.Fprintln(writer, 2)
			}
		}
	}
}

func reverse(s string) (result string) {
	for _, v := range s {
		result = string(v) + result
	}
	return
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div != 0 {
		quot++
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	minimum := nums[0]
	for _, num := range nums {
		if num < minimum {
			minimum = num
		}
	}
	return minimum
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	maximum := nums[0]
	for _, num := range nums {
		if num > maximum {
			maximum = num
		}
	}
	return maximum
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

package main

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

func ishw(s string) bool {
	for i := 0; i < len(s); i++ {
		if s[i] != s[len(s)-i-1] {
			return false
		}
	}
	return true
}

func runcase(n, k int, s string) int {
	if k == 0 {
		return 1
	}
	if ishw(s) {
		return 1
	}
	return 2
}

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

	var t, n, k int
	var s string
	fmt.Fscanf(cin, "%d\n", &t)
	for i := 0; i < t; i++ {
		fmt.Fscanf(cin, "%d %d\n", &n, &k)
		fmt.Fscanf(cin, "%s\n", &s)
		fmt.Println(runcase(n, k, s))
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	t := readInt(in)
	b := strings.Builder{}
	for i := 0; i < t; i++ {
		b.WriteString(solve(in) + "\n")
	}
	fmt.Println(b.String())
}

func solve(in *bufio.Reader) string {
	args := readArrInt(in)
	str := readString(in)

	k := args[1]

	count := 1

	for i := 0; i < k; i++ {
		if op1(str) == op2(str) {
			return strconv.Itoa(count)
		} else {
			return "2"
		}
	}
	return "1"
}

func op1(str string) string {
	return str + rev(str)
}

func op2(str string) string {
	return rev(str) + str
}

func rev(str string) string {
	b := strings.Builder{}
	for i := 0; i < len(str); i++ {
		b.WriteByte(str[len(str) - 1 - i])
	}
	return b.String()
}

func isPalindrome(str string) bool {
	for i := 0; i < len(str); i++ {
		if str[i] != str[len(str) - 1 - i] {
			return false
		}
	}

	return true
}

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

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

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

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

package main

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

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

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

		var s string
		fmt.Fscan(in, &s)

		isPalindrome := func(s string) bool {
			for i := 0; i < len(s)/2; i++ {
				if s[i] != s[len(s)-i-1] {
					return false
				}
			}
			return true
		}

		if k == 0 || isPalindrome(s) {
			fmt.Fprintln(out, 1)
		} else {
			fmt.Fprintln(out, 2)
		}
	}
}

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(curCase int) {
		var n, k int
		var s string
		Fscan(in, &n, &k, &s)
		if k == 0 {
			Fprintln(out, 1)
			return
		}
		for i, n := 0, len(s); i < n/2; i++ {
			v, w := s[i], s[n-1-i]
			if v != w {
				Fprintln(out, 2)
				return
			}
		}
		Fprintln(out, 1)
	}

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

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

func isMirrored(str string) bool {
	half := len(str) / 2
	for i := 0; i < half; i++ {
		if str[i] != str[len(str)-1-i] {
			return false
		}
	}
	return true
}

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

	t, _ := reader.ReadString('\r')

	tCount, _ := strconv.Atoi(t[:len(t)-1])

	for i := 0; i < tCount; i++ {
		nAndKStr, _ := reader.ReadString('\r')
		nAndK := strings.Split(nAndKStr, " ")
		k, _ := strconv.Atoi(nAndK[1][:len(nAndK[1])-1])

		input, _ := reader.ReadString('\r')

		if k > 0 && !isMirrored(input[1:len(input)-1]) {
			fmt.Println(2)
		} else {
			fmt.Println(1)
		}
	}
}

func main() {
	resultsCount()
}

package main
import(
    "fmt"
    "bufio"
    "os"
    "strings"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var x,y int
		fmt.Fscan(in,&x,&y)
		var word string
		fmt.Fscan(in,&word)
		if y == 0 || x == 1 {
			fmt.Fprintln(out,1)
		} else {
			list:= strings.Split(word,"")
			for j,k:=0,len(list)-1;j<k;j,k=j+1,k-1{
				list[j],list[k] = list[k],list[j]
			}
			word2:= strings.Join(list,"")
			if word == word2 {
				fmt.Fprintln(out,1)
			} else {
				fmt.Fprintln(out,2)
			}
		}
	}
}

package main

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

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

func scan(a ...interface{}) {
	fmt.Fscan(in, a...)
}
func printf(f string, a ...interface{}) {
	fmt.Fprintf(out, f, a...)
}
func Reverse(s string) string {
	runes := []rune(s)
	for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
		runes[i], runes[j] = runes[j], runes[i]
	}
	return string(runes)
}
func solve() {
	var n, k int
	scan(&n, &k)
	var s string
	scan(&s)
	if k >= 1 {
		t := Reverse(s)
		s1 := t + s
		s2 := s + t
		if s1 != s2 {
			printf("2\n")
			return
		}
	}
	printf("1\n")

}
func main() {
	defer out.Flush()
	n := 0
	scan(&n)
	for i := 0; i < n; i++ {
		solve()
	}
}

package main

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

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

func (in *CF1634A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1634A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1634A
Date: 9/03/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1634/A
Problem: CF1634A
**/
func (in *CF1634A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, k := in.NextInt(), in.NextInt()
		str := in.NextString()

		mapa := make(map[uint8]int)
		for i := 0; i < len(str); i++ {
			mapa[str[i]]++
		}

		rev := ""
		for i := n - 1; i >= 0; i-- {
			rev += string(str[i])
		}
		cant := len(mapa)
		ans := 1
		if k >= 1 && cant > 1 && rev != str{
			ans = 2
		}

		fmt.Println(ans)
	}

}

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

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

package main

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

var in = bufio.NewReader(os.Stdin)

func main() {
	t := readInt(in)
	for ; t != 0; t-- {
		solve()
	}
}

func solve() {
	input := readArrInt(in)
	n, k := input[0], input[1]
	s, _ := in.ReadString('\n')
	s = strings.Trim(s, " \n\r")
	flag := true
	for i := 0; i < n; i++ {
		if s[i] != s[n-1-i] {
			flag = false
		}
	}
	if k == 0 {
		fmt.Println(1)
	} else if flag {
		fmt.Println(1)
	} else {
		fmt.Println(2)
	}
}

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

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

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

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

package main

import (
	"fmt"
)

func test() {
	var n, k int
	var x string
	fmt.Scan(&n, &k, &x)
	if k==0 {
		fmt.Println(1)
		return
	}
	for i, j := 0, len(x)-1; i < j; i, j = i+1, j-1 {
		if x[i] != x[j] {
			fmt.Println(2)
			return
		}
	}
	fmt.Println(1)
}
func main() {
	var no int
	fmt.Scan(&no)
	for i := 0; i < no; i++ {
		test()
	}
}

package main

import (
	"bufio"
	"crypto/rand"
	"fmt"
	"math"
	"math/big"
	"os"
	"sort"
	"strconv"
)

func main() {

	reader := bufio.NewReader(os.Stdin)
	writter := bufio.NewWriter(os.Stdout)
	defer writter.Flush()

	t := 0
	fmt.Fscan(reader, &t)

	for t > 0 {
		n, k := 0, 0
		fmt.Fscan(reader, &n, &k)

		s := ""
		fmt.Fscan(reader, &s)

		fmt.Fprintln(writter, solve(n, k, s))

		t--
	}

	// n, k := 3, 1
	// s := "aab"
	// fmt.Println(solve(n, k, s))

	//getPower()

}

func solve(n, k int, s string) int {

	if k < 1 {
		return 1
	}

	if palindrome(s) {
		return 1
	}

	return 2
}

func palindrome(str string) bool {

	size := len(str)

	for i := 0; i < size/2; i++ {
		if str[i] != str[size-1-i] {
			return false
		}
	}

	return true

}

func reverseString(str string) string {
	result := ""

	for i := len(str) - 1; i >= 0; i-- {

		result += string(str[i])

	}

	return result
}

func arraySum(array []int) int {
	sum := 0

	for i := range array {
		sum += array[i]
	}

	return sum
}

func numSum(num int) int {
	sum := 0

	for i := num; i > 0; i-- {
		sum += i
	}

	return sum
}

func strToint(str string) int {
	num, _ := strconv.Atoi(str)
	return num
}

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

	return b
}

func getFactor(num int) []int {
	result := []int{}

	for i := 1; i <= int(math.Sqrt(float64(num))); i++ {
		if num%i == 0 {
			result = append(result, i)
			result = append(result, num/i)
		}
	}

	return result
}

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

	return gcd(b%a, a)
}

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

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

		fmt.Println()

	}
}

func checkPrime(num int) bool {

	if num < 2 {
		return false
	}

	for i := 2; i <= int(math.Sqrt(float64(num))); i++ {
		if num%i == 0 {
			return false
		}
	}

	return true
}

func getPower() {
	A := []int{}

	for i := 1; i <= 38; i++ {
		A = append(A, i)
	}

	for j := 0; j < 10000; j++ {
		for i := range A {
			target, _ := rand.Int(rand.Reader, big.NewInt(38))
			A[i], A[target.Int64()] = A[target.Int64()], A[i]
		}
	}

	B, _ := rand.Int(rand.Reader, big.NewInt(8))

	result := A[:6]
	sort.Ints(result)

	fmt.Println(result)
	fmt.Println(int(B.Int64()) + 1)
}

func xor(a, b int) int {
	aBit := strconv.FormatInt(int64(a), 2)
	bBit := strconv.FormatInt(int64(b), 2)

	size := max(len(aBit), len(bBit))

	for i := size - len(aBit); i > 0; i-- {
		aBit = "0" + aBit
	}
	for i := size - len(bBit); i > 0; i-- {
		bBit = "0" + bBit
	}

	result := ""
	for i := range aBit {
		if aBit[i] == bBit[i] {
			result += "0"
		} else {
			result += "1"
		}
	}

	//fmt.Println(result)
	num, _ := strconv.ParseInt(result, 2, 64)

	return int(num)
}

package main

import (
	"fmt"
)

func main() {
	var q int
	fmt.Scan(&q)
	for i := 0; i < q; i++ {
		var n, k int
		var s string
		fmt.Scan(&n, &k)
		fmt.Scan(&s)

		if same(s) || k < 1 {
			fmt.Println(1)
		} else {
			fmt.Println(2)
		}
	}
}

func same(s string) bool {
	for i := 0; i < len(s); i++ {
		if s[i] != s[len(s)-i-1] {
			return false
		}
	}
	return true
}

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 main() {
	defer flush()

	var ntc int
	scan(&ntc)
	for t := 0; t < ntc; t++ {
		var n, k int
		scan(&n, &k)
		var s[]byte
		scan(&s)
		pal := true
		for i := 0; i < n-i-1; i++ {
			pal = pal && s[i] == s[n-i-1]
		}
		ans := 1
		if !pal && k >= 1 {
			ans = 2
		}
		printf("%d\n", ans)
	}
}

package main

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

func isPalindrome(s string) bool {
	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		if s[i] != s[j] {
			return false
		}
	}
	return true
}

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

	for ; t > 0; t-- {
		var n, k int
		var s string
		fmt.Fscan(in, &n, &k, &s)

		isDiff := false
		for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
			if s[i] != s[j] {
				isDiff = true
			}
		}

		if isDiff {
			if k == 0 {
				fmt.Println(1)
			} else {
				fmt.Println(2)
			}
		} else {
			fmt.Println(1)
		}
	}
}

package main

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

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

func check(s string) bool {
	n := len(s)
	for i := 0; i < n; i++ {
		if s[i] != s[n-1-i] {
			return false
		}
	}
	return true
}

func main() {
	defer out.Flush()
	var t, n, k int
	var s string
	for fmt.Fscan(in, &t); t > 0; t-- {
		fmt.Fscan(in, &n, &k, &s)
		if check(s) || k == 0 {
			fmt.Fprintln(out, 1)
		} else {
			fmt.Fprintln(out, 2)
		}
	}
}

package main

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

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

	for ii := 0; ii < t; ii++ {
		var n, k int
		fmt.Fscan(in, &n, &k)
		var s string
		fmt.Fscan(in, &s)
		rev := true
		for i := 0; i < n/2; i++ {
			if s[i] != s[n-i-1] {
				rev = false
				break
			}
		}
		if rev || k == 0 {
			fmt.Fprintln(out, 1)
		} else {
			fmt.Fprintln(out, 2)
		}
	}
}

// 1 0 1 2

//nolint
package main

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

//{
var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
func print(a ...interface{})            { fmt.Fprint(writer, a...) }
func println(a ...interface{})          { fmt.Fprintln(writer, a...) }
func scan(a ...interface{})             { fmt.Fscan(reader, a...) }

func main() {
	defer writer.Flush()

	Main()
}

func Main() {
	var t int
	scan(&t)
	for ; t > 0; t-- {
		Solve()
	}
}

//}

func Solve() {
	var n, k int
	scan(&n, &k)
	var st string
	scan(&st)
	if !isPalindrome(st) && k > 0 {
		println(2)
	} else {
		println(1)
	}
}

func isPalindrome(st string) bool {
	for i := 0; len(st)-i-1 > i; i++ {
		if st[i] != st[len(st)-i-1] {
			return false
		}
	}
	return true
}

package main

import "fmt"

/*
In the first test case of the example:
Input
4
3 2
aab
3 3
aab
7 1
abacaba
2 0
ab

Output :
2
2
1
1
After the first operation the string s can become either aabbaa or baaaab.
After the second operation there are 2 possibilities for s: aabbaaaabbaa and baaaabbaaaab.
*/
/*
s = aab
rev = baa

pre = aabbaa
post = baaaab
*/

type testCase struct {
	length int
	layer  int
	str    string
}

func reverse(str string) (rev string) {
	l := len(str)
	for i := 0; i < l; i++ {
		rev = rev + str[l-i-1:l-i]
	}
	return rev
}

func possibilities(layer int, str, rev string) (count int) {
	if layer == 0 {
		return 1
	}

	pre := str + reverse(str)
	post := reverse(str) + str

	if pre == post {
		return 1
	}

	return 2
}

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

	tests := []testCase{}
	for i := 0; i < n; i++ {
		test := testCase{}
		fmt.Scanf("%d %d\n", &test.length, &test.layer)
		fmt.Scanf("%s\n", &test.str)
		tests = append(tests, test)
	}

	for j := 0; j < n; j++ {
		str := tests[j].str
		layer := tests[j].layer
		rev := reverse(str)
		fmt.Println(possibilities(layer, str, rev))
	}
}

package main
import "fmt"

func main(){
    var t, n, k int
    var s string
    fmt.Scanln(&t)
    for t > 0 {
        fmt.Scanf("%d %d\n", &n, &k)
        //fmt.Println(n, k)
        fmt.Scanln(&s)
        ok := true
        for i := 0; i < n/2; i++ {
            ok = ok && s[i] == s[n - i - 1]
        }
        if k == 0 || ok {
            fmt.Println(1)
        } else {
            fmt.Println(2)
        }
        t--
    }
}

package main

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

func isMirrored(str string) bool {
	half := len(str) / 2
	for i := 0; i < half; i++ {
		if str[i] != str[len(str)-1-i] {
			return false
		}
	}
	return true
}

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

	t, _ := reader.ReadString('\r')

	tCount, _ := strconv.Atoi(t[:len(t)-1])

	for i := 0; i < tCount; i++ {
		nAndKStr, _ := reader.ReadString('\r')
		nAndK := strings.Split(nAndKStr, " ")
		k, _ := strconv.Atoi(nAndK[1][:len(nAndK[1])-1])

		input, _ := reader.ReadString('\r')

		if k > 1 && !isMirrored(input[1:len(input)-1]) {
			fmt.Println(2)
		} else {
			fmt.Println(1)
		}
	}
}

func main() {
	resultsCount()
}

package main

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

func isMirrored(str string) bool {
	half := len(str) / 2
	for i := 0; i < half; i++ {
		if str[i] != str[len(str)-1-i] {
			return false
		}
	}
	return true
}

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

	t, _ := reader.ReadString('\r')

	tCount, _ := strconv.Atoi(t[:len(t)-1])

	for i := 0; i < tCount; i++ {
		nAndKStr, _ := reader.ReadString('\r')
		nAndK := strings.Split(nAndKStr, " ")
		k, _ := strconv.Atoi(nAndK[1][:len(nAndK[1])-1])

		input, _ := reader.ReadString('\r')

		if k > 1 && !isMirrored(input[:len(input)-1]) {
			fmt.Println(2)
		} else {
			fmt.Println(1)
		}
	}
}

func main() {
	resultsCount()
}

package main

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

func isMirrored(str string) bool {
	half := len(str) / 2
	for i := 0; i < half; i++ {
		if str[i] != str[len(str)-1-i] {
			return false
		}
	}
	return true
}

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

	t, _ := reader.ReadString('\n')

	tCount, _ := strconv.Atoi(t[:len(t)-1])

	for i := 0; i < tCount; i++ {
		nAndKStr, _ := reader.ReadString('\n')
		nAndK := strings.Split(nAndKStr, " ")
		k, _ := strconv.Atoi(nAndK[1][:len(nAndK[1])-1])

		input, _ := reader.ReadString('\n')

		if k > 1 && !isMirrored(input[:len(input)-1]) {
			println(2)
		} else {
			println(1)
		}
	}
}

func main() {
	resultsCount()
}

package main

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

func isMirrored(str string) bool {
	half := len(str) / 2
	for i := 0; i < half; i++ {
		if str[i] != str[len(str)-1-i] {
			return false
		}
	}
	return true
}

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

	t, _ := reader.ReadString('\n')

	tCount, _ := strconv.Atoi(t[:len(t)-1])

	for i := 0; i < tCount; i++ {
		nAndKStr, _ := reader.ReadString('\n')
		nAndK := strings.Split(nAndKStr, " ")
		k, _ := strconv.Atoi(nAndK[1][:len(nAndK[1])-1])

		input, _ := reader.ReadString('\n')

		if k > 1 && !isMirrored(input[:len(input)-1]) {
			fmt.Println(2)
		} else {
			fmt.Println(1)
		}
	}
}

func main() {
	resultsCount()
}

package main

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

func isMirrored(str string) bool {
	half := len(str) / 2
	for i := 0; i < half; i++ {
		if str[i] != str[len(str)-1-i] {
			return false
		}
	}
	return true
}

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

	t, _ := reader.ReadString('\n')

	tCount, _ := strconv.Atoi(t[:len(t)-1])

	for i := 0; i < tCount; i++ {
		nAndKStr, _ := reader.ReadString('\n')
		nAndK := strings.Split(nAndKStr, " ")
		k, _ := strconv.Atoi(nAndK[1][:len(nAndK[1])-1])

		input, _ := reader.ReadString('\n')

		if k > 1 && !isMirrored(input) {
			fmt.Println(2)
		} else {
			fmt.Println(1)
		}
	}
}

func main() {
	resultsCount()
}

package main
import(
    "fmt"
    "bufio"
    "os"
    "strings"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var x,y int
		fmt.Fscan(in,&x,&y)
		var word string
		fmt.Fscan(in,&word)
		if y == 0 || y == 1 || x == 1 {
			fmt.Fprintln(out,1)
		} else {
			list:= strings.Split(word,"")
			for j,k:=0,len(list)-1;j<k;j,k=j+1,k-1{
				list[j],list[k] = list[k],list[j]
			}
			word2:= strings.Join(list,"")
			if word == word2 {
				fmt.Fprintln(out,1)
			} else {
				fmt.Fprintln(out,2)
			}
		}
	}
}

package main
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var x,y int
		fmt.Fscan(in,&x,&y)
		var word string
		fmt.Fscan(in,&word)
		if y == 0 || y == 1 || x == 1 {
			fmt.Fprintln(out,1)
		} else {
			fmt.Fprintln(out,2)
		}
	}
}

package main
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var x,y int
		fmt.Fscan(in,&x,&y)
		var word string
		fmt.Fscan(in,&word)
		if y == 0 || y == 1 {
			fmt.Fprintln(out,1)
		} else {
			fmt.Fprintln(out,2)
		}
	}
}

package main

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

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

func scan(a ...interface{}) {
	fmt.Fscan(in, a...)
}
func printf(f string, a ...interface{}) {
	fmt.Fprintf(out, f, a...)
}
func Reverse(s string) string {
	runes := []rune(s)
	for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
		runes[i], runes[j] = runes[j], runes[i]
	}
	return string(runes)
}
func solve() {
	var n, k int
	scan(&n, &k)
	var s string
	scan(&s)
	if k > 1 {
		t := Reverse(s)
		s1 := t + s
		s2 := s + t
		if s1 != s2 {
			printf("2\n")
			return
		}
	}
	printf("1\n")

}
func main() {
	defer out.Flush()
	n := 0
	scan(&n)
	for i := 0; i < n; i++ {
		solve()
	}
}

package main

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

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

func scan(a ...interface{}) {
	fmt.Fscan(in, a...)
}
func printf(f string, a ...interface{}) {
	fmt.Fprintf(out, f, a...)
}

func solve() {
	var n, k int
	scan(&n, &k)
	var s string
	scan(&s)
	if k > 1 {
		printf("2\n")
	} else {
		printf("1\n")
	}
}
func main() {
	defer out.Flush()
	n := 0
	scan(&n)
	for i := 0; i < n; i++ {
		solve()
	}
}

package main

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

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

func (in *CF1634A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1634A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1634A
Date: 9/03/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1634/A
Problem: CF1634A
**/
func (in *CF1634A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		_, k := in.NextInt(), in.NextInt()
		str := in.NextString()

		mapa := make(map[uint8]int)
		for i := 0; i < len(str); i++ {
			mapa[str[i]]++
		}

		cant := len(mapa)
		ans := 1
		if k > 1 && cant > 1 {
			ans = 2
		}

		fmt.Println(ans)
	}

}

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

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

package main

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

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

func (in *CF1634A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1634A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1634A
Date: 19/02/22
User: epradere
URL: https://codeforces.com/problemset/problem/1634/A
Problem: CF1634A
**/
func (in *CF1634A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, k := in.NextInt(), in.NextInt()
		str := in.NextString()

		mapa := make(map[uint8]int)
		for i := 0; i < n; i++ {
			val := mapa[str[i]]
			mapa[str[i]] = val + 1
		}
		min := len(mapa)

		if min > k {
			min = k
		}
		if min == 0 {
			min = 1
		}

		fmt.Println(min)
	}
}

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

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

package main

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

var in = bufio.NewReader(os.Stdin)

func main() {
	t := readInt(in)
	for ; t != 0; t-- {
		solve()
	}
}

func solve() {
	input := readArrInt(in)
	n, k := input[0], input[1]
	s, _ := in.ReadString('\n')
	s = strings.Trim(s, " \n\r")
	flag := true
	for i := 0; i < n; i++ {
		if s[i] != s[n-1-i] {
			flag = false
		}
	}
	if k == 0 {
		fmt.Println(0)
	} else if flag {
		fmt.Println(1)
	} else {
		fmt.Println(2)
	}
}

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

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

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

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

package main

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

var in = bufio.NewReader(os.Stdin)

func main() {
	t := readInt(in)
	for ; t != 0; t-- {
		solve()
	}
}

func solve() {
	n := readInt(in)
	k := readInt(in)
	s, _ := in.ReadString('\n')
	s = strings.Trim(s, " \n\r")
	flag := true
	for i := 0; i < n; i++ {
		if s[i] != s[n-1-i] {
			flag = false
		}
	}
	if k == 0 {
		fmt.Println(0)
	} else if flag {
		fmt.Println(1)
	} else {
		fmt.Println(2)
	}
}

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

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

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

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

package main

import (
	"fmt"
)

func test() {
	var n, k int
	var x string
	fmt.Scan(&n, &k, &x)
	if k <= 1 {
		fmt.Println(1)
		return
	}
	for i, j := 0, len(x)-1; i < j; i, j = i+1, j-1 {
		if x[i] != x[j] {
			fmt.Println(2)
			return
		}
	}
	fmt.Println(1)
}
func main() {
	var no int
	fmt.Scan(&no)
	for i := 0; i < no; i++ {
		test()
	}
}

package main

import (
	"fmt"
)

func main() {
	var q int
	fmt.Scan(&q)
	for i := 0; i < q; i++ {
		var n, k int
		var s string
		fmt.Scan(&n, &k)
		fmt.Scan(&s)

		if same(s) || k < 2 {
			fmt.Println(1)
		} else {
			fmt.Println(2)
		}
	}
}

func same(s string) bool {
	for i := 0; i < len(s); i++ {
		if s[i] != s[len(s)-i-1] {
			return false
		}
	}
	return true
}

package main

import (
	"fmt"
)

func main() {
	var q int
	fmt.Scan(&q)
	for i := 0; i < q; i++ {
		var n, k int
		var s string
		fmt.Scan(&n, &k)
		fmt.Scan(&s)

		if same(s) || k < 2 {
			fmt.Println(1)
		} else {
			fmt.Println(2)
		}
	}
}

func same(s string) bool {
	for i := 1; i < len(s); i++ {
		if s[i] != s[0] {
			return false
		}
	}
	return true
}

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 main() {
	defer flush()

	var ntc int
	scan(&ntc)
	for t := 0; t < ntc; t++ {
		var n int
		var x, y, ai int64
		scan(&n, &x, &y)
		x %= 2
		y %= 2
		for i := 0; i < n; i++ {
			scan(&ai)
			if ai%2 == 1 {
				x ^= 1
			}
		}
		if x == y {
			printf("Alice\n")
		} else {
			printf("Bob\n")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"unicode/utf8"
)

func isPal(s string) bool {
	for len(s) > 0 {
		if len(s) == 1 {
			return true
		}
		r1, sz1 := utf8.DecodeRuneInString(s)
		r2, sz2 := utf8.DecodeLastRuneInString(s)
		if r1 != r2 {
			return false
		}
		s = s[sz1 : len(s)-sz2]
	}
	return true
}

func main() {
	in := bufio.NewReader(os.Stdin)
	var tests int
	fmt.Fscan(in, &tests)
	for i := 0; i < tests; i++ {
		var n, k int
		var s string
		fmt.Fscan(in, &n, &k, &s)
		if k < 2 || isPal(s) {
			fmt.Println(1)
		} else {
			fmt.Println(2)
		}
	}
}

package main

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

func isPalindrome(s string) bool {
	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		if s[i] != s[j] {
			return false
		}
	}
	return true
}

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

	for ; t > 0; t-- {
		var n, k int
		var s string
		fmt.Fscan(in, &n, &k, &s)

		isDiff := false
		for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
			if s[i] != s[j] {
				isDiff = true
			}
		}

		if isDiff {
			fmt.Println(2)

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


package main

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

func main() {

	var read = bufio.NewReader(os.Stdin)
	var write = bufio.NewWriter(os.Stdout)
	defer write.Flush()
	var n int
	fmt.Fscan(read, &n)
	mink := 1000000000
	for i := 0; i < n; i++ {
		var x int
		fmt.Fscan(read, &x)
		d := i
		if (n - 1 - i) > d {
			d = n - 1 - i
		}
		k := x / d
		if k < mink {
			mink = k
		}
	}
	fmt.Fprint(write, mink)

}

package main

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

var DEBUG = true

func main() {
	defer Flush()

	N, A := readInts(0)

	k := 1001001001

	for i, a := range A {
		if 0 < i {
			k1 := a/i + 1
			k = min(k, k1)
			for a < k*i {
				k--
			}
		}
		if i < N-1 {
			d := N - 1 - i
			k1 := a/d + 1
			k = min(k, k1)
			for a < k*d {
				k--
			}
		}
	}
	println(k)
}

// -----------------------------------------------------------------------------

// IO helper functions

// Returns next token from input.  It must be initialized by SetInput()
// before the first call.
var nextToken func() ([]byte, error)
var nextLine func() ([]byte, error)

// Holds a buffer for output.  It must be initialized by SetOutput().
// All IO fucntions (read*() and [e]print*()) should write to OutputWriter
// instead of this.
var OutputBuffer *bufio.Writer

// Holds an io.Writer.  It must be initialized by SetOutput()
var OutputWriter io.Writer

// Set IO functions for interactive input/output.
func SetInteractive(w io.Writer, r io.Reader) {
	SetUnbefferedInput(r)
	OutputBuffer = nil
	OutputWriter = w
}

// Setup OutputBuffer and OutputWriter.
func SetOutput(w io.Writer) {
	OutputBuffer = bufio.NewWriter(w)
	OutputWriter = OutputBuffer
}

// Flushes OutputBuffer
func Flush() {
	if OutputBuffer != nil {
		OutputBuffer.Flush()
	}
}

// Returns true if c is a white space
func IsSpace(c byte) bool {
	switch c {
	case '\t', '\n', '\v', '\f', '\r', ' ':
		return true
	}
	return false
}

func IsNewLine(c byte) bool {
	switch c {
	case '\n', '\r':
		return true
	}
	return false
}

// Setup nextToken with input buffer.
func SetInput(r io.Reader) {
	buf := new(bytes.Buffer)
	var b []byte

	var i int
	rest := func() ([]byte, error) {
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		return b[j:i], nil
	}
	initial := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextToken = rest
		return rest()
	}
	nextToken = initial

	restLn := func() ([]byte, error) {
		for i < len(b) && IsNewLine(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsNewLine(b[i]) {
			i++
		}
		return b[j:i], nil
	}

	initialLn := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextLine = restLn
		return restLn()
	}
	nextLine = initialLn
}

// Setup nextToken without input buffer.
func SetUnbefferedInput(r io.Reader) {
	buf := bufio.NewReader(r)
	var b []byte

	var i int
	nextToken = func() ([]byte, error) {
		var err error
		if i == len(b) {
			b, err = buf.ReadBytes('\n')
			if err != nil {
				return nil, err
			}
			i = 0
			j := len(b) - 1
			for 0 <= j && IsSpace(b[j]) {
				j--
			}
			b = b[0 : j+1]
		}
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		if i == j {
			return nil, io.ErrUnexpectedEOF
		}
		return b[j:i], nil
	}
}

// -----------------------------------------------------------------------------

// IO functions

// Reads next token and return it as []byte
func readb() []byte {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Reads next token and return it as string
func reads() string {
	return string(readb())
}

// Read next line as []byte.  Trailing '\n' will not be included.
// See also comments on readb()
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Read next line as string
func readsln() string {
	return string(readbln())
}

// Reads next token and return it as int64
func readll() int64 {
	i, err := strconv.ParseInt(reads(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// Reads next token and return it as int
func readi() int {
	return int(readll())
}

// Reads next token and return it as float64
func readf() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// Write args to OutputWriter with the format f
func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(OutputWriter, f, args...)
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}

// Write args to stderr with the format f
func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

// Write args to stderr without format
func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func dbgf(f string, args ...interface{}) {
	if !DEBUG {
		return
	}
	fmt.Fprintf(os.Stderr, f, args...)
}

func dbg(args ...interface{}) {
	if !DEBUG {
		return
	}
	fmt.Fprintln(os.Stderr, args...)
}

// -----------------------------------------------------------------------------

// Utilities

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

func sumSlicell(a []int64) int64 {
	var res int64
	for _, v := range a {
		res += v
	}
	return res
}

func readInts(N int) (int, []int) {
	if N == 0 {
		N = readi()
	}
	a := make([]int, N)
	for i := range a {
		a[i] = readi()
	}
	return N, a
}

func readIntsll(N int) (int, []int64) {
	if N == 0 {
		N = readi()
	}
	a := make([]int64, N)
	for i := range a {
		a[i] = readll()
	}
	return N, a
}

// -----------------------------------------------------------------------------

// Simple math functions

const (
	// big prime
	INF  = 1000000007
	INF2 = 1000000009
	INF3 = 998244353
)

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

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

func minf(a, b float64) float64 {
	if a < b {
		return a
	}
	return b
}

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

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

func maxf(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

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

func absll(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func absf(a float64) float64 {
	if a < 0 {
		return -a
	}
	return a
}

// egcd(a, b) returns d, x, y:
//   d is gcd(a,b)
//   x, y are  integers that satisfy ax + by = d
func egcd(a, b int) (int, int, int) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcd(b, a%b)
	return d, y, x - a/b*y
}

func egcdll(a, b int64) (int64, int64, int64) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcdll(b, a%b)
	return d, y, x - a/b*y
}

func gcd(a, b int) int {
	d, _, _ := egcd(a, b)
	return d
}

func gcdll(a, b int64) int64 {
	d, _, _ := egcdll(a, b)
	return d
}

// set up IO functions
func init() {
	// for non-interactive
	SetInput(os.Stdin)
	SetOutput(os.Stdout)

	// Enable below when interactive.  Its ok to leave above intact.
	// SetInteractive(os.Stdout, os.Stdin)
}

package main

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

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

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

func Solve(n int, a []int) int {
	k := int(math.Pow10(9))
	for i, elem := range a {
		d := max(i, n-i-1)
		k = min(k, elem/d)
	}
	return k
}

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

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}
	fmt.Fprintf(writer, "%d\n", Solve(n, a))
}

package main

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

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

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

	scanner.Scan()
	n, err := strconv.Atoi(scanner.Text())

	if err != nil {
		fmt.Println("err getting n ", err)
	}

	arr := make([]int, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		arr[i], _ = strconv.Atoi(scanner.Text())
	}

	minSeen := math.MaxInt32
	for i := 0; i < len(arr); i++ {
		if i != 0 {
			minSeen = min(minSeen, min(arr[0], arr[i])/i)
		}
		if i != len(arr)-1 {
			minSeen = min(minSeen, min(arr[i], arr[len(arr)-1])/(len(arr)-1-i))
		}
	}

	fmt.Println(minSeen)
}

package main

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

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

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

	if err != nil {
		fmt.Println("err getting n ", err)
	}

	arr := make([]int, n)
	index := 0
	for {
		more := scanner.Scan()
		if !more {
			break
		}
		arrStr := strings.Fields(scanner.Text())
		for i := 0; i < len(arrStr); i++ {
			temp, err := strconv.Atoi(arrStr[i])
			if err != nil {
				fmt.Println(err)
			}
			arr[index] = temp
			index++
		}
	}

	if index != n {
		fmt.Println("index did not finish,", index)
	}

	minSeen := math.MaxInt32
	for i := 0; i < len(arr); i++ {
		if i != 0 {
			minSeen = min(minSeen, min(arr[0], arr[i])/i)
		}
		if i != len(arr)-1 {
			minSeen = min(minSeen, min(arr[i], arr[len(arr)-1])/(len(arr)-1-i))
		}
	}

	fmt.Println(minSeen)
}

package main

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

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

func readInt(reader *bufio.Reader) int {
	str, _ := reader.ReadString('\n')
	i, _ := strconv.Atoi(str[:len(str)-1])
	return i
}

func readline(reader *bufio.Reader) string {
	str, _ := reader.ReadString('\n')
	return str[:len(str)-1]
}

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

	arr := make([]int, n)
	scanner.Scan()
	arrStr := strings.Split(scanner.Text(), " ")
	for i := 0; i < n; i++ {
		arr[i], _ = strconv.Atoi(arrStr[i])
	}
	fmt.Println("input = ", n, arr)

	minSeen := math.MaxInt32
	for i := 0; i < len(arr); i++ {
		if i != 0 {
			minSeen = min(minSeen, min(arr[0], arr[i])/i)
		}
		if i != len(arr)-1 {
			minSeen = min(minSeen, min(arr[i], arr[len(arr)-1])/(len(arr)-1-i))
		}
	}

	fmt.Println(minSeen)
}

package main

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

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

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

	if err != nil {
		fmt.Println("err getting n ", err)
	}

	arr := make([]int, n)
	index := 0
	for {
		more := scanner.Scan()
		if !more {
			break
		}
		arrStr := strings.Fields(scanner.Text())
		for i := 0; i < len(arrStr); i++ {
			temp, err := strconv.Atoi(arrStr[i])
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
			}
			arr[index] = temp
			index++
		}
	}

	if index != n {
		fmt.Println("index did not finish,", index)
	}

	minSeen := math.MaxInt32
	for i := 0; i < len(arr); i++ {
		if i != 0 {
			minSeen = min(minSeen, min(arr[0], arr[i])/i)
		}
		if i != len(arr)-1 {
			minSeen = min(minSeen, min(arr[i], arr[len(arr)-1])/(len(arr)-1-i))
		}
	}

	fmt.Println(minSeen)
}

package main

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

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

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

	if err != nil {
		fmt.Println("err getting n ", err)
	}

	arr := make([]int, n)
	index := 0
	for {
		more := scanner.Scan()
		if !more {
			break
		}
		arrStr := strings.Fields(scanner.Text())
		for i := 0; i < len(arrStr); i++ {
			temp, err := strconv.Atoi(arrStr[i])
			if err != nil {
				fmt.Println(err)
			}
			arr[index] = temp
			index++
		}
	}

	minSeen := math.MaxInt32
	for i := 0; i < len(arr); i++ {
		if i != 0 {
			minSeen = min(minSeen, min(arr[0], arr[i])/i)
		}
		if i != len(arr)-1 {
			minSeen = min(minSeen, min(arr[i], arr[len(arr)-1])/(len(arr)-1-i))
		}
	}

	fmt.Println(minSeen)
}


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 = 1
	// fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		var n, m int
		fmt.Fscan(r, &n, &m)
		a := make([][3]int, m)
		for i := range a {
			fmt.Fscan(r, &a[i][0], &a[i][1], &a[i][2])
		}
		w1 := do(n, m, a)
		for _, v := range w1 {
			fmt.Fprint(w, v, " ")
		}
		// fmt.Fprintln(w)
	}
}

const mx = 30

func do(n, m int, a [][3]int) []int {
	r := make([]int, n)
	for i := range r {
		r[i] = 1<<mx - 1
	}
	for _, a := range a {
		i, j, x := a[0]-1, a[1]-1, a[2]
		r[i] &= x
		r[j] &= x
	}
	t := make(map[int][]int)
	for _, a := range a {
		i, j := a[0]-1, a[1]-1
		t[i] = append(t[i], j)
		t[j] = append(t[j], i)
	}
	for i := range r {
		if len(t[i]) == 0 {
			r[i] = 0
			continue
		}
		s := r[t[i][0]]
		for k := 1; k < len(t[i]); k++ {
			j := t[i][k]
			if i == j {
				s = 0
				break
			}
			s &= r[j]
		}
		r[i] ^= s
	}
	return r
}

package main

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

const MOD int64 = 1000000007

var n, q int

var a [][3]int

var graph [][][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)

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

	a = make([][3]int, q)

	graph = make([][][2]int, n)

	for j := 0; j < q; j++ {
		fmt.Fscan(read, &a[j][0], &a[j][1], &a[j][2])
		a[j][0]--
		a[j][1]--

		if a[j][0] == a[j][1] {
			graph[a[j][0]] = append(graph[a[j][0]], [2]int{a[j][1], a[j][2]})
		} else {
			graph[a[j][0]] = append(graph[a[j][0]], [2]int{a[j][1], a[j][2]})
			graph[a[j][1]] = append(graph[a[j][1]], [2]int{a[j][0], a[j][2]})
		}

	}

	solve()
	fmt.Println()

}

type pair struct {
	v int
	idx []int
}

func solve()  {

	ans := make([]int, n)

	for b := 30; b>= 0; b-- {

		mp := make(map[int]bool)
		seen := make([]bool, n)

		for i := 0; i < q; i++ {
			if (a[i][2] >> uint(b)) & 0x1 == 0 {
				mp[a[i][0]] = true
				mp[a[i][1]] = true

				seen[a[i][0]] = true
				seen[a[i][1]] = true
			} else {
				if a[i][0] == a[i][1] {

					ans[a[i][0]] = ans[a[i][0]] | (1 << uint(b))

					seen[a[i][0]] = true

				}
			}
		}

		for k,_ := range mp {

			for j := 0; j < len(graph[k]); j++ {
				idx := graph[k][j][0]
				if  seen[idx] {
					continue
				}
				seen[idx] = true

				ans[idx] = ans[idx] | (1 << uint(b))
			}
		}

		for i := 0; i < n; i++ {
			if seen[i] {
				continue
			}
			seen[i] = true

			ed := graph[i]

			for j := 0; j < len(ed); j++ {
				e := ed[j]

				if seen[e[0]] {
					continue
				}

				seen[e[0]] = true

				idx := e[0]

				ans[idx] = ans[idx] | (1 << uint(b))
			}

		}
	}

	for j := 0; j < n; j++ {
		fmt.Print(ans[j], " ")
	}
}

//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 [][3]int

var graph [][][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)

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

	a = make([][3]int, q)

	graph = make([][][2]int, n)

	for j := 0; j < q; j++ {
		fmt.Fscan(read, &a[j][0], &a[j][1], &a[j][2])
		a[j][0]--
		a[j][1]--

		graph[a[j][0]] = append(graph[a[j][0]], [2]int{a[j][1], a[j][2]})
		graph[a[j][1]] = append(graph[a[j][1]], [2]int{a[j][0], a[j][2]})

	}

	solve()
	fmt.Println()

}

type pair struct {
	v int
	idx []int
}

func solve()  {

	ans := make([]int, n)

	for b := 30; b>= 0; b-- {

		mp := make(map[int]bool)
		seen := make([]bool, n)

		for i := 0; i < q; i++ {
			if (a[i][2] >> uint(b)) & 0x1 == 0 {
				mp[a[i][0]] = true
				mp[a[i][1]] = true

				seen[a[i][0]] = true
				seen[a[i][1]] = true
			}
		}

		for k,_ := range mp {

			for j := 0; j < len(graph[k]); j++ {
				idx := graph[k][j][0]
				if  seen[idx] {
					continue
				}
				seen[idx] = true

				ans[idx] = ans[idx] | (1 << uint(b))
			}
		}

		for i := 0; i < n; i++ {
			if seen[i] {
				continue
			}
			seen[i] = true

			ed := graph[i]

			for j := 0; j < len(ed); j++ {
				e := ed[j]

				if seen[e[0]] && e[0] != i {
					continue
				}

				seen[e[0]] = true

				idx := e[0]

				ans[idx] = ans[idx] | (1 << uint(b))
			}

		}
	}

	for j := 0; j < n; j++ {
		fmt.Print(ans[j], " ")
	}
}

//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 [][3]int

var graph [][][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)

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

	a = make([][3]int, q)

	graph = make([][][2]int, n)

	for j := 0; j < q; j++ {
		fmt.Fscan(read, &a[j][0], &a[j][1], &a[j][2])
		a[j][0]--
		a[j][1]--

		if a[j][0] < a[j][1] {
			graph[a[j][0]] = append(graph[a[j][0]], [2]int{a[j][1], a[j][2]})
		} else {
			graph[a[j][1]] = append(graph[a[j][1]], [2]int{a[j][0], a[j][2]})
		}
	}

	solve()
	fmt.Println()

}

type pair struct {
	v int
	idx []int
}

func solve()  {

	ans := make([]int, n)

	for b := 30; b>= 0; b-- {

		mp := make(map[int]bool)
		seen := make([]bool, n)

		for i := 0; i < q; i++ {
			if (a[i][2] >> uint(b)) & 0x1 == 0 {
				mp[a[i][0]] = true
				mp[a[i][1]] = true

				seen[a[i][0]] = true
				seen[a[i][1]] = true
			}
		}

		for k,_ := range mp {

			for j := 0; j < len(graph[k]); j++ {
				idx := graph[k][j][0]
				if  seen[idx] {
					continue
				}
				seen[idx] = true

				ans[idx] = ans[idx] | (1 << uint(b))
			}
		}

		for i := 0; i < n; i++ {
			if seen[i] {
				continue
			}
			seen[i] = true

			ed := graph[i]

			for j := 0; j < len(ed); j++ {
				e := ed[j]

				if seen[e[0]] && e[0] != i {
					continue
				}

				seen[e[0]] = true

				idx := e[0]

				ans[idx] = ans[idx] | (1 << uint(b))
			}

		}
	}

	for j := 0; j < n; j++ {
		fmt.Print(ans[j], " ")
	}
}

//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 [][3]int

var graph [][][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)

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

	a = make([][3]int, q)

	graph = make([][][2]int, n)

	for j := 0; j < q; j++ {
		fmt.Fscan(read, &a[j][0], &a[j][1], &a[j][2])
		a[j][0]--
		a[j][1]--

		if a[j][0] < a[j][1] {
			graph[a[j][0]] = append(graph[a[j][0]], [2]int{a[j][1], a[j][2]})
		} else {
			graph[a[j][1]] = append(graph[a[j][1]], [2]int{a[j][0], a[j][2]})
		}
	}

	solve()
	fmt.Println()

}

type pair struct {
	v int
	idx []int
}

func solve()  {

	ans := make([]int, n)

	for b := 30; b>= 0; b-- {

		mp := make(map[int]bool)
		seen := make([]bool, n)

		for i := 0; i < q; i++ {
			if (a[i][2] >> uint(b)) & 0x1 == 0 {
				mp[a[i][0]] = true
				mp[a[i][1]] = true

				seen[a[i][0]] = true
				seen[a[i][1]] = true
			}
		}

		for k,_ := range mp {

			for j := 0; j < len(graph[k]); j++ {
				idx := graph[k][j][0]
				if  seen[idx] {
					continue
				}
				seen[idx] = true

				ans[idx] = ans[idx] | (1 << uint(b))
			}
		}

		for i := 0; i < n; i++ {
			if seen[i] {
				continue
			}
			seen[i] = true

			ed := graph[i]

			for j := 0; j < len(ed); j++ {
				e := ed[j]

				if seen[e[0]] && e[0] != i {
					continue
				}

				idx := e[0]

				ans[idx] = ans[idx] | (1 << uint(b))
			}

		}
	}

	for j := 0; j < n; j++ {
		fmt.Print(ans[j], " ")
	}
}

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

const cf = true

func solve() {
	var n, k int
	fmt.Fscan(in, &n, &k)
	arr := make([]int, n)
	for i := range arr {
		fmt.Fscan(in, &arr[i])
	}
	ans := 3000
	for p := 0; p <= 3000; p++ {
		maxP := p
		good := true
		for i := range arr {
			pi := arr[i] + 1
			if p != 0 {
				pi = arr[i] / p
			}
			if pi == 0 {
				good = false
				break
			}
			pi = min(pi, k)
			maxP = max(maxP, arr[i]/pi)
			if pi > k {
				good = false
				break
			}
		}
		if !good {
			continue
		}
		ans = min(ans, maxP-p)
	}
	fmt.Fprintln(out, ans)
}

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

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

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

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

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

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

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

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

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

var exists = struct{}{}

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

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

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

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

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

type Ordered interface {
	Integer | Float | ~string
}

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

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

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

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

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

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

package main

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

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

	n := readInt(in)
	for i := 0; i < n; i++ {
		solution(in, out)
	}
}

func solution(in *bufio.Reader, out *bufio.Writer) {
	v := readArrInt(in)
	k := v[1]
	arr := readArrInt(in)

	best := math.MaxInt
	for i := 0; i <= 3000; i++ {
		tt := highestLowOffK(arr, k, i)
		if tt >= 0 {
			best = Min(best, i-tt)
			if best == 0 {
				break
			}
		}
	}

	out.WriteString(strconv.Itoa(best) + "\n")

}

func highestLowOffK(arr []int, k int, highest int) int {
	tops := math.MinInt
	low := 0
	high := highest

	for low <= high {
		mid := (low + high) / 2
		if canMakeAllBetween(arr, k, mid, highest) {
			tops = mid
			low = mid + 1
		} else {
			high = mid - 1
		}
	}
	return tops
}

func canMakeAllBetween(arr []int, k int, low int, high int) bool {
	for _, val := range arr {
		if !canMakeOneBetween(val, 1, k, low, high) {
			return false
		}
	}
	return true
}

func canMakeOneBetween(val int, low int, high int, lowtarget int, hightarget int) bool {
	for {
		if val/high > hightarget {
			return false
		}

		if val/low < lowtarget {
			return false
		}

		mid := (low + high) / 2
		if val/mid > hightarget {
			low = mid + 1
		} else if val/mid < lowtarget {
			high = mid - 1
		} else {
			return true
		}
	}
}

func Max(sum int, sum2 int) int {
	if sum > sum2 {
		return sum
	}
	return sum2

}

func Min(sum int, sum2 int) int {
	if sum < sum2 {
		return sum
	}
	return sum2
}

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

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

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

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

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

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

package main

import (
	"bufio"
	"fmt"
	"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, k int
		fmt.Fscan(r, &n, &k)
		a := make([]int, n)
		for i := range a {
			fmt.Fscan(r, &a[i])
		}
		w1 := do(n, k, a)
		fmt.Fprintln(w, w1)
	}
}

func do(n, k int, a []int) int {
	ans := -1
	for v := 0; v <= a[0]; v++ {
		m1 := -1
		m2 := -1
		for _, va := range a {
			k2 := k
			if v > 0 && va/v < k {
				k2 = va / v
			}
			m := va / k2
			if m1 < 0 || m1 < m {
				m1 = m
			}
			if m2 < 0 || m2 > m {
				m2 = m
			}
		}
		if ans < 0 || ans > m1-m2 {
			ans = m1 - m2
		}
	}
	return ans
}

package main

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

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

	var buf bytes.Buffer

	tc := readNum(reader)

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

	fmt.Print(buf.String())
}

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

func normalize(s string) string {

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

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

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

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

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

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

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

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

	return i
}

func solve(A []int, k int) int {
	n := len(A)
	if n == 1 {
		return 0
	}
	ans := 1 << 30
	for v := 0; v <= A[0]; v++ {
		// if min value is v
		var tmp int
		for i := 0; i < n; i++ {
			p := k
			if v > 0 {
				p = min(k, A[i]/v)
			}
			tmp = max(A[i]/p-v, tmp)
		}
		ans = min(ans, tmp)
	}

	return ans
}

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

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"
	"os"
)
func max(x,y int)int{
	if x>y{
		return x
	}
	return y
}
func min(x,y int)int{
	if x<y{
		return x
	}
	return y
}
func abs(x int)int{
	if x<0{
		return -x
	}
	return x
}
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 int
		fmt.Fscan(in,&n, &k)
		a := make([]int, n+5)
		mi := 989999
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&a[i])
			mi = min(mi,a[i])
		}
		ret :=100000009
		for i:=0;i<=mi;i++{
			check := false
			ma := -1
			for j:=1;j<=n;j++{
				l := 1
				r := k
				ans := -1
				for l<=r{
					mid := (l+r)>>1
					if a[j]/mid >= i{
						ans = mid
						l = mid+1
					}else{
						r = mid-1
					}
				}
				if a[j]/ans==i{
					check = true
				}
				ma = max(ma,a[j]/ans)
			}
			if check {
				ret = min(ret,ma-i)
			}
		}
		fmt.Fprintln(out,ret)
	}
}

package main

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

const MOD int64 = 1000000007

var n, k int

var a[]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] }

type Item struct {
	rightEnd int
	divideBy int
}

const LOCAL = false

func main() {

	read := bufio.NewReader(os.Stdin)

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

	if LOCAL {

		n = 1e5
		k = 1e5

		a = make([]int, 1e5)

		for i := 1; i <= 1e5; i++ {
			a[i - 1] = i
		}

		st := time.Now().UnixNano()
		solve()
		en := time.Now().UnixNano()

		fmt.Println((en - st)/1000000)

		return
	}

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

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

		a = make([]int, n)

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

		fmt.Println(solve())
	}
}

type pair struct {
	v int
	idx []int
}

func solve() int {

	var mn2 int = 1e6

	for i := a[n - 1]; i >= a[n - 1]/k; i-- {
		mx := i

		if i == 0 {
			return 0
		}

		var mn int = 1e6

		can := false

		for j := 0; j < n; j++ {
			if a[j] < mx {
				mn = min(mn, a[j])

			} else if a[j] > mx {
				if a[j]/k > mx {
					break
				}

				d := a[j]/mx
				if d > k {
					d = k
				} else {
					if a[j]/(d) > mx {
						d++
					}
				}

				t := a[j]/d

				if t == mx {
					can = true
				}

				mn = min(mn, t)

			} else {
				can = true
				mn = min(mn, a[j])
			}
		}

		if can {
			mn2 = min(mn2, mx - mn)
		}
	}
	return mn2
}

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

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

type intIntTreeMap struct {
	endNode   *nodeIntIntTreeMap
	beginNode *nodeIntIntTreeMap
	count     int
	// Less returns a < b
	Less func(a int, b int) bool
}

type nodeIntIntTreeMap struct {
	right   *nodeIntIntTreeMap
	left    *nodeIntIntTreeMap
	parent  *nodeIntIntTreeMap
	isBlack bool
	key     int
	value   int
}

// New creates and returns new TreeMap.
// Parameter less is a function returning a < b.
func newIntIntTreeMap(less func(a int, b int) bool) *intIntTreeMap {
	endNode := &nodeIntIntTreeMap{isBlack: true}
	return &intIntTreeMap{beginNode: endNode, endNode: endNode, Less: less}
}

// Len returns total count of elements in a map.
// Complexity: O(1).
func (t *intIntTreeMap) Len() int { return t.count }

// Set sets the value and silently overrides previous value if it exists.
// Complexity: O(log N).
func (t *intIntTreeMap) Set(key int, value int) {
	parent := t.endNode
	current := parent.left
	less := true
	for current != nil {
		parent = current
		switch {
		case t.Less(key, current.key):
			current = current.left
			less = true
		case t.Less(current.key, key):
			current = current.right
			less = false
		default:
			current.value = value
			return
		}
	}
	x := &nodeIntIntTreeMap{parent: parent, value: value, key: key}
	if less {
		parent.left = x
	} else {
		parent.right = x
	}
	if t.beginNode.left != nil {
		t.beginNode = t.beginNode.left
	}
	t.insertFixup(x)
	t.count++
}

// Del deletes the value.
// Complexity: O(log N).
func (t *intIntTreeMap) Del(key int) {
	z := t.findNode(key)
	if z == nil {
		return
	}
	if t.beginNode == z {
		if z.right != nil {
			t.beginNode = z.right
		} else {
			t.beginNode = z.parent
		}
	}
	t.count--
	removeNodeIntIntTreeMap(t.endNode.left, z)
}

// Clear clears the map.
// Complexity: O(1).
func (t *intIntTreeMap) Clear() {
	t.count = 0
	t.beginNode = t.endNode
	t.endNode.left = nil
}

// Get retrieves a value from a map for specified key and reports if it exists.
// Complexity: O(log N).
func (t *intIntTreeMap) Get(id int) (int, bool) {
	node := t.findNode(id)
	if node == nil {
		node = t.endNode
	}
	return node.value, node != t.endNode
}

// Contains checks if key exists in a map.
// Complexity: O(log N)
func (t *intIntTreeMap) Contains(id int) bool { return t.findNode(id) != nil }

// Range returns a pair of iterators that you can use to go through all the keys in the range [from, to].
// More specifically it returns iterators pointing to lower bound and upper bound.
// Complexity: O(log N).
func (t *intIntTreeMap) Range(from, to int) (forwardIteratorIntIntTreeMap, forwardIteratorIntIntTreeMap) {
	return t.LowerBound(from), t.UpperBound(to)
}

// LowerBound returns an iterator pointing to the first element that is not less than the given key.
// Complexity: O(log N).
func (t *intIntTreeMap) LowerBound(key int) forwardIteratorIntIntTreeMap {
	result := t.endNode
	node := t.endNode.left
	if node == nil {
		return forwardIteratorIntIntTreeMap{tree: t, node: t.endNode}
	}
	for {
		if t.Less(node.key, key) {
			if node.right != nil {
				node = node.right
			} else {
				return forwardIteratorIntIntTreeMap{tree: t, node: result}
			}
		} else {
			result = node
			if node.left != nil {
				node = node.left
			} else {
				return forwardIteratorIntIntTreeMap{tree: t, node: result}
			}
		}
	}
}

// UpperBound returns an iterator pointing to the first element that is greater than the given key.
// Complexity: O(log N).
func (t *intIntTreeMap) UpperBound(key int) forwardIteratorIntIntTreeMap {
	result := t.endNode
	node := t.endNode.left
	if node == nil {
		return forwardIteratorIntIntTreeMap{tree: t, node: t.endNode}
	}
	for {
		if !t.Less(key, node.key) {
			if node.right != nil {
				node = node.right
			} else {
				return forwardIteratorIntIntTreeMap{tree: t, node: result}
			}
		} else {
			result = node
			if node.left != nil {
				node = node.left
			} else {
				return forwardIteratorIntIntTreeMap{tree: t, node: result}
			}
		}
	}
}

// Iterator returns an iterator for tree map.
// It starts at the first element and goes to the one-past-the-end position.
// You can iterate a map at O(N) complexity.
// Method complexity: O(1)
func (t *intIntTreeMap) Iterator() forwardIteratorIntIntTreeMap {
	return forwardIteratorIntIntTreeMap{tree: t, node: t.beginNode}
}

// Reverse returns a reverse iterator for tree map.
// It starts at the last element and goes to the one-before-the-start position.
// You can iterate a map at O(N) complexity.
// Method complexity: O(log N)
func (t *intIntTreeMap) Reverse() reverseIteratorIntIntTreeMap {
	node := t.endNode.left
	if node != nil {
		node = mostRightIntIntTreeMap(node)
	}
	return reverseIteratorIntIntTreeMap{tree: t, node: node}
}

func (t *intIntTreeMap) findNode(id int) *nodeIntIntTreeMap {
	current := t.endNode.left
	for current != nil {
		switch {
		case t.Less(id, current.key):
			current = current.left
		case t.Less(current.key, id):
			current = current.right
		default:
			return current
		}
	}
	return nil
}

func mostLeftIntIntTreeMap(x *nodeIntIntTreeMap) *nodeIntIntTreeMap {
	for x.left != nil {
		x = x.left
	}
	return x
}

func mostRightIntIntTreeMap(x *nodeIntIntTreeMap) *nodeIntIntTreeMap {
	for x.right != nil {
		x = x.right
	}
	return x
}

func successorIntIntTreeMap(x *nodeIntIntTreeMap) *nodeIntIntTreeMap {
	if x.right != nil {
		return mostLeftIntIntTreeMap(x.right)
	}
	for x != x.parent.left {
		x = x.parent
	}
	return x.parent
}

func predecessorIntIntTreeMap(x *nodeIntIntTreeMap) *nodeIntIntTreeMap {
	if x.left != nil {
		return mostRightIntIntTreeMap(x.left)
	}
	for x.parent != nil && x != x.parent.right {
		x = x.parent
	}
	return x.parent
}

func rotateLeftIntIntTreeMap(x *nodeIntIntTreeMap) {
	y := x.right
	x.right = y.left
	if x.right != nil {
		x.right.parent = x
	}
	y.parent = x.parent
	if x == x.parent.left {
		x.parent.left = y
	} else {
		x.parent.right = y
	}
	y.left = x
	x.parent = y
}

func rotateRightIntIntTreeMap(x *nodeIntIntTreeMap) {
	y := x.left
	x.left = y.right
	if x.left != nil {
		x.left.parent = x
	}
	y.parent = x.parent
	if x == x.parent.left {
		x.parent.left = y
	} else {
		x.parent.right = y
	}
	y.right = x
	x.parent = y
}

func (t *intIntTreeMap) insertFixup(x *nodeIntIntTreeMap) {
	root := t.endNode.left
	x.isBlack = x == root
	for x != root && !x.parent.isBlack {
		if x.parent == x.parent.parent.left {
			y := x.parent.parent.right
			if y != nil && !y.isBlack {
				x = x.parent
				x.isBlack = true
				x = x.parent
				x.isBlack = x == root
				y.isBlack = true
			} else {
				if x != x.parent.left {
					x = x.parent
					rotateLeftIntIntTreeMap(x)
				}
				x = x.parent
				x.isBlack = true
				x = x.parent
				x.isBlack = false
				rotateRightIntIntTreeMap(x)
				break
			}
		} else {
			y := x.parent.parent.left
			if y != nil && !y.isBlack {
				x = x.parent
				x.isBlack = true
				x = x.parent
				x.isBlack = x == root
				y.isBlack = true
			} else {
				if x == x.parent.left {
					x = x.parent
					rotateRightIntIntTreeMap(x)
				}
				x = x.parent
				x.isBlack = true
				x = x.parent
				x.isBlack = false
				rotateLeftIntIntTreeMap(x)
				break
			}
		}
	}
}

// nolint: gocyclo
//noinspection GoNilness
func removeNodeIntIntTreeMap(root *nodeIntIntTreeMap, z *nodeIntIntTreeMap) {
	var y *nodeIntIntTreeMap
	if z.left == nil || z.right == nil {
		y = z
	} else {
		y = successorIntIntTreeMap(z)
	}
	var x *nodeIntIntTreeMap
	if y.left != nil {
		x = y.left
	} else {
		x = y.right
	}
	var w *nodeIntIntTreeMap
	if x != nil {
		x.parent = y.parent
	}
	if y == y.parent.left {
		y.parent.left = x
		if y != root {
			w = y.parent.right
		} else {
			root = x // w == nil
		}
	} else {
		y.parent.right = x
		w = y.parent.left
	}
	removedBlack := y.isBlack
	if y != z {
		y.parent = z.parent
		if z == z.parent.left {
			y.parent.left = y
		} else {
			y.parent.right = y
		}
		y.left = z.left
		y.left.parent = y
		y.right = z.right
		if y.right != nil {
			y.right.parent = y
		}
		y.isBlack = z.isBlack
		if root == z {
			root = y
		}
	}
	if removedBlack && root != nil {
		if x != nil {
			x.isBlack = true
		} else {
			for {
				if w != w.parent.left {
					if !w.isBlack {
						w.isBlack = true
						w.parent.isBlack = false
						rotateLeftIntIntTreeMap(w.parent)
						if root == w.left {
							root = w
						}
						w = w.left.right
					}
					if (w.left == nil || w.left.isBlack) && (w.right == nil || w.right.isBlack) {
						w.isBlack = false
						x = w.parent
						if x == root || !x.isBlack {
							x.isBlack = true
							break
						}
						if x == x.parent.left {
							w = x.parent.right
						} else {
							w = x.parent.left
						}
					} else {
						if w.right == nil || w.right.isBlack {
							w.left.isBlack = true
							w.isBlack = false
							rotateRightIntIntTreeMap(w)
							w = w.parent
						}
						w.isBlack = w.parent.isBlack
						w.parent.isBlack = true
						w.right.isBlack = true
						rotateLeftIntIntTreeMap(w.parent)
						break
					}
				} else {
					if !w.isBlack {
						w.isBlack = true
						w.parent.isBlack = false
						rotateRightIntIntTreeMap(w.parent)
						if root == w.right {
							root = w
						}
						w = w.right.left
					}
					if (w.left == nil || w.left.isBlack) && (w.right == nil || w.right.isBlack) {
						w.isBlack = false
						x = w.parent
						if !x.isBlack || x == root {
							x.isBlack = true
							break
						}
						if x == x.parent.left {
							w = x.parent.right
						} else {
							w = x.parent.left
						}
					} else {
						if w.left == nil || w.left.isBlack {
							w.right.isBlack = true
							w.isBlack = false
							rotateLeftIntIntTreeMap(w)
							w = w.parent
						}
						w.isBlack = w.parent.isBlack
						w.parent.isBlack = true
						w.left.isBlack = true
						rotateRightIntIntTreeMap(w.parent)
						break
					}
				}
			}
		}
	}
}

// ForwardIterator represents a position in a tree map.
// It is designed to iterate a map in a forward order.
// It can point to any position from the first element to the one-past-the-end element.
type forwardIteratorIntIntTreeMap struct {
	tree *intIntTreeMap
	node *nodeIntIntTreeMap
}

// Valid reports if an iterator's position is valid.
// In other words it returns true if an iterator is not at the one-past-the-end position.
func (i forwardIteratorIntIntTreeMap) Valid() bool { return i.node != i.tree.endNode }

// Next moves an iterator to the next element.
// It panics if goes out of bounds.
func (i *forwardIteratorIntIntTreeMap) Next() {
	if i.node == i.tree.endNode {
		panic("out of bound iteration")
	}
	i.node = successorIntIntTreeMap(i.node)
}

// Prev moves an iterator to the previous element.
// It panics if goes out of bounds.
func (i *forwardIteratorIntIntTreeMap) Prev() {
	i.node = predecessorIntIntTreeMap(i.node)
	if i.node == nil {
		//panic("out of bound iteration")
	}
}

// Key returns a key at an iterator's position
func (i forwardIteratorIntIntTreeMap) Key() int { return i.node.key }

// Value returns a value at an iterator's position
func (i forwardIteratorIntIntTreeMap) Value() int { return i.node.value }

// ReverseIterator represents a position in a tree map.
// It is designed to iterate a map in a reverse order.
// It can point to any position from the one-before-the-start element to the last element.
type reverseIteratorIntIntTreeMap struct {
	tree *intIntTreeMap
	node *nodeIntIntTreeMap
}

// Valid reports if an iterator's position is valid.
// In other words it returns true if an iterator is not at the one-before-the-start position.
func (i reverseIteratorIntIntTreeMap) Valid() bool { return i.node != nil }

// Next moves an iterator to the next element in reverse order.
// It panics if goes out of bounds.
func (i *reverseIteratorIntIntTreeMap) Next() {
	if i.node == nil {
		panic("out of bound iteration")
	}
	i.node = predecessorIntIntTreeMap(i.node)
}

// Prev moves an iterator to the previous element in reverse order.
// It panics if goes out of bounds.
func (i *reverseIteratorIntIntTreeMap) Prev() {
	if i.node != nil {
		i.node = successorIntIntTreeMap(i.node)
	} else {
		i.node = i.tree.beginNode
	}
	if i.node == i.tree.endNode {
		panic("out of bound iteration")
	}
}

// Key returns a key at an iterator's position
func (i reverseIteratorIntIntTreeMap) Key() int { return i.node.key }

// Value returns a value at an iterator's position
func (i reverseIteratorIntIntTreeMap) Value() int { return i.node.value }

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1706D2(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	ge := [3001]int{}
	var T, n, k, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		up := -1
		for ; n > 0; n-- {
			Fscan(in, &v)
			for i := up + 1; i <= v; i++ {
				ge[i] = v
			}
			up = v
		}
		if up <= k {
			Fprintln(out, 0)
			continue
		}
		ans := int(1e9)
	o:
		for mx := up / k; mx <= up; mx++ {
			mi := mx
			for j := 1; (j-1)*(mx+1) <= up; j++ {
				v := ge[(j-1)*(mx+1)]
				if v >= j*(mx+1) {
					j = v/(mx+1) + 1
				}
				if v/j < mi {
					mi = v / j
					if mx-mi >= ans {
						continue o
					}
				}
			}
			ans = mx - mi
		}
		Fprintln(out, ans)
	}
}

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

package main

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

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

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

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

func do(n, k int, a []int) int {
	sort.Ints(a)
	ans := -1
	for i := 1; i <= k; i++ {
		min := a[0] / i
		if min == 0 {
			ans2 := a[n-1] / k
			if ans < 0 || ans > ans2 {
				ans = ans2
			}
			break
		}
		max := min
		for j := 1; j < n; j++ {
			k2 := a[j] / min
			if k2 > k {
				k2 = k
			}
			max2 := a[j] / k2
			if max < max2 {
				max = max2
			}
		}
		ans2 := max - min
		if ans < 0 || ans > ans2 {
			ans = ans2
		}
	}
	return ans
}

package main

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

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

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

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

func do(n, k int, a []int) int {
	sort.Ints(a)
	ans := -1
	for i := 1; i <= k; i++ {
		min := a[0] / i
		max := min
		for j := 1; j < n; j++ {
			k2 := k
			if min != 0 {
				if v := a[j] / min; k2 > v {
					k2 = v
				}
			}
			max2 := a[j] / k2
			if max < max2 {
				max = max2
			}
		}
		ans2 := max - min
		if ans < 0 || ans > ans2 {
			ans = ans2
		}
	}
	return ans
}