package main

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

func check(str1, str2 string, l, r int) bool {
	cnt := 0
	for i := l; i <= r; i++ {
		if str1[i] == str2[i] {
			cnt++
		}
	}
	if cnt == 0 || cnt == r-l+1 {
		return true
	}
	return false
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var t int
	fmt.Fscanf(in, "%d\n", &t)
	for case_idx := 0; case_idx < t; case_idx++ {
		var len int
		var s1, s2 string
		fmt.Fscanf(in, "%d\n%s\n%s\n", &len, &s1, &s2)
		one_cnt := 0
		zero_cnt := 0
		l := 0
		r := 0
		success := true
		for i := 0; i < len; i++ {
			if s1[i] == '0' {
				zero_cnt++
			} else {
				one_cnt++
			}
			if one_cnt == zero_cnt {
				r = i
				success = check(s1, s2, l, r)
				if !success {
					break
				}
				l = r + 1
			}
		}
		if r <= l && r != len {
			for i := l; i < len; i++ {
				if s1[i] != s2[i] {
					success = false
					break
				}
			}
		}
		if success {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
	out.Flush()
}

package main

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

var reader *bufio.Reader = bufio.NewReaderSize(os.Stdin, 1024*1024)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func scanf(f string, args ...interface{})  { fmt.Fscanf(reader, f, args...) }
func printf(f string, args ...interface{}) { fmt.Fprintf(writer, f, args...) }

type Pair struct {
	x, y int
}

type Pairs []Pair

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

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

func flip(s *[]rune) {
	for i, j := 0, len(*s)-1; i < len(*s)/2; {
		if (*s)[i] == 1 {
			(*s)[i] = 0
		} else {
			(*s)[i] = 1
		}
		if (*s)[j] == 1 {
			(*s)[i] = 0
		} else {
			(*s)[i] = 1
		}
		i++
		j--
	}
}

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

func main() {
	defer writer.Flush()

	var t int
	scanf("%d\n", &t)

	for t > 0 {
		var n int
		scanf("%d\n", &n)
		var a, b string
		f := true
		scanf("%s\n%s\n", &a, &b)
		a, b = a+"0", b+"0"
		cnt := 0
		for i := 0; i < n; i++ {
			if a[i] == '1' {
				cnt++
			} else {
				cnt--
			}
			if (a[i] == b[i]) != (a[i+1] == b[i+1]) && cnt != 0 {
				printf("%s\n", "NO")
				f = false
				break
			}
		}
		if f == true {
			printf("%s\n", "YES")
		}
		t--
	}
}

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{}) (int, error) {
	return fmt.Fprintf(writer, f, a...)
}
func scanf(f string, a ...interface{}) (int, error) {
	return fmt.Fscanf(reader, f, a...)
}

func main() {
	defer writer.Flush()
	var n int
	_, err := scanf("%d\n", &n)
	if err != nil {
		panic(err)
	}

	for i := 0; i < n; i++ {
		var l int
		_, err := scanf("%d\n", &l)
		if err != nil {
			panic(err)
		}
		a, err := get_slice()
		if err != nil {
			panic(err)
		}
		b, err := get_slice()
		if err != nil {
			panic(err)
		}
		if t := can_transform(a, b); t {
			printf("YES\n")
		} else {
			printf("NO\n")
		}
	}
}

func get_slice() ([]bool, error) {
	var s string
	_, err := scanf("%s\n", &s)
	if err != nil {
		return nil, err
	}
	a := make([]bool, len(s))
	for j := 0; j < len(s); j++ {
		a[j] = s[j] == '1'
	}
	return a, nil
}

func can_transform(a, b []bool) bool {
	swap_locs := swap_locations(a)
	flip := false
	for i := len(a) - 1; i >= 0; i-- {
		if (a[i] != flip) == b[i] {
			continue
		}
		if !swap_locs[i] {
			return false
		}
		flip = !flip
	}
	return true
}

func swap_locations(a []bool) []bool {
	score := 0
	locs := make([]bool, len(a))
	for i := 0; i < len(a); i++ {
		if a[i] {
			score++
		} else {
			score--
		}
		locs[i] = score == 0
	}
	return locs
}

package main

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

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

	var t int
	for Fscan(in, &t); t > 0; t-- {
		var n int
		Fscan(in, &n)
		a, b := make([]byte, n), make([]byte, n)
		Fscan(in, &a, &b)

		nums := make([][2]int, n + 1)
		for i := 1; i <= n; i++ {
			if a[i - 1] == '1' {
				nums[i][0], nums[i][1] = nums[i-1][0], nums[i-1][1]+1
			} else {
				nums[i][0], nums[i][1] = nums[i-1][0] + 1, nums[i-1][1]
			}
		}

		var pos []int
		left, right := 0, 0
		for left < n && right < n {
			for left < n && a[left] == b[left] {
				left++
			}
			right = left
			if left < n && left >= 1 {
				pos = append(pos, left)
			}
			for right < n && a[right] != b[right] {
				right++
			}
			if right <= n && right > left {
				pos = append(pos, right)
			}
			left = right
		}

		//Fprintln(out, "pos: ", pos)
		flag := true
		for _, val := range pos {
			if nums[val][0] != nums[val][1] {
				flag = false
				Fprintln(out, "NO")
				break
			}
		}
		if flag {
			Fprintln(out, "YES")
		}
	}
}

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

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

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

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

package main

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

var in = NewIn()
var out = NewOut()

func main() {
	defer out.flush()

	cases()
	// solve()
}

/* --- Solution B --- */
func solve() {
	n := in.nextInt()

	from := in.next()
	to := in.next()

	if n == 1 && from != to {
		out.println("NO")
		return
	}

	ok := true
	zeros, ones := 0, 0
	eq, diff := 0, 0
	for i := 0; i < n; i++ {
		if from[i] == '0' {
			zeros++
		} else {
			ones++
		}
		if from[i] == to[i] {
			eq++
		} else {
			diff++
		}

		if zeros == ones {
			if diff*eq != 0 {
				ok = false
				break
			}
			eq, diff = 0, 0
			zeros, ones = 0, 0
		}
	}

	if ok && diff == 0 {
		out.println("YES")
	} else {
		out.println("NO")
	}
}

func cases() {
	t := in.nextInt()
	for ; t > 0; t-- {
		solve()
	}
}

/* --- I/O --- */
const BUF_MAX_CAP int = 1e6

type In struct {
	scanner *bufio.Scanner
}

func NewIn() *In {
	scn := bufio.NewScanner(os.Stdin)

	buf := make([]byte, BUF_MAX_CAP)
	scn.Buffer(buf, BUF_MAX_CAP)
	scn.Split(bufio.ScanWords)

	return &In{scn}
}

func (in *In) nextInt() int {
	in.scanner.Scan()
	i, _ := strconv.Atoi(in.scanner.Text())
	return i
}

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

func (in *In) next() string {
	in.scanner.Scan()
	return in.scanner.Text()
}

type Out struct {
	writer *bufio.Writer
}

func NewOut() *Out {
	out := Out{bufio.NewWriter(os.Stdout)}
	return &out
}

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

func (out *Out) printf(format string, a ...interface{}) {
	fmt.Fprintf(out.writer, format, a...)
}

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

func (out *Out) flush() {
	out.writer.Flush()
}

package main

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

// var m []int
// err = json.Unmarshal([]byte(r.FormValue("example")), &m)

// for i := 0; i < len(m); i++ {
// 	fmt.Println(m[i])
// }

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

func MKO(x, y int64) int64 {
	if x < y {
		return 0
	} else {
		return x - y + 1
	}
}

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

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func GCD(x, y int64) int64 {
	var l, q int64
	l = MAX(x, y)
	q = MIN(x, y)
	var m int64
	for l*q > 0 {
		a := l % q
		b := l - a
		cc := int64(b / q)
		remain := l - cc*q
		l = MAX(remain, q)
		q = MIN(remain, q)
		if l*q == 0 {
			m = l + q
			break
		}

	}
	return m

}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}

}

// sort.Slice(arr, func(i, j int) bool {
// 	return arr[i] > arr[j]
// })
var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)

	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(input, &n)
		A := make([]byte, 0)
		B := make([]byte, 0)
		DIFFERENET := make([]int, n+1)
		ANOTHER := make([]int, 0)
		ALLOWED := make([]int, n+1)
		fmt.Fscan(input, &A)
		fmt.Fscan(input, &B)
		ZEROS := 0
		ONES := 0
		for i := 0; i < n; i++ {
			if string(A[i]) == "1" {
				ONES++

			} else {
				ZEROS++
			}
			if string(A[i]) != string(B[i]) {
				DIFFERENET[i+1] = 1
			}
			if ONES == ZEROS {
				ALLOWED[i+1] = 1
			}
		}
		DIFFERENET = append(DIFFERENET, 0)

		for q := 1; q < n+1; q++ {
			if (DIFFERENET[q] + DIFFERENET[q+1]) == 1 {
				ANOTHER = append(ANOTHER, q)
			}
		}
		k := len(ANOTHER)

		jj := 0
		for i := 0; i < k; i++ {
			if ALLOWED[ANOTHER[i]] == 0 {
				fmt.Println("NO")
				jj = 1
				break
			}
		}
		if jj == 0 {

			fmt.Println("YES")
		}

	}
}

package main

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

