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

		var a, b int
		var x, y int
		min := 10
		for i, v := range s {
			if v == '1' {
				if i%2 == 0 {
					a++
					x++
				} else {
					b++
					y++
				}
			} else if v == '?' && i%2 == 0 {
				a++
			} else if v == '?' && i%2 != 0 {
				y++
			}

			if a > b+((10-i)/2) || y > x+(9-i)/2 {
				min = i + 1
				break
			}
		}

		fmt.Fprintln(out, min)
	}
}

package main

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

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

func main() {
	var test int
	fmt.Fscan(in, &test)
	for i := 0; i < test; i++ {
		solve()
	}

}
func solve() {
	var givenString string
	fmt.Fscan(in, &givenString)
	ans := rec(givenString, 5, 5, 1, 0, 0, 0)
	fmt.Println(ans)
}
func rec(givenString string, firstPlayerMove int, secondPlayerMove int, currentPlayer int, firstPlayerScore int, SecondPlayerScore int, currentIndex int) int {
	if currentIndex == 10 {
		return 0
	}
	if firstPlayerMove+firstPlayerScore < SecondPlayerScore {
		return 0
	}
	if secondPlayerMove+SecondPlayerScore < firstPlayerScore {
		return 0
	}
	if string(givenString[currentIndex]) == "1" {
		if currentPlayer%2 == 0 {
			secondPlayerMove--
			SecondPlayerScore++
		} else {
			firstPlayerMove--
			firstPlayerScore++
		}
		return 1 + rec(givenString, firstPlayerMove, secondPlayerMove, currentPlayer+1, firstPlayerScore, SecondPlayerScore, currentIndex+1)
	} else if string(givenString[currentIndex]) == "0" {
		if currentPlayer%2 == 0 {
			secondPlayerMove--
		} else {
			firstPlayerMove--
		}
		return 1 + rec(givenString, firstPlayerMove, secondPlayerMove, currentPlayer+1, firstPlayerScore, SecondPlayerScore, currentIndex+1)
	} else {
		if currentPlayer%2 == 0 {
			first := rec(givenString, firstPlayerMove, secondPlayerMove-1, currentPlayer+1, firstPlayerScore, SecondPlayerScore, currentIndex+1)
			second := rec(givenString, firstPlayerMove, secondPlayerMove-1, currentPlayer+1, firstPlayerScore, SecondPlayerScore+1, currentIndex+1)
			return 1 + int(math.Min(float64(first), float64(second)))
		} else {
			first := rec(givenString, firstPlayerMove-1, secondPlayerMove, currentPlayer+1, firstPlayerScore, SecondPlayerScore, currentIndex+1)
			second := rec(givenString, firstPlayerMove-1, secondPlayerMove, currentPlayer+1, firstPlayerScore+1, SecondPlayerScore, currentIndex+1)
			return 1 + int(math.Min(float64(first), float64(second)))
		}
	}
}

//x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-TEMPLATE_STARTING-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-

type pair struct {
	a int64
	b int64
}

