package main

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

type bufReader struct {
	r   *bufio.Reader
	buf []byte
	i   int
}

var reader = &bufReader{
	bufio.NewReader(os.Stdin),
	make([]byte, 0),
	0,
}

func (r *bufReader) readLine() {
	if r.i < len(r.buf) {
		return
	}
	r.buf = make([]byte, 0)
	r.i = 0
	for {
		line, isPrefix, err := r.r.ReadLine()
		if err != nil {
			panic(err)
		}
		r.buf = append(r.buf, line...)
		if !isPrefix {
			break
		}
	}
}

func (r *bufReader) next() string {
	r.readLine()
	from := r.i
	for ; r.i < len(r.buf); r.i++ {
		if r.buf[r.i] == ' ' {
			break
		}
	}
	s := string(r.buf[from:r.i])
	r.i++
	return s
}

func (r *bufReader) nextLine() string {
	r.readLine()
	s := string(r.buf[r.i:])
	r.i = len(r.buf)
	return s
}

var writer = bufio.NewWriter(os.Stdout)

func next() string {
	return reader.next()
}

func nextInt64() int64 {
	i, err := strconv.ParseInt(reader.next(), 10, 64)
	if err != nil {
		panic(err)
	}
	return i
}

func nextInt() int {
	return int(nextInt64())
}

func nextLine() string {
	return reader.nextLine()
}

func out(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

func max64(x, y int64) int64 {
	if x > y {
		return x
	}
	return y
}

func max(x, y int) int {
	return int(max64(int64(x), int64(y)))
}

func min64(x, y int64) int64 {
	if x < y {
		return x
	}
	return y
}

func min(x, y int) int {
	return int(min64(int64(x), int64(y)))
}

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

func abs(x int) int {
	return int(abs64(int64(x)))
}

func joinInt64s(a []int64, sep string) string {
	b := make([]string, len(a))
	for i, v := range a {
		b[i] = strconv.FormatInt(v, 10)
	}
	return strings.Join(b, sep)
}

func joinInts(a []int, sep string) string {
	b := make([]string, len(a))
	for i, v := range a {
		b[i] = strconv.Itoa(v)
	}
	return strings.Join(b, sep)
}

func divUp64(x, y int64) int64 {
	return (x + y - 1) / y
}

func divUp(x, y int) int {
	return int(divUp64(int64(x), int64(y)))
}

func gcd64(x, y int64) int64 {
	if x < y {
		x, y = y, x
	}
	for y != 0 {
		x, y = y, x%y
	}
	return x
}

func gcd(x, y int) int {
	return int(gcd64(int64(x), int64(y)))
}

func lcm64(x, y int64) int64 {
	return x * y / gcd64(x, y)
}

func lcm(x, y int) int {
	return int(lcm64(int64(x), int64(y)))
}

func pow64(x, y int64) int64 {
	return int64(math.Pow(float64(x), float64(y)))
}

func pow(x, y int) int {
	return int(pow64(int64(x), int64(y)))
}

func main() {
	solveTC()
	writer.Flush()
}

func solveTC() {
	t := nextInt()
	for tc := 0; tc < t; tc++ {
		solve()
	}
}

func solve() {
	nextInt()
	k := nextInt()

	letterCounts := []int{}
	for i := 0; i < 26; i++ {
		letterCounts = append(letterCounts, 0)
	}
	s := next()
	for _, c := range s {
		letterCounts[c-'a']++
	}
	pairs := 0
	extraLetters := 0
	for _, c := range letterCounts {
		pairs += c / 2
		extraLetters += c % 2
	}
	colorCounts := []int{}
	startCount := pairs / k
	for i := 0; i < k; i++ {
		colorCounts = append(colorCounts, startCount*2)
		pairs -= startCount
	}
	for (extraLetters < k-1) && (pairs > 0) {
		extraLetters += 2
		pairs--
	}
	for i := 0; i < k; i++ {
		if extraLetters > 0 {
			colorCounts[i]++
			extraLetters--
		}
	}
	for i := k - 1; i >= 0; i-- {
		if pairs > 0 {
			colorCounts[i] += 2
			pairs--
		}
	}
	minCount := 1000000
	for _, i := range colorCounts {
		minCount = min(minCount, i)
	}
	out(minCount)
}

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
		var s string
		fmt.Fscan(in, &n, &k, &s)

		m := make([]int, 26)
		for _, ch := range s {
			m[ch-'a']++
		}

		var pairs, z int
		for i, v := range m {
			d := v / 2
			pairs += d
			m[i] -= d * 2

			z += m[i]
		}

		min := pairs / k * 2
		z += (pairs % k) * 2
		if z >= k {
			min++
		}
		fmt.Println(min)
	}
}

package main

import (
	"bufio"
	"bytes"
	"container/heap"
	"fmt"
	"os"
)

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

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

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

	fmt.Print(buf.String())
}

func normalize(s string) string {

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

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

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

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

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

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

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

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

	return i
}

func solve(n int, k int, s string) int {
	cnt := make([]int, 26)

	for i := 0; i < n; i++ {
		cnt[int(s[i]-'a')]++
	}
	check := func(l int) bool {
		arr := make(IntHeap, 26)

		copy(arr, cnt)

		heap.Init(&arr)

		for j := 0; j < k; j++ {
			var tmp int

			for tmp+1 < l && arr.Len() > 0 {
				x := heap.Pop(&arr).(int)
				y := x / 2 * 2
				if y == 0 {
					break
				}
				// y is atleast 2
				if tmp+y <= l {
					tmp += y
					x -= y
				} else {
					// tmp + y > l
					z := (l - tmp) / 2 * 2
					tmp += z
					x -= z
				}
				if x > 0 {
					heap.Push(&arr, x)
				}
			}
			if tmp+1 < l {
				return false
			}
		}

		if l&1 == 1 {
			var left int
			for i := 0; i < len(arr); i++ {
				left += arr[i]
			}
			return left >= k
		}

		return true
	}

	r := (n+k-1)/k + 1
	l := 1

	for l < r {
		mid := (l + r) / 2
		if !check(mid) {
			r = mid
		} else {
			l = mid + 1
		}
	}
	return r - 1
}

// An IntHeap is a min-heap of ints.
type IntHeap []int

func (h IntHeap) Len() int           { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] > h[j] }
func (h IntHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *IntHeap) Push(x interface{}) {
	// Push and Pop use pointer receivers because they modify the slice's length,
	// not just its contents.
	*h = append(*h, x.(int))
}

func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

package main

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

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

	var T, n, k int
	var s string
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k, &s)
		cnt := [26]int{}
		for _, b := range s {
			cnt[b-'a']++
		}
		pair, odd := 0, 0
		for _, c := range cnt {
			pair += c / 2
			odd += c & 1
		}
		ans := pair / k * 2
		if odd+pair%k*2 >= k {
			ans++
		}
		Fprintln(out, ans)
	}
}

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

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)
	var s string
	Fscan(in,&s)
	cnt := make([]int,26)
	for _,v := range s{
		cnt[v - 'a']++
	}
	even := 0
	odd := 0
	for _,v := range cnt{
		even += v / 2
		odd += v % 2
	}
	ans := 2 * (even / k)
	odd += 2 * (even % k)
	if odd >= k{
		ans++
	}
	Fprintln(out,ans)
}
func max(a,b int)int{
	if a > b{
		return a
	}
	return b
}
func min(a,b int)int{
	if a > b{
		return b
	}
	return a
}
func gcd(a,b int)int{
	if a % b == 0{
		return b
	}
	return gcd(b,a%b)
}

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

package main

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

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

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

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func main() {
	defer writer.Flush()
// 	redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n, k int
	scan(&n, &k)
	var s string
	scan(&s)
	var c = [26]int{}
	for i := range s {
		c[s[i]-'a']++
	}
	var pairs, singles int
	for i := 'a'; i <= 'z'; i++ {
		pairs += c[i-'a'] / 2
		if c[i-'a']%2 == 1 {
			singles++
		}
	}
	var (
		l, r  = 1, n + 1
		check = func(x int) bool {
			if x%2 == 1 {
				r, a, b := 0, pairs/(x/2), singles+2*(pairs%(x/2))
				mn := min(a, b)
				r += mn; a -= mn; b -= mn
				if a > 0 {
					r += a - ((a + x - 1) / x)
				}
				return r >= k
			}
			return (pairs / (x / 2)) >= k
		}
	)
	for r-l > 1 {
		m := l + (r-l)/2
		if check(m) {
			l = m
		} else {
			r = m
		}
	}
	printf("%v\n", l)
}

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

// generated by 1e9y at 10-01-2022
// template version: 2022-01

package main

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

// 0000.A
func solve(n, k int, str string) int {
	index := map[rune]int{}
	for _, s := range str {
		index[s]++
	}
	var pairs, singles int
	for _, v := range index {
		pairs += v / 2
		singles += v % 2
	}

	// Println(str, pairs, singles)
	res := pairs / k * 2
	if (pairs%k*2 + singles) >= k {
		res++
	}

	return res
}

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

	t := ReadInt(in)
	for i := 0; i < t; i++ {
		line := ReadIntSlice(in)
		n, k := line[0], line[1]
		str := ReadLine(in)
		Println(solve(n, k, str))
	}
}

// helpers for convenient input reading

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

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

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

func ReadIntSlice(in *bufio.Reader) []int {
	parts := ReadLineParts(in)
	slice := make([]int, len(parts))
	for i, n := range parts {
		slice[i] = MustAtoi(n)
	}
	return slice
}

func ReadInt64Slice(in *bufio.Reader) []int64 {
	parts := ReadLineParts(in)
	slice := make([]int64, len(parts))
	for i, n := range parts {
		slice[i] = MustParseInt(n, 10, 64)
	}
	return slice
}

func ReadUint64Slice(in *bufio.Reader) []uint64 {
	parts := ReadLineParts(in)
	slice := make([]uint64, len(parts))
	for i, n := range parts {
		slice[i] = MustParseUint(n, 10, 64)
	}
	return slice
}

func MustAtoi(s string) int {
	v, _ := strconv.Atoi(s)
	return v
}

func MustParseInt(s string, base, bitSize int) int64 {
	v, _ := strconv.ParseInt(s, base, bitSize)
	return v
}

func MustParseUint(s string, base, bitSize int) uint64 {
	v, _ := strconv.ParseUint(s, base, bitSize)
	return v
}

package main

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

var cf = true

const sz = int('z' - 'a' + 1)

var arr [sz]int

func solve() {
	var n, k int
	var str string
	for i := 0; i < sz; i++ {
		arr[i] = 0
	}
	fmt.Fscan(in, &n, &k, &str)
	var evenPairs, odd int
	for _, elem := range str {
		arr[elem-'a']++
	}
	for _, elem := range arr {
		evenPairs += elem / 2
	}
	if evenPairs%k != 0 {
		evenPairs -= evenPairs % k
	}
	odd = n - evenPairs*2
	ans := (evenPairs / k) * 2
	if odd >= k {
		ans++
	}
	fmt.Fprintln(out, ans)
}

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

type intSet struct {
	itms map[int]struct{}
}

type stringSet struct {
	itms map[string]struct{}
}

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

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

func (s *intSet) remove(item int) *intSet {
	s.init()
	delete(s.itms, item)
	return s
}

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

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

func (s *intSet) size() int {
	s.init()
	return len(s.itms)
}

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

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