/*
1
10
0111010000
0100101100
---
YES
=========
1
2
10
00
---
NO
*/

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

	bld := strings.Builder{}

	var t int
	scanf("%d\n", &t)

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

		var a, b string
		scanf("%s\n%s\n", &a, &b)

		if n%2 != 0 && a[n-1] != b[n-1] {
			fmt.Fprintf(&bld, "NO\n")
			continue
		}

		possible := true

		flipCnt := 0
		oneCnt := strings.Count(a, "1")

		for i := n - 1; i >= 0; i-- {
			ai := a[i]
			if flipCnt%2 != 0 {
				if ai == '0' {
					ai = '1'
				} else {
					ai = '0'
				}
			}

			ok := ai == b[i]
			if ok {
				if ai == '1' {
					oneCnt--
				}
				continue
			}

			zeroCnt := i - oneCnt + 1
			flippable := oneCnt == zeroCnt

			if flippable {
				if b[i] == '1' {
					oneCnt--
				}
				flipCnt++
			} else {
				possible = false
				break
			}
		}

		if possible {
			fmt.Fprintf(&bld, "YES\n")
		} else {
			fmt.Fprintf(&bld, "NO\n")
		}
	}

	printf("%s", bld.String())
}

const maxUint = ^uint(0)

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

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

//goland:noinspection GoUnusedType
type pair struct {
	fst rune
	snd int
}

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

//goland:noinspection GoUnusedFunction
func printf(f string, a ...interface{}) {
	if _, err := fmt.Fprintf(writer, f, a...); err != nil {
		panic(err)
	}
}

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

//goland:noinspection GoUnusedFunction
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

//goland:noinspection GoUnusedFunction
func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

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

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

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

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

	return true
}

package main

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

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

	var T, n int
	var s, t []byte
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &s, &t)
		for ; n > 0 && s[n-1] == t[n-1]; n-- {
		}
		c := [2]int{}
		for i, b := range s[:n] {
			if c[0] != c[1] && b == t[i] != (s[i-1] == t[i-1]) {
				break
			}
			c[b&1]++
		}
		if c[0] != c[1] {
			Fprintln(out, "NO")
		} else {
			Fprintln(out, "YES")
		}
	}
}

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

package main

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

func binSearchLE(arr []int, target int) int {
	start := 0
	end := len(arr) - 1
	ans := -1
	for start <= end {
		mid := (start + end) / 2
		if arr[mid] <= target {
			start = mid + 1
		}
		if arr[mid] > target {
			ans = mid
			end = mid - 1
		}
	}
	if ans == -1 {
		if end == len(arr)-1 {
			return len(arr)
		} else {
			return 0
		}
	}
	return ans
}

func sol(a, b string) string {
	if a == b {
		return "YES"
	}
	counts0 := make([]int, len(a))
	counts1 := make([]int, len(a))

	c0, c1 := 0, 0
	for i := range a {
		if a[i] == '0' {
			c0++
		} else {
			c1++
		}
		counts0[i] = c0
		counts1[i] = c1
	}

	flipped := false
	for i := len(a)-1; i >= 0; i-- {
		if !flipped {
			if a[i] == b[i] {
				continue
			}
		} else {
			if a[i] != b[i] {
				continue
			}
		}

		if counts0[i] != counts1[i] {
			return "NO"
		}
		//log.Println("flip at:", i)
		flipped = !flipped
	}

	return "YES"
}

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

//func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(os.Stdout, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
func scanIntsArr(arrLen int) []int {
	arr := make([]int, arrLen)
	for i := 0; i < len(arr); i++ {
		scanf("%d", &arr[i])
	}
	scanf("\n")
	return arr
}
func scanIntsArr64(arrLen int) []int64 {
	arr := make([]int64, arrLen)
	for i := 0; i < len(arr); i++ {
		scanf("%d", &arr[i])
	}
	scanf("\n")
	return arr
}
func scanIntsLines(arrLen int) []int {
	arr := make([]int, arrLen)
	for i := 0; i < len(arr); i++ {
		scanf("%d\n", &arr[i])
	}
	return arr
}
func printIntsLines(arr []int) {
	for i := 0; i < len(arr); i++ {
		printf("%d\n", arr[i])
	}
}
func scanStrsLines(arrLen int) []string {
	arr := make([]string, arrLen)
	for i := 0; i < len(arr); i++ {
		scanf("%s\n", &arr[i])
	}
	return arr
}
func printStrsLines(arr []string) {
	for i := 0; i < len(arr); i++ {
		printf("%s\n", arr[i])
	}
}

func main() {
	n := 0
	scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		m := 0
		a, b := "", ""
		scanf("%d\n", &m)
		scanf("%s\n", &a)
		scanf("%s\n", &b)
		printf("%s\n", sol(a, b))
	}
}

package main

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

type Scanner struct {
	bScanner *bufio.Scanner
}

func NewScanner() *Scanner {
	bScanner := bufio.NewScanner(os.Stdin)
	bScanner.Split(bufio.ScanWords)
	bScanner.Buffer([]byte{}, math.MaxInt32)
	return &Scanner{
		bScanner,
	}
}

func (s *Scanner) next() string {
	s.bScanner.Scan()
	return s.bScanner.Text()
}

func (s *Scanner) nextInt() int {
	s.bScanner.Scan()
	res, err := strconv.Atoi(s.bScanner.Text())
	if err != nil {
		panic(err)
	}
	return res
}

func (s *Scanner) nextFloat() float64 {
	s.bScanner.Scan()
	res, err := strconv.ParseFloat(s.bScanner.Text(), 64)
	if err != nil {
		panic(err)
	}
	return res
}

func (s *Scanner) nextInts(N int) []int {
	res := make([]int, N)
	for i := 0; i < N; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func min(nums ...int) int {
	ans := math.MaxInt32
	for _, num := range nums {
		if num < ans {
			ans = num
		}
	}
	return ans
}

func max(nums ...int) int {
	ans := math.MinInt32
	for _, num := range nums {
		if num > ans {
			ans = num
		}
	}
	return ans
}

func main() {
	sc := NewScanner()
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	TC := sc.nextInt()
	for tc := 0; tc < TC; tc++ {
		N := sc.nextInt()
		a := sc.next()
		b := sc.next()

		mask := make([]int, N)
		for i := 0; i < N; i++ {
			mask[i] = 0
			if a[i] != b[i] {
				mask[i] = 1
			}
		}

		prefixSum := make([]int, N)
		prefixSum[0] = int(a[0] - '0')
		for i := 1; i < N; i++ {
			prefixSum[i] = prefixSum[i-1] + int(a[i]-'0')
		}

		curFlip := 0
		canTransform := true
		for i := N - 1; i >= 0 && canTransform; i-- {
			if mask[i]^curFlip == 1 {
				// it's even range and #1 == #0
				if i%2 == 1 && 2*prefixSum[i] == i+1 {
					curFlip ^= 1
				} else {
					canTransform = false
				}
			}
		}
		if canTransform {
			fmt.Fprintln(wr, "YES")
		} else {
			fmt.Fprintln(wr, "NO")
		}
	}

}

package main

import (
	"fmt"
)

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

	for i := 0; i < n; i++ {
		var l int
		var s string
		_, err := fmt.Scanf("%d\n", &l)
		if err != nil {
			panic(err)
		}
		a := make([]bool, l)
		_, err = fmt.Scanln(&s)
		if err != nil {
			panic(err)
		}
		for j := 0; j < l; j++ {
			a[j] = s[j] == '1'
		}
		b := make([]bool, l)
		_, err = fmt.Scanln(&s)
		if err != nil {
			panic(err)
		}
		for j := 0; j < l; j++ {
			b[j] = s[j] == '1'
		}
		if t := can_transform(a, b, make(map[string]bool)); t {
			println("YES")
		} else {
			println("NO")
		}
	}
}

func equal(a, b []bool) bool {
	for i := range a {
		if a[i] != b[i] {
			return false
		}
	}
	return true
}

func flip_first(n int, a []bool) []bool {
	var new []bool
	for i := 0; i < n; i++ {
		new = append(new, !a[i])
	}
	for i := n; i < len(a); i++ {
		new = append(new, a[i])
	}
	return new
}

func can_transform(a, b []bool, seen map[string]bool) bool {
	if have_seen := seen[fmt.Sprint(a)]; have_seen {
		return false
	}
	seen[fmt.Sprint(a)] = true
	if equal(a, b) {
		return true
	}
	score := 0
	for i := 0; i < len(a); i++ {
		if a[i] {
			score++
		} else {
			score--
		}
		if score == 0 {
			new_a := flip_first(i+1, a)
			rec := can_transform(new_a, b, seen)
			if rec {
				return true
			}
		}
	}
	return false
}

package main

import (
	"fmt"
)

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

	for i := 0; i < n; i++ {
		var l int
		var s string
		_, err := fmt.Scanf("%d\n", &l)
		if err != nil {
			panic(err)
		}
		a := make([]bool, l)
		fmt.Scanln(&s)
		for j := 0; j < l; j++ {
			a[j] = s[j] == '1'
		}
		b := make([]bool, l)
		fmt.Scanln(&s)
		for j := 0; j < l; j++ {
			b[j] = s[j] == '1'
		}
		if t := can_transform(a, b, make(map[string]bool)); t {
			println("YES")
		} else {
			println("NO")
		}
	}
}

func equal(a, b []bool) bool {
	for i := range a {
		if a[i] != b[i] {
			return false
		}
	}
	return true
}

func flip_first(n int, a []bool) []bool {
	var new []bool
	for i := 0; i < n; i++ {
		new = append(new, !a[i])
	}
	for i := n; i < len(a); i++ {
		new = append(new, a[i])
	}
	return new
}

func can_transform(a, b []bool, seen map[string]bool) bool {
	if have_seen := seen[fmt.Sprint(a)]; have_seen {
		return false
	}
	seen[fmt.Sprint(a)] = true
	if equal(a, b) {
		return true
	}
	score := 0
	for i := 0; i < len(a); i++ {
		if a[i] {
			score++
		} else {
			score--
		}
		if score == 0 {
			new_a := flip_first(i+1, a)
			rec := can_transform(new_a, b, seen)
			if rec {
				return true
			}
		}
	}
	return false
}

