package main

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

type Int int64

const (
	INF = 3e5 + 10
	L0  = Int(0)
	L1  = Int(1)
)

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

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var n int
		Fscan(in, &n)
		a := make([]int, n)

		cnt := 0
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
			if a[i] < 0 {
				cnt++
				a[i] = -a[i]
			}
		}

		flag := true
		for i := 0; i < n; i++ {
			if i < cnt {
				a[i] = -a[i]
			}
			if i > 0 && a[i] < a[i-1] {
				flag = false
			}
		}

		if flag {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}

	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func twodi(n int,m int,v int) [][]int {
	r := make([][]int,n); for i:=0;i<n;i++ { x := make([]int,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func abs(a int) int { if a < 0 { return -a }; return a }
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	const inf = 1000000000
	T := gi()
	for tt:=1;tt<=T;tt++ {
		N := gi(); A := gis(N)
		cntneg := 0; for _,a := range A { if a < 0 { cntneg++ } }
		// Negatives clearly go on the left end of the array
		for i:=0;i<N;i++ { if i < cntneg { A[i] = -abs(A[i]) } else { A[i] = abs(A[i]) } }
		ans := "YES"
		for i:=0;i<N-1;i++ { if A[i] > A[i+1] { ans = "NO"; break } }
		fmt.Fprintln(wrtr,ans)
	}
}

package main

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

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

	var t int
	var n int
	in.Scan()
	t, _ = strconv.Atoi(in.Text())
	sol := func() bool {

		in.Scan()
		n, _ = strconv.Atoi(in.Text())
		a := make([]int, n)
		d := 0
		for i := 0; i < n; i++ {
			in.Scan()
			a[i], _ = strconv.Atoi(in.Text())
			if a[i] < 0 {
				d++
				a[i] = -a[i]
			}
		}
		for i := 1; i < d; i++ {
			if a[i-1] < a[i] {
				return false
			}
		}
		for i := d; i < n-1; i++ {
			if a[i] > a[i+1] {
				return false
			}
		}
		return true
	}
	for ; t > 0; t-- {
		r := sol()
		if r {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

func main() {
	in := bufio.NewScanner(os.Stdin)
	in.Split(bufio.ScanWords)
	var t int
	var n int
	in.Scan()
	t, _ = strconv.Atoi(in.Text())
	sol := func() bool {

		in.Scan()
		n, _ = strconv.Atoi(in.Text())
		a := make([]int, n)
		d := 0
		for i := 0; i < n; i++ {
			in.Scan()
			a[i], _ = strconv.Atoi(in.Text())
			if a[i] < 0 {
				d++
				a[i] = -a[i]
			}
		}
		// fmt.Println(d)
		// fmt.Println(a[:d], a[d:], a)
		for i := 1; i < d; i++ {
			if a[i-1] < a[i] {
				return false
			}
		}
		for i := d; i < n-1; i++ {
			if a[i] > a[i+1] {
				return false
			}
		}
		return true
	}
	for ; t > 0; t-- {
		r := sol()
		if r {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)

func slove() {
	defer w.Flush()
	ans := "No"
	defer func(){fmt.Fprintln(w, ans)}()
	var n int
	fmt.Fscan(r, &n)
	nums := make([]int, n)
	for i:=0;i<n;i++ {
		fmt.Fscan(r, &nums[i])
	}
	nega, posi := 0, 0
	for i := range nums {
		if nums[i] < 0 {
			nega++
		} else {
			posi++
		}
	}
	for i:=1;i<nega;i++ {
		if abs(nums[i]) > abs(nums[i-1]) {
			return
		}
	}
	for i:=nega+1;i<n;i++ {
		if abs(nums[i]) < abs(nums[i-1]) {
			return
		}
	}
	ans = "Yes"
}

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

func main() {
	var n int
	fmt.Fscan(r, &n)
	for i:=1;i<=n;i++ {
		slove()
	}
}

package main

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

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

func a1670(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T int
	var n int
	var x int

loop:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int, n)
		neg := 0
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			if x < 0 {
				neg++
				x = -x
			}
			a[i] = x
		}

		for i := 1; i < neg; i++ {
			if a[i] > a[i-1] {
				Fprintln(out, "NO")
				continue loop
			}
		}
		for i := neg+1; i < n;  i++ {
			if a[i] < a[i-1] {
				Fprintln(out, "NO")
				continue loop
			}
		}

		Fprintln(out, "YES")
	}
}

package main

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

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

func main() {
	defer out.Flush()

	var tc int
	fmt.Fscan(in, &tc)
	for ; tc > 0; tc-- {
		var n int
		fmt.Fscan(in, &n)
		a := make([]int, n)
		negatives := 0
		for i := range a {
			fmt.Fscan(in, &a[i])
			if a[i] < 0 {
				negatives++
				a[i] = -a[i]
			}
		}
		for i := 0; i < negatives; i++ {
			a[i] = -a[i]
		}
		ok := true
		for i := 1; ok && i < len(a); i++ {
			ok = a[i-1] <= a[i]
		}
		if ok {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

/*

 */

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	a := make([]int, n)
	negCnt := 0

	for i := range a {
		scanf("%d", &a[i])
		if a[i] < 0 {
			negCnt++
		}
	}
	scanf("\n")

	for i, num := range a {
		if i < negCnt {
			a[i] = -abs(num)
		} else {
			a[i] = abs(num)
		}
	}

	if sort.IntsAreSorted(a) {
		fmt.Fprintln(writer, "YES")
	} else {
		fmt.Fprintln(writer, "NO")
	}
}

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

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

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

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

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

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

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

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

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

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func sum(nums ...int) (sum int) {
	for _, num := range nums {
		sum += num
	}
	return
}

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

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

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

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

	return true
}

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

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

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

	num = abs(num)

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

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

//goland:noinspection GoUnusedFunction
func factorize(num int64) []int64 {
	factors := make([]int64, 0)

	for ftr := int64(2); ftr*ftr <= num; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > 1 {
		factors = append(factors, num)
	}

	return factors
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1670A
Date: 11/2/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1670/A
Problem: CF1670A
**/
func (in *CF1670A) Run() {
  for t := in.NextInt(); t > 0; t-- {
    n := in.NextInt()
    arr := make([]int, n)

    for i := 0; i < n; i++ {
      val := in.NextInt()
      arr[i] = val
    }

    ans := true

    i, j := 0, 0
    for i < n {
      if arr[i] < 0 {
        arr[j] *= -1
        arr[i] *= -1
        j++
      }
      i++
    }

    sort := true
    for i := 1; i < n; i++ {
      if arr[i-1] > arr[i] {
        sort = false
      }
    }

    ans = sort

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

}

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

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

package main

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

type Int int64

const (
	INF = 3e5 + 10
	L0  = Int(0)
	L1  = Int(1)
)

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

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var n int
		Fscan(in, &n)
		a := make([]int, n+1)
		cnt := 0
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
			if a[i] < 0 {
				cnt++
				a[i] = -a[i]
			}
		}

		flag := true
		for i := 0; i < n; i++ {
			if i < cnt {
				a[i] = -a[i]
			}
			if i > 0 && a[i] < a[i-1] {
				flag = false

			}
		}
		if flag {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1670A
Date: 11/2/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1670/A
Problem: CF1670A
**/
func (in *CF1670A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		arr := make([]int, n)

		order := true
		last := -1
		for i := 0; i < n; i++ {
			val := in.NextInt()
			arr[i] = val

			if i > 0 {
				if last > arr[i] {
					order = false
				}
			}
			last = val
		}

		ans := true

		pivot := -1
		for i := 2; i < n; i++ {
			if int(math.Abs(float64(arr[i-2]))) > int(math.Abs(float64(arr[i-1]))) && int(math.Abs(float64(arr[i-1]))) < int(math.Abs(float64(arr[i]))) {
				pivot = i - 1
				break
			}
		}
		if pivot != -1 {

			changes := 0

			for i := 0; i < pivot; i++ {
				if arr[i] > 0 {
					changes++
				}
			}

			for i := pivot + 1; i < n; i++ {
				if arr[i] < 0 {
					changes--
				}
			}

			if changes != 0 {
				ans = false
			}
		}

		if pivot == -1 && !order {
			ans = false
		}

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

}

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

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

package main

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

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

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

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

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

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

/**
Run solve the problem CF1670A
Date: 11/2/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1670/A
Problem: CF1670A
**/
func (in *CF1670A) Run() {
  for t := in.NextInt(); t > 0; t-- {
    n := in.NextInt()
    arr := make([]int, n)

    order := true
    last := -1
    for i := 0; i < n; i++ {
      val := in.NextInt()
      arr[i] = val

      if i > 0 {
        if last >= arr[i] {
          order = false
        }
      }
      last = val
    }

    ans := true

    pivot := -1
    for i := 2; i < n; i++ {
      if int(math.Abs(float64(arr[i-2]))) > int(math.Abs(float64(arr[i-1]))) && int(math.Abs(float64(arr[i-1]))) < int(math.Abs(float64(arr[i]))) {
        pivot = i - 1
        break
      }
    }
    if pivot != -1 {

      changes := 0

      for i := 0; i < pivot; i++ {
        if arr[i] > 0 {
          changes++
        }
      }

      for i := pivot + 1; i < n; i++ {
        if arr[i] < 0 {
          changes--
        }
      }

      if changes != 0 {
        ans = false
      }
    }

    if pivot == -1 && !order {
      ans = false
    }

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

}

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

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

package main

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

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

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

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

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

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

/**
Run solve the problem CF1670A
Date: 11/2/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1670/A
Problem: CF1670A
**/
func (in *CF1670A) Run() {
  for t := in.NextInt(); t > 0; t-- {
    n := in.NextInt()
    arr := make([]int, n)

    for i := 0; i < n; i++ {
      val := in.NextInt()
      arr[i] = val
    }

    ans := true

    pivot := -1
    for i := 2; i < n; i++ {
      if int(math.Abs(float64(arr[i-2]))) > int(math.Abs(float64(arr[i-1]))) && int(math.Abs(float64(arr[i-1]))) < int(math.Abs(float64(arr[i]))) {
        pivot = i - 1
        break
      }
    }
    if pivot != -1 {

      changes := 0

      for i := 0; i < pivot; i++ {
        if arr[i] > 0 {
          changes++
        }
      }

      for i := pivot + 1; i < n; i++ {
        if arr[i] < 0 {
          changes--
        }
      }

      if changes != 0 {
        ans = false
      }

    }

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

}

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

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

package main

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

type Int int64

const (
	INF = 3e5 + 10
	L0  = Int(0)
	L1  = Int(1)
)

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

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var n int
		Fscan(in, &n)
		a := make([]int, n)

		cnt := 0
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
			if a[i] < 0 {
				cnt++
				a[i] = -a[i]
			}
		}

		flag := true
		for i := 0; i < cnt; i++ {
			a[i] = -a[i]
			if i > 0 && a[i] < a[i-1] {
				flag = false
			}
		}

		if flag {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}

	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main

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

type Int int64

const (
	INF = 3e5 + 10
	L0  = Int(0)
	L1  = Int(1)
)

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

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var n int
		Fscan(in, &n)
		a := make([]int, n)

		cnt := 0
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
			if a[i] < 0 {
				cnt++
				a[i] = -a[i]
			}
		}

		flag := true
		for i := n - 2; i >= 0; i-- {
			if a[i] > a[i+1] {
				if cnt > 0 && -a[i] <= a[i+1] {
					cnt--
					a[i] = -a[i]
				} else {
					flag = false
					break
				}

			}
		}
		for i := 0; i < n && cnt > 0; i++ {
			if a[i] > 0 && (i == 0 || a[i-1] < -a[i]) {
				cnt--
				a[i] = -a[i]
			}
		}

		if flag && cnt == 0 {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}

	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func twodi(n int,m int,v int) [][]int {
	r := make([][]int,n); for i:=0;i<n;i++ { x := make([]int,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func abs(a int) int { if a < 0 { return -a }; return a }
func max(a,b int) int { if a > b { return a }; return b }
func min(a,b int) int { if a > b { return b }; return a }
func tern(cond bool, a int, b int) int { if cond { return a }; return b }
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	const inf = 1000000000
	T := gi()
	for tt:=1;tt<=T;tt++ {
		N := gi(); A := gis(N)
		cntneg := 0; for _,a := range A { if a < 0 { cntneg++ } }
		minneg := twodi(N,2,inf)
		maxneg := twodi(N,2,-1)
		minneg[0][0] = 0; minneg[0][1] = 1; maxneg[0][0] = 0; maxneg[0][1] = 1
		for i:=1;i<N;i++ {
			if abs(A[i]) >= abs(A[i-1]) {
				minneg[i][0] = min(minneg[i-1][0],minneg[i-1][1])
				maxneg[i][0] = max(maxneg[i-1][0],maxneg[i-1][1])
			}
			if abs(A[i]) <= abs(A[i-1]) {
				minneg[i][1] = minneg[i-1][1]+1
				maxneg[i][1] = tern(maxneg[i-1][1] >= 0,maxneg[i-1][1]+1,-1)
			}
		}
		ans := "NO"
		if cntneg >= minneg[N-1][0] && cntneg <= maxneg[N-1][0] { ans = "YES" }
		if cntneg >= minneg[N-1][1] && cntneg <= maxneg[N-1][1] { ans = "YES" }
		fmt.Fprintln(wrtr,ans)
	}
}


package main

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

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

func main() {
	var (
		s   string
		n   int
		pos []int
		ans int
	)

	fmt.Fscan(reader, &s)
	n = len(s)
	for i := 0; i+4 <= n; i++ {
		if s[i:i+4] == "bear" {
			pos = append(pos, i)
		}
	}

	lb := 0
	for _, p := range pos {
		l := p - lb + 1
		r := n - p - 3
		//fmt.Println(l, r)
		ans += l * r
		lb = p + 1
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var s string

	fmt.Scan(&s)

	count := 0

	for i:=0; i<=len(s)-4; i++ {
		sub :=s[i: len(s)]
		if index:=strings.Index(sub, "bear"); index!=-1 {
			count += len(sub) - (index + 3)
		}
	}

	fmt.Println(count)
}

// http://codeforces.com/problemset/problem/385/B
// B. Bear and Strings

package main

import (
	"fmt"
)

// bear mach :-)
func Gep(st, ch byte) byte {
	switch ch {
	case 'b':
		{
			return 1
		}
	case 'e':
		{
			if 1 == st {
				return 2
			}
			return 0
		}

	case 'a':
		{
			if 2 == st {
				return 3
			}
			return 0
		}
	case 'r':
		{
			if 3 == st {
				return 4
			}
			return 0
		}
	}
	return 0
}

func main() {
	var (
		j, lw, pbeg int32
		st          byte
		word        string
		ans         int
	)

	fmt.Scan(&word)
	lw = int32(len(word))
	st, pbeg = 0, -1
	for j = 0; j < lw; j++ {
		st = Gep(st, word[j])
		if st == 4 {
			ans += int(j-3-pbeg) * int(lw-j)
			pbeg = j - 3
		}
	}

	fmt.Println(ans)

}

package main

import (
	"fmt"
)

func main() {
	var s string

	fmt.Scanf("%s", &s)

	var starts []int

	for i:=0; i<=len(s)-4; i++ {
		if s[i:i+4] == "bear" {
			starts = append(starts, i)
		}
	}

	count := 0
	for i:=0; i<=len(s)-4; i++ {
		end := -1
		for j:=0; j<len(starts); j++ {
			if starts[j] >= i {
				end = starts[j]
				break
			}
		}

		if(end != -1) {
			end += 3
			count += len(s)-end
		}
	}

	fmt.Println(count)
}

package main

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

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

func main() {
	var (
		s   string
		n   int
		pos []int
		ans int
	)

	fmt.Fscan(reader, &s)
	n = len(s)
	for i := 0; i+4 <= n; i++ {
		if s[i:i+4] == "bear" {
			pos = append(pos, i)
		}
	}

	lb := 0
	for _, p := range pos {
		l := lb + 1
		r := n - p - 3
		ans += l * r
		lb = l + 4
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
	// "regexp"
	"strings"
)

func main() {
	var s string

	fmt.Scan(&s)

	news := s
	count := 0
	f, b, lastLength := 0, 0, 0
	for index:=strings.Index(news, "bear"); index != -1; index=strings.Index(news, "bear") {
		f, b = lastLength + index, len(s)-lastLength-index-4
		// fmt.Println(f, b)
		if f!=0 && b!=0 {
			count += f*(b+1)
		} else if f==0 {
			count += b+1
		} else {
			count += f+1
		}

		lastLength += index+4

		if len(news) > index + 7 {
			news = news[index+4:]
		} else {
 			break
		}
	}

	// pattern := regexp.MustCompile("[:lower:]*bear[:lower:]*")
	// count := 0

	// for i:=0; i<length-3; i++ {
	// 	for j:=i+4; j<=length; j++ {
	// 		if pattern.MatchString(s[i:j]) {
	// 			count++
	// 		}
	// 	}
	// }

	fmt.Println(count)
}


// Author: sighduck
// URL: https://codeforces.com/contest/1197/problem/C

package main

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

func Solve(n int, k int, a []int) int64 {
	if k == n {
		return 0
	}
	d := []int{}
	for i := 0; i < n-1; i++ {
		d = append(d, a[i+1]-a[i])
	}
	sort.Ints(d)
	cost := int64(0)
	for i := 0; i < n-k; i++ {
		cost += int64(d[i])
	}
	return cost
}

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

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

	fmt.Fprintf(writer, "%d\n", Solve(n, k, a))
}

package main

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

/* solution */

func solve() {

	n, k := readInt(), readInt()
	diff := make([]int, n-1)

	first := readInt()
	l, r := first, first
	for i := 0; i < n-1; i++ {
		r = readInt()
		diff[i] = l - r
		l = r
	}

	sort.Ints(diff)

	ans := r - first
	for i := 0; i < k-1; i++ {
		ans += diff[i]
	}

	printInt(ans)

}

/* 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)
	reader.Split(bufio.ScanWords)

	solve()

}

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

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

package main

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

func main() {

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

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

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

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

	for i := 0; i < n-1; i++ {
		a[i] -= a[i+1]
	}
	a = a[:n-1]
	sort.Ints(a)

	for i := 0; i < k-1; i++ {
		s += a[i]
	}
	fmt.Fprintf(writer, "%d\n", s)

	writer.Flush()
}


package main

import . "fmt"
import "bufio"
import "os"

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

func ans() {
	var n int
	Fscan(in, &n)
	Fprintln(out, "1")
	if n == 1 {
		return
	}
	for i := 2; i <= n; i++ {
		Fprint(out, "1 ")
		for j := 2; j < i; j++ {
			Fprint(out, "0 ")
		}
		Fprintln(out, "1")
	}

}

func main() {
	defer out.Flush()
	var t int
	Fscan(in, &t)
	for i := 0; i < t; i++ {
		ans()
	}
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1734B
Date: 9/28/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1734/B
Problem: CF1734B
**/
func (in *CF1734B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		for i := 0; i < n; i++ {
			for j := 0; j <= i; j++ {
				if j == 0 || j == i {
					fmt.Print(1, " ")
				} else {
					fmt.Print(0, " ")
				}
			}
			fmt.Println()
		}
	}

}

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

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

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

		for i := 0; i < n; i++ {
			for j := 0; j < i+1; j++ {
				var d int
				if j == 0 || j == i {
					d = 1
				}
				fmt.Fprintf(out, "%d ", d)
			}
			fmt.Fprintln(out)
		}
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"
)

// https://space.bilibili.com/206214
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	solve := func(curCase int) {
		var n int
		Fscan(in, &n)
		for i := 1; i <= n; i++ {
			a := make([]int, i)
			a[0] = 1
			a[len(a)-1]= 1
			for _, v := range a {
				Fprint(out, v, " ")
			}
			Fprintln(out)
		}
	}

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

	leftData, _ := ioutil.ReadAll(in)
	if s := strings.TrimSpace(string(leftData)); s != "" {
		panic("有未读入的数据：\n" + s)
	}

}

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

package main

import "fmt"

func main() {
	var t int
	_, _ = fmt.Scanln(&t)

	for j := 0; j < t; j++ {
		var n int
		_, _ = fmt.Scanln(&n)
		for i := 1; i <= n; i++ {
			for k := 1; k <= i; k++ {
				if k == 1 || k == i {
					fmt.Print("1 ")
				} else {
					fmt.Print("0 ")
				}
			}
			fmt.Println()
		}
	}
}

package main

import "fmt"

func main() {
	var t int
	_, _ = fmt.Scanln(&t)

	for j := 0; j < t; j++ {
		var n int
		_, _ = fmt.Scanln(&n)
		for i := 1; i <= n; i++ {
			for k := 1; k <= i; k++ {
				if i%2 == 0 {
					fmt.Print("1 ")
				} else if k == 1 || k == i {
					fmt.Print("1 ")
				} else {
					fmt.Print("0 ")
				}
			}
			fmt.Println()
		}
	}
}

package main

import "fmt"

func main() {
	var t int
	_, _ = fmt.Scanln(&t)

	for j := 0; j < t; j++ {
		var n int
		_, _ = fmt.Scanln(&n)
		for i := 1; i <= n; i++ {
			for k := 1; k <= i; k++ {
				if i%2 == 0 {
					fmt.Print(1)
				} else if k == 1 || k == i {
					fmt.Print(1)
				} else {
					fmt.Print(0)
				}
			}
			fmt.Println()
		}
	}
}


package main

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

const (
	MaxN       = 5005
	MaxK       = 5005
	NoSolution = -1
)

var n, k, x int
var F [MaxN][MaxK]int64
var a [MaxN]int64

func init() {
	for i := 0; i < MaxN; i++ {
		for j := 0; j < MaxK; j++ {
			F[i][j] = NoSolution
		}
	}
}

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

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d %d\n", &n, &k, &x)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &a[i])
	}
	for i := 0; i < k; i++ {
		F[i][1] = a[i]
	}
	for z := 2; z <= x; z++ {
		d := make([]int, 0)
		for i := 1; i < n; i++ {
			if z > i + 1 {
				continue
			}
			if len(d) > 0 && i - d[0] > k {
				d = d[1:]
			}
			if F[i-1][z-1] != - 1 {
				for len(d) > 0 && F[d[len(d) - 1]][z-1] < F[i-1][z-1] {
					d = d[:len(d) - 1]
				}
				d = append(d, i-1)
			}
			currMax := int64(-1)
			if len(d) > 0 {
				currMax = F[d[0]][z-1]
			}
			if currMax != -1 {
				F[i][z] = currMax + a[i]
			}
		}
	}
	ans := int64(-1)
	for i := 0; i < k; i++ {
		ans = max(ans, F[n-i-1][x])
	}
	fmt.Println(ans)
}

package main

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

const (
	MaxN       = 5005
	MaxK       = 5005
	NoSolution = -1
)

var n, k, x int
var F [MaxN][MaxK]int64
var a [MaxN]int64

func init() {
	for i := 0; i < MaxN; i++ {
		for j := 0; j < MaxK; j++ {
			F[i][j] = NoSolution
		}
	}
}

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

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d %d\n", &n, &k, &x)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &a[i])
	}
	for i := 0; i < k; i++ {
		F[i][1] = a[i]
	}
	for z := 2; z <= x; z++ {
		d := make([]int, 0)
		for i := 1; i < n; i++ {
			if z > i + 1 {
				continue
			}
			if len(d) > 0 && i - d[0] > k {
				d = d[1:]
			}
			if F[i-1][z-1] != - 1 {
				for len(d) > 0 && F[d[len(d) - 1]][z-1] < F[i-1][z-1] {
					d = d[:len(d) - 1]
				}
				d = append(d, i-1)
			}
			currMax := int64(NoSolution)
			if len(d) > 0 {
				currMax = F[d[0]][z-1]
			}
			if currMax != NoSolution {
				F[i][z] = currMax + a[i]
			}
		}
	}
	ans := int64(NoSolution)
	for i := 0; i < k; i++ {
		ans = max(ans, F[n-i-1][x])
	}
	fmt.Println(ans)
}

package main

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

const (
	MaxN       = 5005
	MaxK       = 5005
	NoSolution = -1
)

var n, k, x int
var F [MaxN][MaxK]int64
var a [MaxN]int64

func init() {
	for i := 0; i < MaxN; i++ {
		for j := 0; j < MaxK; j++ {
			F[i][j] = NoSolution
		}
	}
}

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

func getMax(startPos, row int) int64 {
	best := int64(-1)
	for j := 1; j <= k; j++ {
		if startPos - j < 0 {
			break
		}
		if F[startPos-j][row] != NoSolution {
			best = max(best, F[startPos-j][row])
		}
	}
	return best
}

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d %d\n", &n, &k, &x)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &a[i])
	}
	for i := 0; i < k; i++ {
		F[i][1] = a[i]
	}
	for z := 2; z <= x; z++ {
		d := make([]int, 0)

		for i := 1; i < n; i++ {
			if z > i + 1 {
				continue
			}
			if len(d) > 0 && i - d[0] > k {
				d = d[1:]
			}
			if F[i-1][z-1] != - 1 {
				for len(d) > 0 && F[d[len(d) - 1]][z-1] < F[i-1][z-1] {
					d = d[:len(d) - 1]
				}
				d = append(d, i-1)
			}
			currMax := int64(-1)
			if len(d) > 0 {
				currMax = F[d[0]][z-1]
			}
			if currMax != -1 {
				F[i][z] = currMax + a[i]
			}
		}
	}
	ans := int64(-1)
	for i := 0; i < k; i++ {
		ans = max(ans, F[n-i-1][x])
	}
	fmt.Println(ans)
}