func sortPair(pairs []pair) {
	sort.Slice(pairs, func(i, j int) bool {
		return pairs[i].a < pairs[j].a
	})
}
func max(a int64, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

func printArray(arr []int) {
	for i := 1; i < len(arr); i++ {
		fmt.Print(arr[i], " ")
	}
	fmt.Println()
}

type runeSort []rune

func (r runeSort) Len() int           { return len(r) }
func (r runeSort) Swap(i, j int)      { r[i], r[j] = r[j], r[i] }
func (r runeSort) Less(i, j int) bool { return r[i] < r[j] }

package main

import (
	"fmt"
)

func main() {
	var T int
	fmt.Scanf("%d\n", &T)
	for t:= 0; t<T; t++{
		var s string
		fmt.Scanf("%s\n",&s)
		var a,b,c,d int = 0,0,0,0
		ans := 10
		for i := 0; i < 10; i++{
			if s[i] == '1'{
				if i % 2 == 0 {
					a++
				}else{
					b++
				}
			}
			if s[i] == '?'{
				if i % 2 ==0{
					c++
				}else{
					d++
				}
			}
			if a + c > b + (10 - i) / 2 || b + d > a + (9 - i) / 2{
				ans = i + 1
				break
			}
		}
		fmt.Printf("%d\n", ans)
	}
}

package main

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

func main() {
	defer wr.Flush()
	t := scanInt()

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

func solve() {
	word := scanWord()
	res := strings.Split(word, "")

	aWins := make([]int, 10)
	bWins := make([]int, 10)

	for i := range res {
		if res[i] == "1" {
			aWins[i] = 1
			bWins[i] = 1
		} else if res[i] == "0" {
			aWins[i] = 0
			bWins[i] = 0
		} else {
			if i%2 == 0 {
				aWins[i] = 1
				bWins[i] = 0
			} else {
				aWins[i] = 0
				bWins[i] = 1
			}
		}
	}

	var aScore, bScore = 0, 0
	var aRem, bRem = 5, 5
	var minPen int

	for i := range aWins {
		if i%2 == 0 {
			aScore += aWins[i]
		} else {
			bScore += aWins[i]
			bRem -= 1
		}
		if aScore > bScore + bRem || i == 9 {
			minPen = i + 1
			break
		}
	}

	aScore, bScore = 0, 0

	for i := range bWins {
		if i%2 == 0 {
			aScore += bWins[i]
			aRem -= 1
		} else {
			bScore += bWins[i]
		}
		if bScore > aScore + aRem || i == 9 {
			if i + 1 < minPen {
				minPen = i + 1
				break
			}
		}
	}

	fmt.Println(minPen)

}

var (
	sc *bufio.Scanner
	wr *bufio.Writer
)

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

	wr = bufio.NewWriter(os.Stdout)
}

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

func scanWord() string {
	sc.Scan()
	ret := sc.Text()
	return ret
}

package main

import "fmt"

func min_penalty_balls(pred_res string){
	team_a_certain := 0
	team_b_certain := 0
	team_a_uncertain := 0
	team_b_uncertain := 0
	for i := 0; i < len(pred_res); i++{
		ch := pred_res[i]
		if i%2 == 0 {
			switch ch{
				case '1': team_a_certain += 1
				case '?': team_a_uncertain += 1
			}
		}else{
			switch ch{
				case '1': team_b_certain += 1
				case '?': team_b_uncertain += 1
			}
		}
		var max_a = team_a_certain + team_a_uncertain
		var min_b = (10-i)/2 + team_b_certain
		var max_b = team_b_certain + team_b_uncertain
		var min_a = (9-i)/2 + team_a_certain
		if max_a > min_b{
			fmt.Println(i+1)
			break
		}
		if max_b > min_a{
			fmt.Println(i+1)
			break
		}
		if i == 9{
			fmt.Println(10)
		}
	}
}

func main(){
	var n int
	fmt.Scanln(&n)
	for i := 0; i<n; i++{
		var pred string
		fmt.Scanln(&pred)
		min_penalty_balls(pred)
	}
}

package main

import "fmt"

const maxKick=10
type score struct {
	possible int
	max,min int
}
func updateScore(sc *score,ch int32){
	if ch=='1'{
		sc.max++
		sc.min++
	}else if ch=='?'{
		sc.max++
	}
	sc.possible--
}
func solve (s string) int{
	var score1,score2 score
	score1.possible=maxKick/2
	score2.possible=maxKick/2
	for i,ch:=range s{
		if i%2==0{
			updateScore(&score1,ch)
		}else{
			updateScore(&score2,ch)
		}
		if score1.max>score2.possible+score2.min || score2.max>score1.possible+score1.min{
			return i+1
		}

	}
	return maxKick
}
func penalty(){
	var cases int
	_,err:=fmt.Scanf("%d\n",&cases)
	if err!=nil{
		return
	}
	for i:=0;i<cases;i++{
		var str string
		_,err=fmt.Scanf("%s\n",&str)
		if err!=nil{
			return
		}
		ans:=solve(str)
		fmt.Printf("%d\n",ans)
	}
}
func main() {
	penalty()
}

package main

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

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

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

func _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func _string() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	for t := _int(); t > 0; t-- {
		printf("%d\n", sol(_string()))
	}
}