package main

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

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

	var t int
	for Fscan(in, &t); t > 0; t-- {
		var n int
		Fscan(in, &n)
		a, b := make([]byte, n), make([]byte, n)
		Fscan(in, &a, &b)

		nums := make([][2]int, n + 1)
		for i := 1; i <= n; i++ {
			if a[i - 1] == '1' {
				nums[i][0], nums[i][1] = nums[i-1][0], nums[i-1][1]+1
			} else {
				nums[i][0], nums[i][1] = nums[i-1][0] + 1, nums[i-1][1]
			}
		}

		var pos []int
		left, right := 0, 0
		for left < n && right < n {
			for left < n && a[left] == b[left] {
				left++
			}
			right = left
			if left < n && left > 1 {
				pos = append(pos, left)
			}
			for right < n && a[right] != b[right] {
				right++
			}
			if right <= n && right > left {
				pos = append(pos, right)
			}
			left = right
		}

		flag := true
		for _, val := range pos {
			if nums[val][0] != nums[val][1] {
				flag = false
				Fprintln(out, "NO")
				break
			}
		}
		if flag {
			Fprintln(out, "YES")
		}
	}
}

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

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

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

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

package main

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

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

	var t int
	for Fscan(in, &t); t > 0; t-- {
		var n int
		Fscan(in, &n)
		a, b := make([]byte, n), make([]byte, n)
		Fscan(in, &a, &b)

		nums := make([][2]int, n + 1)
		for i := 1; i <= n; i++ {
			if a[i - 1] == '1' {
				nums[i][0], nums[i][1] = nums[i-1][0], nums[i-1][1]+1
			} else {
				nums[i][0], nums[i][1] = nums[i-1][0] + 1, nums[i-1][1]
			}
		}

		var pos []int
		left, right := 0, 0
		for left < n && right < n {
			for left < n && a[left] == b[left] {
				left++
			}
			right = left
			if left < n && left > 1 {
				pos = append(pos, left)
			}
			for right < n && a[right] != b[right] {
				right++
			}
			if right <= n {
				pos = append(pos, right)
			}
			left = right
		}

		flag := true
		for _, val := range pos {
			if nums[val][0] != nums[val][1] {
				flag = false
				Fprintln(out, "NO")
				break
			}
		}
		if flag {
			Fprintln(out, "YES")
		}
	}
}

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

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

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

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

package main

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

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

	var t int
	for Fscan(in, &t); t > 0; t-- {
		var n int
		Fscan(in, &n)
		a, b := make([]byte, n), make([]byte, n)
		Fscan(in, &a, &b)

		nums := make([][2]int, n + 1)
		for i := 1; i <= n; i++ {
			if a[i - 1] == '1' {
				nums[i][0], nums[i][1] = nums[i-1][0], nums[i-1][1]+1
			} else {
				nums[i][0], nums[i][1] = nums[i-1][0] + 1, nums[i-1][1]
			}
		}

		var pos []int
		left, right := 0, 0
		for left < n && right < n {
			for left < n && a[left] == b[left] {
				left++
			}
			right = left
			if left < n && left > 1 {
				pos = append(pos, left)
			}
			for right < n && a[right] != b[right] {
				right++
			}
			if right < n {
				pos = append(pos, right)
			}
			left = right
		}

		flag := true
		for _, val := range pos {
			if nums[val][0] != nums[val][1] {
				flag = false
				Fprintln(out, "NO")
				break
			}
		}
		if flag {
			Fprintln(out, "YES")
		}
	}
}

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

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

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

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

package main

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

var in = NewIn()
var out = NewOut()

func main() {
	defer out.flush()

	cases()
	// solve()
}

/* --- Solution B --- */
func solve() {
	n := in.nextInt()

	from := in.next()
	to := in.next()

	if n == 1 && from != to {
		out.println("NO")
		return
	}

	ok := true
	zeros, ones := 0, 0
	eq, diff := 0, 0
	for i := 0; i < n; i++ {
		if from[i] == '0' {
			zeros++
		} else {
			ones++
		}
		if from[i] == to[i] {
			eq++
		} else {
			diff++
		}

		if zeros == ones {
			if diff*eq != 0 {
				ok = false
				break
			}
			eq, diff = 0, 0
			zeros, ones = 0, 0
		}
	}

	if ok && diff*eq == 0 {
		out.println("YES")
	} else {
		out.println("NO")
	}
}

func cases() {
	t := in.nextInt()
	for ; t > 0; t-- {
		solve()
	}
}

/* --- I/O --- */
const BUF_MAX_CAP = 200_043

type In struct {
	scanner *bufio.Scanner
}

func NewIn() *In {
	scn := bufio.NewScanner(os.Stdin)

	buf := make([]byte, BUF_MAX_CAP)
	scn.Buffer(buf, BUF_MAX_CAP)
	scn.Split(bufio.ScanWords)

	return &In{scn}
}

func (in *In) nextInt() int {
	in.scanner.Scan()
	i, _ := strconv.Atoi(in.scanner.Text())
	return i
}

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

func (in *In) next() string {
	in.scanner.Scan()
	return in.scanner.Text()
}

type Out struct {
	writer *bufio.Writer
}

func NewOut() *Out {
	out := Out{bufio.NewWriter(os.Stdout)}
	return &out
}

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

func (out *Out) printf(format string, a ...interface{}) {
	fmt.Fprintf(out.writer, format, a...)
}

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

func (out *Out) flush() {
	out.writer.Flush()
}

package main

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

var in = NewIn()
var out = NewOut()

func main() {
	defer out.flush()

	cases()
	// solve()
}

/* --- Solution B --- */
func solve() {
	n := in.nextInt()

	from := in.next()
	to := in.next()

	ok := true
	zeros, ones := 0, 0
	eq, diff := 0, 0
	for i := 0; i < n; i++ {
		if from[i] == '0' {
			zeros++
		} else {
			ones++
		}
		if from[i] == to[i] {
			eq++
		} else {
			diff++
		}

		if zeros == ones {
			if diff*eq != 0 {
				ok = false
				break
			}
			eq, diff = 0, 0
			zeros, ones = 0, 0
		}
	}

	if ok && diff*eq == 0 {
		out.println("YES")
	} else {
		out.println("NO")
	}
}

func cases() {
	t := in.nextInt()
	for ; t > 0; t-- {
		solve()
	}
}

/* --- I/O --- */
const BUF_MAX_CAP = 200_043

type In struct {
	scanner *bufio.Scanner
}

func NewIn() *In {
	scn := bufio.NewScanner(os.Stdin)

	buf := make([]byte, BUF_MAX_CAP)
	scn.Buffer(buf, BUF_MAX_CAP)
	scn.Split(bufio.ScanWords)

	return &In{scn}
}

func (in *In) nextInt() int {
	in.scanner.Scan()
	i, _ := strconv.Atoi(in.scanner.Text())
	return i
}

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

func (in *In) next() string {
	in.scanner.Scan()
	return in.scanner.Text()
}

type Out struct {
	writer *bufio.Writer
}

func NewOut() *Out {
	out := Out{bufio.NewWriter(os.Stdout)}
	return &out
}

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

func (out *Out) printf(format string, a ...interface{}) {
	fmt.Fprintf(out.writer, format, a...)
}

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

func (out *Out) flush() {
	out.writer.Flush()
}

package main

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

/*
1
10
0111010000
0100101100
---
YES
=========
*/

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

	bld := strings.Builder{}

	var t int
	scanf("%d\n", &t)

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

		var a, b string
		scanf("%s\n%s\n", &a, &b)

		if n%2 != 0 && a[n-1] != b[n-1] {
			fmt.Fprintf(&bld, "NO\n")
			continue
		}

		possible := true

		flipCnt := 0
		oneCnt := strings.Count(a, "1")

		for i := n - 1; i > 0; i-- {
			ai := a[i]
			if flipCnt%2 != 0 {
				if ai == '0' {
					ai = '1'
				} else {
					ai = '0'
				}
			}

			ok := ai == b[i]
			if ok {
				if ai == '1' {
					oneCnt--
				}
				continue
			}

			zeroCnt := i - oneCnt + 1
			flippable := oneCnt == zeroCnt

			if flippable {
				if b[i] == '1' {
					oneCnt--
				}
				flipCnt++
			} else {
				possible = false
				break
			}
		}

		if possible {
			fmt.Fprintf(&bld, "YES\n")
		} else {
			fmt.Fprintf(&bld, "NO\n")
		}
	}

	printf("%s", bld.String())
}

const maxUint = ^uint(0)

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

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

//goland:noinspection GoUnusedType
type pair struct {
	fst rune
	snd int
}

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

//goland:noinspection GoUnusedFunction
func printf(f string, a ...interface{}) {
	if _, err := fmt.Fprintf(writer, f, a...); err != nil {
		panic(err)
	}
}

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

//goland:noinspection GoUnusedFunction
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

//goland:noinspection GoUnusedFunction
func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

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

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

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

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

	return true
}

package main

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

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

	var T, n int
	var s, t []byte
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &s, &t)
		c := [2]int{}
		for i, b := range s {
			if b != t[i] {
				c[b&1]++
			} else if c[0] != c[1] {
				break
			}
		}
		if c[0] != c[1] {
			Fprintln(out, "NO")
		} else {
			Fprintln(out, "YES")
		}
	}
}

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

package main

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