package main

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

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

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

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

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

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

func main() {
	defer writer.Flush()

	var n, k, m int
	scanf("%d %d %d\n", &n, &k, &m)
	k = min(n, k)

	var nums []int64
	for i := 0; i < n; i++ {
		var x int64
		scanf("%d", &x)
		nums = append(nums, x)
	}

	dp := [201][201]int64{}
	for i := 0; i < m; i++ {
		dp[0][i] = nums[0]
	}
	dp[0][m] = -1

	for i := 1; i < n; i++ {
		for j := 0; j < m; j++ {
			if i < k {
				dp[i][j] = nums[i]
			} else {
				dp[i][j] = -1
			}
			for x := max(0, i-k); x < i; x++ {
				if dp[x][j+1] > -1 {
					dp[i][j] = max64(dp[i][j], dp[x][j+1]+nums[i])
				}
			}
		}
		dp[i][m] = -1
	}

	var result int64
	result = -1
	for i := n - k; i < n; i++ {
		result = max64(result, dp[i][0])
	}

	printf("%d\n", result)
}

package main

import (
	"fmt"
)

func main() {
	var n, k, x int64
	dp := make([][210]int64, 210)
	fmt.Scan(&n, &k, &x)
	var ans int64 = -1
	for i := int64(1); i <= n; i++ {
		var tmp int64
		fmt.Scan(&tmp)
		if i <=k {
			dp[i][1] = tmp

			if  i > n-k &&ans < dp[i][1] {
				ans = dp[i][1]
			}
		}
		for j := int64(i-1); i-j <= k && j > 0; j-- {
			var jj = int64(x-1)
			for ; jj >= 1; jj-- {
				if dp[j][jj]>0&& dp[i][jj+1] < dp[j][jj]+tmp {
					dp[i][jj+1] = dp[j][jj] + tmp
					if i > n-k && ans < dp[i][jj+1] {
						ans = dp[i][jj+1]
					}
				}
			}
		}

	}
	//for i := int64(1); i <= n; i++{
	//	for j := int64(1); j <= x; j++{
	//		fmt.Printf("%d,",dp[i][j])
	//	}
	//	fmt.Printf("\n")
	//}
	fmt.Print(ans)
}