func (s *stringSet) remove(item string) *stringSet {
	s.init()
	delete(s.itms, item)
	return s
}

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

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

func (s *stringSet) 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)
}

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

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

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

const float64EqualityThreshold = 1e-9

func almostEqual(a, b float64) bool {
	return math.Abs(a-b) <= float64EqualityThreshold
}

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

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

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

package main

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

var cf = true

const sz = int('z' - 'a' + 1)

var arr [sz]int

func solve() {
	var n, k int
	var str string
	for i := 0; i < sz; i++ {
		arr[i] = 0
	}
	fmt.Fscan(in, &n, &k, &str)
	var evenPairs, odd int
	for _, elem := range str {
		arr[elem-'a']++
	}
	for _, elem := range arr {
		evenPairs += elem / 2
	}
	fmt.Println(evenPairs)
	if evenPairs%k != 0 {
		evenPairs -= evenPairs % k
	}
	odd = n - evenPairs*2
	fmt.Println(evenPairs, odd)
	fmt.Println()
	ans := (evenPairs / k) * 2
	if odd >= k {
		ans++
	}
	fmt.Fprintln(out, ans)
}

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

type intSet struct {
	itms map[int]struct{}
}

type stringSet struct {
	itms map[string]struct{}
}

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

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

func (s *intSet) remove(item int) *intSet {
	s.init()
	delete(s.itms, item)
	return s
}

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

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

func (s *intSet) size() int {
	s.init()
	return len(s.itms)
}

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

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

func (s *stringSet) remove(item string) *stringSet {
	s.init()
	delete(s.itms, item)
	return s
}

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

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

func (s *stringSet) 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)
}

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

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

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

const float64EqualityThreshold = 1e-9

func almostEqual(a, b float64) bool {
	return math.Abs(a-b) <= float64EqualityThreshold
}

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

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

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

package main

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

var cf = true

var z = int('z' - 'a' + 1)
var arr ['z' - 'a' + 1]int

func solve() {
	for i := 0; i < z; i++ {
		arr[i] = 0
	}
	var n, k int
	var str string
	fmt.Fscan(in, &n, &k, &str)
	for _, elem := range str {
		arr[elem-'a']++
	}
	even := make([]int, 0, z)
	odd := make([]int, 0, z)
	for i := 0; i < z; i++ {
		if arr[i] == 0 {
			continue
		}
		if arr[i]%2 == 0 {
			even = append(even, i)
		} else {
			odd = append(odd, i)
		}
	}
	sort.Slice(even, func(i, j int) bool {
		return arr[even[i]] < arr[even[j]]
	})
	sort.Slice(odd, func(i, j int) bool {
		return arr[odd[i]] < arr[odd[j]]
	})
	ptr1, ptr2 := len(even)-1, len(odd)-1
	limit := n / k
	avail := n - limit*k
	ans := MaxInt
	for ; k > 0; k-- {
		cur := 0
		oddUsed := false
		for cur < limit {
			begCur := cur
			needed := limit - cur
			if !oddUsed && ptr2 >= 0 {
				if arr[odd[ptr2]] > needed {
					//fmt.Println("used odd ", odd[ptr2], " ", needed)
					arr[odd[ptr2]] -= needed
					cur += needed
					if needed%2 == 1 {
						oddUsed = true
					}
					if arr[odd[ptr2]]%2 == 0 {
						even = append(even, odd[ptr2])
						sort.Slice(even, func(i, j int) bool {
							return arr[even[i]] < arr[even[j]]
						})
						ptr1++
						odd = odd[:len(odd)-1]
						ptr2--
					} else {
						sort.Slice(odd, func(i, j int) bool {
							return arr[odd[i]] < arr[odd[j]]
						})
					}
				} else {
					//fmt.Println("used odd ", odd[ptr2], " ", arr[odd[ptr2]])
					cur += arr[odd[ptr2]]
					ptr2--
					oddUsed = true
				}
				continue
			}
			if oddUsed && needed%2 == 1 {
				if avail > 0 {
					avail--
				} else {
					needed--
				}
			}
			if ptr1 >= 0 {
				if arr[even[ptr1]] > needed {
					//fmt.Println("used even ", even[ptr1], " ", needed)
					arr[even[ptr1]] -= needed
					cur += needed
					if needed%2 == 1 {
						oddUsed = true
					}
					if arr[even[ptr1]]%2 == 1 {
						odd = append(odd, even[ptr1])
						sort.Slice(odd, func(i, j int) bool {
							return arr[odd[i]] < arr[odd[j]]
						})
						ptr2++
						even = even[:len(even)-1]
						ptr1--
					} else {
						sort.Slice(even, func(i, j int) bool {
							return arr[even[i]] < arr[even[j]]
						})
					}
				} else {
					//fmt.Println("used even ", even[ptr1], " ", arr[even[ptr1]])
					cur += arr[even[ptr1]]
					ptr1--
				}
			}
			if begCur == cur {
				break
			}
		}
		//fmt.Println(cur)
		ans = min(ans, cur)
	}
	//fmt.Println("------")
	fmt.Fprintln(out, ans)
}

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

type intSet struct {
	itms map[int]struct{}
}

type stringSet struct {
	itms map[string]struct{}
}

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

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

func (s *intSet) remove(item int) *intSet {
	s.init()
	delete(s.itms, item)
	return s
}

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

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

func (s *intSet) size() int {
	s.init()
	return len(s.itms)
}

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

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

func (s *stringSet) remove(item string) *stringSet {
	s.init()
	delete(s.itms, item)
	return s
}

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

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

func (s *stringSet) 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)
}

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

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

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

const float64EqualityThreshold = 1e-9

func almostEqual(a, b float64) bool {
	return math.Abs(a-b) <= float64EqualityThreshold
}

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

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

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

package main

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

var cf = true

var z = int('z' - 'a' + 1)
var arr ['z' - 'a' + 1]int

func solve() {
	for i := 0; i < z; i++ {
		arr[i] = 0
	}
	var n, k int
	var str string
	fmt.Fscan(in, &n, &k, &str)
	for _, elem := range str {
		arr[elem-'a']++
	}
	even := make([]int, 0, z)
	odd := make([]int, 0, z)
	for i := 0; i < z; i++ {
		if arr[i] == 0 {
			continue
		}
		if arr[i]%2 == 0 {
			even = append(even, i)
		} else {
			odd = append(odd, i)
		}
	}
	sort.Slice(even, func(i, j int) bool {
		return arr[even[i]] > arr[even[j]]
	})
	sort.Slice(odd, func(i, j int) bool {
		return arr[odd[i]] > arr[odd[j]]
	})
	ptr1, ptr2 := len(even)-1, len(odd)-1
	limit := n / k
	avail := n - limit*k
	ans := MaxInt
	for ; k > 0; k-- {
		cur := 0
		oddUsed := false
		for cur < limit {
			begCur := cur
			needed := limit - cur
			if !oddUsed && ptr2 >= 0 {
				if arr[odd[ptr2]] > needed {
					//fmt.Println("used odd ", odd[ptr2], " ", needed)
					arr[odd[ptr2]] -= needed
					cur += needed
					if needed%2 == 1 {
						oddUsed = true
					}
					if arr[odd[ptr2]]%2 == 0 {
						even = append(even, odd[ptr2])
						sort.Slice(even, func(i, j int) bool {
							return arr[even[i]] > arr[even[j]]
						})
						ptr1++
						odd = odd[:len(odd)-1]
						ptr2--
					} else {
						sort.Slice(odd, func(i, j int) bool {
							return arr[odd[i]] > arr[odd[j]]
						})
					}
				} else {
					//fmt.Println("used odd ", odd[ptr2], " ", arr[odd[ptr2]])
					cur += arr[odd[ptr2]]
					ptr2--
					oddUsed = true
				}
				continue
			}
			if oddUsed && needed%2 == 1 {
				if avail > 0 {
					avail--
				} else {
					needed--
				}
			}
			if ptr1 >= 0 {
				if arr[even[ptr1]] > needed {
					//fmt.Println("used even ", even[ptr1], " ", needed)
					arr[even[ptr1]] -= needed
					cur += needed
					if needed%2 == 1 {
						oddUsed = true
					}
					if arr[even[ptr1]]%2 == 1 {
						odd = append(odd, even[ptr1])
						sort.Slice(odd, func(i, j int) bool {
							return arr[odd[i]] > arr[odd[j]]
						})
						ptr2++
						even = even[:len(even)-1]
						ptr1--
					} else {
						sort.Slice(even, func(i, j int) bool {
							return arr[even[i]] > arr[even[j]]
						})
					}
				} else {
					//fmt.Println("used even ", even[ptr1], " ", arr[even[ptr1]])
					cur += arr[even[ptr1]]
					ptr1--
				}
			}
			if begCur == cur {
				break
			}
		}
		//fmt.Println(cur)
		ans = min(ans, cur)
	}
	//fmt.Println("------")
	fmt.Fprintln(out, ans)
}

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

type intSet struct {
	itms map[int]struct{}
}

type stringSet struct {
	itms map[string]struct{}
}

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

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

func (s *intSet) remove(item int) *intSet {
	s.init()
	delete(s.itms, item)
	return s
}

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

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

func (s *intSet) size() int {
	s.init()
	return len(s.itms)
}

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

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

func (s *stringSet) remove(item string) *stringSet {
	s.init()
	delete(s.itms, item)
	return s
}

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

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

func (s *stringSet) 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)
}

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

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

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

const float64EqualityThreshold = 1e-9

func almostEqual(a, b float64) bool {
	return math.Abs(a-b) <= float64EqualityThreshold
}

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

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

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

package main

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

var cf = true

var z = int('z' - 'a' + 1)
var arr ['z' - 'a' + 1]int