func check(str1, str2 string, l, r int) bool {
	cnt := 0
	for i := l; i <= r; i++ {
		if str1[i] == str2[i] {
			cnt++
		}
	}
	if cnt == 0 || cnt == r-l+1 {
		return true
	}
	return false
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var t int
	fmt.Fscanf(in, "%d\n", &t)
	for case_idx := 0; case_idx < t; case_idx++ {
		var len int
		var s1, s2 string
		fmt.Fscanf(in, "%d\n%s\n%s\n", &len, &s1, &s2)
		one_cnt := 0
		zero_cnt := 0
		l := 0
		r := 0
		var success bool
		for i := 0; i < len; i++ {
			if s1[i] == '0' {
				zero_cnt++
			} else {
				one_cnt++
			}
			if one_cnt == zero_cnt {
				r = i
				success = check(s1, s2, l, r)
				if !success {
					break
				}
				l = r + 1
			}
		}
		if s1 == s2 {
			fmt.Fprintln(out, "YES")
		} else if success {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
	out.Flush()
}


package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	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 int
	scan(&n)
	var s string
	scan(&s)
	var ans = 1 << 30
	for l := 2; l <= 10; l++ {
		for i := 0; i+l-1 < len(s); i++ {
			cnt := [3]int{}
			ss := s[i : i+l]
			for j := range ss {
				cnt[ss[j]-'a']++
			}
			if cnt[0] > cnt[1] && cnt[0] > cnt[2] && ans > l {
				ans = l
			}
		}
	}
	if ans == 1<<30 {
		ans = -1
	}
	printf("%v\n", ans)
}

package main

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

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

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

	str := []string{"aa", "aba", "aca", "abca", "acba", "abbacca", "accabba"}

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

		ans := -1
	Loop:
		for _, s2 := range str {
			for i := 0; i < n-len(s2)+1; i++ {
				if s[i:i+len(s2)] == s2 {
					ans = len(s2)
					break Loop
				}
			}
		}
		fmt.Fprintln(out, ans)
	}
}

package main

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

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

	var tc int
	fmt.Fscan(in, &tc)
	for ; tc > 0; tc-- {
		var n int
		var s string
		fmt.Fscan(in, &n, &s)
		mn := 12
		for i := 0; i < n-1; i++ {
			cnt := [3]int{}
			cnt[s[i]-'a']++
			for j := 1; j < mn && i+j < n; j++ {
				cnt[s[i+j]-'a']++
				if cnt[0] > cnt[1] && cnt[0] > cnt[2] {
					mn = j
				}
			}
		}
		if mn == 12 {
			out.WriteString("-1\n")
		} else {
			fmt.Fprintln(out, mn+1)
		}
	}
}

package main

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

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

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

	str := []string{"aa", "aba", "aca", "abca", "acba", "abbacca", "accabba"}

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

		ans := -1
		for _, s2 := range str {
			for i := 0; i < n-len(s2)+1; i++ {
				if s[i:i+len(s2)] == s2 {
					ans = len(s2)
					break
				}
			}
		}
		fmt.Fprintln(out, ans)
	}
}

package main

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

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

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

		ans := math.MaxInt32
		for i, ch := range s {
			if ch == 'a' {
				if i < n-1 && s[i+1] == 'a' {
					ans = 2
					break
				} else if i < n-2 && s[i+2] == 'a' {
					ans = min(ans, 3)
				} else if i < n-3 && (s[i:i+4] == "abca" || s[i:i+4] == "acba") {
					ans = min(ans, 4)
				}
			}
		}

		if ans == math.MaxInt32 {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, ans)
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"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 int
		var s string
		fmt.Fscan(in, &n, &s)

		l, r := -1, -1
		var b, c int
		min := math.MaxInt32
		for i, ch := range s {
			if ch == 'a' {
				if l < 0 {
					l = i
				} else if r < 0 {
					r = i
				} else {
					l, r = r, i
				}

				if r > 0 && r-l+1 < min && r-l+1 > b && r-l+1 > c {
					min = r - l + 1
				}
				b, c = 0, 0
			} else if ch == 'b' {
				b++
			} else {
				c++
			}
		}

		if min == math.MaxInt32 {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, min)
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"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 int
		var s string
		fmt.Fscan(in, &n, &s)

		a, b := -1, -1
		min := math.MaxInt32
		for i, ch := range s {
			if ch == 'a' {
				if a < 0 {
					a = i
				} else if b < 0 {
					b = i
				} else {
					a, b = b, i
				}

				if b > 0 && b-a+1 < min {
					min = b - a + 1
				}
			}
		}

		if min == math.MaxInt32 {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, min)
		}
	}
}

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

		a, b := -1, -1
		min := -1
		for i, ch := range s {
			if ch == 'a' {
				if a < 0 {
					a = i
				} else if b < 0 {
					b = i
				} else {
					a, b = b, i
				}

				if b > 0 && b-a+1 < min {
					min = b - a + 1
				}
			}
		}

		fmt.Fprintln(out, min)
	}
}

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

		a, b := -1, -1
		min := -1
		for i, ch := range s {
			if ch == 'a' {
				if a < 0 {
					a = i
				} else if b < 0 {
					b = i
				} else {
					a, b = b, i
				}

				if b > 0 && b-a+1 > min {
					min = b - a + 1
				}
			}
		}

		fmt.Fprintln(out, min)
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var t int
	fmt.Fscan(in,&t)
	for u:=1;u<=t;u++{
		var n int
		fmt.Fscan(in,&n)
		var b []byte
		fmt.Fscan(in,&b)
		ans:=-1
		for i:=0;i<=len(b)-1;i++{
			if b[i]=='a'{
				if i-1>=0{
					if b[i-1]=='a'{
						ans=2
					}
				}
				if i+1<=len(b)-1{
					if b[i+1]=='a'{
						ans=2
					}
				}
			}
		}
		if ans==2{
			fmt.Fprintln(out,ans)
			continue
		}
		for i:=0;i<=len(b)-1;i++{
			if b[i]=='a'{
				if i-2>=0{
					if b[i-2]=='a'{
						ans=3
					}
				}
				if i+2<=len(b)-1{
					if b[i+2]=='a'{
						ans=3
					}
				}
			}
		}
		if ans==3{
			fmt.Fprintln(out,ans)
			continue
		}
		for i:=0;i<len(b);i++{
			if i+3<=len(b)-1&&b[i]=='a'&&b[i+3]=='a'{
				if b[i+1]=='b'&&b[i+2]=='c'{
					ans=4
				}
				if b[i+1]=='c'&&b[i+2]=='b'{
					ans=4
				}
			}
		}
		fmt.Fprintln(out,ans)
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var t int
	fmt.Fscan(in,&t)
	for u:=1;u<=t;u++{
		var n int
		fmt.Fscan(in,&n)
		var b []byte
		fmt.Fscan(in,&b)
		ans:=-1
		for i:=0;i<=len(b)-1;i++{
			if b[i]=='a'{
				if i-1>=0{
					if b[i-1]=='a'{
						ans=2
					}
				}
				if i+1<=len(b)-1{
					if b[i+1]=='a'{
						ans=2
					}
				}
			}
		}
		if ans==2{
			fmt.Fprintln(out,ans)
			continue
		}
		for i:=0;i<=len(b)-1;i++{
			if b[i]=='a'{
				if i-2>=0{
					if b[i-2]=='a'{
						ans=3
					}
				}
				if i+2<=len(b)-1{
					if b[i+2]=='a'{
						ans=3
					}
				}
			}
		}
		if ans==3{
			fmt.Fprintln(out,ans)
			continue
		}
		fmt.Fprintln(out,ans)
	}
}


package main

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

func Init() {
}

func Solve(io *FastIO) {
    N := io.NextInt()
    A := io.NextLongArray(N)

    var sum int64
    freq := make(map[int64]int)
    for _, v := range A {
        sum += v
        freq[v]++
    }

    if sum & 1 == 1 {
        io.Println("NO")
        return
    }

    want := sum / int64(2)
    var curr int64
    used := make(map[int64]int)
    for _, v := range A {
        if freq[want - curr] > 0 || used[curr - want] > 0 {
            io.Println("YES")
            return
        }
        curr += v
        freq[v]--
        used[v]++
    }
    io.Println("NO")
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

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

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

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

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

	var n int
	Fscan(in, &n)
	a := make([]int64, n)
	s := make([]int64, n+1)
	for i := range a {
		Fscan(in, &a[i])
		s[i+1] = s[i] + a[i]
	}
	if s[n]&1 > 0 {
		Fprint(out, "NO")
		return
	}
	mp := map[int64]bool{0: true}
	for i, v := range a {
		mp[v] = true
		if mp[s[i+1]-s[n]/2] {
			Fprint(out, "YES")
			return
		}
	}
	mp = map[int64]bool{0: true}
	for i := len(a) - 1; i >= 0; i-- {
		mp[a[i]] = true
		if mp[s[n]/2-s[i]] {
			Fprint(out, "YES")
			return
		}
	}
	Fprint(out, "NO")
}

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

package main

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

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

	var n int
	Fscan(in, &n)
	a := make([]int64, n)
	s := make([]int64, n+1)
	for i := range a {
		Fscan(in, &a[i])
		s[i+1] = s[i] + a[i]
	}
	if s[n]&1 > 0 {
		Fprint(out, "NO")
		return
	}
	mp := make(map[int64]bool, n)
	mp[0] = true
	for i, v := range a {
		mp[v] = true
		if mp[s[i+1]-s[n]/2] {
			Fprint(out, "YES")
			return
		}
	}
	mp = make(map[int64]bool, n)
	mp[0] = true
	for i := len(a) - 1; i >= 0; i-- {
		v := a[i]
		mp[v] = true
		if mp[s[n]/2-s[i]] {
			Fprint(out, "YES")
			return
		}
	}
	Fprint(out, "NO")
}

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