package main

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

const (
	MaxN       = 2505
	MaxK       = 2505
	NoSolution = -1
)

var n, k, x int
var F [MaxN][MaxK]int64
var a [MaxN]int64

func init() {
	for i := 0; i < MaxN; i++ {
		for j := 0; j < MaxK; j++ {
			F[i][j] = NoSolution
		}
	}
}

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

func getMax(startPos, row int) int64 {
	best := int64(-1)
	for j := 1; j <= k; j++ {
		if startPos - j < 0 {
			break
		}
		if F[startPos-j][row] != NoSolution {
			best = max(best, F[startPos-j][row])
		}
	}
	return best
}

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d %d\n", &n, &k, &x)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &a[i])
	}
	for i := 0; i < k; i++ {
		F[i][1] = a[i]
	}
	for i := 1; i < n; i++ {
		for z := 2; z <= x; z++ {
			if z > i + 1 {
				break
			}
			currMax := getMax(i, z-1)
			if currMax != -1 {
				F[i][z] = currMax + a[i]
			}
		}
	}

	ans := int64(-1)
	for i := 0; i < k; i++ {
		ans = max(ans, F[n-i-1][x])
	}
	fmt.Println(ans)
}

package main

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

const (
	MaxN        = 256
	MaxX        = 256
	NotComputed = -2
)