func sol(s string) int {
	return min(max1(s), max2(s))
}

func max1(s string) int {
	g1 := 0
	g2 := 0
	r1 := 5
	r2 := 5
	for i := range s {
		if i&1 == 0 { // team 1
			r1--
			if s[i] == '?' || s[i] == '1' {
				g1++
			}
		} else { // team 2
			r2--
			if s[i] == '1' {
				g2++
			}
		}
		if g1 > g2+r2 {
			return i + 1
		}
	}
	return 10
}

func max2(s string) int {
	g1 := 0
	g2 := 0
	r1 := 5
	r2 := 5
	for i := range s {
		if i&1 == 1 { // team 2
			r2--
			if s[i] == '?' || s[i] == '1' {
				g2++
			}
		} else {
			r1--
			if s[i] == '1' {
				g1++
			}
		}
		if g2 > g1+r1 {
			return i + 1
		}
	}
	return 10
}

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

package main

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

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

func main() {
	var test int
	fmt.Fscan(in, &test)
	for i := 0; i < test; i++ {
		solve()
	}

}
func solve() {
	ans = math.MaxInt64
	var givenString string
	fmt.Fscan(in, &givenString)
	helper(givenString, "first", 0, 0, 0, 0, 0)
	fmt.Println(ans)

}

func helper(givenString string, currentTurn string, numberOfTurnBy1 int, numberOfTurnBy2 int, firstTeamScore int, secondTeamScore int, totalTurn int) {
	if totalTurn == 10 {
		ans = int64(math.Min(float64(ans), float64(totalTurn)))
		return
	}
	if currentTurn == "second" {
		if secondTeamScore+(5-numberOfTurnBy2) < firstTeamScore {
			ans = int64(math.Min(float64(ans), float64(totalTurn)))
			return
		} else if numberOfTurnBy2 == 4 && firstTeamScore > secondTeamScore && string(givenString[totalTurn]) == "0" {
			ans = int64(math.Min(float64(ans), float64(totalTurn+1)))
			return
		}
	} else if currentTurn == "first" {
		if firstTeamScore+(5-numberOfTurnBy1) < secondTeamScore {
			ans = int64(math.Min(float64(ans), float64(totalTurn)))
			return
		} else if numberOfTurnBy1 == 4 && secondTeamScore > firstTeamScore && string(givenString[totalTurn]) == "0" {
			ans = int64(math.Min(float64(ans), float64(totalTurn+1)))
			return
		}
	}
	if currentTurn == "first" {
		if string(givenString[totalTurn]) == "?" {
			helper(givenString, "second", numberOfTurnBy1+1, numberOfTurnBy2, firstTeamScore+0, secondTeamScore, totalTurn+1)
			helper(givenString, "second", numberOfTurnBy1+1, numberOfTurnBy2, firstTeamScore+1, secondTeamScore, totalTurn+1)
		} else if string(givenString[totalTurn]) == "1" {
			helper(givenString, "second", numberOfTurnBy1+1, numberOfTurnBy2, firstTeamScore+1, secondTeamScore, totalTurn+1)
		} else {
			helper(givenString, "second", numberOfTurnBy1+1, numberOfTurnBy2, firstTeamScore+0, secondTeamScore, totalTurn+1)
		}
	} else {
		if string(givenString[totalTurn]) == "?" {
			helper(givenString, "first", numberOfTurnBy1, numberOfTurnBy2+1, firstTeamScore+0, secondTeamScore, totalTurn+1)
			helper(givenString, "first", numberOfTurnBy1, numberOfTurnBy2+1, firstTeamScore, secondTeamScore+1, totalTurn+1)
		} else if string(givenString[totalTurn]) == "1" {
			helper(givenString, "first", numberOfTurnBy1, numberOfTurnBy2+1, firstTeamScore, secondTeamScore+1, totalTurn+1)
		} else {
			helper(givenString, "first", numberOfTurnBy1, numberOfTurnBy2+1, firstTeamScore+0, secondTeamScore, totalTurn+1)
		}
	}
}

//x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-TEMPLATE_STARTING-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-

type pair struct {
	a int64
	b int64
}