func solve() {
	for i := 0; i < z; i++ {
		arr[i] = 0
	}
	var n, k int
	var str string
	fmt.Fscan(in, &n, &k, &str)
	for _, elem := range str {
		arr[elem-'a']++
	}
	even := make([]int, 0, z)
	odd := make([]int, 0, z)
	for i := 0; i < z; i++ {
		if arr[i] == 0 {
			continue
		}
		if arr[i]%2 == 0 {
			even = append(even, i)
		} else {
			odd = append(odd, i)
		}
	}
	for i := 0; i < len(even); i++ {
		for j := i + 1; j < len(even); j++ {
			if arr[even[i]] < arr[even[j]] {
				even[i], even[j] = even[j], even[i]
			}
		}
	}
	for i := 0; i < len(odd); i++ {
		for j := i + 1; j < len(odd); j++ {
			if arr[odd[i]] < arr[odd[j]] {
				odd[i], odd[j] = odd[j], odd[i]
			}
		}
	}
	ptr1, ptr2 := 0, 0
	limit := n / k
	ans := MaxInt
	for ; k > 0; k-- {
		cur := 0
		oddUsed := false
		for cur < limit {
			begCur := cur
			needed := limit - cur
			if !oddUsed && ptr2 < len(odd) {
				if arr[odd[ptr2]] > needed {
					//fmt.Println("used odd ", odd[ptr2], " ", needed)
					arr[odd[ptr2]] -= needed
					cur += needed
					if needed%2 == 1 {
						oddUsed = true
					}
					if arr[odd[ptr2]]%2 == 0 {
						even = append(even, odd[ptr2])
						for ind := len(even) - 1; ind > ptr1 && arr[even[ind]] > arr[even[ind-1]]; ind-- {
							even[ind], even[ind-1] = even[ind-1], even[ind]
						}
						ptr2++
					} else {
						for ind := ptr2; ind < len(odd)-1 && arr[odd[ind]] < arr[odd[ind+1]]; ind++ {
							odd[ind], odd[ind+1] = odd[ind+1], odd[ind]
						}
					}
					continue
				} else {
					//fmt.Println("used odd ", odd[ptr2], " ", arr[odd[ptr2]])
					cur += arr[odd[ptr2]]
					ptr2++
					oddUsed = true
				}
			}
			needed = limit - cur
			if needed%2 == 1 && oddUsed {
				needed--
				if needed == 0 {
					break
				}
			}
			if ptr1 < len(even) {
				if arr[even[ptr1]] > needed {
					//fmt.Println("used even ", even[ptr1], " ", needed)
					arr[even[ptr1]] -= needed
					cur += needed
					if needed%2 == 1 {
						oddUsed = true
					}
					if arr[even[ptr1]]%2 == 1 {
						odd = append(odd, even[ptr1])
						for ind := len(odd) - 1; ind > ptr2 && arr[odd[ind]] > arr[odd[ind-1]]; ind-- {
							odd[ind], odd[ind-1] = odd[ind-1], odd[ind]
						}
						ptr1++
					} else {
						for ind := ptr1; ind < len(even)-1 && arr[even[ind]] < arr[even[ind+1]]; ind++ {
							even[ind], even[ind+1] = even[ind+1], even[ind]
						}
					}
				} else {
					//fmt.Println("used even ", even[ptr1], " ", arr[even[ptr1]])
					cur += arr[even[ptr1]]
					ptr1++
				}
			}
			if begCur == cur {
				break
			}
		}
		//fmt.Println(cur)
		ans = min(ans, cur)
	}
	//fmt.Println("------")
	fmt.Fprintln(out, ans)
}

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

type intSet struct {
	itms map[int]struct{}
}

type stringSet struct {
	itms map[string]struct{}
}

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

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

func (s *intSet) remove(item int) *intSet {
	s.init()
	delete(s.itms, item)
	return s
}

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

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

func (s *intSet) size() int {
	s.init()
	return len(s.itms)
}

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

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

func (s *stringSet) remove(item string) *stringSet {
	s.init()
	delete(s.itms, item)
	return s
}

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

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

func (s *stringSet) 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)
}

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

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

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

const float64EqualityThreshold = 1e-9

func almostEqual(a, b float64) bool {
	return math.Abs(a-b) <= float64EqualityThreshold
}

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

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

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

package main

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

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
		var s string
		fmt.Fscan(in, &n, &k, &s)

		m := make([]int, 26)
		z := make([]int, k)
		var pos int
		for _, ch := range s {
			if m[ch-'a'] == 0 {
				m[ch-'a']++
			} else {
				z[pos] += 2
				m[ch-'a'] = 0
				pos++
				if pos >= k {
					pos = 0
				}
			}
		}

		for _, v := range m {
			if v > 0 && z[pos]%2 == 0 {
				z[pos]++
				pos++
				if pos == k {
					pos = 0
				}
			}
		}

		for i := k - 1; i >= 0; i-- {
			if z[i]%2 != 0 {
				continue
			}

			j := i - 1
			for j >= 0 {
				if z[j]-z[i] == 2 {
					z[i]++
					z[j]--
					break
				}
				j--
			}

			if j == 0 {
				break
			}
		}

		fmt.Fprintln(out, z[k-1])
	}
}

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
		var s string
		fmt.Fscan(in, &n, &k, &s)

		m := make([]int, 26)
		z := make([]int, k)
		var pos int
		for _, ch := range s {
			if m[ch-'a'] == 0 {
				m[ch-'a']++
			} else {
				z[pos] += 2
				m[ch-'a'] = 0
				pos++
				if pos >= k {
					pos = 0
				}
			}
		}

		for _, v := range m {
			if v > 0 && z[pos]%2 == 0 {
				z[pos]++
				pos++
				if pos == k {
					pos = 0
				}
			}
		}

		if k > 1 && z[k-1]%2 == 0 && z[k-2]-z[k-1] == 2 {
			z[k-1]++
		}

		if z[k-1] == 0 {
			fmt.Fprintln(out, 1)
		} else {
			fmt.Fprintln(out, z[k-1])
		}
	}
}


package main

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

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

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

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func main() {
	defer writer.Flush()
// 	redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n, m int
	scan(&n, &m)
	var pos = make(map[string][2]int)
	for i := 0; i < n; i++ {
		var s string
		scan(&s)
		for j := range s {
			if j+2 <= len(s) {
				pos[s[j:j+2]] = [2]int{i, j}
			}
			if j+3 <= len(s) {
				pos[s[j:j+3]] = [2]int{i, j}
			}
		}
	}
	var s string
	scan(&s)
	var (
		dp   = make([]bool, len(s))
		prev = make([]int, len(s))
	)
	for i := range s {
		for l := 2; l <= 3; l++ {
			if i-l+1 < 0 {
				continue
			}
			_, ok1 := pos[s[i-l+1:i+1]]
			ok2 := i-l < 0 || dp[i-l]
			if ok1 && ok2 {
				dp[i] = true
				prev[i] = i - l
			}
		}
	}
	if !dp[len(s)-1] {
		printf("-1\n")
		return
	}
	var ans = [][3]int{}
	for i := len(s) - 1; i >= 0 && dp[i]; {
		j := prev[i]
		p := pos[s[j+1:i+1]]
		ans = append(ans, [3]int{p[1], p[1] + i - j - 1, p[0]})
		i = j
	}
	printf("%v\n", len(ans))
	for i := len(ans) - 1; i >= 0; i-- {
		printf("%v %v %v\n", ans[i][0]+1, ans[i][1]+1, ans[i][2]+1)
	}
}

package main

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

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

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

	for tc > 0 {
		tc--
		reader.ReadString('\n')
		n, m := readTwoNums(reader)
		A := make([]string, n)
		for i := 0; i < n; i++ {
			A[i] = readString(reader)
		}
		S := readString(reader)
		res := solve(n, m, A, S)
		if len(res) == 0 {
			buf.WriteString("-1\n")
		} else {
			buf.WriteString(fmt.Sprintf("%d\n", len(res)))
			for _, cur := range res {
				buf.WriteString(fmt.Sprintf("%d %d %d\n", cur[0], cur[1], cur[2]))
			}
		}
	}

	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' {
			return s[:i]
		}
	}
	return s
}

func normalize(s string) string {

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

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

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

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

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

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

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

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

	return i
}

func solve(n int, m int, A []string, S string) [][]int {
	// 貌似只有2位或者3位的字符串才需要记录
	marks := make([][]Mark, 4)

	// at most 999
	for i := 0; i < 4; i++ {
		marks[i] = make([]Mark, 1000)
	}

	set := func(key string, l, r, i int) {
		if r == l {
			// at least length 2
			return
		}
		x := r - l + 1

		y, _ := strconv.Atoi(key)

		marks[x][y] = Mark{l, r, i}
	}

	get := func(key string) (bool, Mark) {
		if len(key) < 2 {
			return false, Mark{}
		}
		x := len(key)
		y, _ := strconv.Atoi(key)
		cur := marks[x][y]
		if cur.r-cur.l+1 == len(key) {
			return true, cur
		}
		return false, cur
	}

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			for x := 2; x <= 3 && j+x <= m; x++ {
				set(A[i][j:j+x], j, j+x-1, i)
			}
		}
	}

	ans := make([]Mark, m)

	vis := make([]bool, m)

	var build func(i int) bool

	build = func(i int) bool {
		if i == m || ans[i].Len() > 1 {
			return true
		}
		if vis[i] {
			return false
		}
		vis[i] = true

		for x := 2; x <= 3 && i+x <= m; x++ {
			ok, mark := get(S[i : i+x])
			if ok {
				ans[i] = mark
				if build(i + mark.Len()) {
					return true
				}
				ans[i] = Mark{}
			}
		}
		return false
	}

	if build(0) {
		var res [][]int
		pos := 0
		for pos < m {
			cur := ans[pos]
			res = append(res, []int{cur.l + 1, cur.r + 1, cur.i + 1})
			pos += cur.Len()
		}
		return res
	}
	return nil
}

type Mark struct {
	l int
	r int
	i int
}

func (this Mark) Len() int {
	return this.r - this.l + 1
}

const MOD1 = 10000000000007
const MOD2 = 1000000000009

package main

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

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

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

	for tc > 0 {
		tc--
		reader.ReadString('\n')
		n, m := readTwoNums(reader)
		A := make([]string, n)
		for i := 0; i < n; i++ {
			A[i] = readString(reader)
		}
		S := readString(reader)
		res := solve(n, m, A, S)
		if len(res) == 0 {
			buf.WriteString("-1\n")
		} else {
			for _, cur := range res {
				buf.WriteString(fmt.Sprintf("%d %d %d\n", cur[0], cur[1], cur[2]))
			}
		}
	}

	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' {
			return s[:i]
		}
	}
	return s
}

func normalize(s string) string {

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

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

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

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

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

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

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

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

	return i
}

func solve(n int, m int, A []string, S string) [][]int {
	// 貌似只有2位或者3位的字符串才需要记录
	marks := make([][]Mark, 4)

	// at most 999
	for i := 0; i < 4; i++ {
		marks[i] = make([]Mark, 1000)
	}

	set := func(key string, l, r, i int) {
		if r == l {
			// at least length 2
			return
		}
		x := r - l + 1

		y, _ := strconv.Atoi(key)

		marks[x][y] = Mark{l, r, i}
	}

	get := func(key string) (bool, Mark) {
		if len(key) < 2 {
			return false, Mark{}
		}
		x := len(key)
		y, _ := strconv.Atoi(key)
		cur := marks[x][y]
		if cur.r-cur.l+1 == len(key) {
			return true, cur
		}
		return false, cur
	}

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			for x := 2; x <= 3 && j+x <= m; x++ {
				set(A[i][j:j+x], j, j+x-1, i)
			}
		}
	}

	ans := make([]Mark, m)

	var build func(i int) bool

	build = func(i int) bool {
		if i == m {
			return true
		}
		for x := 2; x <= 3 && i+x <= m; x++ {
			ok, mark := get(S[i : i+x])
			if ok {
				ans[i] = mark
				if build(i + mark.Len()) {
					return true
				}
				ans[i] = Mark{}
			}
		}
		return false
	}

	if build(0) {
		var res [][]int
		pos := 0
		for pos < m {
			cur := ans[pos]
			res = append(res, []int{cur.l + 1, cur.r + 1, cur.i + 1})
			pos += cur.Len()
		}
		return res
	}
	return nil
}

type Mark struct {
	l int
	r int
	i int
}

func (this Mark) Len() int {
	return this.r - this.l + 1
}

const MOD1 = 10000000000007
const MOD2 = 1000000000009


package main

import "fmt"

func main() {
	var n, m int
	fmt.Scan(&n, &m)
	arr := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&arr[i])
	}

	var fr, lr int
	var fc, lc int
	fr = -1
	lr = -1
	fc = -1
	lc = -1
	for i := 0; i < n; i++ {
		if rowBlack(arr, i) {
			if fr < 0 {
				fr = i
			}
			lr = i
		}
	}
	for i := 0; i < m; i++ {
		if columnBlack(arr, i) {
			if fc < 0 {
				fc = i
			}
			lc = i
		}
	}
	fmt.Println(fr+(lr-fr)/2+1, fc+(lc-fc)/2+1)
}