var n, k, x int
var F [MaxN][MaxX]int64
var a [MaxN]int64

func init() {
	for i := 0; i < MaxN; i++ {
		for j := 0; j < MaxX; j++ {
			F[i][j] = NotComputed
		}
	}
}

func f(pos, places int) int64 {
	if F[pos][places] != NotComputed {
		return F[pos][places]
	}

	if places == 0 {
		if pos < k {
			F[pos][places] = a[pos]
		} else {
			F[pos][places] = -1
		}
		return F[pos][places]
	}

	curr := int64(-1)
	for i := 0; i < k; i++ {
		if pos - i - 1 < 0 {
			break
		}
		vnext := f(pos - i - 1, places - 1)
		if vnext != -1 && a[pos] + vnext > curr {
			curr = a[pos] + vnext
		}
	}
	F[pos][places] = curr
	return F[pos][places]
}

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d %d\n", &n, &k, &x)

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

	ans := int64(-1)
	for i := 0; i < k; i++ {
		curr := f(n-i-1, x-1)
		if curr > ans {
			ans = curr
		}
	}

	fmt.Println(ans)
}

package main

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

const (
	MaxN       = 2505
	MaxK       = 2505
	NoSolution = -1
)

var n, k, x int
var F [MaxN][MaxK]int64
var a [MaxN]int64

func init() {
	for i := 0; i < MaxN; i++ {
		for j := 0; j < MaxK; j++ {
			F[i][j] = NoSolution
		}
	}
}

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

func getMax(startPos, row int) int64 {
	best := int64(-1)
	for j := 1; j <= k; j++ {
		if startPos - j < 0 {
			break
		}
		if F[startPos-j][row] != NoSolution {
			best = max(best, F[startPos-j][row])
		}
	}
	return best
}

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d %d\n", &n, &k, &x)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &a[i])
	}
	for i := 0; i < k; i++ {
		F[i][1] = a[i]
	}
	for z := 2; z <= x; z++ {
		for i := 1; i < n; i++ {
			if z > i + 1 {
				continue
			}
			currMax := getMax(i, z-1)
			if currMax != -1 {
				F[i][z] = currMax + a[i]
			}
		}
	}

	ans := int64(-1)
	for i := 0; i < k; i++ {
		ans = max(ans, F[n-i-1][x])
	}
	fmt.Println(ans)
}

package main

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

const (
	MaxN       = 2505
	MaxK       = 2505
	NoSolution = -1
)

var n, k, x int
var F [MaxN][MaxK]int64
var a [MaxN]int64

func init() {
	for i := 0; i < MaxN; i++ {
		for j := 0; j < MaxK; j++ {
			F[i][j] = NoSolution
		}
	}
}

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

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d %d\n", &n, &k, &x)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &a[i])
	}
	for i := 0; i < k; i++ {
		F[i][1] = a[i]
	}
	for i := 1; i < n; i++ {
		for z := 2; z <= x; z++ {
			if z > i + 1 {
				break
			}
			for j := 1; j <= k; j++ {
				if i < j {
					break
				}
				if F[i-j][z-1] != NoSolution {
					F[i][z] = max(F[i][z], F[i-j][z-1] + a[i])
				}
			}
		}
	}

	//for i := 0; i < n; i++ {
	//	if i == 0 {
	//		for j := 0; j < n; j++ {
	//			fmt.Printf("%3d ", j)
	//		}
	//		fmt.Println()
	//		for j := 0; j < n; j++ {
	//			fmt.Printf("====")
	//		}
	//		fmt.Println()
	//	}
	//	for j := 0; j <= x; j++ {
	//		fmt.Printf("%3d ", F[i][j])
	//	}
	//	fmt.Println()
	//}

	ans := int64(-1)
	for i := 0; i < k; i++ {
		ans = max(ans, F[n-i-1][x])
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
)

func main() {
	var n, k, x int64
	dp := make([][210]int64, 210)
	fmt.Scan(&n, &k, &x)
	var ans int64 = 0
	for i := int64(1); i <= n; i++ {
		var tmp int64
		fmt.Scan(&tmp)
		dp[i][1] = tmp

		if ans < dp[i][1] {
			ans = dp[i][1]
		}
		for j := int64(i-1); i-j <= k && j > 0; j-- {
			var jj = int64(x-1)
			for ; jj >= 1; jj-- {
				if dp[j][jj]>0&& dp[i][jj+1] < dp[j][jj]+tmp {
					dp[i][jj+1] = dp[j][jj] + tmp
					if ans < dp[i][jj+1] {
						ans = dp[i][jj+1]
					}
				}
			}
		}

	}
	//for i := int64(1); i <= n; i++{
	//	for j := int64(1); j <= x; j++{
	//		fmt.Printf("%d,",dp[i][j])
	//	}
	//	fmt.Printf("\n")
	//}
	fmt.Print(ans)
}

package main

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

const (
	MaxN       = 2505
	MaxK       = 2505
	NoSolution = -1
)

var n, k, x int
var F [MaxN][MaxK]int64
var a [MaxN]int64

func init() {
	for i := 0; i < MaxN; i++ {
		for j := 0; j < MaxK; j++ {
			F[i][j] = NoSolution
		}
	}
}

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

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d %d\n", &n, &k, &x)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &a[i])
	}
	for i := 0; i < k; i++ {
		F[i][1] = a[i]
	}
	for i := k; i < n; i++ {
		for j := 1; j < k; j++ {
			for z := 2; z <= x; z++ {
				if F[i-j][z-1] != NoSolution {
					F[i][z] = max(F[i][z], F[i-j][z-1] + a[i])
				}
				if F[i-j][z] != NoSolution {
					F[i][z] = max(F[i][z], F[i-j][z])
				}
			}
		}
	}
	ans := int64(-1)
	for i := 0; i < k; i++ {
		ans = max(ans, F[n-i-1][x])
	}
	fmt.Println(ans)
}

package main

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

const (
	MaxN       = 2505
	MaxK       = 2505
	NoSolution = -1
)

var n, k, x int
var F [MaxN][MaxK]int64
var a [MaxN]int64

func init() {
	for i := 0; i < MaxN; i++ {
		for j := 0; j < MaxK; j++ {
			F[i][j] = NoSolution
		}
	}
}

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