func sortPair(pairs []pair) {
	sort.Slice(pairs, func(i, j int) bool {
		return pairs[i].a < pairs[j].a
	})
}
func max(a int64, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

func printArray(arr []int) {
	for i := 1; i < len(arr); i++ {
		fmt.Print(arr[i], " ")
	}
	fmt.Println()
}

type runeSort []rune

func (r runeSort) Len() int           { return len(r) }
func (r runeSort) Swap(i, j int)      { r[i], r[j] = r[j], r[i] }
func (r runeSort) Less(i, j int) bool { return r[i] < r[j] }


package main
import "fmt"
import "bufio"
import "os"

var input *bufio.Reader
const p int64 = int64(203)
const MOD int64 = int64(1000000007)
const N int = 1000005
var hash, rev, pw [N]int64

func pow(a, n int64) int64 {
    ans := int64(1)
    for ; n>0; n/=2 {
        if n%2==1 {
            ans = ans * a % MOD
        }
        a = a * a % MOD
    }
    return ans
}

func gao(t []byte, length int) {
    for i, j := range t {
        hash[i+length] = pw[length+i]*int64(j)%MOD
        if i+length > 0 {
            hash[i+length] += hash[i+length-1]
            hash[i+length] %= MOD
        }
    }
}

func gaogao(s string) []int64 {
    ans := make([]int64, len(s))
    for i, j := range s {
        ans[i] = pw[i]*int64(j)%MOD
        if i>0 {
            ans[i] += ans[i-1]
            ans[i] %= MOD
        }
    }
    return ans
}

func gethash(length, lhash int) int64 {
    if length > lhash {
        return -1
    } else if length == lhash {
        return hash[lhash-1]
    } else {
        return (hash[lhash-1] - hash[lhash-length-1] + MOD) * rev[lhash-length] % MOD
    }
}

func main(){
    input = bufio.NewReader(os.Stdin)

    rev[0] = 1; pw[0] = 1
    rev[1] = pow(p, MOD-2); pw[1] = p
    for i:=2; i<=1000002; i++ {
        rev[i] = rev[i-1] * rev[1] % MOD
        pw[i] = pw[i-1] * p %MOD
    }

    var s, t string
    var n int
    fmt.Fscanf(input, "%d\n%s", &n, &s)
    gao([]byte(s), 0)
    ans := []byte(s)

    for i:=1; i<n; i++ {
        fmt.Fscanf(input, "%s", &t)
        h := gaogao(t)
        flag := false
        for j:=len(h)-1; j>=0; j-- {
            if h[j] == gethash(j+1, len(ans)) {
                gao([]byte(t[j+1:]), len(ans))
                for k:=j+1; k<len(t); k++ {
                    ans = append(ans, t[k])
                }
                flag = true
                break
            }
        }
        if !flag {
            gao([]byte(t[:]), len(ans))
            for k:=0; k<len(t); k++ {
                ans = append(ans, t[k])
            }
        }
    }

    fmt.Println(string(ans))

}

package main

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

func Sol1200E(reader io.Reader, writer io.Writer) {
	maxMergeLen := func(s string) int {
		n := len(s)
		f := make([]int, n)
		cnt := 0
		for i := 1; i < n; i++ {
			c := s[i]
			for cnt > 0 && s[cnt] != c {
				cnt = f[cnt-1]
			}
			if s[cnt] == c {
				cnt++
			}
			f[i] = cnt
		}
		return f[n-1]
	}

	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	var w, tmpMerge string
	Fscan(in, &n, &w)
	ans := append(make([]byte, 0, 1e6), w...)
	for n--; n > 0; n-- {
		Fscan(in, &w)
		if len(ans) >= len(w) {
			tmpMerge = w + "$" + string(ans[len(ans)-len(w):])
		} else {
			tmpMerge = w[:len(ans)] + "$" + string(ans)
		}
		if l := maxMergeLen(tmpMerge); l < len(w) {
			ans = append(ans, w[l:]...)
		}
	}
	Fprintln(out, string(ans))
}

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

package main
import "fmt"
import "bufio"
import "os"

var input *bufio.Reader
const p int64 = int64(203)
const MOD int64 = int64(1000000007)
var hash [1000005]int64
var rev [1000005]int64
var pw [1000005]int64

func pow(a, n int64) int64 {
    ans := int64(1)
    for ; n>0; n/=2 {
        if n%2==1 {
            ans = ans * a % MOD
        }
        a = a * a % MOD
    }
    return ans
}

func gao(t []byte, length int) {
    for i, j := range t {
        hash[i+length] = pw[length+i]*int64(j)%MOD
        if i+length > 0 {
            hash[i+length] += hash[i+length-1]
            hash[i+length] %= MOD
        }
    }
}

func gaogao(s string) []int64 {
    ans := make([]int64, len(s))
    for i, j := range s {
        ans[i] = pw[i]*int64(j)%MOD
        if i>0 {
            ans[i] += ans[i-1]
            ans[i] %= MOD
        }
    }
    return ans
}

func gethash(length, lhash int) int64 {
    if length > lhash {
        return -1
    } else if length == lhash {
        return hash[lhash-1]
    } else {
        return (hash[lhash-1] - hash[lhash-length-1] + MOD) * rev[lhash-length] % MOD
    }
}

func main(){
    input = bufio.NewReader(os.Stdin)

    rev[0] = 1; pw[0] = 1
    rev[1] = pow(p, MOD-2)
    pw[1] = p
    for i:=2; i<=1000002; i++ {
        rev[i] = rev[i-1] * rev[1] % MOD
        pw[i] = pw[i-1] * p %MOD
    }

    var s, t string
    var n int
    fmt.Fscanf(input, "%d\n%s", &n, &s)
    gao([]byte(s), 0)
    ans := []byte(s)

    for i:=1; i<n; i++ {
        fmt.Fscanf(input, "%s", &t)
        h := gaogao(t)
        flag := false
        /*
        fmt.Println(string(ans))
        for j:=0; j<len(ans); j++ {
            fmt.Printf("%d ", hash[j])
        }
        fmt.Println("")
        */
        for j:=len(h)-1; j>=0; j-- {
            //fmt.Printf("%d %d %d %d\n", i, j, h[j], gethash(j+1, len(ans)))
            if h[j] == gethash(j+1, len(ans)) {
                gao([]byte(t[j+1:len(t)]), len(ans))
                for k:=j+1; k<len(t); k++ {
                    ans = append(ans, t[k])
                }
                flag = true
                break
            }
        }
        if !flag {
            gao([]byte(t[0:len(t)]), len(ans))
            for k:=0; k<len(t); k++ {
                ans = append(ans, t[k])
            }
        }
    }

    fmt.Println(string(ans))

}

package main

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

func Sol1200E(reader io.Reader, writer io.Writer) {
	min := func(a, b int) int {
		if a <= b {
			return a
		}
		return b
	}

	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	var w, prevW string
	Fscan(in, &n, &prevW)

	Fprint(out, prevW)
	for i := 0; i < n; i++ {
		Fscan(in, &w)
		common := min(len(prevW), len(w))
		for ; common > 0; common-- {
			if prevW[len(prevW)-common:] == w[:common] {
				break
			}
		}
		Fprint(out, w[common:])
		prevW = w
	}
}

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


// 297B
package main

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

func main() {
	var i, n, m, k int
	var a, b sort.IntSlice
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n)
	fmt.Fscan(in, &m)
	fmt.Fscan(in, &k)
	a = make(sort.IntSlice, n, n)
	b = make(sort.IntSlice, m, m)
	for i = 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}
	for i = 0; i < m; i++ {
		fmt.Fscan(in, &b[i])
	}
	a.Sort()
	b.Sort()
	for n != 0 && m != 0 && a[n-1] <= b[m-1] {
		n--
		m--
	}

	if n != 0 {
		fmt.Print("YES")
	} else {
		fmt.Print("NO")
	}
}