package main

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

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

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

	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(r, "%d", &a[i])
	}

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

func canSplit(a []int) bool {
	sum := int64(0)
	for _, e := range a {
		sum += int64(e)
	}

	if sum%2 != 0 {
		return false
	}

	av := make(map[int64]int)
	for _, e := range a {
		av[int64(e)]++
	}

	in := make(map[int64]int)
	cur := int64(0)
	for _, e := range a {
		cur += int64(e)
		in[int64(e)]++
		av[int64(e)]--
		if cur == sum/2 {
			return true
		} else if cur > sum/2 {
			d := cur - sum/2
			if in[d] > 0 {
				return true
			}
		} else {
			d := sum/2 - cur
			if av[d] > 0 {
				return true
			}
		}
	}

	return false
}

package main

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

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

	var n int
	Fscan(in, &n)
	a := make([]int64, n)
	s := make([]int64, n+1)
	for i := range a {
		Fscan(in, &a[i])
		s[i+1] = s[i] + a[i]
	}
	if s[n]&1 > 0 {
		Fprint(out, "NO")
		return
	}
	mp := make(map[int64]bool, n)
	mp[0] = true
	for i, v := range a {
		mp[v] = true
		if mp[s[i+1]-s[n]/2] {
			Fprint(out, "YES")
			return
		}
	}
	mp = make(map[int64]bool, n)
	mp[0] = true
	for i := len(a) - 1; i >= 0; i-- {
		v := a[i]
		mp[v] = true
		if mp[s[n]/2-s[i+1]] {
			Fprint(out, "YES")
			return
		}
	}
	Fprint(out, "NO")
}

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

package main

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

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

	var n int
	Fscan(in, &n)
	a := make([]int64, n)
	s := make([]int64, n+1)
	for i := range a {
		Fscan(in, &a[i])
		s[i+1] = s[i] + a[i]
	}
	if s[n]&1 > 0 {
		Fprint(out, "NO")
		return
	}
	mp := make(map[int64]bool, n)
	for i, v := range a {
		mp[v] = true
		if mp[s[i+1]-s[n]/2] {
			Fprint(out, "YES")
			return
		}
	}
	Fprint(out, "NO")
}

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

package main

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

func Init() {
}

func Solve(io *FastIO) {
    N := io.NextInt()
    A := io.NextLongArray(N)

    var sum int64
    freq := make(map[int64]int)
    for _, v := range A {
        sum += v
        freq[v]++
    }

    if sum % 1 == 1 {
        io.Println("NO")
        return
    }

    want := sum / int64(2)
    var curr int64
    for _, v := range A {
        if freq[want - curr] > 0 {
            io.Println("YES")
            return
        }
        curr += v
        freq[v]--
    }
    io.Println("NO")
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

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

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

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

func Init() {
}

func Solve(io *FastIO) {
    N := io.NextInt()
    A := io.NextLongArray(N)

    var sum int64
    freq := make(map[int64]int)
    for _, v := range A {
        sum += v
        freq[v]++
    }

    if sum & 1 == 1 {
        io.Println("NO")
        return
    }

    want := sum / int64(2)
    var curr int64
    for _, v := range A {
        if freq[want - curr] > 0 {
            io.Println("YES")
            return
        }
        curr += v
        freq[v]--
    }
    io.Println("NO")
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

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

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}


package main

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

var scan *bufio.Scanner
var writer io.Writer

func init() {
	scan = bufio.NewScanner(os.Stdin)
	writer = os.Stdout
	scan.Split(bufio.ScanWords)
}

func nextWord() string {
	scan.Scan()
	str := scan.Text()
	return str
}

func nextInt() int {
	i, e := strconv.Atoi(nextWord())
	if e != nil {
		panic(e)
	}
	return i
}

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

func main() {
	var n, m int
	var num int64
	n = nextInt()
	for i := 0; i < n; i++ {
		m = nextInt()
		numMap := make(map[int]int)
		for j := 0; j < m; j++ {
			num = nextInt64()
			if num <= 2048 {
				numMap[int(num)]++
			}
		}
		k := 1
		for j := 1; j < 13; j++ {
			if k == 2048 {
				if numMap[k] > 0 {
					fmt.Fprintln(writer, "YES")

				} else {
					fmt.Fprintln(writer, "NO")
				}
				break
			}
			for ; numMap[k] > 1; {
				numMap[k] -= 2
				numMap[k + k]++
			}
			k += k
		}
	}
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var (
		count int
	)
	_, _ = fmt.Fscan(input, &count)

	for count > 0 {
		count--

		var setNum int
		_, _ = fmt.Fscan(input, &setNum)

		hasAns := false
		countMap := map[uint32]int{}
		for i := 0; i < setNum; i++ {
			var num uint32
			_, _ = fmt.Fscan(input, &num)
			if num > 2048 || hasAns {
				continue
			}
			if num == 2048 {
				hasAns = true
			}
			if _, ok := countMap[num]; ok {
				countMap[num] = countMap[num] + 1
			} else {
				countMap[num] = 1
			}
		}

		if hasAns {
			fmt.Println("YES")
			continue
		}

		for i := 1; i < 2048; i *= 2 {
			if _, ok := countMap[uint32(i)]; ok {

				if _, ok := countMap[uint32(i*2)]; ok {
					countMap[uint32(i*2)] = countMap[uint32(i)]/2 + countMap[uint32(i*2)]
				} else {
					countMap[uint32(i*2)] = countMap[uint32(i)] / 2
				}
				countMap[uint32(i)] = countMap[uint32(i)] % 2

			} else {
				countMap[uint32(i)] = 0
			}
		}

		if countMap[uint32(2048)] > 0 {
			fmt.Println("YES")
			continue
		}
		fmt.Println("NO")

	}

}

//HOWDY! IT'S ME! A HARD BLACKISH DICK MOVE!
package main

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

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

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

//str := PrintlnInts(X...)

/*******************************************************************/
func print(b bool) {
	if b {
		fmt.Println("YES")
		return
	}
	fmt.Println("NO")
}

func main() {
	table := make(map[int]int)
	for i, p := 0, 1; p <= 2048; i, p = i+1, 2*p {
		table[p] = i
	}
	for q := ReadInt(); q > 0; q-- {
		n := ReadInt()
		s := ReadIntSlice(n)
		freq := make([]int, 12)
		for _, v := range s {
			if v > 2048 {
				continue
			}
			freq[table[v]]++
		}
		for i := 0; i < 11; i++ {
			freq[i+1] += freq[i] / 2
		}
		print(freq[11] > 0)
	}

}

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

func min(args ...int) int {
	if len(args) == 0 {
		panic("empty!")
	}
	min := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] < min {
			min = args[i]
		}
	}
	return min
}

func max(args ...int) int {
	if len(args) == 0 {
		panic("empty!")
	}
	max := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] > max {
			max = args[i]
		}
	}
	return max
}

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

func gcd(args ...int) int {
	if len(args) == 0 {
		panic("empty!")
	}
	gcd := args[0]
	for i := 1; i < len(args); i++ {
		gcd = _gcd(gcd, args[i])
	}
	return gcd
}

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

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

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9))
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

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

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

func readInt64() int64 {
	return readIntNbits(64)
}

func readInt() int {
	return int(readIntNbits(32))
}

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

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

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

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

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

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

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

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

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

	return string(res)
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	q := 0
	fmt.Fscanf(in, "%d\n", &q)
	for ii := 0; ii < q; ii++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
		}
		fmt.Fscanf(in, "\n")

		sort.Ints(a)
		sum := 0
		found := false
		for i := 0; i < n; i++ {
			if a[i] > 2048 {
				break
			}
			sum += a[i]
			if sum >= 2048 {
				found = true
				break
			}
		}
		if found {
			fmt.Fprintf(out, "YES\n")
		} else {
			fmt.Fprintf(out, "NO\n")
		}
	}
	return
}

func minstep(a, b byte) int {
	return min(int(a-b+26)%26, int(b-a+26)%26)
}

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

package main

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

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

	return n
}

const TARGET = 2048

func solve(a []uint32) bool {
	nums := make([]uint32, 0, len(a))
	m := map[uint32]int{}

	for _, n := range a {
		m[n]++

		if m[n] == 2 {
			nums = append(nums, n)
		}
	}

	for m[TARGET] == 0 && len(nums) > 0 {
		n := nums[0]
		m[n] -= 2
		if m[n] < 2 {
			nums = nums[1:]
		}

		n *= 2
		m[n]++
		if m[n] == 2 {
			nums = append(nums, n)
		}
	}

	return m[TARGET] > 0
}