func getMax(startPos, row int) int64 {
	best := int64(-1)
	for j := 1; j <= k; j++ {
		if startPos < j {
			break
		}
		if F[startPos-j][row-1] != NoSolution {
			best = max(best, F[startPos-j][row-1])
		}
	}
	return best
}

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d %d\n", &n, &k, &x)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &a[i])
	}
	for i := 0; i < k; i++ {
		F[i][1] = a[i]
	}
	for i := 1; i < n; i++ {
		for z := 2; z <= x; z++ {
			if z > i + 1 {
				break
			}
			currMax := getMax(i, z-1)
			if currMax != -1 {
				F[i][z] = currMax + a[i]
			}
		}
	}

	ans := int64(-1)
	for i := 0; i < k; i++ {
		ans = max(ans, F[n-i-1][x])
	}
	fmt.Println(ans)
}


package main

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

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

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

func main() {
	defer writer.Flush()
	////////////////////
	var count int
	scan(&count)
	for count != 0 {
		solve()
		count--
	}

}

func solve() {
	s := ""
	scan(&s)
	b := false
	if check(s, 'A', 'B', 'C') {
		b = true
	} else if check(s, 'A', 'C', 'B') {
		b = true
	} else if check(s, 'B', 'A', 'C') {
		b = true
	} else if check(s, 'B', 'C', 'A') {
		b = true
	} else if check(s, 'C', 'A', 'B') {
		b = true
	} else if check(s, 'C', 'B', 'A') {
		b = true
	} else {
		print("No", "\n")
	}
	if b {
		print("Yes", "\n")
	}
}

func check(s string, plus rune, minus rune, temp rune) bool {
	count := 0
	count2 := 0
	for _, v := range s {
		if v == plus {
			count++
			count2++
		}
		if v == minus {
			count--
			count2--
		}
		if v == temp {
			count++
			count2--
		}
		if count < 0 {
			count = -1000
		}
		if count2 < 0 {
			count2 = -1000
		}
	}
	if count == 0 || count2 == 0 {
		return true
	}
	return false
}

package main

import . "fmt"

func main() {
	var t int
	var a []byte

	for Scan(&t); t > 0; t-- {
		Scan(&a)
		if solve(a) {
			Println("YES")
		} else {
			Println("NO")
		}
	}
}

func solve(a []byte) bool {
	for m := 1; m < (1<<3)-1; m++ {
		bal := 0
		for _, c := range a {
			bal += (m>>((c-'A'))&1)*2 - 1
			if bal < 0 {
				break
			}
		}
		if bal == 0 {
			return true
		}
	}
	return false
}

package main

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

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

	for tc := 0; tc < tcs; tc++ {
		var s string
		_, _ = fmt.Fscan(in, &s)

		ans := false
		ans = ans || isPossible(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(s, "A", "("), "B", "("), "C", "("))
		ans = ans || isPossible(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(s, "A", "("), "B", "("), "C", ")"))
		ans = ans || isPossible(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(s, "A", "("), "B", ")"), "C", "("))
		ans = ans || isPossible(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(s, "A", "("), "B", ")"), "C", ")"))
		ans = ans || isPossible(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(s, "A", ")"), "B", "("), "C", "("))
		ans = ans || isPossible(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(s, "A", ")"), "B", "("), "C", ")"))
		ans = ans || isPossible(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(s, "A", ")"), "B", ")"), "C", "("))
		ans = ans || isPossible(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(s, "A", ")"), "B", ")"), "C", ")"))

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

func isPossible(s string) bool {
	var stack = make(Stack, 0)

	for i := 0; i < len(s); i++ {
		if s[i] == '(' {
			stack.Push(string('('))
		} else {
			if stack.IsEmpty() {
				return false
			}
			stack.Pop()
		}
	}
	return stack.IsEmpty()
}

type Stack []string

// IsEmpty: check if stack is empty
func (s *Stack) IsEmpty() bool {
	return len(*s) == 0
}

// Push a new value onto the stack
func (s *Stack) Push(str string) {
	*s = append(*s, str) // Simply append the new value to the end of the stack
}

// Remove and return top element of stack. Return false if stack is empty.
func (s *Stack) Pop() (string, bool) {
	if s.IsEmpty() {
		return "", false
	} else {
		index := len(*s) - 1   // Get the index of the top most element.
		element := (*s)[index] // Index into the slice and obtain the element.
		*s = (*s)[:index]      // Remove it from the stack by slicing it off.
		return element, true
	}
}

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 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 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 o := 0; o < t; o++ {
		var n int

		fmt.Fscan(input, &n)

		STRING := make([]byte, 0)
		fmt.Fscan(input, &STRING)
		intiNum := 0
		EndNumb := 0
		MiddleNum := 0
		if string(STRING[0]) == string(STRING[len(STRING)-1]) {
			fmt.Println("NO")
		} else {
			init := string(STRING[0])
			end := string(STRING[len(STRING)-1])
			for i := 0; i < len(STRING); i++ {
				if string(STRING[i]) == init {
					intiNum++
				} else {
					if string(STRING[i]) == end {
						EndNumb++
					} else {
						MiddleNum++
					}
				}
			}
			if intiNum+MiddleNum != EndNumb && EndNumb+MiddleNum != intiNum {
				fmt.Println("NO")
			} else {
				if intiNum > EndNumb {
					open := 0
					close := 0
					q := 0
					for j := 0; j < len(STRING); j++ {
						if string(STRING[j]) == init {
							open++
						} else {
							close++
						}
						if close > open {
							fmt.Println("NO")
							q = 1
							break
						}
					}
					if q == 0 {
						for m := len(STRING) - 1; m > -1; m-- {
							if string(STRING[m]) != init {
								close++
							} else {
								open++
							}
							if open > close {
								fmt.Println("NO")
								q = 1
								break
							}
						}
					}
					if q == 0 {
						fmt.Println("YES")
					}

				} else {
					open := 0
					close := 0
					q := 0
					for j := 0; j < len(STRING); j++ {
						if string(STRING[j]) != end {
							open++
						} else {
							close++
						}
						if close > open {
							fmt.Println("NO")
							q = 1
							break
						}
					}
					if q == 0 {
						for m := len(STRING) - 1; m > -1; m-- {
							if string(STRING[m]) == end {
								close++
							} else {
								open++
							}
							if open > close {
								fmt.Println("NO")
								q = 1
								break
							}
						}
					}
					if q == 0 {
						fmt.Println("YES")
					}

				}
			}

		}
	}
}

package main

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

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

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &s)
		a := 0
		b := 0
		c := 0
		ac := 0
		ab := 0
		bc := 0
		for i := range s {
			if s[i] == 'A' {
				if a >= 0 {
					a++
				}
				if ac >= 0 {
					ac++
				}
				if ab >= 0 {
					ab++
				}
				b--
				c--
				bc--
			}
			if s[i] == 'B' {
				if b >= 0 {
					b++
				}
				if ab >= 0 {
					ab++
				}
				if bc >= 0 {
					bc++
				}
				a--
				c--
				ac--
			}
			if s[i] == 'C' {
				if c >= 0 {
					c++
				}
				if ac >= 0 {
					ac++
				}
				if bc >= 0 {
					bc++
				}
				a--
				b--
				ab--
			}
		}

		if a == 0 || b == 0 || c == 0 || ab == 0 || ac == 0 || bc == 0 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

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

	var T int
	var s []byte
O:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &s)
	o:
		for i := 0; i < 8; i++ {
			c := 0
			for _, b := range s {
				c += i>>(b-'A')&1*2 - 1
				if c < 0 {
					continue o
				}
			}
			if c == 0 {
				Fprintln(out, "YES")
				continue O
			}
		}
		Fprintln(out, "NO")
	}
}
func main() { CF1494A(os.Stdin, os.Stdout) }

package main
import . "fmt"

// github.com/EndlessCheng/codeforces-go
func main() {
	var t int
	var s []byte
T:
	for Scan(&t); t > 0; t-- {
		Scan(&s)
	o:
		for i := 0; i < 8; i++ {
			c := 0
			for _, b := range s {
				c += i>>(b-'A')&1*2 - 1
				if c < 0 {
					continue o
				}
			}
			if c == 0 {
				Println("YES")
				continue T
			}
		}
		Println("NO")
	}
}

// 1494A. ABC String
package main

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