package main

import "os"
import "bufio"
import "strconv"
import "sort"

func main(){
	var n, m, k int
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	w := bufio.NewWriter(os.Stdout)
	r.Scan()
	n, _ = strconv.Atoi(r.Text())
	r.Scan()
	m, _ = strconv.Atoi(r.Text())
	r.Scan()
	k, _ = strconv.Atoi(r.Text())
	a := make([]int, 111111)
	b := make([]int, 111111)
	k = k
	for i:=0; i<n; i++ {
		r.Scan()
		a[i], _ = strconv.Atoi(r.Text())
	}
	for i:=0; i<m; i++ {
		r.Scan()
		b[i], _ = strconv.Atoi(r.Text())
	}
	sort.Ints(a)
	sort.Sort(sort.Reverse(sort.IntSlice(a)))
	sort.Ints(b)
	sort.Sort(sort.Reverse(sort.IntSlice(b)))
	for j:=0; j<n; j++ {
		if a[j] > b[j] {
			w.WriteString("YES\n")
			w.Flush()
			return
		}
	}
	w.WriteString("NO\n")
	w.Flush()
}

package main

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

func main() {
	var (
		na, nb, k, i int32
		a, b         sort.IntSlice
		in           *bufio.Reader
	)

	in = bufio.NewReader(os.Stdin)

	fmt.Fscan(in, &na)
	fmt.Fscan(in, &nb)
	fmt.Fscan(in, &k)

	a = make(sort.IntSlice, na, na)
	b = make(sort.IntSlice, nb, nb)

	for i = 0; i < na; i++ {
		fmt.Fscan(in, &a[i])
	}
	for i = 0; i < nb; i++ {
		fmt.Fscan(in, &b[i])
	}

	a.Sort()
	b.Sort()

	na--
	nb--
	for na >= 0 && nb >= 0 {
		if a[na] > b[nb] {
			break
		}
		na--
		nb--
	}

	if na < 0 {
		fmt.Println("NO")
	} else {
		fmt.Println("YES")
	}

}