func main() {
	var q uint32

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

	scanner.Scan()
	q = toInt(scanner.Bytes())
	r := make([]string, q)

	for i := uint32(0); i < q; i++ {
		var n int

		scanner.Scan()
		n = int(toInt(scanner.Bytes()))

		a := make([]uint32, n)
		for j := range a {
			scanner.Scan()
			a[j] = toInt(scanner.Bytes())
		}

		res := "NO"
		if solve(a) {
			res = "YES"
		}

		r[i] = res
	}

	for _, s := range r {
		fmt.Println(s)
	}
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var testCase int

	_, err := fmt.Fscan(input, &testCase)
	if err != nil {
	}

	for testCase > 0 {
		testCase--

		var x int
		_, err := fmt.Fscan(input, &x)
		if err != nil {
		}

		s := make([]int, x)
		for i := 0; i < x; i++ {
			_, err = fmt.Fscan(input, &s[i])
			if err != nil {
			}
		}

		result := "NO"
		//sort.Ints(s)
		//y := make([]int, x)
		//j := 0
		//for i := 0; i < x; {
		//	if s[i] == 2048 {
		//		result = "YES"
		//		break
		//	}
		//	if i < x-1 && s[i] == s[i+1] {
		//		yy := s[i] + s[i+1]
		//		if yy > y[j] {
		//			y[j+1] = yy
		//		} else {
		//			y[j+1] = yy + y[j]
		//		}
		//		j++
		//		i += 2
		//	} else if s[i] == y[j] {
		//		y[j+1] = s[i] + y[j]
		//		j++
		//		i++
		//	} else {
		//		i++
		//	}
		//	if y[j] == 2048 {
		//		result = "YES"
		//		break
		//	}
		//}

		if len(s) == 1 && s[0] == 2048 {
			result = "YES"
		}
		for len(s) > 1 {
			sort.Ints(s)
			//fmt.Println(s)
			if s[0] == 2048 {
				result = "YES"
				break
			} else if s[0] > 2048 {
				break
			}
			if s[0] == s[1] {
				s[0] = math.MaxInt32
				s[1] = s[1] + s[1]
			} else {
				s[0] = math.MaxInt32
			}
		}

		fmt.Println(result)
	}

}

package main

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

func clear(arr []byte) []byte {
	if string(arr[len(arr)-2:len(arr)]) == "\r\n" {
		return arr[:len(arr)-2]
	} else if string(arr[len(arr)-1]) == "\n" {
		return arr[:len(arr)-1]
	}
	return arr
}

func isWin(b []byte) string {
	sum := 0
	for _, v := range strings.Split(string(clear(b)), " ") {
		value, _ := strconv.Atoi(v)
		if value > 2048 {
			continue
		}
		sum += value
		if sum >= 2048 {
			break
		}
	}
	if sum >= 2048 {
		return "YES"
	}
	return "NO"
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	line, _ := reader.ReadBytes('\n')
	q, _ := strconv.Atoi(string(clear(line)))

	for i := 0; i < q; i++ {
		reader.ReadBytes('\n')
		array, _ := reader.ReadBytes('\n')
		fmt.Printf("%v\n", isWin(array))
	}
}

package main

import (
	"bufio"
	"os"
	"runtime"
	"strconv"
)

/* solution */

func solve() {

	q := readInt()
	for i := 0; i < q; i++ {
		if solveOne() {
			print("YES")
		} else {
			print("NO")
		}
	}

}

func solveOne() bool {

	n := readInt()

	var cnt [11 + 2]int

	for i := 0; i < n; i++ {
		v := readInt()
		k := 0
		for v > 0 && k <= 12 {
			v = v / 2
			k++
		}
		cnt[k-1]++
	}

	for i := 1; i <= 11; i++ {
		cnt[i] += cnt[i-1] / 2
	}

	return cnt[11] > 0

}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

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

	reader = bufio.NewScanner(f)

	const maxCapacity = 1024 * 1024
	buf := make([]byte, maxCapacity)
	reader.Buffer(buf, maxCapacity)

	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func printInt64(i int64) {
	print(strconv.FormatInt(i, 10))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

package main

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

const INF  = 1000000001

type Pair struct {
	first, second int
}

func sort(arr []int) []int {
	if len(arr) < 2 {
		return arr
	}
	left := 0
	right := len(arr) - 1
	pivot := rand.Int() % len(arr)

	arr[pivot], arr[right] = arr[right], arr[pivot]

	for i := 0; i < len(arr); i++ {
		if arr[i] < arr[right] {
			arr[i], arr[left] = arr[left], arr[i]
			left++
		}
	}

	arr[left], arr[right] = arr[right], arr[left]

	sort(arr[:left])
	sort(arr[left+1:])
	return arr
}

func solution(reader io.Reader, writer io.Writer) {
	var t int
	fmt.Fscanf(reader, "%d\n", &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscanf(reader, "%d\n", &n)
		var s int
		for j := 0; j < n; j++ {
			var a int
			fmt.Fscanf(reader, "%d", &a)
			if a <= 2048 {
				s += a
			}
		}
		if s >= 2048 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
		fmt.Fscanf(reader, "\n")
	}
}

func main() {

	//stdin, err := os.Open("file.in")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("file.out")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)
	defer writer.Flush()

	solution(reader, writer)
}

// Author: sighduck
// URL: https://codeforces.com/contest/1221/problem/A

package main

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

func Solve(n int, s []int) string {
	count := make(map[int]int)
	for _, elem := range s {
		count[elem] += 1
	}
	for i := 1; i <= 2048; i *= 2 {
		count[i*2] += count[i] / 2
	}
	if count[2048] > 0 {
		return "YES"
	}
	return "NO"
}

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

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

package main
import (
	"fmt"
	"sort"
)

func main()  {
	var query int
	fmt.Scanf("%d\n", &query)
	for i := 0; i < query; i++{
		var n int
		fmt.Scanf( "%d\n", &n)
		var number [128]int
		for j := 0; j < n; j++{
			if(j == n - 1) {
				fmt.Scanf("%d\n", &number[j])
			}else{
				fmt.Scanf("%d", &number[j])

			}
		}
		sort.Ints(number[0:n])
		start := 0
		flag := false
		number[n] = 0
		for {
			if flag || start >= n || number[start] > 2048 {
				break
			}
			//start = 0;
			//j = 5
			//4,3,2,1,0
			for j := start; j <= n; j++ {
				if number[j] == 2048 {
					flag = true
					break
				}
				if number[j] != number[start] {
					if j - start > 1 {
						for k := j - 1; k > j - 1 - (j - start) / 2; k--{
							number[k] = number[start] * 2
						}
						start = j - (j -start) / 2
					}else {
						start ++
					}
					break
				}
			}
		}
		if flag {
			fmt.Println("YES")
		}else{
			fmt.Println("NO")
		}
	}
}

package main

import (
    "fmt"
    "bufio"
    "os"
)
var input *bufio.Reader

func solve() {
    var (
        N int
    )
    fmt.Fscanf(input, "%d\n", &N)
    arr := make([]int64, N)
    for i:=0 ; i<N ; i++ {
        fmt.Fscanf(input, "%d ", &arr[i])
    }
    fmt.Fscanf(input, "\n")
    freq := make(map[int64] int)
    for i:=0 ; i<N ; i++ {
        freq[arr[i]] += 1
    }
    // check 2048 in freq
    // if not do stacking
    // if no stacking can be done, print no
    for {
        //fmt.Println(freq)
        if freq[2048] > 0 {
            fmt.Println("YES")
            return
        }
        var key int64 = 1
        flag := false
        for i:=0 ; i<=10 ; i++ {
            if freq[key] >= 2 {
                freq[key] -= 2
                freq[key*2] += 1
                flag = true
                break
            }
            key = key * 2
        }
        if flag==false {
            fmt.Println("NO")
            return
        }
    }

}

func main () {
    input = bufio.NewReader(os.Stdin)
    var T int
    fmt.Fscanf(input, "%d\n", &T)
    for i:=0 ; i<T ; i++ {
        solve()
    }
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"math/big"
	"os"
	"strconv"
)

var scan *bufio.Scanner
var reader io.Reader
var writer io.Writer

func init() {
	scan = bufio.NewScanner(os.Stdin)
	reader = os.Stdin
	writer = os.Stdout
	scan.Split(bufio.ScanWords)
}

func main() {
	solve()
}

func solve() {
	q := nextInt()
	for i := 0; i < q; i++ {
		n := nextInt()
		s := make([]int, n)
		r := 0
		res := "No"
		for j := 0; j < n; j++ {
			s[j] = nextInt()
			if s[j] > 2048 {
				continue
			}
			if s[j] <= 2048 {
				r += s[j]
				if r >= 2048 {
					res = "Yes"
				}
			}
		}
		fmt.Fprintln(writer, res)
	}
}

func nextWord() string {
	var s string
	fmt.Fscan(reader, &s)
	return s
}

func nextInt() int {
	i, e := strconv.Atoi(nextWord())
	if e != nil {
		panic(e)
	}
	return i
}

func max(x int, y ...int) int {
	res := float64(x)
	for _, z := range y {
		res = math.Max(res, float64(z))
	}
	return int(res)
}

func min(x int, y ...int) int {
	res := float64(x)
	for _, z := range y {
		res = math.Min(res, float64(z))
	}
	return int(res)
}

func abs(x int) int {
	return int(math.Abs(float64(x)))
}

func sqrt(x int) int {
	return int(math.Sqrt(float64(x)))
}

func int2Int(x int) big.Int {
	var res big.Int
	res.SetInt64(int64(x))
	return res
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var (
		count int
	)
	_, _ = fmt.Fscan(input, &count)

	for count > 0 {
		count--

		var setNum int
		_, _ = fmt.Fscan(input, &setNum)

		tmp := 0
		hasAns := false
		for i := 0; i < setNum; i++ {
			var num uint32
			_, _ = fmt.Fscan(input, &num)
			if num > 2048 || hasAns {
				continue
			}
			if num == 2048 {
				hasAns = true
			}
			tmp += int(math.Log2(float64(num)))
		}

		if hasAns{
			fmt.Println("YES")
			continue
		}

		if tmp >= 20 {
			fmt.Println("YES")
			continue
		}
		fmt.Println("NO")

	}

}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var testCase int

	_, err := fmt.Fscan(input, &testCase)
	if err != nil {
	}

	for testCase > 0 {
		testCase--

		var x int
		_, err := fmt.Fscan(input, &x)
		if err != nil {
		}

		s := make([]int, x)
		y := make([]int, x)
		for i := 0; i < x; i++ {
			_, err = fmt.Fscan(input, &s[i])
			if err != nil {
			}
		}

		sort.Ints(s)
		j := 0
		result := "NO"
		for i := 0; i < x; {
			if s[i] == 2048{
				result = "YES"
				break
			}
			if i < x-1 && s[i] == s[i+1] {
				yy := s[i] + s[i+1]
				if yy > y[j] {
					y[j+1] = yy
				} else {
					y[j+1] = yy + y[j]
				}
				j++
				i += 2
			} else if s[i] == y[j] {
				y[j+1] = s[i] + y[j]
				j++
				i++
			} else {
				i++
			}
			if y[j] == 2048 {
				result = "YES"
				break
			}
		}
		fmt.Println(result)
	}

}