func rowBlack(arr []string, r int) bool {
	for _, ch := range arr[r] {
		if ch == 'B' {
			return true
		}
	}
	return false
}

func columnBlack(arr []string, c int) bool {
	for _, row := range arr {
		if row[c] == 'B' {
			return true
		}
	}
	return false
}

package main

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

func main() {
	r := bufio.NewReaderSize(os.Stdin, 1024*1024)
	dims := readInts(r, 2)
	n := dims[0]
	m := dims[1]

	for i := 0; i < n; i++ {
		row := readString(r, m)
		for q, ch := range row {
			if ch == 'B' {
				l := 0
				for ; i + l < n - 1; {
					if next := readString(r, m); next[q] == 'B' {
						l++
					} else {
						break
					}
				}
				m := l/2
				fmt.Println(i+m+1,q+m+1)
				return
			}
		}
	}
}

func readInts(reader *bufio.Reader, n int) []int {
	tmpInts := strings.Split(readLine(reader), " ")

	var result []int
	for i := 0; i < n; i++ {
		tmpInt, err := strconv.ParseInt(tmpInts[i], 10, 64)
		checkError(err)
		result = append(result, int(tmpInt))
	}
	return result
}

func readString(reader *bufio.Reader, l int) string {
	sTemp := readLine(reader)
	if len(sTemp) < l {
		l = len(sTemp)
	}
	return sTemp[:l]
}

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

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

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

package main

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

func main() {
    var height, width, lines, square_left  int
    var line string

    square_top:=0
    square_width:=0

    fmt.Scanf("%d %d",&height, &width)
    for{
        lines,_=fmt.Scan(&line)
        if lines==0 {break}
        square_top++;

        for i:=0;i<width;i++{
            if line[i]=='B' {
                square_left=i;
                for x:=i;x<width;x++{
                   if line[x]=='B'{
                      square_width++
                   } else {
                       break
                   }
                }
                break
            }
        }
        if square_width>0 {
            halfwidth:=(square_width-1)/2
            fmt.Printf("%d %d",square_top+halfwidth,square_left+halfwidth+1)
            break
        }
    }
}

package main

import (
	"fmt"
)

type Point struct {
	x int
	y int
}

func main() {

	var n int
	var m int

	fmt.Scan(&n)
	fmt.Scan(&m)

	var s string
	var p1 Point
	var p2 Point
	p1.x = -1
	p1.y = -1
	p2.x = -1
	p2.y = -1
	for i := 0; i < n; i++ {
		fmt.Scan(&s)

		for j, sub := range s {

			if string(sub) == "B" && p1.x == -1 {

				p1.x = i + 1
				p1.y = j + 1

			} else if string(sub) == "B" {

				p2.x = i + 1
				p2.y = j + 1
			}
		}
	}

	if p2.x == -1 || p2.y == -1 {
		fmt.Println(p1.x, p1.y)
	} else {

		fmt.Println(((p1.x+p2.x))/2, ((p1.y+p2.y))/2)
	}

}

package main

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

func main()  {

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

	var n, m int
	fmt.Fscan(in, &n, &m)

	ch := make([][]byte, n)
	for i := 0; i < n; i++ {
		ch[i] = make([]byte, m)
	}

	for i := 0; i < n; i++ {
		var s string
		fmt.Fscan(in, &s)
		for j := 0; j < m; j++ {
			ch[i][j] = s[j]
		}
	}

	xLT, yLT, xRB, yRB := n - 1, m - 1, 0, 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if ch[i][j] == 'B' {
				if xLT > i {
					xLT = i
				}
				if yLT > j {
					yLT = j
				}
				if xRB < i {
					xRB = i
				}
				if yRB < j {
					yRB = j
				}
			}
		}
	}

	fmt.Fprintln(out, xLT + (xRB - xLT) / 2 + 1, yLT + (yRB - yLT) / 2 + 1)

}

package main

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

func main() {
    var height, width, lines, square_left  int
    var line string

    square_top:=0
    square_width:=0

    fmt.Scanf("%d %d",&height, &width)
    for{
        lines,_=fmt.Scan(&line)
        if lines==0 {break}

        for i:=0;i<width;i++{
            if line[i]=='B' {
                square_left=i;
                for x:=i;x<width;x++{
                   if line[i]=='B'{
                      square_width++
                   } else {
                       break
                   }
                }
                break
            }
        }

        if square_width>0 {
            fmt.Printf("%d %d",square_top+square_width/2,square_left+square_width/2)
            break
        }
        square_top++;
    }
}

package main

import "fmt"

func main() {
	var n, m int
	fmt.Scan(&n, &m)
	arr := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&arr[i])
		fmt.Println(arr[i])
	}

	var fr, lr int
	var fc, lc int
	fr = -1
	lr = -1
	fc = -1
	lc = -1
	for i := 0; i < n; i++ {
		if rowBlack(arr, i) {
			if fr < 0 {
				fr = i
			}
			lr = i
		}
	}
	for i := 0; i < m; i++ {
		if columnBlack(arr, i) {
			if fc < 0 {
				fc = i
			}
			lc = i
		}
	}
	fmt.Println(fr+(lr-fr)/2+1, fc+(lc-fc)/2+1)
}

func rowBlack(arr []string, r int) bool {
	for _, ch := range arr[r] {
		if ch == 'B' {
			return true
		}
	}
	return false
}

func columnBlack(arr []string, c int) bool {
	for _, row := range arr {
		if row[c] == 'B' {
			return true
		}
	}
	return false
}


package main

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

func main() {
	var n, x int
	fmt.Scanf("%d %d\n", &n, &x)
	counter := map[int]int{}
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for ; n > 0 && scanner.Scan(); n-- {
		tmp, _ := strconv.Atoi(scanner.Text())
		if tmp >= x {
			continue
		}
		counter[tmp]++
		for counter[tmp] > tmp {
			q := counter[tmp] / (tmp + 1)
			if r := counter[tmp] % (tmp + 1); r > 0 {
				counter[tmp] = r
			} else {
				delete(counter, tmp)
			}
			if tmp+1 < x {
				counter[tmp+1] += q
				tmp++
			}
		}
	}
	if len(counter) > 0 {
		fmt.Println("No")
	} else {
		fmt.Println("Yes")
	}
}

package main

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

func main() {
	var n, x int
	fmt.Scanf("%d %d\n", &n, &x)
	counter := map[int]int{}
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for ; n > 0 && scanner.Scan(); n-- {
		if tmp, _ := strconv.Atoi(scanner.Text()); tmp < x {
			counter[tmp]++
			for counter[tmp] > tmp {
				quotient := counter[tmp] / (tmp + 1)
				remainder := counter[tmp] % (tmp + 1)
				if remainder == 0 {
					delete(counter, tmp)
				} else {
					counter[tmp] = remainder
				}
				if tmp+1 < x {
					counter[tmp+1] += quotient
					tmp++
				}
			}
		}
	}
	if len(counter) > 0 {
		fmt.Println("No")
		return
	}
	fmt.Println("Yes")
}

package main

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

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

	var n, x int
	fmt.Fscan(r, &n, &x)
	cnt := make([]int, x+1)

	for i := 0; i < n; i++ {
		var t int
		fmt.Fscan(r, &t)
		cnt[t]++
	}

	res := "YES"
	for i := 1; i < x; i++ {
		cnt[i+1] += cnt[i]/(i+1)
		cnt[i] %= (i+1)

		if cnt[i] > 0 {
			res = "NO"
			break
		}
	}

	fmt.Fprintln(w, res)
}

package main

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

const mod = 998244353

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n, k int
	Fscan(in, &n, &k)
	arr := make([]int, k+1)
	for i := 0; i < n; i++ {
		var idx int
		Fscan(in, &idx)
		arr[idx]++
	}
	for i := 1; i < len(arr)-1; i++ {
		if arr[i]%(i+1) != 0 {
			Fprintln(out, "No")
			return
		}
		arr[i+1] += arr[i] / (i + 1)
	}
	Fprintln(out, "Yes")
}

func max(i, j int) int {
	if i > j {
		return i
	}
	return j
}
func lcm(a, b int) int {
	return a * b / gcd(a, b)
}
func gcd(a, b int) int {
	if a%b == 0 {
		return b
	}
	return gcd(b, a%b)
}

func main() {

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

// 78 ms	8500 KB
package main

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

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

	var n, x int
	fmt.Fscanf(in, "%d %d\n", &n, &x)
	counter := map[int]int{}
	for ; n > 0; n-- {
		var tmp int
		fmt.Fscan(in, &tmp)
		if tmp < x {
			counter[tmp]++
			for counter[tmp] > tmp {
				quotient := counter[tmp] / (tmp + 1)
				remainder := counter[tmp] % (tmp + 1)
				if remainder == 0 {
					delete(counter, tmp)
				} else {
					counter[tmp] = remainder
				}
				if tmp+1 < x {
					counter[tmp+1] += quotient
					tmp++
				}
			}
		}
	}
	if len(counter) > 0 {
		fmt.Fprintln(out, "No")
	} else {
		fmt.Fprintln(out, "Yes")
	}
}

// 109 ms	22000 KB
package main

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

func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n, x, tmp int
	fmt.Fscanf(in, "%d %d\n", &n, &x)
	counter := map[int]int{}
	for ; n > 0; n-- {
		fmt.Fscan(in, &tmp)
		if tmp >= x {
			continue
		}
		counter[tmp]++
		for counter[tmp] > tmp {
			q := counter[tmp] / (tmp + 1)
			if r := counter[tmp] % (tmp + 1); r > 0 {
				counter[tmp] = r
			} else {
				delete(counter, tmp)
			}
			if tmp+1 < x {
				counter[tmp+1] += q
				tmp++
			}
		}
	}
	if len(counter) > 0 {
		fmt.Println("No")
	} else {
		fmt.Println("Yes")
	}
}

package main

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

func main() {
	var n, x int
	fmt.Scanf("%d %d\n", &n, &x)
	counter := make([]int, x)
	distinct := 0
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for ; n > 0 && scanner.Scan(); n-- {
		tmp, _ := strconv.Atoi(scanner.Text())
		if tmp >= x {
			continue
		}
		if counter[tmp] == 0 {
			distinct++
		}
		counter[tmp]++
		for counter[tmp] > tmp {
			increase := counter[tmp] / (tmp + 1)
			counter[tmp] = counter[tmp] % (tmp + 1)
			if counter[tmp] == 0 {
				distinct--
			}
			tmp++
			if tmp < x {
				if counter[tmp] == 0 {
					distinct++
				}
				counter[tmp] += increase
			} else {
				break
			}
		}
	}
	if distinct > 0 {
		fmt.Println("No")
		return
	}
	fmt.Println("Yes")
}

package main

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