package main

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

func Readint(r *bufio.Reader) int32 {
	var c byte = 0
	for {
		c, _ = r.ReadByte()
		if c >= '0' && c <= '9' {
			break
		}
	}
	var ret int32 = 0
	for {
		ret = 10*ret + int32(c-'0')
		c, _ = r.ReadByte()
		if c < '0' || c > '9' {
			break
		}
	}
	return ret
}

func main() {
	var (
		na, nb, k, i int32
		a, b         sort.IntSlice
		in           *bufio.Reader
	)

	in = bufio.NewReader(os.Stdin)

	na = Readint(in)
	nb = Readint(in)
	k = Readint(in)
	k++

	a = make(sort.IntSlice, na, na)
	b = make(sort.IntSlice, nb, nb)

	for i = 0; i < na; i++ {
		a[i] = int(Readint(in))
	}
	for i = 0; i < nb; i++ {
		b[i] = int(Readint(in))
	}

	a.Sort()
	b.Sort()

	na--
	nb--
	for na >= 0 && nb >= 0 {
		if a[na] > b[nb] {
			break
		}
		na--
		nb--
	}

	if na < 0 {
		fmt.Println("NO")
	} else {
		fmt.Println("YES")
	}

}

// 297B
package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, m, k int
	a := make([]int, 100100)
	b := make([]int, 100100)
	fmt.Scan(&n, &m, &k)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	for i := 0; i < m; i++ {
		fmt.Scan(&b[i])
	}
	sort.Ints(a)
	sort.Ints(b)
	for n != 0 && m != 0 && a[n-1] <= b[m-1] {
		n--
		m--
	}

	if n != 0 {
		fmt.Print("YES")
	} else {
		fmt.Print("NO")
	}
}

// 297B
package main

import (
	"fmt"
	"sort"
)

func main() {
	var i, j, n, m, k int
	a := make([]int, 100100)
	b := make([]int, 100100)
	fmt.Scan(&n, &m, &k)
	for i = 0; i < n; i++ {
		fmt.Scan(&a[i])
		sort.Ints(a)
	}
	for i = 0; i < m; i++ {
		fmt.Scan(&b[i])
		sort.Ints(b)
	}
	i, j = 0, 0
	for i <= n && j <= m {
		if a[n-i] == b[m-j] {
			i++
			j++
		} else if a[n-i] > b[m-j] {
			i++
		} else {
			j++
		}
		if i > j {
			fmt.Println("YES")
			return
		}
	}
	fmt.Println("NO")
}