package main

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

var bufin *bufio.Reader
var bufout *bufio.Writer

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()
	var n, m int
	var num int64
	fmt.Fscanf(bufin, "%d\n", &n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(bufin, "%d\n", &m)
		numMap := make(map[int]int)
		for j := 0; j < m; j++ {
			fmt.Fscanf(bufin, "%d\n", &num)
			if num <= 2048 {
				numMap[int(num)]++
			}
		}
		k := 1
		for j := 1; j < 13; j++ {
			for ; numMap[k] > 1; {
				numMap[k] -= 2
				numMap[k + k]++
			}
			if k == 2048 {
				if numMap[k] > 0 {
					fmt.Fprintf(bufout, "YES")
				} else {
					fmt.Fprintf(bufout, "NO")
				}
				break
			}
			k += k
		}
	}
}

package main
import (
	"fmt"
	"sort"
)

func main()  {
	var query int
	fmt.Scanln(&query)
	for i := 0; i < query; i++{
		var n int
		fmt.Scanln( &n)
		var number [128]int
		for j := 0; j < n; j++{
			fmt.Scan(&number[j])
		}
		sort.Ints(number[0:n])
		start := 0
		flag := false
		number[n] = 0
		for {
			if flag || start >= n || number[start] > 2048 {
				break
			}
			//start = 0;
			//j = 5
			//4,3,2,1,0
			for j := start; j <= n; j++ {
				if number[j] == 2048 {
					flag = true
					break
				}
				if number[j] != number[start] {
					if j - start > 1 {
						for k := j - 1; k > j - 1 - (j - start) / 2; k--{
							number[k] = number[start] * 2
						}
						start = j - (j -start) / 2
					}else {
						start ++
					}
					break
				}
			}
		}
		if flag {
			fmt.Println("YES")
		}else{
			fmt.Println("NO")
		}
	}
}

package main
import (
	"fmt"
	"sort"
)

func main()  {
	var query int
	fmt.Scanln(&query)
	for i := 0; i < query; i++{
		var n int
		var tmp string
		fmt.Scanln( &tmp)
		var number [128]int
		for j := 0; j < n; j++{
			fmt.Scan(&number[j])
		}
		fmt.Scanln()
		sort.Ints(number[0:n])
		start := 0
		flag := false
		number[n] = 0
		for {
			if flag || start >= n || number[start] > 2048 {
				break
			}
			//start = 0;
			//j = 5
			//4,3,2,1,0
			for j := start; j <= n; j++ {
				if number[j] == 2048 {
					flag = true
					break
				}
				if number[j] != number[start] {
					if j - start > 1 {
						for k := j - 1; k > j - 1 - (j - start) / 2; k--{
							number[k] = number[start] * 2
						}
						start = j - (j -start) / 2
					}else {
						start ++
					}
					break
				}
			}
		}
		if flag {
			fmt.Println("YES")
		}else{
			fmt.Println("NO")
		}
	}
}

package main
import (
	"fmt"
	"sort"
)

func main()  {
	var query int
	fmt.Scanln(&query)
	for i := 0; i < query; i++{
		var n int
		fmt.Scanln( &n)
		fmt.Printf("number %d\n", n)
		var number [128]int
		for j := 0; j < n; j++{
			fmt.Scanf("%d", &number[j])
		}
		sort.Ints(number[0:n])
		start := 0
		flag := false
		number[n] = 0
		for {
			if flag || start >= n || number[start] > 2048 {
				break
			}
			//start = 0;
			//j = 5
			//4,3,2,1,0
			for j := start; j <= n; j++ {
				if number[j] == 2048 {
					flag = true
					break
				}
				if number[j] != number[start] {
					if j - start > 1 {
						for k := j - 1; k > j - 1 - (j - start) / 2; k--{
							number[k] = number[start] * 2
						}
						start = j - (j -start) / 2
					}else {
						start ++
					}
					break
				}
			}
		}
		if flag {
			fmt.Println("YES")
		}else{
			fmt.Println("NO")
		}
	}
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var (
		count int
	)
	_, _ = fmt.Fscan(input, &count)

	for count > 0 {
		count--

		var setNum int
		_, _ = fmt.Fscan(input, &setNum)

		hasAns := false
		countMap := map[uint32]int{}
		for i := 0; i < setNum; i++ {
			var num uint32
			_, _ = fmt.Fscan(input, &num)
			if num > 2048 || hasAns {
				continue
			}
			if num == 2048 {
				hasAns = true
			}
			if _, ok := countMap[num]; ok {
				countMap[num] = countMap[num] + 1
			} else {
				countMap[num] = 1
			}
		}

		if hasAns {
			fmt.Println("YES")
			continue
		}

		for i := 2; i < 4096; i *= 2 {
			if _, ok := countMap[uint32(i)]; ok {

				if _, ok := countMap[uint32(i*2)]; ok {
					countMap[uint32(i*2)] = countMap[uint32(i)]/2 + countMap[uint32(i*2)]
				} else {
					countMap[uint32(i*2)] = countMap[uint32(i)] / 2
				}
				countMap[uint32(i)] = countMap[uint32(i)] % 2

			} else {
				countMap[uint32(i)] = 0
			}
		}

		if countMap[uint32(2048)] > 0 {
			fmt.Println("YES")
			continue
		}
		fmt.Println("NO")

	}

}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var (
		count int
	)
	_, _ = fmt.Fscan(input, &count)

	for count > 0 {
		count--

		var setNum int
		_, _ = fmt.Fscan(input, &setNum)

		hasAns := false
		countMap := map[uint32]int{}
		for i := 0; i < setNum; i++ {
			var num uint32
			_, _ = fmt.Fscan(input, &num)
			if num > 2048 || hasAns {
				continue
			}
			if num == 2048 {
				hasAns = true
			}
			if _, ok := countMap[num]; ok {
				countMap[num] = countMap[num] + 1
			} else {
				countMap[num] = 1
			}
		}

		if hasAns {
			fmt.Println("YES")
			continue
		}

		for i := 1; i < 4096; i *= 2 {
			if _, ok := countMap[uint32(i)]; ok {

				if _, ok := countMap[uint32(i*2)]; ok {
					countMap[uint32(i*2)] = countMap[uint32(i)]/2 + countMap[uint32(i*2)]
				} else {
					countMap[uint32(i*2)] = countMap[uint32(i)] / 2
				}
				countMap[uint32(i)] = countMap[uint32(i)] % 2

			} else {
				countMap[uint32(i)] = 0
			}
		}

		if countMap[uint32(2048)] > 0 {
			fmt.Println("YES")
			continue
		}
		fmt.Println("NO")

	}

}


package main

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

func solve() {
	n := readInt()
	a := make([]int, n)
	b := make([]int, n)
	heapA := NewMinHeap(n)
	for i := 0; i < n; i++ {
		a[i] = readInt()
		heapA.Add(i, -a[i])
	}
	heapB := NewMinHeap(n)
	for i := 0; i < n; i++ {
		b[i] = readInt()
		heapB.Add(i, b[i])
	}
	for i := 0; i < n; i++ {
		first := heapA.Min()
		indexA := heapA.ArgMin()
		heapA.Remove(indexA)
		indexB := heapB.ArgMin()
		heapB.Remove(indexB)
		a[indexB] = -first
	}
	var buffer bytes.Buffer
	for i := 0; i < n; i++ {
		if i > 0 {
			buffer.WriteString(" ")
		}
		buffer.WriteString(strconv.Itoa(a[i]))
	}
	fmt.Println(buffer.String())
}

type MinHeap struct {
	a    []int
	vmap []int
	imap []int
	n    int
	pos  int
}

const INF = math.MaxInt32

func NewMinHeap(m int) *MinHeap {
	heap := &MinHeap{}
	m += 2
	heap.n = m
	heap.a = make([]int, heap.n)
	heap.vmap = make([]int, heap.n)
	heap.imap = make([]int, heap.n)
	heap.pos = 1
	for i := 0; i < heap.n; i++ {
		heap.a[i] = INF
		heap.vmap[i] = -1
		heap.imap[i] = -1
	}
	return heap
}

func (h *MinHeap) Add(ind, x int) int {
	ret := h.imap[ind]
	if h.imap[ind] < 0 {
		h.a[h.pos] = x
		h.vmap[h.pos] = ind
		h.imap[ind] = h.pos
		h.pos++
		h.up(h.pos - 1)
	}
	if ret != -1 {
		return h.a[ret]
	}
	return x
}

func (h *MinHeap) Update(ind, x int) int {
	ret := h.imap[ind]
	if h.imap[ind] < 0 {
		h.a[h.pos] = x
		h.vmap[h.pos] = ind
		h.imap[ind] = h.pos
		h.pos++
		h.up(h.pos - 1)
	} else {
		h.a[ret] = x
		h.up(ret)
		h.down(ret)
	}
	return x
}