func main() {
	var n, x int
	fmt.Scanf("%d %d\n", &n, &x)
	counter := make([]int, x)
	distinct := 0
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for ; n > 0 && scanner.Scan(); n-- {
		tmp, _ := strconv.Atoi(scanner.Text())
		if tmp >= x {
			continue
		}
		if counter[tmp] == 0 {
			distinct++
		}
		counter[tmp]++
		for counter[tmp] > tmp {
			increase := counter[tmp] / (tmp + 1)
			counter[tmp] = counter[tmp] % (tmp + 1)
			if counter[tmp] == 0 {
				distinct--
			}
			tmp++
			if tmp < x {
				counter[tmp] += increase
			} else {
				break
			}
		}
	}
	if distinct > 0 {
		fmt.Println("No")
		return
	}
	fmt.Println("Yes")
}

package main

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

func main() {
	var tmp, x int
	fmt.Scanf("%d %d\n", &tmp, &x)
	counter := map[int]int{}
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	nums := strings.Fields(scanner.Text())
	for i := range nums {
		tmp, _ = strconv.Atoi(nums[i])
		if tmp >= x {
			continue
		}
		counter[tmp]++
		for counter[tmp] >= tmp+1 {
			quotient := counter[tmp] / (tmp + 1)
			remainder := counter[tmp] % (tmp + 1)
			if remainder == 0 {
				delete(counter, tmp)
			} else {
				counter[tmp] = remainder
			}
			if tmp+1 < x {
				counter[tmp+1] += quotient
				tmp++
			}
		}
	}
	if len(counter) > 0 {
		fmt.Println("No")
		return
	}
	fmt.Println("Yes")
}

package main

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

func main() {
	var tmp, x int
	fmt.Scanf("%d %d\n", &tmp, &x)
	counter := map[int]int{}
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	nums := strings.Fields(scanner.Text())
	for i := range nums {
		tmp, _ = strconv.Atoi(nums[i])
		if tmp >= x {
			continue
		}
		counter[tmp]++
		for counter[tmp] >= tmp+1 {
			increase := counter[tmp] / (tmp + 1)
			counter[tmp] = counter[tmp] % (tmp + 1)
			tmp++
			if tmp < x {
				counter[tmp] += increase
			}
		}
	}
	for k, v := range counter {
		if v > 0 && k < x {
			fmt.Println("No")
			return
		}
	}
	fmt.Println("Yes")
}

package main

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

func main() {
	var tmp, x int
	fmt.Scanf("%d %d\n", &tmp, &x)
	counter := map[int]int{}
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	nums := strings.Fields(scanner.Text())

	for i := range nums {
		tmp, _ = strconv.Atoi(nums[i])
		if tmp >= x {
			continue
		}
		if _, ok := counter[tmp]; !ok {
			counter[tmp] = 1
		} else {
			counter[tmp]++
			j := tmp
			for counter[j] >= j+1 {
				increase := counter[j] / (j + 1)
				counter[j] = counter[j] % (j + 1)
				if counter[j] == 0 {
					delete(counter, j)
				}
				j++
				if j < x {
					counter[j] += increase
				}
			}
		}
	}
	if len(counter) > 0 {
		fmt.Println("No")
		return
	}
	fmt.Println("Yes")
}

package main

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

func main() {
	var num, x int
	fmt.Scanf("%d %d\n", &num, &x)
	counter := make([]int, 500001)
	distinct := 0
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	line := scanner.Text()
	nums := strings.Fields(line)

	for i := range nums {
		tmp, _ := strconv.Atoi(nums[i])
		if tmp >= x {
			continue
		}
		if counter[tmp] == 0 {
			distinct++
		}
		counter[tmp]++
		for counter[tmp] >= tmp+1 {
			increase := counter[tmp] / (tmp + 1)
			counter[tmp] = counter[tmp] % (tmp + 1)
			if counter[tmp] == 0 {
				distinct--
			}
			tmp++
			if tmp < x {
				counter[tmp] += increase
			}
		}
	}
	if distinct > 0 {
		fmt.Println("No")
		return
	}
	fmt.Println("Yes")
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var num, x int
	fmt.Scanf("%d %d\n", &num, &x)
	a := make([]int, num)
	for i := 0; i < num; i++ {
		fmt.Scanf("%d", &a[i])
	}
	sort.Ints(a)

	// combine
	for i := 0; i < num; {
		j := i
		for ; j < num-1 && a[j] == a[j+1]; j++ {
		}
		if i != j {
			nextI := a[i] + i
			if j-i+1 >= a[i]+1 {
				// reset
				for k := i; k < i+a[j]; k++ {
					a[k] = 0
				}
				a[j]++
			}
			i = nextI
		} else {
			i++
		}
		fmt.Println(a)
	}

	count := 0
	for _, n := range a {
		if n < x && n > 0 {
			count++
			break
		}
	}
	if count > 0 {
		fmt.Println("No")
	} else {
		fmt.Println("Yes")
	}
}


package main

import (
	"fmt"
)

func main() {
	var n, pos int
	var str string
	var charX, charR byte
	var set, valid bool

	fmt.Scan(&n)

	pos = 0
	set = false
	valid = true
	for i:=0; i<n; i++ {
		fmt.Scan(&str)

		if !set {
			charX = str[0]
			charR = str[1]
			if charX == charR {
				valid = false
			}
			set = true
		}

		for j:=0; j<len(str); j++ {
			if (j==pos || j == (len(str)-pos-1)) && str[j] != charX {
				valid = false
			}
			if (j != pos && j != (len(str)-pos-1)) && str[j] != charR {
				valid = false
			}
		}

		pos++
	}

	if valid {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

package main

import "fmt"

func main() {
    var n int
    var s [305]string
    var x,y byte
    fmt.Scan(&n)
    for i := 0; i < n; i++ {
        fmt.Scan(&s[i])
    }
    ans := true
    for i := 0; (i < n) && ans; i++ {
        for j := 0; j < n; j++ {
            if (i == j) || (i == (n-j-1)) {
                if x != 0 {
                    if x != s[i][j] { ans = false; break }
                } else {
                    x = s[i][j]
                }
            } else {
                if y != 0 {
                    if y != s[i][j] { ans = false; break }
                } else {
                    y = s[i][j]
                }
            }
        }
    }
    if ans && (x != y) { fmt.Println("YES") } else { fmt.Println("NO") }
}

package main

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

const maxn = 310

var s [maxn]string

func main() {
    scanner.Split(bufio.ScanWords)
    n := scanner.NextInt()
    ans := true
    for i := 0; i < n; i++ {
        s[i] = scanner.Next()
        for j := 0; j < n; j++ {
            if i == j || i+j == n-1 {
                if s[i][j] != s[0][0] {
                    ans = false
                }
            } else {
                if s[i][j] == s[0][0] {
                    ans = false
                }
                if s[i][j] != s[0][1] {
                    ans = false
                }
            }
        }
    }
    if ans {
        fmt.Println("YES")
    } else {
        fmt.Println("NO")
    }
}

type Scanner struct {
    *bufio.Scanner
}

var scanner = Scanner{bufio.NewScanner(os.Stdin)}

func (self *Scanner) Next() string {
    if !self.Scan() {
        panic(self.Err())
    }
    return self.Text()
}

func (self *Scanner) NextInt() int {
    x, err := strconv.Atoi(self.Next())
    if err != nil {
        panic(err)
    }
    return x
}

func (self *Scanner) NextInt64() int64 {
    x, err := strconv.ParseInt(self.Next(), 10, 64)
    if err != nil {
        panic(err)
    }
    return x
}

func (self *Scanner) NextDouble() float64 {
    x, err := strconv.ParseFloat(self.Next(), 64)
    if err != nil {
        panic(err)
    }
    return x
}

// cf404a project main.go
package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	var s []string
	for i := 0; i < n; i++ {
		var t string
		fmt.Scan(&t)
		s = append(s, t)
	}
	x := s[0][0]
	o := s[0][1]
	flag := x != o
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if (i == j || i == n-j-1) && s[i][j] != x {

				flag = false
				break
			}
			if i != j && i != n-j-1 && s[i][j] != o {
				flag = false
				break
			}

		}
		if !flag {
			break
		}
	}
	if flag {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

package main

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

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

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

	n := nextInt()

	startIndex, endIndex := 0, n-1
	line := next()
	var xLetter, otherLetter uint8 = line[0], line[1]

	if xLetter == otherLetter {
		println("NO")
		return
	}

	if line[startIndex]!=xLetter || line[startIndex] != line[endIndex] {
		println("NO")
		return
	}

	for j:=0; j<n; j++ {
		if j!=startIndex && j!=endIndex && line[j]!=otherLetter {
			println("NO")
			return
		}
	}
	startIndex++
	endIndex--

	for i:=1; i<n; i++ {
		line = next()

		if line[startIndex]!=xLetter || line[startIndex] != line[endIndex] {
			println("NO")
			return
		}

		for j:=0; j<n; j++ {
			if j!=startIndex && j!=endIndex && line[j]!=otherLetter {
				println("NO")
				return
			}
		}
		startIndex++
		endIndex--
	}

	println("YES")
}

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

func nextInt() int {
	n, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return n
}

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

func print(a ...interface{}) {
	fmt.Fprint(writer, a...)
}