// 297B
package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, m, k int
	a := make([]int, 100100)
	b := make([]int, 100100)
	fmt.Scan(&n, &m, &k)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	for i := 0; i < m; i++ {
		fmt.Scan(b[i])
	}
	sort.Ints(a)
	sort.Ints(b)
	for n != 0 && m != 0 && a[n-1] <= b[m-1] {
		n--
		m--
	}

	if n != 0 {
		fmt.Print("YES")
	} else {
		fmt.Print("NO")
	}
}

// 297B
package main

import (
	"fmt"
	"sort"
)

func main() {
	var x, y, i, n, m, k int
	a := make([]int, 100100)
	b := make([]int, 100100)
	fmt.Scan(&n, &m, &k)
	for i = 0; i < n; i++ {
		fmt.Scan(&a[i])
		sort.Ints(a)
	}
	for i = 0; i < m; i++ {
		fmt.Scan(&b[i])
		sort.Ints(b)
	}
	x, y = 0, 0
	for x <= n && y <= m {
		if a[n-x] == b[m-y] {
			x++
			y++
		} else if a[n-x] > b[m-y] {
			x++
		} else {
			y++
		}
		if x > y {
			fmt.Println("YES")
			return
		}
	}
	fmt.Println("NO")
}

// 297B
package main

import (
	"fmt"
	"sort"
)

func main() {
	var i, n, m, k int
	a := make([]int, 100100)
	b := make([]int, 100100)
	fmt.Scan(&n, &m, &k)
	for i = 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	for i = 0; i < m; i++ {
		fmt.Scan(&b[i])
	}
	sort.Ints(a)
	sort.Ints(b)
	l := 1
	for i = 0; i < n; i++ {
		for l <= m && a[i] > b[l] {
			l++
		}
		if l > m {
			break
		}
		l++
	}
	if i == n+1 {
		fmt.Print("NO")
	} else {
		fmt.Print("YES")
	}
}


package main
import (
	"os"
	"fmt"
	"bufio"
)
var rd = bufio.NewReader(os.Stdin)
func main() {
	t := 0
	fmt.Fscan(rd, &t)
	for tc := 0; tc < t; tc++ {
		n, q := 0, 0
		s := ""
		fmt.Fscan(rd, &n, &q, &s)
		for i := 0; i < q; i++ {
			l, r := 0, 0
			fmt.Fscan(rd, &l, &r)
			l--; r--
			ok := false
			for j := 0; j < l && !ok; j++ {
				if s[j] == s[l] {
					ok = true
				}
			}
			for j := r + 1; j < n && !ok; j++ {
				if s[j] == s[r] {
					ok = true
				}
			}
			if ok {
				fmt.Println("YES")
			} else {
				fmt.Println("NO")
			}
		}
	}
}

package main

import (
	"fmt"
)

const N int = 1e2 + 5

func main() {
	var t int
	fmt.Scan(&t)

	for tc := 1; tc <= t; tc++ {
		var length, query int
		var str string
		fmt.Scan(&length, &query, &str)

		str = "#" + str
		length = len(str)

		var prefixSum [N][2]int
		for i := 1; i < length; i++ {
			for j := 0; j <= 1; j++ {
				if int(str[i]-'0') == j {
					prefixSum[i][j]++
				}
				prefixSum[i][j] += prefixSum[i-1][j]
			}
		}

		ok := func(l int, r int, id int) bool {
			if l > r || l < 1 || r >= length {
				return false
			}
			if (prefixSum[r][id] - prefixSum[l-1][id]) > 0 {
				return true
			} else {
				return false
			}
		}

		for query > 0 {
			var l, r int
			fmt.Scan(&l, &r)
			if ok(1, l-1, int(str[l]-'0')) || ok(r+1, length-1, int(str[r]-'0')) {
				fmt.Println("YES")
			} else {
				fmt.Println("NO")
			}
			query--
		}
	}
}

package main

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

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

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