func (h *MinHeap) Remove(ind int) int {
	if h.pos == 1 {
		return INF
	}
	if h.imap[ind] == -1 {
		return INF
	}

	h.pos--
	rem := h.imap[ind]
	ret := h.a[rem]
	h.vmap[rem] = h.vmap[h.pos]
	h.imap[h.vmap[h.pos]] = rem
	h.imap[ind] = -1
	h.a[rem] = h.a[h.pos]
	h.a[h.pos] = INF
	h.vmap[h.pos] = -1

	h.up(rem)
	h.down(rem)
	return ret
}

func (h *MinHeap) Min() int {
	return h.a[1]
}

func (h *MinHeap) ArgMin() int {
	return h.vmap[1]
}

func (h *MinHeap) Size() int {
	return h.pos - 1
}

func (h *MinHeap) up(cur int) {
	for c, p := cur, cur>>1; p >= 1 && h.a[p] > h.a[c]; c, p = c>>1, p>>1 {
		h.a[p], h.a[c] = h.a[c], h.a[p]
		h.imap[h.vmap[p]], h.imap[h.vmap[c]] = h.imap[h.vmap[c]], h.imap[h.vmap[p]]
		h.vmap[p], h.vmap[c] = h.vmap[c], h.vmap[p]
	}
}

func (h *MinHeap) down(cur int) {
	for c := cur; 2*c < h.pos; {
		b := 0
		if h.a[2*c] < h.a[2*c+1] {
			b = 2 * c
		} else {
			b = 2*c + 1
		}
		if h.a[b] < h.a[c] {
			h.a[c], h.a[b] = h.a[b], h.a[c]
			h.imap[h.vmap[c]], h.imap[h.vmap[b]] = h.imap[h.vmap[b]], h.imap[h.vmap[c]]
			h.vmap[c], h.vmap[b] = h.vmap[b], h.vmap[c]
			c = b
		} else {
			break
		}
	}
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

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

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

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

package main

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

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

	var tuples = make(SortInterFace, N)
	for i := 0; i < N; i++ {
		tuples[i] = tuple{B[i], i}
	}
	sort.Ints(A)

	sort.Sort(tuples)

	ans := make([]int, N)
	for i := 0; i < N; i++ {
		ans[tuples[i].order] = A[i]
	}

	PrintI(ans...)
}

func mapToString(arr []int) []string {
	ret := make([]string, len(arr))
	for i := 0; i < len(arr); i++ {
		ret[i] = strconv.Itoa(arr[i])
	}
	return ret
}

func PrintI(args ...int) {
	fmt.Println(strings.Join(mapToString(args), " "))
}

type tuple struct {
	value int
	order int
}

type SortInterFace []tuple

func (s SortInterFace) Len() int {
	return len(s)
}
func (s SortInterFace) Less(i, j int) bool {
	return s[i].value > s[j].value
}
func (s SortInterFace) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

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

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

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

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

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

	return mp
}

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

/*
URL:
https://codeforces.com/problemset/problem/840/A
*/

package main

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

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

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

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

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

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

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

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

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

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

var (
	m    int
	A, B []int
)

func main() {
	m = ReadInt()
	A, B = ReadIntSlice(m), ReadIntSlice(m)

	T := []Tuple{}
	for i := 0; i < m; i++ {
		T = append(T, Tuple{idx: i, a: -1, b: B[i]})
	}
	sort.SliceStable(T, func(i, j int) bool {
		return T[i].b > T[j].b
	})
	sort.Sort(sort.IntSlice(A))

	for i := 0; i < m; i++ {
		T[i].a = A[i]
	}

	sort.SliceStable(T, func(i, j int) bool {
		return T[i].idx < T[j].idx
	})

	answers := make([]int, m)
	for i := 0; i < m; i++ {
		answers[i] = T[i].a
	}
	fmt.Println(PrintIntsLine(answers...))
}

type Tuple struct {
	idx, a, b int
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

/*
URL:
https://codeforces.com/problemset/problem/840/A
*/

package main

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

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

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

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

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

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

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

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

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

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

var (
	m    int
	A, B []int
)

func main() {
	m = ReadInt()
	A, B = ReadIntSlice(m), ReadIntSlice(m)

	fmt.Println(PrintIntsLine(A...))
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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


package main

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

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

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n, k := readTwoNums(reader)
		s, _ := reader.ReadString('\n')
		fmt.Println(solve(n, k, s[:n]))
	}
}

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, k int, s string) int {
	// find all divisors of k
	cnt := make(map[byte]int)

	for i := 0; i < n; i++ {
		cnt[s[i]]++
	}

	arr := make([]int, 0, len(cnt))
	for _, v := range cnt {
		arr = append(arr, v)
	}

	sort.Ints(arr)

	check := func(l, k int) bool {
		// can we have a l length segments length, each seg has k beads
		for i := len(arr) - 1; i >= 0 && arr[i] >= l && k > 0; i-- {
			x := arr[i] / l
			k -= x
		}

		return k <= 0
	}

	find := func(k int) int {
		// one segment has k
		var left, right = 1, n + 1

		for left < right {
			mid := (left + right) / 2
			if !check(mid, k) {
				right = mid
			} else {
				left = mid + 1
			}
		}
		right--
		return right * k
	}

	best := 1
	for x := 1; x*x <= k; x++ {
		if k%x != 0 {
			continue
		}

		best = max(best, find(x))

		if k/x != x {
			best = max(best, find(k/x))
		}
	}

	return best
}

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

package main

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

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

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n, k := readTwoNums(reader)
		s, _ := reader.ReadString('\n')
		fmt.Println(solve(n, k, s[:n]))
	}
}

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, k int, s string) int {
	cnt := make(map[byte]int)

	for i := 0; i < n; i++ {
		cnt[s[i]]++
	}

	cycles := make([]int, n)

	used := make([]bool, n)

	for l := n; l > 0; l-- {
		var p int
		for i := 0; i < l; i++ {
			if used[i] {
				continue
			}
			used[i] = true
			var x = 1

			j := (i + k) % l

			for !used[j] {
				x++
				used[j] = true
				j = (j + k) % l
			}
			cycles[p] = x
			p++
		}

		canFill := true

		sort.Ints(cycles[:p])
		pq := make(IntHeap, 0, len(cnt))

		for _, v := range cnt {
			heap.Push(&pq, v)
		}

		for p > 0 {
			cur := cycles[p-1]
			p--

			top := heap.Pop(&pq).(int)

			if top < cur {
				canFill = false
				break
			}

			heap.Push(&pq, top-cur)
		}

		if canFill {
			return l
		}

		for i := 0; i < n; i++ {
			used[i] = false
		}
	}
	return 0
}

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

func solve1(n, k int, s string) int {
	// find all divisors of k
	cnt := make(map[byte]int)

	for i := 0; i < n; i++ {
		cnt[s[i]]++
	}

	arr := make([]int, 0, len(cnt))
	for _, v := range cnt {
		arr = append(arr, v)
	}

	sort.Ints(arr)

	check := func(l, k int) bool {
		// can we have a l length segments length, each seg has k beads
		for i := len(arr) - 1; i >= 0 && arr[i] >= l && k > 0; i-- {
			x := arr[i] / l
			k -= x
		}

		return k <= 0
	}

	find := func(k int) int {
		// one segment has k
		var left, right = 1, n + 1

		for left < right {
			mid := (left + right) / 2
			if !check(mid, k) {
				right = mid
			} else {
				left = mid + 1
			}
		}
		right--
		return right * k
	}

	best := 1
	for x := 1; x*x <= k; x++ {
		if k%x != 0 {
			continue
		}

		best = max(best, find(x))

		if k/x != x {
			best = max(best, find(k/x))
		}
	}

	return best
}

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

package main

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

var (
	inn = bufio.NewScanner(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func main() {
	defer out.Flush()
	inn.Buffer([]byte{}, 1000000)
	inn.Split(bufio.ScanWords)
	//prep
	for tt := rd(); tt > 0; tt-- {
		n, k := rd(), rd()
		s := rds()
		cnt := [26]int{}
		for _, ch := range s {
			cnt[ch-'a']++
		}
		for i := n; i > 0; i-- {
			t := i / gcd(i, k)
			sum := 0
			for j := 0; j < 26; j++ {
				sum += cnt[j] / t
			}
			if sum*t >= i {
				Fprintln(out, i)
				break
			}
		}
	}
}

//-------------------template------------------
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func maxll(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func minll(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func rd() (x int) {
	inn.Scan()
	x, _ = strconv.Atoi(string(inn.Bytes()))
	return x
}
func rdll() (x int64) {
	inn.Scan()
	x, _ = strconv.ParseInt(string(inn.Bytes()), 10, 64)
	return x
}
func rds() string {
	inn.Scan()
	return string(inn.Bytes())
}

package main

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

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

	var t, n, k int

	fmt.Fscanf(reader, "%d\n", &t)
	for ; t > 0; t-- {
		var s string
		fmt.Fscanf(reader, "%d %d\n%s\n", &n, &k, &s)
		lets := make([]int, 33)
		for i := 0; i < n; i++ {
			lets[s[i]-'a']++
		}

		sort.Ints(lets)

		maxLen := lets[32]
		if k == n {
			maxLen = n
			fmt.Println(maxLen)
			continue
		}

		//fmt.Println(lets)
		kk := make([]int, 0, 2000)
		for i := 2; i <= k; i++ {
			if k%i == 0 {
				kk = append(kk, i)
			}
		}
		for _, g := range kk {
			for zn := 1; zn <= n/g; zn++ {
				gs := 0
				for i := 32; i >= 0 && lets[i] >= zn; i-- {
					gs += (lets[i] / zn)
					if gs >= g {
						if g*zn > maxLen {
							maxLen = g * zn
						}
						break
					}
				}
			}
		}

		fmt.Println(maxLen)
	}
}