func valid_sequence(s string) bool {
	stack := make([]byte, 0)
	for i := 0; i < len(s); i++ {
		if s[i] == '(' {
			stack = append(stack, s[i])
		} else {
			if len(stack) == 0 {
				return false
			}
			stack = stack[:len(stack)-1]
		}
	}
	return len(stack) == 0
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var s string
		fmt.Fscan(reader, &s)
		A := "()"
		B := "()"
		C := "()"
		ok := false
		for i := 0; i < 2; i++ {
			for j := 0; j < 2; j++ {
				for k := 0; k < 2; k++ {
					res := []byte(s)
					m := make(map[byte]byte)
					m['A'] = A[i]
					m['B'] = B[j]
					m['C'] = C[k]
					for l := 0; l < len(s); l++ {
						res[l] = m[s[l]]
					}
					if valid_sequence(string(res)) {
						ok = true
					}
				}
			}
		}
		if ok {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t      int
	a      string
)

func check(x []byte, y byte) bool {
	z := make([]byte, len(x))
	copy(z, x)
	for i := range z {
		if z[i] != '(' && z[i] != ')' {
			z[i] = y
		}
	}
	temp := 0
	for i := range z {
		if z[i] == '(' {
			temp++
		} else {
			temp--
		}
		if temp < 0 {
			return false
		}
	}
	return temp == 0
}

func solve(s string) string {
	l := len(s)
	if s[0] == s[l-1] {
		return "NO"
	}
	x := []byte(s)
	for i := range x {
		if s[i] == s[0] {
			x[i] = '('
		}
		if s[i] == s[l-1] {
			x[i] = ')'
		}
	}
	if check(x, '(') || check(x, ')') {
		return "YES"
	}
	return "NO"
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &a)
		fmt.Println(solve(a))
	}
}

package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
	"strings"
)

var bytes []byte

func nextByte() byte {
	b := bytes[0]
	bytes = bytes[1:]
	return b
}

func ScanInt64() int64 {
	var negative bool
	var result int64

	b := nextByte()

	for ; b < '0' || b > '9'; b = nextByte() {
		if b == '-' {
			negative = true
		}
	}

	for ; '0' <= b && b <= '9'; b = nextByte() {
		result = (result << 3) + (result << 1) + int64(b-'0')
	}

	if negative {
		return -result
	} else {
		return result
	}
}

func ScanInt() int {
	return int(ScanInt64())
}

func ScanString() string {
	builder := strings.Builder{}

	for b := nextByte(); b != '\r'; b = nextByte() {
		if b != '\n' {
			builder.WriteByte(b)
		}
	}

	return builder.String()
}

func init() {
	f, err := os.Open("in.txt")

	if err == nil {
		defer f.Close()
	} else {
		f = os.Stdin
	}

	bytes, _ = ioutil.ReadAll(bufio.NewReader(f))
}

func main() {
	for t := ScanInt(); t > 0; t-- {
		s := ScanString()
		n := len(s)

		if s[0] == s[n-1] {
			fmt.Println("NO")
			continue
		}

		c := 'A' + 'B' + 'C' - s[0] - s[n-1]
		s = strings.ReplaceAll(s, s[0:1], "(")
		s = strings.ReplaceAll(s, s[n-1:n], ")")

		if len(strings.ReplaceAll(s, "(", "")) > len(strings.ReplaceAll(s, ")", "")) {
			s = strings.ReplaceAll(s, string(c), "(")
		} else {
			s = strings.ReplaceAll(s, string(c), ")")
		}

		open := 0

		for _, c := range s {
			if c == '(' {
				open++
			} else {
				open--
				if open < 0 {
					break
				}
			}
		}

		if open == 0 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

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

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

func main() {
	defer writer.Flush()
	////////////////////
	var count int
	scan(&count)
	for count != 0 {
		solve()
		count--
	}

}

func solve() {
	s := ""
	scan(&s)
	b := false
	if check(s, 'A', 'B', false) {
		b = true
	} else if check(s, 'B', 'A', true) {
		b = true
	} else if check(s, 'A', 'B', true) {
		b = true
	} else if check(s, 'B', 'A', false) {
		b = true
	} else {
		print("No", "\n")
	}
	if b {
		print("Yes", "\n")
	}
}

func check(s string, plus rune, minus rune, c bool) bool {
	count := 0
	for _, v := range s {
		if v == plus {
			count++
		}
		if v == minus {
			count--
		}
		if v == 'C' {
			if c {
				count++
			} else {
				count--
			}
		}
		if count < 0 {
			return false
		}
	}
	if count == 0 {
		return true
	}
	return false
}

package main

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

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

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

func main() {
	defer writer.Flush()
	////////////////////
	var count int
	scan(&count)
	for count != 0 {
		solve()
		count--
	}

}

func solve() {
	s := ""
	scan(&s)
	if check(s, 'A', 'B', false) {
		print("Yes", "\n")
		return
	}
	if check(s, 'B', 'A', true) {
		print("Yes", "\n")
		return
	}
	if check(s, 'A', 'B', true) {
		print("Yes", "\n")
		return
	}
	if check(s, 'B', 'A', false) {
		print("Yes", "\n")
		return
	}
	print("No", "\n")
}

func check(s string, plus rune, minus rune, c bool) bool {
	count := 0
	for _, v := range s {
		if v == plus {
			count++
		}
		if v == minus {
			count--
		}
		if v == 'C' {
			if c {
				count++
			} else {
				count--
			}
		}
		if count < 0 {
			return false
		}
	}
	if count == 0 {
		return true
	}
	return false
}

package main

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

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

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

func main() {
	defer writer.Flush()
	////////////////////
	var count int
	scan(&count)
	for count != 0 {
		solve()
		count--
	}

}

func solve() {
	s := ""
	scan(&s)
	if check(s, 'A', 'B', 'C') {
		print("Yes", "\n")
		return
	}
	if check(s, 'B', 'A', 'C') {
		print("Yes", "\n")
		return
	}
	print("No", "\n")
}

func check(s string, plus rune, minus rune, c rune) bool {
	count := 0
	count2 := 0
	for _, v := range s {
		if v == plus {
			count++
			count2++
		}
		if v == minus {
			count--
			count2--
		}
		if v == c {
			count++
			count2--
		}
		if count < 0 {
			count = 1000
		}
		if count2 < 0 {
			count2 = 1000
		}
	}
	if count == 0 || count2 == 0 {
		return true
	}
	return false
}

package main

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

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

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

func main() {
	defer writer.Flush()
	////////////////////
	var count int
	scan(&count)
	for count != 0 {
		solve()
		count--
	}

}

func solve() {
	s := ""
	aca, bcb, acb, bca := 0, 0, 0, 0
	scan(&s)
	for _, v := range s {
		if v == 'A' {
			aca++
			acb++
			bca--
			bcb--
		}
		if v == 'B' {
			aca--
			acb--
			bca++
			bcb++
		}
		if v == 'C' {
			aca++
			acb--
			bca--
			bcb++
		}
		if aca < 0 {
			aca = -100
		}
		if acb < 0 {
			acb = -100
		}
		if bca < 0 {
			bca = -100
		}
		if bcb < 0 {
			bcb = -100
		}
	}
	if aca == 0 || acb == 0 || bca == 0 || bcb == 0 {
		print("YES", "\n")
	} else {
		print("NO", "\n")
	}
}


//
package main

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

func solve(k int, n string) int {
	sum, count, i := 0, 0, 0
	arr := []byte(n)

	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })

	for i := range arr {
		sum += int(arr[i] - '0')
	}

	needed := k - sum
	for needed > 0 {
		needed -= 9 - int(arr[i]-'0')
		count++
		i++
	}

	return count
}

func main() {
	r := bufio.NewReader(os.Stdin)
	var k int
	var n string
	fmt.Fscanf(r, "%d\n", &k)
	fmt.Fscanf(r, "%s\n", &n)
	fmt.Fprintf(os.Stdout, "%d\n", solve(k, n))
}

//
package main

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

func solve(k int, n string) int {
	sum := 0
	arr := []byte(n)
	res := 0

	for i := range arr {
		sum += int(arr[i] - '0')
		if int(sum) > k {
			return 0
		}
	}

	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })

	for i := 0; sum < k; i++ {
		if arr[i] != '9' {
			sum += int('9' - arr[i])
			res++
		}
	}

	return res
}

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

	return n2
}

func main() {
	r := bufio.NewReader(os.Stdin)
	var k int
	var n string
	fmt.Fscanf(r, "%d\n", &k)
	fmt.Fscanf(r, "%s\n", &n)
	fmt.Fprintf(os.Stdout, "%d\n", solve(k, n))
}

package main

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

func Init() {
}

func Solve(io *FastIO) {
	K := io.NextInt()
	N := io.NextString()

	sum := 0
	freq := make([]int, 10)
	for _, c := range N {
		d := c - '0'
		freq[d]++
		sum += int(d)
	}
	wantChange := K - sum

	L := 0
	R := len(N)
	good := R
	for L <= R {
		M := (L + R) >> 1
		if GetMaxChange(freq, M) >= wantChange {
			R = M - 1
			good = M
		} else {
			L = M + 1
		}
	}
	io.Println(good)
}

func GetMaxChange(freq []int, digits int) int {
	change := 0
	for d, f := range freq {
		use := Min(f, digits)
		change += use * (9 - d)
		digits -= use
	}
	return change
}

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

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

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

	var num string
	fmt.Scanf("%s\n", &num)

	digitCount := make(map[int]int)
	sum := 0
	for _, c := range num {
		sum += int(c - '0')
		digitCount[int(c-'0')]++
	}

	if sum >= k {
		fmt.Println(0)
		return
	}

	need := k - sum
	digits := 0

	for digit := 0; digit < 9; digit++ {
		valueAdded := 9 - digit

		// fmt.Println(need)
		if need > digitCount[digit]*valueAdded {
			need -= digitCount[digit] * valueAdded
			digits += digitCount[digit]
			continue
		}

		digits += need / valueAdded
		if need%valueAdded != 0 {
			digits++
		}
		break
	}

	fmt.Println(digits)
}