func solveA() {
	var n int
	scanf("%d\n", &n)
	ans := 0
	if n % 2 == 0 {
		if n >= 4 {
			ans = 2
		} else if n == 2 {
			ans = 1
		}
	} else {
		if n >= 5 {
			ans = 3
		} else if n == 3 {
			ans = 2
		}
	}
	printf("%d\n", ans)
}

type point struct {
	left int
	right int
}

func solveB() {
	var n, q int
	scanf("%d %d\n", &n, &q)
	var s string
	scanf("%s\n", &s)
	var P0, P1 point = point{n, -1}, point{n, -1}
	for i := 0; i < n; i++ {
		if s[i] == '0' {
			P0.right = i
		} else {
			P1.right = i
		}
	}

	for i := n - 1; i >= 0; i-- {
		if s[i] == '0' {
			P0.left = i
		} else {
			P1.left = i
		}
	}

	for ; q > 0; q-- {
		var l, r int
		scanf("%d %d\n", &l, &r)
		l--
		r--
		ans := "NO"
		if (s[l] == '0' && P0.left < l || s[l] == '1' && P1.left < l) ||
			(s[r] == '0' && P0.right > r || s[r] == '1' && P1.right > r) {
			ans = "YES"
		}

		printf("%s\n", ans)
	}
}

func main() {
	defer writer.Flush()
	var t int
	scanf("%d\n", &t)
	for ; t > 0; t-- {
		solveB()
	}
}


// 384B
package main

import (
	"fmt"
)

func main() {
	var n, m, k int
	fmt.Scan(&n, &m, &k)
	fmt.Println((m - 1) * (m) / 2)
	for i := 1; i <= m; i++ {
		for j := i + 1; j <= m; j++ {
			if k == 0 {
				fmt.Println(i, j)
			} else {
				fmt.Println(j, i)
			}
		}
	}
}

package main

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

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

func main() {
	var n, m, k int
	fmt.Fscan(reader, &n, &m, &k)
	fmt.Println(m * (m - 1) / 2)
	if k == 0 {
		for i := 0; i < m; i++ {
			for j := 0; j < m-i-1; j++ {
				fmt.Println(j+1, j+2)
			}
		}
	} else {
		for i := 0; i < m; i++ {
			for j := m - 1; j >= 1+i; j-- {
				fmt.Println(j+1, j)
			}
		}
	}
}

package main
import "fmt"

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

	fmt.Println(m*(m-1)/2)
	for u:=1; u<=m; u++ {
		for i:= u+1; i<=m; i++{
			if k==0 {
				fmt.Println(u,"",i)
			}else{
				fmt.Println(i,"",u)
			}
		}
	}

}

package main
import "fmt"

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

	fmt.Println(m*(m-1)/2)
	for u:=1; u<=m; u++ {
		for i:= u+1; i<=m; i++{
			if k==0 {
				fmt.Println(u,i)
			}else{
				fmt.Println(i,u)
			}
		}
	}

}

package main

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

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

func main() {
	var n, m, k int
	fmt.Fscan(reader, &n, &m, &k)
	fmt.Println(m * (m - 1) / 2)
	if k == 0 {
		for i := 0; i < m; i++ {
			for j := i; j+1 < m; j++ {
				fmt.Println(j+1, j+2)
			}
		}
	} else {
		for i := m - 1; i >= 0; i-- {
			for j := i; j >= 1; j-- {
				fmt.Println(j+1, j)
			}
		}
	}
}

package main

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

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

func main() {
	var n, m, k int
	fmt.Fscan(reader, &n, &m, &k)
	fmt.Println(m * (m - 1) / 2)
	if k == 0 {
		for i := 0; i < m; i++ {
			for j := 0; j < m-i; j++ {
				fmt.Println(j+1, j+2)
			}
		}
	} else {
		for i := 0; i < m; i++ {
			for j := m - 1; j >= 1+i; j-- {
				fmt.Println(j+1, j)
			}
		}
	}
}

package main
import "fmt"

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

	fmt.Println(m*(m-1)/2)
	for u:=0; u<=m; u++ {
		for i:= u+1; i<=m; i++{
			if k==0 {
				fmt.Println(u,"",i)
			}else{
				fmt.Println(i,"",u)
			}
		}
	}

}