package main

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

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

	var is_x bool = true
	var diagonal rune
	var blank rune

	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < n; i++ {
		for j := 0; j <= n; j++ {
			if c, _, err := reader.ReadRune(); err != nil {
				if err == io.EOF {
					break
				} else {
					return
				}
			} else {
				if '\n' == c {
					continue
				} else if '\r' == c { // Read the following '\n'
					if _, _, err := reader.ReadRune(); err != nil {
						if err == io.EOF {
							break
						} else {
							return
						}
					}
					continue
				}

				if i == 0 && j == 0 {
					diagonal = c
				} else if i == 0 && j == 1 {
					blank = c
					if blank == diagonal {
						is_x = false
					}
				} else {
					if j == i || j == n-i-1 {
						if c != diagonal {
							is_x = false
						}
					} else {
						if c != blank {
							is_x = false
						}
					}
				}
			}
		}
	}

	if is_x {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

package main

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

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

	var is_x bool = true
	var diagonal byte
	var blank byte

	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < n; i++ {
		if s, err := reader.ReadString('\n'); err != nil {
			if err == io.EOF {
				break
			} else {
				return
			}
		} else {
			for j := 0; j < n; j++ {
				if i == 0 && j == 0 {
					diagonal = s[j]
				} else if i == 0 && j == 1 {
					blank = s[j]
					if blank == diagonal {
						is_x = false
					}
				} else {
					if j == i || j == n-i-1 {
						if s[j] != diagonal {
							is_x = false
						}
					} else {
						if s[j] != blank {
							is_x = false
						}
					}
				}
			}
		}
	}

	if is_x {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

package main

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

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

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

	n := nextInt()

	startIndex, endIndex := 0, n-1
	var otherLetter uint8

	for i:=0; i<n; i++ {
		line := next()

		if line[startIndex] != line[endIndex] {
			println("NO")
			return
		}

		if i==0 {
			if line[0] == line[1] {
				println("NO")
				return
			}

			otherLetter = line[1]
		}

		for j:=0; j<n; j++ {
			if j!=startIndex && j!=endIndex && line[j]!=otherLetter {
				println("NO")
				return
			}
		}
		startIndex++
		endIndex--
	}

	println("YES")
}

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

func nextInt() int {
	n, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return n
}

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

func print(a ...interface{}) {
	fmt.Fprint(writer, a...)
}

package main

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

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

	var is_x bool = true
	var diagonal rune
	var blank rune

	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < n; i++ {
		for j := 0; j <= n; j++ {
			if c, _, err := reader.ReadRune(); err != nil {
				if err == io.EOF {
					break
				} else {
					return
				}
			} else {
				if '\n' == c {
					continue
				} else if '\r' == c { // Read the following '\n'
					if _, _, err := reader.ReadRune(); err != nil {
						if err == io.EOF {
							break
						} else {
							return
						}
					}
					continue
				}

				if i == 0 && j == 0 {
					diagonal = c
				} else if i == 0 && j == 1 {
					blank = c
				} else {
					if j == i || j == n-i-1 {
						if c != diagonal {
							is_x = false
						}
					} else {
						if c != blank {
							is_x = false
						}
					}
				}
			}
		}
	}

	if is_x {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

package main

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

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

	var is_x bool = true
	var diagonal rune
	var blank rune

	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < n; i++ {
		for j := 0; j <= n; j++ {
			if c, _, err := reader.ReadRune(); err != nil {
				if err == io.EOF {
					break
				} else {
					return
				}
			} else {
				if '\n' != c {
					if i == 0 && j == 0 {
						diagonal = c
					} else if i == 0 && j == 1 {
						blank = c
					} else {
						if j == i || j == n-i-1 {
							if c != diagonal {
								is_x = false
							}
						} else {
							if c != blank {
								is_x = false
							}
						}
					}
				}
			}
		}
	}

	if is_x {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

package main

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

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

	var is_x bool = true
	var diagonal rune
	var blank rune

	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < n; i++ {
		for j := 0; j <= n; j++ {
			if c, _, err := reader.ReadRune(); err != nil {
				if err == io.EOF {
					break
				} else {
					return
				}
			} else {
				if '\n' != c && '\r' != c {
					if i == 0 && j == 0 {
						diagonal = c
					} else if i == 0 && j == 1 {
						blank = c
					} else {
						if j == i || j == n-i-1 {
							if c != diagonal {
								is_x = false
							}
						} else {
							if c != blank {
								is_x = false
							}
						}
					}
				}
			}
		}
	}

	if is_x {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

package main

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

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

	var is_x bool = true
	var diagonal rune
	var blank rune

	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < n; i++ {
		for j := 0; j <= n; j++ {
			if c, _, err := reader.ReadRune(); err != nil {
				if err == io.EOF {
					break
				} else {
					return
				}
			} else {
				if '\n' == c {
					continue
				} else if '\r' == c { // Read the following '\n'
					if _, _, err := reader.ReadRune(); err != nil {
						if err == io.EOF {
							break
						} else {
							return
						}
					}
				}

				if i == 0 && j == 0 {
					diagonal = c
				} else if i == 0 && j == 1 {
					blank = c
				} else {
					if j == i || j == n-i-1 {
						if c != diagonal {
							is_x = false
						}
					} else {
						if c != blank {
							is_x = false
						}
					}
				}
			}
		}
	}

	if is_x {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

package main

import "fmt"

func main() {
    var n int
    var s [305]string
    var x,y byte
    fmt.Scan(&n)
    for i := 0; i < n; i++ {
        fmt.Scan(&s[i])
    }
    ans := true
    for i := 0; (i < n) && ans; i++ {
        for j := 0; j < n; j++ {
            if (i == j) || (i == (n-j-1)) {
                if x != 0 {
                    if x != s[i][j] { ans = false; break }
                } else {
                    x = s[i][j]
                }
            } else {
                if y != 0 {
                    if y != s[i][j] { ans = false; break }
                } else {
                    y = s[i][j]
                }
            }
        }
    }
    if ans { fmt.Println("YES") } else { fmt.Println("NO") }
}

// cf404a project main.go
package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	var s []string
	for i := 0; i < n; i++ {
		var t string
		fmt.Scan(&t)
		s = append(s, t)
	}
	x := s[0][0]
	o := s[0][1]
	flag := true
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if (i == j || i == n-j-1) && s[i][j] != x {

				flag = false
				break
			}
			if i != j && i != n-j-1 && s[i][j] != o {
				flag = false
				break
			}

		}
		if !flag {
			break
		}
	}
	if flag {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}


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 int
		var s int
		var k int
		fmt.Fscan(r, &n, &s, &k)
		blocked := make(map[int]bool)
		for i := 0; i < k; i++ {
			var tmp int
			fmt.Fscan(r, &tmp)
			blocked[tmp] = true
		}
		for i := 0; i <= n; i++ {
			_, ok1 := blocked[s-i]
			_, ok2 := blocked[s+i]
			if (!ok1 && s-i > 0) || (!ok2 && s+i <= n) {
				fmt.Fprintln(w, i)
				break
			}
		}
	}
}

package main

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

//=====I/O=====

var (
	sc = bufio.NewScanner(os.Stdin)
	wr = bufio.NewWriter(os.Stdout)
)

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

func scanInt64() int64 {
	sc.Scan()
	a,_ := strconv.ParseInt(sc.Text(),10,64)
	return a
}

func scanFloat64() float64 {
	sc.Scan()
	a,_ := strconv.Atoi(sc.Text())
	return float64(a)
}

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

func scanText() string {
	sc.Scan()
	return sc.Text()
}

func printInts(a ...int) {
	for i, e := range a {
		fmt.Fprint(wr, e)
		if i != len(a)-1 { fmt.Fprint(wr, " ") }
	}
	fmt.Fprintln(wr)
	wr.Flush()
}

//•*¨*•.¸¸♪func•*¨*•.¸¸♪

//•*¨*•.¸¸♪main•*¨*•.¸¸♪

func main() {
	sc.Split(bufio.ScanWords)
	// sc.Buffer(make([]byte, 10000), 100000000)

	t := scanInt()

	for i := 0; i < t; i++ {
		n := scanInt()
		s := scanInt()-1
		k := scanInt()

		f := make(map[int]bool, n)
		for i := 0; i < k; i++ {
			a := scanInt()-1
			f[a] = true
		}

		// fmt.Println(f,s)

		p := 0
		for (s+p>=n||f[s+p]) && (s-p<0||f[s-p]) {
			p++
		}
		fmt.Println(p)
	}
}

package main

import "fmt"

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var n, s, k int
		fmt.Scan(&n, &s, &k)
		closed := make([]bool, 2000+100) // 上下1000階分調べれば十分
		now := len(closed) / 2           // 真ん中あたりにいるとする
		for j := 0; j < k; j++ {
			var v int
			fmt.Scan(&v) // v階のレストランは閉じている
			// 自分のフロアから見てどれだけ離れいてるか？
			d := v - s
			if 0 <= now+d && now+d < len(closed) {
				closed[now+d] = true
			}
		}
		// fmt.Println("n", n, "s", s, "k", k, "closed", closed)
		const inf = int(1e9) + 1
		mind := inf
		for j := 0; j < len(closed); j++ {
			if !closed[j] && 1 <= j-now+s && j-now+s <= n { // j階は空いている
				mind = min(mind, abs(j-now))
			}
		}
		fmt.Println(mind)
	}
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

package main

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

type Int64 []int64

func (u Int64) Len() int { return len(u) }

func (u Int64) Less(i, j int) bool { return u[i] < u[j] }

func (u Int64) Swap(i, j int) {
	u[i], u[j] = u[j], u[i]
}

func binSearch(a []int64, lo, hi int, key int64) bool {
	if lo > hi {
		return false
	}
	mid := lo + (hi-lo)/2
	if a[mid] == key {
		return true
	}
	if a[mid] > key {
		return binSearch(a, lo, mid-1, key)
	}
	return binSearch(a, mid+1, hi, key)
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var t, s, k int
	fmt.Fscanln(reader, &t)
	var n int64
	for i := 0; i < t; i++ {
		fmt.Fscanln(reader, &n, &s, &k)
		not_av := make([]int64, k)
		for j := 0; j < k-1; j++ {
			fmt.Fscanf(reader, "%d ", &not_av[j])
		}
		fmt.Fscanf(reader, "%d\n", &not_av[k-1])
		sort.Sort(Int64(not_av))
		if !binSearch(not_av, 0, k-1, int64(s)) {
			fmt.Fprintln(writer, 0)
			continue
		}
		ji := int64(s - 1)
		jj := int64(s + 1)
		for {
			if ji >= 1 && !binSearch(not_av, 0, k-1, ji) {
				fmt.Fprintln(writer, int64(s)-ji)
				break
			}
			if jj <= n && !binSearch(not_av, 0, k-1, jj) {
				fmt.Fprintln(writer, jj-int64(s))
				break
			}
			ji -= 1
			jj += 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 int
		var s int
		var k int
		fmt.Fscan(r, &n, &s, &k)
		blocked := make(map[int]bool)
		for i := 0; i < k; i++ {
			var tmp int
			fmt.Fscan(r, &tmp)
			blocked[tmp] = true
		}
		for i := 0; i <= n; i++ {
			_, ok1 := blocked[s-i]
			_, ok2 := blocked[s+i]
			if (!ok1 && s-i > 0) || (!ok2 && s+i <= n) {
				fmt.Println(i)
				break
			}
		}
	}
}

package main

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

const (
	MAX_INT int64 = math.MaxInt64
	MIN_INT int64 = math.MinInt64
)

func main() {
	for t := ScanInt64(); t > 0; t-- {
		n, s, k := ScanInt64(), ScanInt64(), ScanInt64()
		a := ScanInt64Slice(int(k))

		m := make(map[int64]bool)
		for i := 0; i < int(k); i++ {
			m[a[i]] = true
		}

		minDiff := MAX_INT

		highFloor := MAX_INT
		lowFloor := MIN_INT
		for i := int64(0); s+i <= n; i++ {
			if _, ok := m[s+i]; !ok {
				highFloor = s + i
				break
			}
		}
		for i := int64(0); s-i > 0; i++ {
			if _, ok := m[s-i]; !ok {
				lowFloor = s - i
				break
			}
		}
		minDiff = Min(abs(s-lowFloor), abs(highFloor-s))
		fmt.Println(minDiff)
	}
	close(out)
	outWg.Wait()
}

func abs(a int64) int64 {
	if a >= 0 {
		return a
	}
	return -a
}
func contains(a []int64, num int64) bool {
	for i := 0; i < len(a); i++ {
		if a[i] == num {
			return true
		}
	}
	return false
}

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

var out chan string
var in *bufio.Scanner
var outWg *sync.WaitGroup

func init() {
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(2e+5))
	in.Split(bufio.ScanWords)

	out = make(chan string, 16)
	outWg = &sync.WaitGroup{}
	outWg.Add(1)

	writer := bufio.NewWriterSize(os.Stdout, int(2e+5))
	go func(writer *bufio.Writer) {
		defer outWg.Done()
		defer writer.Flush()

		for line := range out {
			writer.WriteString(line + "\n")
		}
	}(writer)
}

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

func ScanStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}

func ScanInt64() int64 {
	intStr := ScanWord()
	i, err := strconv.ParseInt(intStr, 10, 64)
	if err != nil {
		panic(err)
	}
	return i
}

func ScanInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

/*********************** Utils ***********************/

func Max(args ...int64) int64 {
	maxVal := MIN_INT
	for _, v := range args {
		if v > maxVal {
			maxVal = v
		}
	}
	return maxVal
}

func Min(args ...int64) int64 {
	minVal := MAX_INT
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
	}
	return minVal
}

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 iToS(v int64) string {
	return strconv.FormatInt(v, 10)
}