package main

import (
	"fmt"
)

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

	var num string
	fmt.Scanf("%s\n", &num)

	digitCount := make(map[int]int)
	sum := 0
	for _, c := range num {
		sum += int(c - '0')
		digitCount[int(c-'0')]++
	}

	if sum >= k {
		fmt.Println(0)
		return
	}

	need := k - sum
	digits := 0

	for digit := 1; digit < 9; digit++ {
		valueAdded := 9 - digit

		// fmt.Println(need)
		if need > digitCount[digit]*valueAdded {
			need -= digitCount[digit] * valueAdded
			digits += digitCount[digit]
			continue
		}

		digits += need / valueAdded
		if need%valueAdded != 0 {
			digits++
		}
		break
	}

	fmt.Println(digits)
}

//
package main

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

func solve(k int, n string) int {
	sum := byte(0)
	arr := []byte(n)
	res := 0

	for i := range arr {
		sum += arr[i] - '0'
		if int(sum) > k {
			return 0
		}
	}

	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })

	diff := byte(k) - sum
	for i := range arr {
		m := 9 - (arr[i] - '0')
		diff -= min(m, diff)
		res++
		if diff == 0 {
			break
		}
	}

	return res
}

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

	return n2
}

func main() {
	r := bufio.NewReader(os.Stdin)
	var k int
	var n string
	fmt.Fscanf(r, "%d\n", &k)
	fmt.Fscanf(r, "%s\n", &n)
	fmt.Fprintf(os.Stdout, "%d\n", solve(k, n))
}

//
package main

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

func solve(k int, n int) int {
	sum := 0
	arr := []int{}
	res := 0

	for n > 0 {
		mod := n % 10
		n /= 10
		sum += mod
		if sum > k {
			return 0
		}
		arr = append(arr, mod)
	}

	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
	diff := k - sum
	for i := range arr {
		m := 9 - arr[i]
		diff -= min(m, diff)
		res++
		if diff == 0 {
			break
		}
	}

	return res
}

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

	return n2
}

func main() {
	r := bufio.NewReader(os.Stdin)
	var k, n int
	fmt.Fscanf(r, "%d\n", &k)
	fmt.Fscanf(r, "%d\n", &n)
	fmt.Fprintf(os.Stdout, "%d\n", solve(k, n))
}

//
package main

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

func solve(k int, n string) int {
	sum := byte(0)
	arr := []byte(n)
	res := 0

	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
	for i := range arr {
		sum += arr[i] - '0'
	}

	diff := byte(k) - sum
	for i := range arr {
		m := 9 - (arr[i] - '0')
		diff -= min(m, diff)
		res++
		if diff == 0 {
			break
		}
	}

	return res
}

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

	return n2
}

func main() {
	r := bufio.NewReader(os.Stdin)
	var k int
	var n string
	fmt.Fscanf(r, "%d\n", &k)
	fmt.Fscanf(r, "%s\n", &n)
	fmt.Fprintf(os.Stdout, "%d\n", solve(k, n))
}

//
package main

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

func solve(k int, n int) int {
	sum := 0
	arr := []int{}
	res := 0

	for n > 0 {
		mod := n % 10
		n /= 10
		sum += mod
		if sum > k {
			return 0
		}
		arr = append(arr, mod)
	}

	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
	diff := k - sum
	for i := range arr {
		m := 9 - arr[i]
		diff -= min(m, diff)
		res++
		if diff == 0 {
			break
		}
	}

	return res
}

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

	return n2
}

func main() {
	r := bufio.NewReader(os.Stdin)
	var k, n int
	fmt.Fscanf(r, "%d\n", &k)
	fmt.Fscanf(r, "%s\n", &n)
	fmt.Fprintf(os.Stdout, "%d\n", solve(k, n))
}

//
package main

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

func solve(k byte, n string) int {
	sum := byte(0)
	arr := []byte(n)
	res := 0

	for i := range arr {
		sum += arr[i] - '0'
		if sum > k {
			return 0
		}
	}

	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })

	diff := byte(k) - sum
	for i := range arr {
		m := 9 - (arr[i] - '0')
		diff -= min(m, diff)
		res++
		if diff == 0 {
			break
		}
	}

	return res
}

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

	return n2
}

func main() {
	r := bufio.NewReader(os.Stdin)
	var k byte
	var n string
	fmt.Fscanf(r, "%d\n", &k)
	fmt.Fscanf(r, "%s\n", &n)
	fmt.Fprintf(os.Stdout, "%d\n", solve(k, n))
}


package main

import (
    "fmt"
    "bufio"
    "os"
)
func main(){
    reader := bufio.NewReader(os.Stdin)
    w, _ := reader.ReadString('\n')
    l, _ := reader.ReadString('\n')
    counter_w := make(map[byte]int)
    counter_l := make(map[byte]int)
    for i:=0; i<len(w); i++{
        if w[i] == ' '{
            continue
        }
        counter_w[w[i]] += 1
    }
    for i:=0; i<len(l); i++{
        if l[i] == ' '{
            continue
        }
        counter_l[l[i]] += 1
    }
    result := "YES"
    for letter, count := range counter_l{
        value, ok := counter_w[letter]
        if ok==false || value < count{
            result = "NO"
            break
        }
    }
    fmt.Println(result)
}

// 43B
package main

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

func main() {
	var i int
	a := make([]int, 150)
	var s, l string
	in := bufio.NewScanner(os.Stdin)
	in.Split(bufio.ScanLines)
	in.Scan()
	s = in.Text()
	in.Scan()
	l = in.Text()
	for i = 0; i < len(s); i++ {
		a[s[i]]++
	}
	for i = 0; i < len(l); i++ {
		if a[l[i]] < 1 && l[i] != ' ' {
			fmt.Print("NO")
			return
		}
		a[l[i]]--
	}
	fmt.Print("YES")
	return
}


package main

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

var t int

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

	fmt.Fscan(input, &t)

	for o := 0; o < t; o++ {
		var n, m int
		fmt.Fscan(input, &n)
		fmt.Fscan(input, &m)

		KArr := make([]int, n+1)
		CArr := make([]int, m+1)
		IndexArr := make([]int, m+1)
		for i := 1; i < n+1; i++ {
			fmt.Fscan(input, &KArr[i])
			IndexArr[KArr[i]] = IndexArr[KArr[i]] + 1
		}
		for j := 1; j < m+1; j++ {
			fmt.Fscan(input, &CArr[j])
		}

		Remainder := n
		INDEX := 0

		for s := 1; Remainder > -1; s++ {
			Remainder = Remainder - IndexArr[s] - 1
			INDEX = INDEX + 1
			if Remainder <= 0 {
				break
			}

		}
		var SUM int64
		if Remainder == 0 {
			for h := 1; h < INDEX+1; h++ {
				SUM = SUM + int64(CArr[h])*int64(IndexArr[h]+1)
			}
			fmt.Println(SUM)
		} else {
			qu := Remainder + IndexArr[INDEX] + 1
			if INDEX > 1 {
				for h := 1; h < INDEX; h++ {
					SUM = SUM + int64(CArr[h])*int64(IndexArr[h]+1)
				}
			}
			SUM = SUM + int64(CArr[INDEX])*int64(qu)
			fmt.Println(SUM)

		}

	}
}

package main

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

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

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

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

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

func sbytes() []byte {
	scanner.Scan()
	return scanner.Bytes()
}

// [2 2 3 3 4]
//  G G G B B
//
// [4 4 4 4 4]
//  B B B B G
func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n, m := sint64(), sint64()
		k := make([]int64, n)
		for i := range k {
			k[i] = sint64()
		}
		c := make([]int64, m)
		for i := range c {
			c[i] = sint64()
		}
		printf("%d\n", sol(n, m, k, c))
	}
}

func sol(n, m int64, k, c []int64) int64 {
	sort.Slice(k, func(il, ir int) bool {
		return k[il] > k[ir]
	})
	ret := int64(0)
	buyN := int64(0)
	for i := range k {
		if k[i]-1 >= buyN {
			buyN++
		}
	}
	nxt := 0
	for i := range k {
		if buyN > 0 {
			ret += c[nxt]
			nxt++
			buyN--
			continue
		}
		ret += c[k[i]-1]
	}
	return ret
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1470A(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}

	var T, n, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		id := make([]int, n)
		for i := range id {
			Fscan(in, &id[i])
		}
		sort.Ints(id)
		c := make([]int, m)
		for i := range c {
			Fscan(in, &c[i])
		}
		ans := int64(0)
		for i, k := range id {
			ans += int64(c[min(n-1-i, k-1)])
		}
		Fprintln(out, ans)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1470A(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}

	var T, n, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		id := make([]int, n)
		for i := range id {
			Fscan(in, &id[i])
		}
		sort.Ints(id)
		c := make([]int, m)
		for i := range c {
			Fscan(in, &c[i])
		}
		ans := int64(0)
		for i := n - 1; i >= 0; i-- {
			ans += int64(c[min(id[i]-1, n-1-i)])
		}
		Fprintln(out, ans)
	}
}

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

package main

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

/*
1
5 4
2 2 3 3 4
3 5 12 20
---
30   4 -> 3, 3 -> 5, 3 -> 12, 2 -> 5, 2 -> 5
======
2
1 1
1
1
1 2
1
1 2
---
1
1
======
1
2 2
1 2
1 2
---
2
======
1
2 2
2 2
1 2
---
3
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n, m int
	scanf("%d %d\n", &n, &m)

	k := make([]int, n)

	for i := range k {
		scanf("%d", &k[i])
	}
	scanf("\n")

	c := make([]int, m)

	for i := range c {
		scanf("%d", &c[i])
	}
	scanf("\n")

	sort.Ints(k)

	sum := int64(0)

	j := 0

	for i := n - 1; i >= 0; i-- {
		if c[j] < c[k[i]-1] {
			sum += int64(c[j])
			j++
		} else {
			sum += int64(c[k[i]-1])
		}
	}

	fmt.Fprintln(writer, sum)
}

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

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

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

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

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

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

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

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

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

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

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

	return nums[idx]
}

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

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

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

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

	return true
}

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

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

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

	num = abs(num)

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

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

package main

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

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
	t   int
	n   int
	m   int
)

func main() {

	defer out.Flush()

	fmt.Fscan(in, &t)

	for ; t > 0; t-- {

		fmt.Fscan(in, &n, &m)
		k := make([]int, n)
		c := make([]int, m)

		for i := range k {
			fmt.Fscan(in, &k[i])
		}

		for i := range c {
			fmt.Fscan(in, &c[i])
		}

		fmt.Fprintln(out, solve(k, c))

	}

}

func solve(k []int, c []int) int64 {

	sum := int64(0)

	reverseSort(k)

	for i := range k {

		if i >= len(c) {
			sum += int64(c[k[i]-1])
		} else if c[i] > c[k[i]-1] {
			sum += int64(c[k[i]-1])
		} else {
			sum += int64(c[i])
		}

	}

	return sum

}

//印出數字陣列
func printIntArr(array []int) {

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

	fmt.Println()

}

//排降序
func reverseSort(a []int) {

	sort.Sort(sort.Reverse(sort.IntSlice(a)))

}

package main

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

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

func main() {
	defer out.Flush()

	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		t--
		fmt.Fprintln(out, solve())
	}
}

func solve() int64 {
	var x, n, m int
	fmt.Fscan(in, &n, &m)
	cnt := make([]int, m)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &x)
		cnt[x-1]++
	}
	c := make([]int64, m)
	for i := 0; i < m; i++ {
		fmt.Fscan(in, &c[i])
	}
	ptr := 0
	res := int64(0)

	for i := m - 1; i >= 0; i-- {
		for cnt[i] > 0 && ptr < i {
			res += c[ptr]
			ptr++
			cnt[i]--
		}
		if cnt[i] > 0 {
			res += c[i] * int64(cnt[i])
		}
	}
	return res
}

package main

import (
	"bufio"
	"fmt"
	"os"
    "sort"
)
var (
    input = bufio.NewReader(os.Stdin)
    output = bufio.NewWriter(os.Stdout)
)

func assert(test bool) {
    if !test {panic("assertion!")}
}

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
func main() {
    var t int
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
    output.Flush()
}

func solve() {
    var n, m int64
    fmt.Fscan(input, &n, &m)
    k := make([]int64, n)
    for i := 0; i < len(k); i++ {
        fmt.Fscan(input, &k[i])
    }
    c, cc := make([]int64, m), make([]int64, m)
    for i := 0; i < len(c); i++ {
        fmt.Fscan(input, &c[i])
        cc[i] = c[i]
    }
    sort.Slice(k, func(i, j int) bool { return c[k[i] - 1] > c[k[j] - 1]})
    sort.Slice(cc, func(i, j int) bool {return cc[i] < cc[j]})
    sum := int64(0)
    for i := 0; i < len(k); i++ {
        if cc[0] < c[k[i] - 1] {
            sum += cc[0]
            cc = cc[1:]
        } else {
            sum += c[k[i] - 1]
        }
    }

    fmt.Fprintln(output, sum)
}

package main

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

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

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &m)
		k := make([]int, n)
		c := make([]int64, m)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &k[i])
			k[i]--
		}
		for i := 0; i < m; i++ {
			fmt.Fscan(in, &c[i])
		}

		sum := int64(0)
		available := 0
		sort.Sort(sort.Reverse(sort.IntSlice(k)))
		for i := range k {
			if available <= k[i] {
				sum += c[available]
				available++
			} else {
				sum += c[k[i]]
			}
		}

		fmt.Println(sum)
	}
}

package main

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

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

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

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

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

func sbytes() []byte {
	scanner.Scan()
	return scanner.Bytes()
}

// [2 3 4 3 2]
// [2 2 3 3 4] (sort)
//  G G G B B
// #G > #B
// G => add relevant elements
// B => add first few elements
// [3 5 12 20]
// G => 5+5+12
// B => 3+5
func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n, m := sint64(), sint64()
		k := make([]int64, n)
		for i := range k {
			k[i] = sint64()
		}
		c := make([]int64, m)
		for i := range c {
			c[i] = sint64()
		}
		printf("%d\n", sol(n, m, k, c))
	}
}

func sol(n, m int64, k, c []int64) int64 {
	sort.Slice(k, func(il, ir int) bool {
		return k[il] < k[ir]
	})
	nb := min(m, n/2)
	ng := n - nb
	ret := int64(0)
	for i := int64(0); i < ng; i++ {
		ret += c[k[i]-1]
	}
	for i := int64(0); i < nb; i++ {
		ret += c[i]
	}
	return ret
}

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

package main

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

var t int

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

	fmt.Fscan(input, &t)

	for o := 0; o < t; o++ {
		var n, m int
		fmt.Fscan(input, &n)
		fmt.Fscan(input, &m)

		KArr := make([]int, n+1)
		CArr := make([]int, m+1)
		IndexArr := make([]int, m+1)
		for i := 1; i < n+1; i++ {
			fmt.Fscan(input, &KArr[i])
			IndexArr[KArr[i]] = IndexArr[KArr[i]] + 1
		}
		for j := 1; j < m+1; j++ {
			fmt.Fscan(input, &CArr[j])
		}

		Remainder := n
		INDEX := 0

		for s := 1; Remainder > -1; s++ {
			Remainder = Remainder - IndexArr[s] - 1
			INDEX = INDEX + 1
			if Remainder <= 0 {
				break
			}

		}
		var SUM int64
		if Remainder == 0 {
			for h := 1; h < INDEX+1; h++ {
				SUM = SUM + int64(CArr[h]*(IndexArr[h]+1))
			}
			fmt.Println(SUM)
		} else {
			qu := Remainder + IndexArr[INDEX] + 1
			if INDEX > 1 {
				for h := 1; h < INDEX; h++ {
					SUM = SUM + int64(CArr[h]*(IndexArr[h]+1))
				}
			}
			SUM = SUM + int64(CArr[INDEX]*qu)
			fmt.Println(SUM)

		}

	}
}

package main

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

var t int

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

	fmt.Fscan(input, &t)

	for o := 0; o < t; o++ {
		var n, m int
		fmt.Fscan(input, &n)
		fmt.Fscan(input, &m)

		KArr := make([]int, n+1)
		CArr := make([]int, m+1)
		IndexArr := make([]int, m+1)
		for i := 1; i < n+1; i++ {
			fmt.Fscan(input, &KArr[i])
			IndexArr[KArr[i]] = IndexArr[KArr[i]] + 1
		}
		for j := 1; j < m+1; j++ {
			fmt.Fscan(input, &CArr[j])
		}

		Remainder := n
		INDEX := 0

		for s := 1; Remainder > -1; s++ {
			Remainder = Remainder - IndexArr[s] - 1
			INDEX = INDEX + 1
			if Remainder <= 0 {
				break
			}

		}
		var SUM int64
		if Remainder == 0 {
			for h := 1; h < INDEX+1; h++ {
				SUM = SUM + int64(CArr[h]*(IndexArr[h]+1))
			}
			fmt.Println(SUM)
		} else {
			qu := Remainder + IndexArr[INDEX] + 1
			for h := 1; h < INDEX; h++ {
				SUM = SUM + int64(CArr[h]*(IndexArr[h]+1))
			}
			SUM = SUM + int64(CArr[INDEX]*qu)
			fmt.Println(SUM)

		}

	}
}