package main

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

var reader *bufio.Reader=bufio.NewReader(os.Stdin)
var writer *bufio.Writer=bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}){fmt.Fprintf(writer,f,a...)}
func scanf(f string,a...interface{}){fmt.Fscanf(reader,f,a...)}

var set = make(map[int]bool)
var n int

func eat(x int)bool{
	if x>0 && x<=n{
		if _,fd := set[x];!fd{
			return true
		}
	}
	return false
}

func main(){
	defer writer.Flush()
	var t,s,k,a int
	scanf("%d\n",&t)
	for cas:=1;cas<=t;cas++{
		scanf("%d%d%d\n",&n,&s,&k)
		set = make(map[int]bool)
		for i:=0;i<k;i++{
			scanf("%d",&a)
			set[a]=true
		}
		scanf("\n")
		for d:=0;d<n;d++{
			if eat(s+d) || eat(s-d) {
				printf("%d\n",d)
				break
			}
		}
	}
}

package main

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

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

	T := readInts(reader)[0]
	// fmt.Println(T)
	for t := 0; t < T; t++ {
		inp := readInts(reader)
		N, S, K := inp[0], inp[1], inp[2]

		CS := readInts(reader)
		mmap := make(map[int]bool, K)
		for _, cs := range CS {
			mmap[cs] = true
		}

		x := findLeft(S, mmap)
		y := findRight(S, N, mmap)

		fmt.Println(minInt(absInt(S-x), absInt(y-S)))
	}
}

func findLeft(x int, mmap map[int]bool) int {
	for i := x; i >= 1; i-- {
		if _, ok := mmap[i]; !ok {
			return i
		}
	}

	return math.MaxInt32
}

func findRight(x, N int, mmap map[int]bool) int {
	for i := x; i <= N; i++ {
		if _, ok := mmap[i]; !ok {
			return i
		}
	}

	return math.MaxInt32
}

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

	return y
}

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

	return x
}

func readInts(reader *bufio.Reader) []int {
	// fmt.Println("readInts being called.....")
	text, err := reader.ReadString('\n')
	if err != nil {
		fmt.Println(err.Error())
	}
	// fmt.Println("~~~~~~", text)
	inputs := strings.Split(strings.TrimSuffix(strings.TrimSuffix(text, "\n"), "\r"), " ")

	temp := make([]int, len(inputs))
	for i := 0; i < len(inputs); i++ {
		temp[i], err = strconv.Atoi(inputs[i])
		if err != nil {
			fmt.Println(err.Error())
		}
	}

	// fmt.Println(temp)
	return temp
}

package main

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

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

	var t int
	fmt.Fscan(r, &t)
	ans := make([]int, t)
	for i := 0; i < t; i++ {
		var n, s, a int
		fmt.Fscan(r, &n, &s, &a)
		m := make([]int, a)
		for j := 0; j < a; j++ {
			fmt.Fscan(r, &m[j])
		}
		sort.Ints(m)
		p := sort.SearchInts(m, s)
		if p < a && m[p] == s {
			down := p
			up := p
			for ; down > 0; down-- {
				if m[down]-m[down-1] > 1 {
					break
				}
			}
			for ; up < a-1; up++ {
				if m[up+1]-m[up] > 1 {
					break
				}
			}
			l := n
			if m[up] < n {
				l = m[up] - s + 1
			}
			if m[down] > 1 && s-m[down]+1 < l {
				l = s - m[down] + 1
			}
			ans[i] = l
		} else {
			ans[i] = 0
		}

		// if p < a && m[p] != s {
		// 	ans[i] = "0"
		// } else {
		// 	flag := true
		// 	for j := 0; p-j > 0 || p+j < a-1; j++ {
		// 		if p-j > 0 && m[p-j]-m[p-j-1] > 1 {
		// 			ans[i] = strconv.Itoa(s - m[p-j])
		// 			flag = false
		// 			break
		// 		}
		// 		if p+j < a-1 && m[p+j+1]-m[p+j] > 1 {
		// 			ans[i] = strconv.Itoa(m[p+j] + 1 - s)
		// 			flag = false
		// 			break
		// 		}
		// 	}
		// 	if flag {

		// 		if m[0] > 1 && m[a-1] < n {
		// 			if s-m[0] < m[a-1]-s {
		// 				ans[i] = strconv.Itoa(s - m[0] + 1)
		// 			} else {
		// 				ans[i] = strconv.Itoa(m[a-1] - s + 1)
		// 			}
		// 		} else if m[0] > 1 {
		// 			ans[i] = strconv.Itoa(s - m[0] + 1)
		// 		} else if m[a-1] < n {
		// 			ans[i] = strconv.Itoa(m[a-1] - s + 1)
		// 		}
		// 	}
		//}
	}
	res := make([]string, t)
	for i, e := range ans {
		res[i] = strconv.Itoa(e)
	}
	fmt.Fprintln(w, strings.Join(res, "\n"))

}

package main

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

func main () {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, n, s, k, x int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n, &s, &k)
		a := make(map[int]bool, n)
		for j := 0; j < k; j++ {
			fmt.Fscan(in, &x)
			a[x - 1] = true
		}
		for j := 0; j <= n; j++ {
			if (s - j - 1 >= 0 && !a[s - j - 1]) || (s + j - 1 < n && !a[s + j - 1]) {
				fmt.Fprintln(out, j)
				break
			}
		}
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var t, s, k, ip, ji, jj int
	fmt.Fscanln(reader, &t)
	var n int64
	for i := 0; i < t; i++ {
		fmt.Fscanln(reader, &n, &s, &k)
		not_av := make([]bool, 1001)
		for j := 0; j < k-1; j++ {
			fmt.Fscanf(reader, "%d ", &ip)
			not_av[ip] = true
		}
		fmt.Fscanf(reader, "%d\n", &ip)
		not_av[ip] = true
		if !not_av[s] {
			fmt.Fprintln(writer, 0)
			continue
		}
		ji = s - 1
		jj = s + 1
		for ji >= 1 || jj <= 1000 {
			if ji >= 1 && !not_av[ji] {
				fmt.Fprintln(writer, s-ji)
				break
			}
			if jj <= 1000 && !not_av[jj] {
				fmt.Fprintln(writer, jj-s)
				break
			}
			ji -= 1
			jj += 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 int
		var s int
		var k int
		fmt.Fscan(r, &n, &s, &k)
		blocked := make(map[int]bool)
		for i := 0; i < k; i++ {
			var tmp int
			fmt.Fscan(r, &tmp)
			blocked[tmp] = true
		}
		for i := 0; i <= n; i++ {
			_, ok1 := blocked[s-i]
			_, ok2 := blocked[s+i]
			if (!ok1 && s-i > 0) || (!ok2 && s+i <= n) {
				fmt.Fprint(w, i)
				break
			}
		}
	}
}

package main

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

const (
   MAX_INT int64 = math.MaxInt64
   MIN_INT int64 = math.MinInt64
)

//func main() {
// n := ScanInt64()
// //𝐻𝑛=𝑙𝑛(𝑛)+0.5772156649
//
// hn := 0.5772156649 + math.Log2(float64(n))
// fmt.Println(hn)
//
// close(out)
// outWg.Wait()
//}

func main() {
   for t := ScanInt64(); t > 0; t-- {
      _, s1, k := ScanInt64(), ScanInt64(), ScanInt64()
      a := ScanInt64Slice(int(k))
      fmt.Println(markLand(s1, a))
   }
}

func markLand(inTheFloor int64, a []int64) int64 {
   if !contains(a, inTheFloor) {
      return 0
   }

   leftMax := make([]int64, len(a))
   for i := 1; i < len(a); i++ {
      leftMax[i] = Max(leftMax[i-1], a[i-1])
   }
   rightMin, element := MAX_INT, int64(0)
   for i := len(a) - 1; i >= 0; i-- {
      if (leftMax[i] < a[i] && rightMin > a[i]) {
         element = a[i]
         break
      }
      rightMin = Min(rightMin, a[i])
   }
   if element == 0 {
      return 0

   } else if element-1 == inTheFloor {
      return ((element + 1) - inTheFloor)
   } else {
      return (element - 1 - inTheFloor)

   }

}

func contains(a []int64, num int64) bool {
   for i := 0; i < len(a); i++ {
      if a[i] == num {
         return true
      }
   }
   return false
}

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

var in *bufio.Scanner

func init() {
   in = bufio.NewScanner(os.Stdin)
   in.Split(bufio.ScanWords)
}

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

func ScanStringSlice(n int) []string {
   s := make([]string, n)
   for i := 0; i < n; i++ {
      s[i] = ScanWord()
   }
   return s
}

func ScanInt64() int64 {
   intStr := ScanWord()
   i, err := strconv.ParseInt(intStr, 10, 64)
   if err != nil {
      panic(err)
   }
   return i
}

func ScanInt64Slice(n int) []int64 {
   arr := make([]int64, n)
   for i := 0; i < n; i++ {
      arr[i] = ScanInt64()
   }
   return arr
}

/*********************** Utils ***********************/

func Max(args ...int64) int64 {
   maxVal := MIN_INT
   for _, v := range args {
      if v > maxVal {
         maxVal = v
      }
   }
   return maxVal
}

func Min(args ...int64) int64 {
   minVal := MAX_INT
   for _, v := range args {
      if v < minVal {
         minVal = v
      }
   }
   return minVal
}

func s(val interface{}) string {
   return fmt.Sprintf("%v", val)
}

package main

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

var reader *bufio.Reader=bufio.NewReader(os.Stdin)
var writer *bufio.Writer=bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}){fmt.Fprintf(writer,f,a...)}
func scanf(f string,a...interface{}){fmt.Fscanf(reader,f,a...)}

var set = make(map[int]bool)

func eat(x int)bool{
	if x>0 {
		if _,fd := set[x];!fd{
			return true
		}
	}
	return false
}

func main(){
	defer writer.Flush()
	var t,n,s,k,a int
	scanf("%d\n",&t)
	for cas:=1;cas<=t;cas++{
		scanf("%d%d%d\n",&n,&s,&k)
		set = make(map[int]bool)
		for i:=0;i<k;i++{
			if i<k-1 {
				scanf("%d",&a)
			}else{
				scanf("%d\n",&a)
			}
			set[a]=true
		}
		for d:=0;d<n;d++{
			if eat(s+d) || eat(s-d) {
				printf("%d\n",d)
				break
			}
		}
	}
}

package main

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

func main() {
	T := readInts()[0]
	fmt.Println("test cases: ", T)
	for t := 0; t < T; t++ {
		inp := readInts()
		N, S, K := inp[0], inp[1], inp[2]

		CS := readInts()
		mmap := make(map[int]bool, K)
		for _, cs := range CS {
			mmap[cs] = true
		}

		x := findLeft(S, mmap)
		y := findRight(S, N, mmap)

		fmt.Println(minInt(absInt(S-x), absInt(y-S)))
	}
}

func findLeft(x int, mmap map[int]bool) int {
	for i := x; i >= 1; i-- {
		if _, ok := mmap[i]; !ok {
			return i
		}
	}

	return math.MaxInt32
}

func findRight(x, N int, mmap map[int]bool) int {
	for i := x; i <= N; i++ {
		if _, ok := mmap[i]; !ok {
			return i
		}
	}

	return math.MaxInt32
}

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

	return y
}

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

	return x
}

func readInts() []int {
	reader := bufio.NewReader(os.Stdin)
	text, _ := reader.ReadString('\n')
	inputs := strings.Split(strings.TrimSuffix(text, "\n"), " ")

	temp := make([]int, len(inputs))
	for i := 0; i < len(inputs); i++ {
		temp[i], _ = strconv.Atoi(inputs[i])
	}

	return temp
}

package main

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

func main() {
	T := readInts()[0]
	for t := 0; t < T; t++ {
		inp := readInts()
		N, S, K := inp[0], inp[1], inp[2]

		CS := readInts()
		mmap := make(map[int]bool, K)
		for _, cs := range CS {
			mmap[cs] = true
		}

		x := findLeft(S, mmap)
		y := findRight(S, N, mmap)

		fmt.Println(minInt(absInt(S-x), absInt(y-S)))
	}
}

func findLeft(x int, mmap map[int]bool) int {
	for i := x; i >= 1; i-- {
		if _, ok := mmap[i]; !ok {
			return i
		}
	}

	return math.MaxInt32
}

func findRight(x, N int, mmap map[int]bool) int {
	for i := x; i <= N; i++ {
		if _, ok := mmap[i]; !ok {
			return i
		}
	}

	return math.MaxInt32
}

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

	return y
}

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

	return x
}

func readInts() []int {
	reader := bufio.NewReader(os.Stdin)
	text, _ := reader.ReadString('\n')
	inputs := strings.Split(strings.TrimSuffix(text, "\n"), " ")

	temp := make([]int, len(inputs))
	for i := 0; i < len(inputs); i++ {
		temp[i], _ = strconv.Atoi(inputs[i])
	}

	return temp
}

package main

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

func main() {
	T := readInts()[0]
	fmt.Println(T)
	for t := 0; t < T; t++ {
		inp := readInts()
		N, S, K := inp[0], inp[1], inp[2]

		CS := readInts()
		mmap := make(map[int]bool, K)
		for _, cs := range CS {
			mmap[cs] = true
		}

		x := findLeft(S, mmap)
		y := findRight(S, N, mmap)

		fmt.Println(minInt(absInt(S-x), absInt(y-S)))
	}
}

func findLeft(x int, mmap map[int]bool) int {
	for i := x; i >= 1; i-- {
		if _, ok := mmap[i]; !ok {
			return i
		}
	}

	return math.MaxInt32
}

func findRight(x, N int, mmap map[int]bool) int {
	for i := x; i <= N; i++ {
		if _, ok := mmap[i]; !ok {
			return i
		}
	}

	return math.MaxInt32
}

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

	return y
}

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

	return x
}

func readInts() []int {
	reader := bufio.NewReader(os.Stdin)
	text, _ := reader.ReadString('\n')
	inputs := strings.Split(strings.TrimSuffix(text, "\n"), " ")

	temp := make([]int, len(inputs))
	for i := 0; i < len(inputs); i++ {
		temp[i], _ = strconv.Atoi(inputs[i])
	}

	return temp
}

package main

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

func main() {
	T := readInts()[0]
	for t := 0; t < T; t++ {
		inp := readInts()
		N, S, K := inp[0], inp[1], inp[2]

		CS := readInts()
		mmap := make(map[int]bool, K)
		for _, cs := range CS {
			mmap[cs] = true
		}

		x := findLeft(S, mmap)
		y := findRight(S, N, mmap)

		fmt.Println(minInt(S-x, y-S))
	}
}

func findLeft(x int, mmap map[int]bool) int {
	for i := x; i >= 1; i-- {
		if _, ok := mmap[i]; !ok {
			return i
		}
	}

	return math.MaxInt32
}

func findRight(x, N int, mmap map[int]bool) int {
	for i := x; i <= N; i++ {
		if _, ok := mmap[i]; !ok {
			return i
		}
	}

	return math.MaxInt32
}

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

	return y
}

func readInts() []int {
	reader := bufio.NewReader(os.Stdin)
	text, _ := reader.ReadString('\n')
	inputs := strings.Split(text, " ")
	temp := make([]int, len(inputs))
	for i := 0; i < len(inputs); i++ {
		temp[i], _ = strconv.Atoi(inputs[i])
	}

	return temp
}


package main

import (
	"fmt"
	"math"
)

func main() {
	var T int
	fmt.Scanf("%d\n", &T)
	var s string
	fmt.Scanf("%s\n",&s)
	var anss string
	minc := math.MaxInt32
	check("RGB", s, &anss, &minc)
	check("RBG", s, &anss, &minc)
	check("GRB", s, &anss, &minc)
	check("GBR", s, &anss, &minc)
	check("BRG", s, &anss, &minc)
	check("BGR", s, &anss, &minc)
	print(anss, minc, len(s))

}

func print(ss string, ans int, leng int){
	fmt.Printf("%d\n", ans)
	for i := 0; i < leng; i+=3 {
		if i + 3 <= leng{
			fmt.Printf("%s", ss)
		}
	}
	leng %= 3
	for i := 0; i < leng; i++ {
		fmt.Printf("%c", ss[i % 3])
	}
	fmt.Printf("\n")
}

func check(ss string, s string, anss *string, minc *int){
	ans := 0
	for e := range s {
		if s[e] != ss[e % 3] {
			ans++
		}
		if ans >= *minc{
			return
		}
	}
	if ans < *minc {
		*minc = ans
		*anss = ss
	}
}

package main

import "fmt"
import "math"

var plans = []string{
	"RGB",
	"RBG",
	"GRB",
	"GBR",
	"BGR",
	"BRG",
}

func main() {
	var n int
	var minLamps []byte
	var lamps []byte
	fmt.Scanf("%d\n", &n)
	fmt.Scanf("%s\n", &lamps)

	var minCount int = math.MaxInt32
	for _, plan := range plans {
		count := 0
		tempLamps := make([]byte, len(lamps))
		copy(tempLamps, lamps)
		for i := 0; i < len(tempLamps); i++ {
			shouldBe := plan[i%3]
			if tempLamps[i] != shouldBe {
				tempLamps[i] = shouldBe
				count++
			}
		}

		if count < minCount {
			minCount = count
			minLamps = tempLamps
		}
	}

	fmt.Println(minCount)
	fmt.Println(string(minLamps))

}

package main

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

var (
	mod int64 = 1000000007
)

func range3(l int64) []int64 {
	ans := make([]int64, 3)
	ans[0] = l / 3
	ans[1] = (l + 2) / 3
	ans[2] = (l + 1) / 3
	return ans
}
func solve(s, t []byte, n int) ([]byte, int) {
	res := make([]byte, n)
	cnt := 0
	f := 0
	for i := 0; i < n; i++ {
		if s[i] != t[f] {
			cnt++
		}
		res[i] = t[f]
		f++
		if f > 2 {
			f = 0
		}
	}
	return res, cnt
}
func main() {
	buf := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanf(buf, "%d\n", &n)
	str := make([]byte, n+1)
	fmt.Fscanf(buf, "%s\n", &str)
	cnt:=-1
	var s []byte
	loop :=make([][]byte,9)
	loop[0] = []byte("BRG")
	loop[1] = []byte("BGR")
	loop[2] = []byte("RBG")
	loop[3] = []byte("RGB")
	loop[4] = []byte("GRB")
	loop[5] = []byte("GBR")
	for i:=0;i < 6;i++{
		tmp, c := solve(str, loop[i], n)
		if cnt == -1 || cnt > c{
			cnt = c
			s = tmp
		}
	}
	fmt.Printf("%d\n",cnt)
	fmt.Printf("%s\n",s)
}

package main

import "fmt"
import "math"

var plans = []string{
	"RGB",
	"RBG",
	"GRB",
	"GBR",
	"BGR",
	"BRG",
}

func main() {
	var n int
	var minLamps []byte
	var lamps []byte
	fmt.Scanf("%d", &n)
	fmt.Scanf("%s", &lamps)

	var minCount int = math.MaxInt32
	for _, plan := range plans {
		count := 0
		tempLamps := make([]byte, len(lamps))
		copy(tempLamps, lamps)
		for i := 0; i < len(tempLamps); i++ {
			shouldBe := plan[(i)%3]
			if tempLamps[i] != shouldBe {
				tempLamps[i] = shouldBe
				count++
			}
		}

		if count < minCount {
			minCount = count
			minLamps = tempLamps
		}
	}

	fmt.Println(minCount)
	fmt.Println(string(minLamps))

}

package main

import "fmt"
import "math"

var plans = []string{
	"RGB",
	"RBG",
	"GRB",
	"GBR",
	"BGR",
	"BRG",
}

func main() {
	var n int
	var minLamps []byte
	var lamps []byte
	fmt.Scanf("%d", &n)
	fmt.Scanf("%s", &lamps)

	var minCount int = math.MaxInt32
	for _, plan := range plans {
		count := 0
		tempLamps := make([]byte, len(lamps))
		copy(tempLamps, lamps)
		for i := 0; i < len(tempLamps); i++ {
			shouldBe := plan[(i)%3]
			if tempLamps[i] != shouldBe {
				tempLamps[i] = shouldBe
				count++
			}
		}

		if count < minCount {
			minCount = count
			minLamps = tempLamps
		}
	}

	fmt.Printf("%d\n%s", minCount, minLamps)

}

package main

import "fmt"
import "math"

var plans = []string{
	"RGB",
	"RBG",
	"GRB",
	"GBR",
	"BGR",
	"BRG",
}

func main() {
	var n int
	var minLamps []byte
	var lamps []byte
	fmt.Scanf("%d", &n)
	fmt.Scanf("%s", &lamps)

	var minCount int = math.MaxInt32
	for _, plan := range plans {
		count := 0
		tempLamps := make([]byte, len(lamps))
		copy(tempLamps, lamps)
		for i := 0; i < len(tempLamps); i++ {
			shouldBe := plan[(i)%3]
			if tempLamps[i] != shouldBe {
				tempLamps[i] = shouldBe
				count++
			}
		}

		if count < minCount {
			minCount = count
			minLamps = tempLamps
		}
	}

	fmt.Println(minCount)
	fmt.Println(minLamps)

}

package main

import (
	"fmt"
	"math"
)

func main() {
	var T int
	fmt.Scanf("%d\n", &T)
	var s string
	fmt.Scanf("%s\n",&s)
	var anss string
	minc := math.MaxInt32
	check("RGB", s, &anss, &minc)
	check("RBG", s, &anss, &minc)
	check("GRB", s, &anss, &minc)
	check("GBR", s, &anss, &minc)
	check("BRG", s, &anss, &minc)
	check("BGR", s, &anss, &minc)
	print(anss, minc, len(s))

}

func print(ss string, ans int, leng int){
	fmt.Printf("%d\n", ans)
	for i := 0; i < leng; i+=3 {
		if i + 3 < leng{
			fmt.Printf("%s", ss)
		}
	}
	leng %= 3
	for i := 0; i < leng; i++ {
		fmt.Printf("%c", ss[i % 3])
	}
	fmt.Printf("\n")
}

func check(ss string, s string, anss *string, minc *int){
	ans := 0
	for e := range s {
		if s[e] != ss[e % 3] {
			ans++
		}
		if ans >= *minc{
			return
		}
	}
	if ans < *minc {
		*minc = ans
		*anss = ss
	}
}