package main

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

func solve() {
	n := get()
	a := get64a(n)

	find := func(start int) (count int64) {
		for i := start; i+1 < n; i += 2 {
			max := a[i-1]
			if a[i+1] > max {
				max = a[i+1]
			}
			if a[i] > max {
				continue
			}
			count += max - a[i] + 1
		}
		return
	}

	if n%2 == 1 {
		put64ln(find(1))
		return
	}

	need := make([]int64, n)

	for i := 1; i+1 < n; i++ {
		max := a[i-1]
		if a[i+1] > max {
			max = a[i+1]
		}
		if a[i] > max {
			continue
		}
		need[i] = max - a[i] + 1
	}

	sumR := int64(0)
	for i := 2; i+1 < n; i += 2 {
		sumR += need[i]
	}

	min := sumR
	sumL := int64(0)

	for i := 1; i+1 < n; i += 2 {
		sumL += need[i]
		sumR -= need[i+1]
		if sumL+sumR < min {
			min = sumL + sumR
		}
	}

	put64ln(min)
}

const (
	once       = false
	maxBufSize = 10000000
)

var (
	splitFunc = bufio.ScanWords
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	in.Split(splitFunc)
	in.Buffer(make([]byte, maxBufSize), maxBufSize)

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

	if once {
		solve()
		return
	}

	t := get()
	for t > 0 {
		solve()
		t--
	}
}

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

func get() int {
	x, _ := strconv.Atoi(gets())
	return x
}

func geta(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get()
	}
	return a
}

func get64() int64 {
	x, _ := strconv.ParseInt(gets(), 10, 64)
	return x
}

func get64a(n int) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = get64()
	}
	return a
}

func getf() float64 {
	x, _ := strconv.ParseFloat(gets(), 64)
	return x
}

func getfa(n int) []float64 {
	a := make([]float64, n)
	for i := range a {
		a[i] = getf()
	}
	return a
}

func gets() string {
	scan()
	return in.Text()
}

func scanN(n int) {
	for n > 0 {
		scan()
		n--
	}
}

func scan() {
	in.Scan()
}

func putc(c rune) {
	out.WriteRune(c)
}

func put(x int) {
	out.WriteString(strconv.Itoa(x))
}

func putln(i int) {
	put(i)
	line()
}

func puta(a []int) {
	for _, v := range a {
		put(v)
		space()
	}
	line()
}

func put64(x int64) {
	out.WriteString(strconv.FormatInt(x, 10))
}

func put64ln(x int64) {
	put64(x)
	line()
}

func put64a(a []int64) {
	for _, v := range a {
		put64(v)
		space()
	}
	line()
}

func puts(s string) {
	out.WriteString(s)
}

func putsln(s string) {
	puts(s)
	line()
}

func yn(y bool) {
	if y {
		yes()
		return
	}
	no()
}

func yes() {
	puts("YES\n")
}

func no() {
	puts("NO\n")
}

func putf(x float64) {
	out.WriteString(strconv.FormatFloat(x, 'f', -1, 64))
}

func putfln(x float64) {
	putf(x)
	line()
}

func putfa(a []float64) {
	for _, v := range a {
		putf(v)
		space()
	}
	line()
}

func line() {
	out.WriteRune('\n')
}

func space() {
	out.WriteRune(' ')
}

// 00
package main

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

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

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		h := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &h[i])
		}
		ans := int64(0)
		for i := 1; i < n-1; i += 2 {
			mx := max(h[i-1], h[i+1])
			if mx >= h[i] {
				ans += int64((mx - h[i] + 1))
			}
		}
		if n%2 == 1 {
			fmt.Println(ans)
		} else {
			cool := make([]int, n)
			for i := 1; i < n-1; i++ {
				mx := max(h[i-1], h[i+1])
				if mx >= h[i] {
					cool[i] = (mx - h[i] + 1)
				}
			}
			// fmt.Println(cool)
			ans1 := int64(0)
			for i := 1; i < n-1; i += 2 {
				ans1 += int64(cool[i])
			}
			ans := ans1
			//fmt.Println("starting is ", ans1)
			for i := n - 3; i > 0; i -= 2 {
				ans1 += int64(cool[i+1] - cool[i])
				ans = min(ans, ans1)
				// fmt.Println("is ", i, ans1)
			}
			fmt.Println(ans)
		}

	}
}

package main

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

var cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	arr := make([]int, n)
	for i := range arr {
		fmt.Fscan(in, &arr[i])
	}
	priceLeft := make([]int64, n)
	priceRight := make([]int64, n)
	for i := 1; i < n-1; i++ {
		curPrice := max(max(arr[i-1], arr[i+1])-arr[i]+1, 0)
		priceLeft[i] = int64(curPrice)
		if i > 1 {
			priceLeft[i] += priceLeft[i-2]
		}
	}
	for i := n - 2; i >= 1; i-- {
		curPrice := max(max(arr[i-1], arr[i+1])-arr[i]+1, 0)
		priceRight[i] = int64(curPrice)
		if i < n-2 {
			priceRight[i] += priceRight[i+2]
		}
	}
	ans := priceLeft[n-2]
	if n%2 == 0 {
		ans = min(priceRight[1], priceRight[2])
		for i := 1; i < n-4; i += 2 {
			ans = min(ans, priceLeft[i]+priceRight[i+3])
		}
	}
	fmt.Fprintln(out, ans)
}

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

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

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

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

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

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

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

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

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

var exists = struct{}{}

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

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

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

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

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

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

type Ordered interface {
	Integer | Float | ~string
}

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

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

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

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

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

package main

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

func solveOdd(n int, a []int64) int64 {
	var res int64 = 0
	for i := 0; i < n; i++ {
		if i%2 == 0 {
			continue
		}
		u := max(a[i-1], a[i+1])
		if u >= a[i] {
			res += u - a[i] + 1
		}
	}
	return res
}

func solve(n int, a []int64) int64 {
	if n%2 == 1 {
		return solveOdd(n, a)
	}
	b := make([]int64, n-2)
	for i := 1; i < n-1; i++ {
		b[i-1] = 0
		u := max(a[i-1], a[i+1])
		if u >= a[i] {
			b[i-1] = u - a[i] + 1
		}
	}

	f := make([][]int64, n-2)
	for i := 0; i < n-2; i++ {
		f[i] = make([]int64, 2)
	}
	for i := 0; i < n-2; i++ {
		if i == 0 || i == 1 {
			f[i][0] = b[i]
			f[i][1] = b[i]
			continue
		}
		f[i][0] = f[i-2][0] + b[i]
		f[i][1] = f[i-2][1] + b[i]
		if i >= 3 && f[i][1] > f[i-3][0]+b[i] {
			f[i][1] = f[i-3][0] + b[i]
		}
	}

	res := min(f[n-3][0], f[n-3][1])
	res = min(res, f[n-4][0])
	return res
}

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

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

func main() {
	inp := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	var test int
	fmt.Fscanln(inp, &test)

	for test > 0 {
		test--

		var n int
		fmt.Fscanln(inp, &n)

		a := make([]int64, n)
		for i := 0; i < n; i++ {
			if i == n-1 {
				fmt.Fscanln(inp, &a[i])
			} else {
				fmt.Fscan(inp, &a[i])
			}
		}

		fmt.Fprintln(out, solve(n, a))
	}

	out.Flush()
}

package main

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

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

func c1706(_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

	cal := func(a, b, c int) int64 {
		if b < c {
			b, c = c, b
		}
		if a > b {
			return 0
		}
		if a == b {
			return 1
		}
		return int64(b - a + 1)
	}

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

		ans := int64(0)
		if n%2 == 1 {
			for i := 1; i < n; i += 2 {
				ans += cal(ar[i], ar[i-1], ar[i+1])
			}
		} else {
			l, r := make([]int64, n), make([]int64, n)
			sl, sr := int64(0), int64(0)
			for i := 1; i <= n-3; i += 2 {
				sl += cal(ar[i], ar[i-1], ar[i+1])
				l[i] = sl
			}
			for i := n - 2; i >= 2; i -= 2 {
				sr += cal(ar[i], ar[i-1], ar[i+1])
				r[i] = sr
			}
			ans = l[n-3]
			if r[2] < ans {
				ans = r[2]
			}
			for i := 1; i+3 < n; i += 2 {
				if l[i]+r[i+3] < ans {
					ans = l[i] + r[i+3]
				}
			}
		}

		Fprintln(out, ans)
	}
}

package main

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

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

	var buf bytes.Buffer

	tc := readNum(reader)

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

	fmt.Print(buf.String())
}

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

func normalize(s string) string {

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

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

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

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

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

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

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

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

	return i
}

func solve(H []int) int64 {
	n := len(H)

	get := func(i int) int64 {
		cur := max3(H[i-1], H[i], H[i+1])

		if cur == H[i-1] {
			cur++
		}
		if cur == H[i+1] {
			cur++
		}
		return int64(cur - H[i])
	}

	// 0 1, 0, 1
	var res int64
	if n%2 == 1 {
		for i := 1; i+1 < n; i += 2 {
			res += get(i)
		}
	} else {
		// 0, 1, 2, 3, 4, 5
		// could be 1, 3 or 2, 4
		dp := make([]int64, n+1)
		dp[n] = 0
		dp[n-1] = 0

		for i := n - 2; i > 0; i-- {
			dp[i] = dp[i+2] + get(i)
		}

		res = min(dp[1], dp[2])

		fp := make([]int64, 2)
		fp[0] = 1
		fp[1] = get(1)

		for i := 2; i < n-2; i++ {
			// if keep i not change
			if i%2 == 0 {
				tmp := fp[1] + min(dp[i+1], dp[i+2])
				res = min(res, tmp)
			} else {
				tmp := min(fp[0], fp[1]) + dp[i+1]
				res = min(res, tmp)
			}
			fp[i%2] += get(i)
		}
	}

	return res
}

func max3(a, b, c int) int {
	if a >= b && a >= c {
		return a
	}
	if b >= a && b >= c {
		return b
	}
	return c
}

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

package main

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

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

func main() {
	defer out.Flush()

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

func solve() int64 {
	var n int
	fmt.Fscan(in, &n)
	h := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &h[i])
	}

	if n%2 != 0 {
		var res int64
		for i := 1; i < n; i += 2 {
			tmp := max(h[i-1], h[i+1]) + 1
			res += max(tmp-h[i], 0)
		}
		return res
	}

	dp := make([][]int64, 2)
	dp[0] = make([]int64, n)
	dp[1] = make([]int64, n)
	for i := 1; i < n-1; i++ {
		tmp := max(h[i-1], h[i+1]) + 1
		tmp = max(tmp-h[i], 0)

		// fmt.Println(i, tmp)

		if i >= 2 {
			dp[0][i] = dp[0][i-2] + tmp
			dp[1][i] = dp[1][i-2] + tmp

			if i >= 3 {
				dp[1][i] = min(dp[1][i], dp[0][i-3]+tmp)
			}
		} else {
			dp[0][i] = tmp
			dp[1][i] = tmp
		}
	}

	// fmt.Println(dp)
	return min(dp[0][n-3], min(dp[0][n-2], dp[1][n-2]))
}

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

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

package main

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

const MAX =  1e5+10

func main(){
	var t, n int
	inputReader := bufio.NewReader(os.Stdin)
	fmt.Fscan(inputReader,&t)
	h := make([]int64, MAX)
	d := make([]int64, MAX)
	p := make([]int64, MAX)
	for (t > 0){
		t--
		fmt.Fscan(inputReader,&n)
		for i:=1; i<=n; i++{
			fmt.Fscan(inputReader,&h[i])
			d[i] = int64(0)
			p[i] = int64(0)
		}
		for i:=3; i<=n; i+=2{
			d[i] = calculateSingle(h[i-2], h[i-1], h[i]) + d[i-2]
		}
		for i:=n-2; i>=1; i-=2 {
			p[i] = calculateSingle(h[i+2], h[i+1], h[i]) + p[i+2]
		}
		if n%2 == 1{
			fmt.Println(d[n])
			continue
		}
		result := d[1] + p[2]
		for i:=3; i<n; i+=2{
			result = min(result, d[i]+p[i+1])
		}
		fmt.Println(result)
	}
}

func calculateSingle(x, y, z int64)int64{
	if y>x && y>z {
		return int64(0)
	}
	return max(x, z) + int64(1) -y
}

func max(x, y int64)int64{
	if x > y {
		return x
	}
	return y
}
func min(x, y int64) int64{
	if x < y {
		return x
	}
	return y
}

package main

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

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

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

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

func do(n int, b []int) int64 {
	c := make([]int, n-2)
	for i := 1; i <= n-2; i++ {
		v := max(b[i-1], b[i+1]) + 1 - b[i]
		if v > 0 {
			c[i-1] = v
		}
	}
	// log.Print(c)
	d := make([]int64, n-2)
	for i, v := range c {
		d[i] = int64(v)
		if i-2 >= 0 {
			d[i] += d[i-2]
		}
		if i%2 == 1 && i-3 >= 0 && d[i-3] < d[i-2] {
			d[i] = d[i-3] + int64(v)
		}
	}
	// log.Print(d)
	if n%2 == 0 {
		return min(d[n-3], d[n-4])
	}
	return d[n-3]
}

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

func min[T int64](a, b T) T {
	if a > b {
		return b
	}
	return a
}

package main

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

const MOD int64 = 1000000007

var n int

var h[]int

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

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

func main() {

	read := bufio.NewReader(os.Stdin)

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

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

		fmt.Fscan(read, &n)

		h = make([]int, n)

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

		fmt.Println(solve())
	}
}

type pair struct {
	v int
	idx []int
}

func solve() int64 {

	if n % 2 == 1 {

		var cnt int64

		for i := 1; i < n - 1; i+=2 {

			prev := h[i-1]
			next := h[i+1]

			cur := h[i]

			m := max(prev, next)

			if cur == m {
				cnt++

			} else if cur < m {
				cnt += (int64(m) - int64(cur)) + 1
			}
		}

		return cnt

	} else {

		var ans int64 = 1e18

		var left int64 = 0

		for i := 1; i < n - 1; i+=2 {
			left += int64(getForIndex(i))
		}

		ans = min(ans, left)

		var right int64

		for k := n - 4; k >= 0; k -=2 {
			v := getForIndex(k + 1)

			left-=int64(v)

			v2 := getForIndex(k + 2)

			right += int64(v2)

			ans = min(ans, left + right)
		}

		return ans
	}
}

func getForIndex(x int) int {
	prev := h[x-1]
	next := h[x+1]

	cur := h[x]

	m := max(prev, next)

	var ans int

	if m >= cur {
		ans = ((m) - (cur)) + 1
	}

	return ans
}

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

	return b
}

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

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

package main

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

var rr = bufio.NewReader(os.Stdin)
var ww = bufio.NewWriter(os.Stdout)

func slove() {
	var n int
	fmt.Fscan(rr, &n)
	nums := make([]int64, n)
	for i:=0;i<len(nums);i++ {
		fmt.Fscan(rr, &nums[i])
	}
	need := make([]int64, n)
	for i:=1;i<n-1;i++ {
		need[i] = max(0, max(nums[i-1], nums[i+1])-nums[i]+1)
	}
	if n % 2 == 1 {
		var ans int64
		for i:=1;i<n;i+=2 {
			ans += need[i]
		}
		fmt.Fprintln(ww, ans)
		return
	}
	l, r := make([]int64, n/2), make([]int64, n/2)
	for i:=1;i<n-1;i+=2 {
		l[(i+1)/2] = l[i/2] + need[i]
		r[(i+1)/2] = r[i/2] + need[i+1]
	}
	ans := l[0]+r[n/2-1]-r[0]
	for i:=0;i<n/2;i++ {
		ans = min(ans, l[i]+r[n/2-1]-r[i])
	}
	fmt.Fprintln(ww, ans)
}

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

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

package main

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

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

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

	cal := func(arr []int64, i int64) int64 {
		return max(0, max(arr[i-1], arr[i+1])-arr[i]+1)
	}

	var T int64
	fmt.Fscan(r, &T)
	for ; T > 0; T-- {
		var n int64
		fmt.Fscan(r, &n)
		arr := make([]int64, n)
		for i := int64(0); i < n; i++ {
			fmt.Fscan(r, &arr[i])
		}

		if n&1 == 1 {
			ans := int64(0)
			for i := int64(1); i < n; i += 2 {
				ans += cal(arr, i)
			}
			fmt.Fprintln(w, ans)
			continue
		}

		ans := int64(0)
		for i := int64(2); i < n; i += 2 {
			ans += cal(arr, i)
		}
		res := ans
		for i := int64(1); i < n-1; i += 2 {
			ans += cal(arr, i) - cal(arr, i+1)
			res = min(res, ans)
		}
		fmt.Fprintln(w, res)
	}
}

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

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

package main

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

func Min[T int | int64 | float64](x, y T) T {
	if x < y {
		return x

	}
	return y

}

func Max[T int | int64 | float64](x, y T) T {
	if x > y {
		return x

	}
	return y

}

func Less[T int | int64 | float64](x, y T) bool {
	return x < y
}

func main() {
	var t int
	read := bufio.NewReader(os.Stdin)
	fmt.Fscan(read, &t)
	for ; t > 0; t-- {
		var n int
		fmt.Fscan(read, &n)
		a := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(read, &a[i])
		}
		var s1, s2 int64
		for i := 1; i < len(a)-1; i++ {
			t := Max(Max(a[i-1], a[i+1])-a[i]+1, 0)
			if i&1 == 1 {
				s1 += t
			} else {
				s2 += t
			}
		}
		if n>=6&&n&1==0 {
			var c1, c2, s int64
			s = math.MaxInt64
			for i := 1; i < len(a)-1; i++ {
				t := Max(Max(a[i-1], a[i+1])-a[i]+1, 0)
				if i&1 == 1 {
					s = Min(s, c1+s2-c2)
					c1 += t
				} else {
					c2 += t
				}
			}
			fmt.Println(Min(s1, Min(s2, s)))
		} else if n&1 == 1 {
			fmt.Println(s1)
		} else {
			fmt.Println(Min(s1, s2))
		}
	}
}

package main

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

func min(a int64, rest ...int64) int64 {
	for _, v := range rest {
		if v < a {
			a = v
		}
	}
	return a
}

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

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

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

		aa := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i])
		}
		bb := make([]int64, n)
		for i := 1; i < n-1; i++ {
			bb[i] = max(0, max(aa[i-1], aa[i+1])+1-aa[i])
		}

		var tot int64
		if n%2 != 0 {
			for i := 1; i < n-1; i += 2 {
				tot += bb[i]
			}
		} else {
			var x int64
			for i := 1; i < n-1; i += 2 {
				x += bb[i]
			}
			tot = x
			for i := n - 3; i > 0; i -= 2 {
				x += bb[i+1] - bb[i]
				tot = min(tot, x)
			}
		}
		fmt.Fprintln(out, tot)

	}
}

package main

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

func diff(a, b, c int64) int64 {
	if b > c && b > a {
		return 0
	} else {
		if (c - b) >= (a - b) {
			return c - b + 1
		} else {
			return a - b + 1
		}
	}
}

func main() {
	scanner := bufio.NewReader(os.Stdin)
	printer := bufio.NewWriter(os.Stdout)
	var ntests int
	fmt.Fscan(scanner, &ntests)
	for itests := 0; itests < ntests; itests++ {
		var n int
		fmt.Fscan(scanner, &n)
		slice := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(scanner, &slice[i])
		}
		if n%2 == 1 {
			var ans int64 = 0
			for i := 1; i < n; i = i + 2 {
				ans += diff(slice[i-1], slice[i], slice[i+1])
			}
			fmt.Fprintln(printer, ans)
		} else {
			var ans1, ans2 int64 = 0, 0
			sol := make([]int64, n)
			// sol[i] is { o(0, n-1) if n-1 % 2 == 0 else e(1, n) }
			for i := 1; i < n-1; i = i + 2 {
				tmp := diff(slice[i-1], slice[i], slice[i+1])
				ans1 += tmp
				sol[i] = ans1
			}
			for i := 2; i < n; i = i + 2 {
				tmp := diff(slice[i-1], slice[i], slice[i+1])
				ans2 += tmp
				sol[i] = ans2
			}
			// for i := 0; i < n; i++ {
			// 	fmt.Fprintln(printer, i, sol[i])
			// }
			// ans3
			var res int64 = 0
			if ans1 >= ans2 {
				res = ans2
			} else {
				res = ans1
			}
			if n == 4 {
				fmt.Fprintln(printer, res)
			} else {
				for i := 3; i < n-1; i = i + 2 {
					if (sol[i-2] + (sol[n-2] - sol[i-1])) < res {
						res = (sol[i-2] + (sol[n-2] - sol[i-1]))
					}
				}
				fmt.Fprintln(printer, res)
			}
		}
	}
	printer.Flush()
}

package main

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

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

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

	cal := func(arr []int, i int) int {
		return max(0, max(arr[i-1], arr[i+1])-arr[i]+1)
	}

	var T int64
	fmt.Fscan(r, &T)
	for ; T > 0; T-- {
		var n int
		fmt.Fscan(r, &n)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &arr[i])
		}

		if n&1 == 1 {
			ans := 0
			for i := 1; i < n; i += 2 {
				ans += cal(arr, i)
			}
			fmt.Fprintln(w, ans)
			continue
		}

		ans := 0
		for i := 2; i < n; i += 2 {
			ans += cal(arr, i)
		}
		res := ans
		for i := 1; i < n-1; i += 2 {
			ans += cal(arr, i) - cal(arr, i+1)
			res = min(res, ans)
		}
		fmt.Fprintln(w, res)
	}
}

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

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

package main

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

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

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

	cal := func(arr []int, i int) int {
		return max(0, max(arr[i-1], arr[i+1])-arr[i]+1)
	}

	var T int64
	fmt.Fscan(r, &T)
	for ; T > 0; T-- {
		var n int
		fmt.Fscan(r, &n)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &arr[i])
		}

		if n&1 == 1 {
			ans := 0
			for i := 1; i < n; i += 2 {
				ans += cal(arr, i)
			}
			fmt.Fprintln(w, ans)
			continue
		}

		ans := 0
		for i := 2; i < n; i += 2 {
			ans += cal(arr, i)
		}
		res := ans
		for i := 1; i < n - 1; i += 2 {
			ans += cal(arr, i) - cal(arr, i+1)
			res = min(res, ans)
		}

		fmt.Fprintln(w, res)
	}
}

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

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

package main

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

func Min[T int | int64 | float64](x, y T) T {
	if x < y {
		return x

	}
	return y

}

func Max[T int | int64 | float64](x, y T) T {
	if x > y {
		return x

	}
	return y

}

func Less[T int | int64 | float64](x, y T) bool {
	return x < y
}

func main() {
	var t int
	read := bufio.NewReader(os.Stdin)
	fmt.Fscan(read, &t)
	for ; t > 0; t-- {
		var n int
		fmt.Fscan(read, &n)
		a := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(read, &a[i])
		}
		var s1, s2 int64
		for i := 1; i < len(a)-1; i++ {
			t := Max(Max(a[i-1], a[i+1])-a[i]+1, 0)
			if i&1 == 1 {
				s1 += t
			} else {
				s2 += t
			}
		}
		if n%6 == 0 {
			var c1, c2, s int64
			s = math.MaxInt64
			for i := 1; i < len(a)-1; i++ {
				t := Max(Max(a[i-1], a[i+1])-a[i]+1, 0)
				if i&1 == 1 {
					s = Min(s, c1+s2-c2)
					c1 += t
				} else {
					c2 += t
				}
			}
			fmt.Println(Min(s1, Min(s2, s)))
		} else if n&1 == 1 {
			fmt.Println(s1)
		} else {
			fmt.Println(Min(s1, s2))
		}
	}
}

package main

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

func Min[T int | int64 | float64](x, y T) T {
	if x < y {
		return x

	}
	return y

}

func Max[T int | int64 | float64](x, y T) T {
	if x > y {
		return x

	}
	return y

}

func Less[T int | int64 | float64](x, y T) bool {
	return x < y
}

func main() {
	var t int
	read := bufio.NewReader(os.Stdin)
	fmt.Fscan(read, &t)
	for ; t > 0; t-- {
		var n int
		fmt.Fscan(read, &n)
		a := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(read, &a[i])
		}
		var s1, s2 int64
		for i := 1; i < len(a)-1; i++ {
			t := Max(a[i-1], a[i+1]) - a[i] + 1
			if t <= 0 {
				continue
			}
			if i&1 == 1 {
				s1 += t
			} else {
				s2 += t
			}
		}
		if n == 6 {
			var	s int64
			t := Max(a[0], a[2]) - a[1] + 1
			if t > 0 {
				s += t
			}
			t = Max(a[3], a[5]) - a[4] + 1
			if t > 0 {
				s += t
			}
			fmt.Println(Min(s, Min(s1, s2)))
		} else if n&1 == 1 {
			fmt.Println(s1)
		} else {
			fmt.Println(Min(s1, s2))
		}
	}
}

package main

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

func Min[T int | int64 | float64](x, y T) T {
	if x < y {
		return x

	}
	return y

}

func Max[T int | int64 | float64](x, y T) T {
	if x > y {
		return x

	}
	return y

}

func Less[T int | int64 | float64](x, y T) bool {
	return x < y
}

func main() {
	var t int
	read := bufio.NewReader(os.Stdin)
	fmt.Fscan(read, &t)
	for ; t > 0; t-- {
		var n int
		fmt.Fscan(read, &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(read, &a[i])
		}
		var s1, s2 int
		for i := 1; i < len(a)-1; i++ {
			t := Max(a[i-1], a[i+1]) - a[i] + 1
			if t <= 0 {
				continue
			}
			if i&1 == 1 {
				s1 += t
			} else {
				s2 += t
			}
		}
		if n == 6 {
			s := 0
			t := Max(a[0], a[2]) - a[1] + 1
			if t > 0 {
				s += t
			}
			t = Max(a[3], a[5]) - a[4] + 1
			if t > 0 {
				s += t
			}
			fmt.Println(Min(s, Min(s1, s2)))
		} else if n&1 == 1 {
			fmt.Println(s1)
		} else {
			fmt.Println(Min(s1, s2))
		}
	}
}

package main

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

func min(a int64, rest ...int64) int64 {
	for _, v := range rest {
		if v < a {
			a = v
		}
	}
	return a
}

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

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

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

		aa := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i])
		}
		bb := make([]int64, n)
		for i := 1; i < n-1; i++ {
			bb[i] = max(0, max(aa[i-1], aa[i+1])+1-aa[i])
		}
		dp := make([]int64, n-1)
		dp[0] = 0
		dp[1] = bb[1]
		if n > 3 {
			dp[2] = bb[2]
		}
		for i := 3; i < n-1; i++ {
			dp[i] = min(dp[i-2], dp[i-3]) + bb[i]
		}
		fmt.Fprintln(out, dp[n-2])

	}
}

package main

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

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

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

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

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

		aa := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i])
		}

		var tot int64
		if n%2 == 0 {
			var a, b int64
			for i := 1; i < n-1; i += 2 {
				a += max(0, max(aa[i-1], aa[i+1])+1-aa[i])
			}
			for i := n - 2; i > 0; i -= 2 {
				b += max(0, max(aa[i-1], aa[i+1])+1-aa[i])
			}
			tot = min(a, b)
			if n > 4 {
				var c int64
				for i := 1; i < n/2; i += 2 {
					c += max(0, max(aa[i-1], aa[i+1])+1-aa[i])
				}
				for i := n - 2; i >= n/2; i -= 2 {
					c += max(0, max(aa[i-1], aa[i+1])+1-aa[i])
				}
				if n > 6 {
					g := n / 2
					c -= max(max(0, max(aa[g-2], aa[g])+1-aa[g-1]), max(0, max(aa[g-1], aa[g+1])+1-aa[g]))
				}
				tot = min(tot, c)
			}
		} else {
			for i := 1; i < n; i += 2 {
				tot += max(0, max(aa[i-1], aa[i+1])+1-aa[i])
			}
		}
		fmt.Fprintln(out, tot)
	}
}

package main

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

func diff(a, b, c int64) int64 {
	if b > c && b > a {
		return 0
	} else {
		if (c - b) >= (a - b) {
			return c - b + 1
		} else {
			return a - b + 1
		}
	}
}

func main() {
	scanner := bufio.NewReader(os.Stdin)
	printer := bufio.NewWriter(os.Stdout)
	var ntests int
	fmt.Fscan(scanner, &ntests)
	for itests := 0; itests < ntests; itests++ {
		var n int
		fmt.Fscan(scanner, &n)
		slice := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(scanner, &slice[i])
		}
		if n%2 == 1 {
			var ans int64 = 0
			for i := 1; i < n; i = i + 2 {
				ans += diff(slice[i-1], slice[i], slice[i+1])
			}
			fmt.Fprintln(printer, ans)
		} else {
			var ans1, ans2 int64 = 0, 0
			sol := make([]int64, n)
			// sol[i] is { o(0, n-1) if n-1 % 2 == 0 else e(1, n) }
			for i := 1; i < n-1; i = i + 2 {
				tmp := diff(slice[i-1], slice[i], slice[i+1])
				ans1 += tmp
				sol[i] = ans1
			}
			for i := 2; i < n; i = i + 2 {
				tmp := diff(slice[i-1], slice[i], slice[i+1])
				ans2 += tmp
				sol[i] = ans2
			}
			// for i := 0; i < n; i++ {
			// 	fmt.Fprintln(printer, i, sol[i])
			// }
			// ans3
			var res int64 = 0
			if ans1 >= ans2 {
				res = ans2
			} else {
				res = ans1
			}
			if n == 4 {
				fmt.Fprintln(printer, res)
			} else {
				for i := 3; i < n-1; i = i + 2 {
					if (sol[i-2] + (sol[i+1] - sol[i-1])) < res {
						res = (sol[i-2] + (sol[i+1] - sol[i-1]))
					}
				}
				fmt.Fprintln(printer, res)
			}
		}
	}
	printer.Flush()
}

package main

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

func diff(a, b, c int64) int64 {
	if b > c && b > a {
		return 0
	} else {
		if (c - b) >= (a - b) {
			return c - b + 1
		} else {
			return a - b + 1
		}
	}
}

func main() {
	scanner := bufio.NewReader(os.Stdin)
	printer := bufio.NewWriter(os.Stdout)
	var ntests int
	fmt.Fscan(scanner, &ntests)
	for itests := 0; itests < ntests; itests++ {
		var n int
		fmt.Fscan(scanner, &n)
		slice := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(scanner, &slice[i])
		}
		if n%2 == 1 {
			var ans int64 = 0
			for i := 1; i < n; i = i + 2 {
				ans += diff(slice[i-1], slice[i], slice[i+1])
			}
			fmt.Fprintln(printer, ans)
		} else {
			currmax := 0
			for i := 1; i < n-1; i++ {
				if slice[i] > slice[i-1] && slice[i] > slice[i+1] {
					currmax++
				}
			}
			if currmax == (n-2)/2 {
				fmt.Fprintln(printer, 0)
			} else {
				var ans1, ans2 int64 = 0, 0
				for i := 1; i < n-1; i = i + 2 {
					ans1 += diff(slice[i-1], slice[i], slice[i+1])
				}
				for i := 2; i < n; i = i + 2 {
					ans2 += diff(slice[i-1], slice[i], slice[i+1])
				}
				if ans1 >= ans2 {
					fmt.Fprintln(printer, ans2)
				} else {
					fmt.Fprintln(printer, ans1)
				}
			}
		}
	}
	printer.Flush()
}

// 00
package main

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

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

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		h := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &h[i])
		}
		ans := 0
		for i := 1; i < n-1; i += 2 {
			mx := max(h[i-1], h[i+1])
			if mx >= h[i] {
				ans += (mx - h[i] + 1)
			}
		}
		if n%2 == 1 {
			fmt.Println(ans)
		} else {
			cool := make([]int, n)
			for i := 1; i < n-1; i++ {
				mx := max(h[i-1], h[i+1])
				if mx >= h[i] {
					cool[i] = (mx - h[i] + 1)
				}
			}
			// fmt.Println(cool)
			ans1 := 0
			for i := 1; i < n-1; i += 2 {
				ans1 += cool[i]
			}
			ans := ans1
			//fmt.Println("starting is ", ans1)
			for i := n - 3; i > 0; i -= 2 {
				ans1 += cool[i+1] - cool[i]
				ans = min(ans, ans1)
				// fmt.Println("is ", i, ans1)
			}
			fmt.Println(ans)
		}

	}
}

package main

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

var cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	arr := make([]int, n)
	for i := range arr {
		fmt.Fscan(in, &arr[i])
	}
	priceLeft := make([]int, n)
	priceRight := make([]int, n)
	for i := 1; i < n-1; i++ {
		curPrice := max(max(arr[i-1], arr[i+1])-arr[i]+1, 0)
		priceLeft[i] = curPrice
		if i > 1 {
			priceLeft[i] += priceLeft[i-2]
		}
	}
	for i := n - 2; i >= 1; i-- {
		curPrice := max(max(arr[i-1], arr[i+1])-arr[i]+1, 0)
		priceRight[i] = curPrice
		if i < n-2 {
			priceRight[i] += priceRight[i+2]
		}
	}
	ans := priceLeft[n-2]
	if n%2 == 0 {
		ans = min(priceRight[1], priceRight[2])
		for i := 1; i < n-4; i += 2 {
			ans = min(ans, priceLeft[i]+priceRight[i+3])
		}
	}
	fmt.Fprintln(out, ans)
}

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

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

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

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

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

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

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

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

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

var exists = struct{}{}

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

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

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

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

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

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

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

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

package main

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

func solveOdd(n int, a []int64) int64 {
	var res int64 = 0
	for i := 0; i < n; i++ {
		if i%2 == 0 {
			continue
		}
		u := max(a[i-1], a[i+1])
		if u >= a[i] {
			res += u - a[i] + 1
		}
	}
	return res
}

func solve(n int, a []int64) int64 {
	if n%2 == 1 {
		return solveOdd(n, a)
	}
	b := make([]int64, n-2)
	for i := 1; i < n-1; i++ {
		b[i-1] = 0
		u := max(a[i-1], a[i+1])
		if u >= a[i] {
			b[i-1] = u - a[i] + 1
		}
	}

	f := make([][]int64, n-2)
	for i := 0; i < n-2; i++ {
		f[i] = make([]int64, 2)
	}
	for i := 0; i < n-2; i++ {
		if i == 0 || i == 1 {
			f[i][0] = b[i]
			f[i][1] = b[i]
			continue
		}
		f[i][0] = f[i-2][0] + b[i]
		f[i][1] = f[i-2][1] + b[i]
		if i >= 3 && f[i][1] > f[i-3][0]+b[i] {
			f[i][1] = f[i-3][0] + b[i]
		}
	}

	return min(f[n-3][0], f[n-3][1])
}

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

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

func main() {
	inp := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	var test int
	fmt.Fscanln(inp, &test)

	for test > 0 {
		test--

		var n int
		fmt.Fscanln(inp, &n)

		a := make([]int64, n)
		for i := 0; i < n; i++ {
			if i == n-1 {
				fmt.Fscanln(inp, &a[i])
			} else {
				fmt.Fscan(inp, &a[i])
			}
		}

		fmt.Fprintln(out, solve(n, a))
	}

	out.Flush()
}

package main

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

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

func c1706(_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

	cal := func(a, b, c int) int {
		if b < c {
			b, c = c, b
		}
		if a > b {
			return 0
		}
		if a == b {
			return 1
		}
		return b - a + 1
	}

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

		ans := 0
		if n%2 == 1 {
			for i := 1; i < n; i += 2 {
				ans += cal(ar[i], ar[i-1], ar[i+1])
			}
		} else {
			l, r := make([]int, n), make([]int, n)
			sl, sr := 0, 0
			for i := 1; i <= n-3; i += 2 {
				sl += cal(ar[i], ar[i-1], ar[i+1])
				l[i] = sl
			}
			for i := n - 2; i >= 2; i -= 2 {
				sr += cal(ar[i], ar[i-1], ar[i+1])
				r[i] = sr
			}
			ans = l[n-3]
			if r[2] < ans {
				ans = r[2]
			}
			for i := 1; i+3 < n; i += 2 {
				if l[i]+r[i+3] < ans {
					ans = l[i] + r[i+3]
				}
			}
		}

		Fprintln(out, ans)
	}
}


package main

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

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

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

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

type point struct {
	left int
	right int
}

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

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

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

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

func sqr(x int64) float64 {
	return float64(x) * float64(x)
}

func solveD() {
	var d, k int64
	scanf("%d %d\n", &d, &k)
	ans := "Ashish"
	z := int64(math.Sqrt(0.5 * sqr(d) / sqr(k)))
	if k * k * (z * z + (z + 1) * (z + 1)) > d * d {
		ans = "Utkarsh"
	}
	printf("%s\n", ans)
}

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

package main
import (
	"os"
	"fmt"
	"bufio"
)
var rd = bufio.NewReader(os.Stdin)
func sqr(x int64) int64 {
	return x * x
}
func ok(x, y, d, k int64) bool {
	return sqr(k * x) + sqr(k * y) <= sqr(d)
}
func main() {
	q := 0
	fmt.Fscan(rd, &q)
	for t := 0; t < q; t++ {
		var d, k, cur int64
		fmt.Fscan(rd, &d, &k)
		for ok(cur + 1, cur + 1, d, k) {
			cur++
		}
		if ok(cur + 1, cur, d, k) {
			fmt.Println("Ashish")
		} else {
			fmt.Println("Utkarsh")
		}
	}
}

package main

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

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

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

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

type point struct {
	left int
	right int
}

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

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

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

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

func sqr(x int) float64 {
	return float64(x * x)
}

func solveD() {
	var d, k int
	scanf("%d %d\n", &d, &k)
	ans := "Ashish"
	z := int(math.Sqrt(0.5 * sqr(d) / sqr(k)))
	if k * k * (z * z + (z + 1) * (z + 1)) > d * d {
		ans = "Utkarsh"
	}
	printf("%s\n", ans)
}

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

package main
import (
	"os"
	"fmt"
	"bufio"
)
var rd = bufio.NewReader(os.Stdin)
func sqr(x int) int {
	return x * x
}
func ok(x, y, d, k int) bool {
	return sqr(k * x) + sqr(k * y) <= sqr(d)
}
func main() {
	q := 0
	fmt.Fscan(rd, &q)
	for t := 0; t < q; t++ {
		d, k, cur := 0, 0, 0
		fmt.Fscan(rd, &d, &k)
		for ok(cur + 1, cur + 1, d, k) {
			cur++
		}
		if ok(cur + 1, cur, d, k) {
			fmt.Println("Ashish")
		} else {
			fmt.Println("Utkarsh")
		}
	}
}


package main

import (
	"fmt"
)

func main() {
	var epochs int
	fmt.Scanf("%d\n", &epochs)
	for ; epochs > 0; epochs-- {
		var q int
		fmt.Scanf("%d\n", &q)
		q = 0
		var input string
		fmt.Scanf("%s\n", &input)
		for _, token := range input {
			if token == 'Q' {
				if q >= 0 {
					q++
				} else {
					q = 1
				}
			} else {
				q--
			}
		}
		if q > 0 {
			fmt.Println("No")
		} else {
			fmt.Println("Yes")
		}
	}
}

package main

import (
	"fmt"
)

func work() {
	var n int
	fmt.Scanln(&n)
	cnt := 0
	var s string
	fmt.Scanln(&s)
	for _, c := range s {
		if c == 'Q' {
			cnt++
		} else if c == 'A' {
			cnt--
			if cnt < 0 {
				cnt = 0
			}
		}
	}
	if cnt != 0 {
		fmt.Println("No")
	} else {
		fmt.Println("Yes")
	}

}

func main() {
	var T int
	fmt.Scanln(&T)
	for i := 0; i < T; i++ {
		work()
	}
}

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n,sum int
		fmt.Fscan(in,&n)
		var s string
		fmt.Fscan(in,&s)
		for j:=n-1;j>=0;j--{
			if s[j] == 65 {
				sum++
			} else {
				sum--
			}
			if sum < 0 {
				fmt.Fprintln(out,"No")
				goto out
			}
		}
		fmt.Fprintln(out,"Yes")
		out:
	}
}

package main

import "fmt"

func main() {
    var cases, num = 0, 0
    fmt.Scanf("%d\n", &cases)
    for i := 0; i < cases; i++ {
        str := ""
        fmt.Scanf("%d\n", &num)
        fmt.Scanf("%s\n", &str)
        res := 0
        j := 0
        for j = 0; j < num; j++ {
            if str[j] == 'Q' {
                res++
            } else if str[j] == 'A' {
                res--
            }
            if res < 0 {
                res = 0
            }
        }
        if res > 0 || j < num {
            fmt.Println("No")
        } else {
            fmt.Println("Yes")
        }
    }
}

package main

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

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

func (in *CF1754A) 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 *CF1754A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1754A
Date: 10/24/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1754/A
Problem: CF1754A
**/
func (in *CF1754A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		str := in.NextString()
		q := 0

		for i := 0; i < n; i++ {
			if str[i] == 'Q' {
				q++
			} else {
				q--
				if q < 0 {
					q = 0
				}
			}
		}
		if q > 0 {
			fmt.Println("NO")
		} else {
			fmt.Println("YES")
		}
	}
}

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

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

package main

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

func main() {
	r, w := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var t, n int
	var s string
	Fscan(r, &t)

	for t > 0 {
		t--
		Fscan(r, &n, &s)
		k := 0
		for i := range s {
			if s[i] == 'Q' {
				if k < 0 {
					k = 0
				}
				k++
			} else {
				k--
			}
		}
		if k > 0 {
			Fprintln(w, "NO")
		} else {
			Fprintln(w, "YES")

		}
	}
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	testcasesStr := scanner.Text()

	testcases, _ := strconv.ParseInt(testcasesStr, 0, 64)

	var i int64

	for i = 0; i < testcases; i++ {

		scanner.Scan()
		messageLengthStr  := scanner.Text()

		scanner.Scan()
		messageOrder  := scanner.Text()

		messageLength, _ := strconv.ParseInt(messageLengthStr, 0, 64)

		validOrder := IsValidOrder(messageLength, messageOrder)

		fmt.Println(validOrder)
	}
}

func IsValidOrder(messageLength int64, messageOrder string) string {
	messageQueues := []string{}

	var i int64

	for i = 0; i < messageLength; i++ {
		character := string(messageOrder[i])

		if character == "Q" {
			messageQueues = append(messageQueues, character)
		} else if character == "A" {
			if len(messageQueues) > 0 {
				messageQueues = append(messageQueues[:0], messageQueues[1:]...)
			}
		}
	}

	if len(messageQueues) == 0 {
		return "YES"
	}
	return "NO"
}

package main

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

var in = bufio.NewReader(os.Stdin)
var tc = 1
var DEBUG = os.Getenv("DEBUG") != ""

func solve() {
	var n int
	var s string
	scan(&n, &s)
	var a, q = 0, 0
	for _, e := range []rune(s) {
		if e == 'Q' {
			q++
		} else {
			a++
			q -= min(a, q)
			a = 0
		}
	}
	printf("%s\n", yn(q <= 0))
}

func main() {
	var T int
	scan(&T)
	for ; T > 0; T-- {
		solve()
		tc++
	}
}

func yn(b bool) string      { return map[bool]string{true: "Yes", false: "No"}[b] }
func scan(a ...interface{}) { fmt.Fscan(in, a...) }
func printf(format string, a ...interface{}) {
	if DEBUG {
		format = fmt.Sprintf("Test #%d: %s", tc, format)
	}
	fmt.Printf(format, a...)
}

func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

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

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

package main

import (
	"fmt"
)

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

	for testCases > 0 {
		testCases--
		var message string
		var length string
		fmt.Scan(&length)
		fmt.Scan(&message)
		count := 0
		possible := true
		message = reverse(message)
		for _, value := range message {
			if value == 81 {
				count--
			} else {
				count++
			}
			// fmt.Println(value)
			if count < 0 {
				possible = false
			}
		}
		if possible {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}
func reverse(message string) string {
	var result string
	for _, v := range message {
		result = string(v) + result
	}
	return result
}

package main

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

var reader = bufio.NewReader(os.Stdin)

const N = 52

var n, m int
var (
	a, g, q [N]int
)

func main() {
	var t int

	fmt.Fscan(reader, &t)
testcase:
	for t > 0 {
		t--
		fmt.Fscan(reader, &n)
		var s string
		fmt.Fscan(reader, &s)
		qcnt := 0
		for i := 0; i < len(s); {
			if s[i] == 'Q' {
				qcnt++
				i++
			} else {
				j := i
				if qcnt > 0 {
					for ; j < len(s) && s[j] == 'A'; j++ {
						if qcnt > 0 {
							qcnt--
						}
					}
				} else {
					fmt.Println("No")
					continue testcase
				}
				i = j
			}
		}
		if qcnt == 0 {
			fmt.Println("Yes")
		} else {
			fmt.Println("No")
		}
	}
}

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

package main

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

func solve(){
	var n int
	var s string
	Fscan(Stdin, &n, &s)
	tot := 0
	for i := 0; i < n; i++ {
		if s[i] == 'Q' {
			tot++
		} else {
			if tot > 0 {
				tot--
			}
		}
	}
	if tot > 0 {
		Fprintln(Stdout, "NO")
	} else {
		Fprintln(Stdout, "YES")
	}
}

func main(){
	var t int
	Fscan(Stdin, &t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int
	l      string
)

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

func solve(n int, l string) string {
	var temp = 0
	for _, x := range l {
		if x == 'Q' {
			temp++
		}
		if x == 'A' {
			temp = max(temp-1, 0)
		}
	}
	if temp > 0 {
		return "No"
	}
	return "Yes"
}

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

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanln(&n)
	for i := 0; i < n; i++ {
		var qn int
		var st string
		fmt.Scanln(&qn)
		fmt.Scanln(&st)
		s := 0
		for j := 0; j < qn; j++ {
			if st[j] == 'Q' {
				s = s + 1
			} else if s > 0 {
				s = s - 1
			}
		}
		if s > 0 {
			fmt.Println("No")
		} else {
			fmt.Println("Yes")
		}

	}
}

package main

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

const MaxMessages = 101

func main() {
	defer writer.Flush()
	var line string
	var n, t int

	scanws(&t)
	for t > 0 {
		scanws(&n)
		scanws(&line)
		printf("%s\n", solve(line, n))
		t--
	}
}

func solve(msgs string, sz int) string {
	if sz <= 1 {
		return "No"
	}
	var index int
	var s []rune
	runes := []rune(msgs)
	s = make([]rune, 0, MaxMessages)
	index = 0
	for _, event := range runes {
		switch {
		case event == 'Q':
			s = append(s, event)
			index++
		case len(s) > 0:
			s = s[:index-1]
			index--
		}
	}

	if len(s) > 0 {
		return "No"
	} else {
		return "Yes"
	}
}

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

package main

import (
	"fmt"
)

func solve() {
	var idx = 0
	var str string
	fmt.Scan(&idx, &str)
	ans := 0
	for _, item := range str {
		if item == 'Q' {
			ans++
		} else {
			ans--
		}
		if ans < 0 {
			ans = 0
		}
	}
	if ans > 0 {
		fmt.Println("No")
	} else {
		fmt.Println("Yes")
	}
}

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		solve()
	}
}

package main

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

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

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {

		var n int
		fmt.Fscan(r, &n)
		b := make([]byte, n)
		fmt.Fscan(r, &b)

		cnt := 0
		for _, v := range b {
			if v == 'Q' {
				cnt++
			} else if cnt > 0 {
				cnt--
			}
		}
		if cnt == 0 {
			fmt.Fprintln(w, "YES")
		} else {
			fmt.Fprintln(w, "NO")
		}
	}

}

package main

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

func main() {
	r, w := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var t, n int
	var s string
	Fscan(r, &t)

	for t > 0{
		t--
		Fscan(r, &n, &s)
		k := 0
		for i := range s{
			if s[i] == 'Q'{
				if k < 0{
					k = 0
				}
				k++
			}else{
				k--
			}
		}
		if k > 0{
			Fprintln(w, "NO")
		}else{
			Fprintln(w, "YES")

		}
	}
}

package main

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

const cf = true

func solve() {
	var n int
	var str string
	fmt.Fscan(in, &n, &str)
	a := 0
	for i := n - 1; i >= 0; i-- {
		if str[i] == 'Q' {
			a--
		} else {
			a++
		}
		if a < 0 {
			fmt.Fprintln(out, "No")
			return
		}
	}
	fmt.Fprintln(out, "Yes")
}

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

func LCM(a, b int) int {
	return a * b / GCD(a, b)
}

func GCD(a, b int) int {
	for a != b {
		if a > b {
			a -= b
		} else {
			b -= a
		}
	}

	return a
}

type SqrtInt interface {
	SignedSqrt | UnsignedSqrt
}

type SignedSqrt interface {
	~int | ~int32 | ~int64
}

type UnsignedSqrt interface {
	~uint | ~uint32 | ~uint64 | ~uintptr
}

func sqrt[T SqrtInt](x T) T {
	l, r := T(0), T(2000000123)
	for l < r {
		mid := (l + r) / 2
		if mid*mid > x {
			r = mid
		} else {
			l = mid + 1
		}
	}
	return l - 1
}

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

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

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

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

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

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

func (s *set[T]) sortedItems() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	sort.Slice(items, func(i, j int) bool {
		return items[i] < items[j]
	})
	return items
}

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

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

var exists = struct{}{}

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

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

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

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

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

type Ordered interface {
	Integer | Float | ~string
}

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

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

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

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

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

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

package main

import "fmt"

func main() {
	var t, n int
	var log []byte

	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&n)
		fmt.Scan(&log)
		var count int

		for j := 0; j < n; j++ {
			if log[j] == 'Q' {
				count++
			} else {
				if count > 0 {
					count--
				}
			}
		}
		if count == 0 {
			fmt.Println("Yes")
		} else {
			fmt.Println("No")
		}
	}

}

package main
import "fmt"

func main () {
    var t int
    fmt.Scan(&t)
    for j := 0; j < t; j++ {
        var n int64
        var s string
        fmt.Scan(&n)
        fmt.Scan(&s)
        c := 0
        f := true
        for i:= len(s)-1;i >= 0; i-- {
            if s[i:i+1]=="A" {
                c++
            } else {
                if c <= 0 {
                    f = false
                    break
                } else {
                    c--
                }
            }
        }
        if f {
            fmt.Println("Yes")
        } else {
            fmt.Println("No")
        }
    }
}

package main

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

var (
	t, n int
	str  string
	q, a int
)

func main() {
	defer flush()
	scan(&t)
	for ; t != 0; t-- {
		scan(&n, &str)
		q, a = 0, 0
		for i := 0; i < n; i++ {
			if str[i] == 'Q' {
				q++
			} else {
				if q != 0 {
					q--
				}
			}
		}

		if q != 0 {
			println("No")
		} else {
			println("Yes")
		}
	}
}

var (
	ioR *bufio.Reader
	ioW *bufio.Writer
)

func init() {
	ioR = bufio.NewReader(os.Stdin)
	ioW = bufio.NewWriter(os.Stdout)
}

func scan(a ...any) {
	fmt.Fscan(ioR, a...)
}

func println(a ...any) {
	fmt.Fprintln(ioW, a...)
}

func flush() {
	ioW.Flush()
}

package main

import "fmt"

func main() {
	var t, n int
	var log []byte

	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&n)
		fmt.Scan(&log)
		var count int

		for j := 0; j < n-1; j++ {
			if log[j] == 'Q' {
				count++
			} else {
				if count > 0 {
					count--
				}
			}
		}
		if count == 0 {
			fmt.Println("Yes")
		} else {
			fmt.Println("No")
		}
	}

}

package main

import "fmt"

func main() {
	var t, n int
	var log []byte

	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&n)
		fmt.Scan(&log)
		var countQ, countA int = 0, 0
		var cond bool

		for j := 0; j < n-1; j++ {

			if log[j] == 'Q' {
				countQ++
				if log[j] == 'Q' && log[j+1] == 'A' {
					for i := 0; i < n-j; i++ {
						if log[i] == 'Q' {
							continue
						}
						if log[i] == 'A' {
							countA++
						}
					}
					if countA >= countQ {
						countQ = 0
						countA = 0
						continue
					} else {
						countQ = 0
						countA = 0
						cond = true
						break
					}
				}
			}
		}

		if !cond {
			fmt.Println("Yes")
		} else {
			fmt.Println("No")
		}
		//QAAAAAQQ
	}

}

package main

import "fmt"

func main() {
	var t, n int
	var log []byte

	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&n)
		fmt.Scan(&log)
		var countQ, countA int = 0, 0
		var cond bool
		for j := 0; j < n; j++ {
			if log[j] == 'A' {
				countA++
			}
			if log[j] == 'Q' {
				countQ++
			}
			if log[j] == 'Q' && j == n-1 {
				cond = true
				break
			}
			//QQQAQAAAAAAAAQQQA
			//QQAQQQQQQQQQQQQQQQQQQQQQQQQQQA
			//3 1
			//1

		}
		var locQ, locA int = 0, 0
		if !cond {
			for i := n - 1; i > 0; i-- {
				if log[i] == 'A' {
					locA++
				}
				if log[i] == 'Q' && log[i-1] == 'Q' {
					locQ++
				}
				if log[i] == 'Q' && log[i-1] == 'A' {
					locQ++
					if locA < locQ {
						cond = true
					}
					break
				}

			}
		}

		if countA >= countQ && !cond {
			fmt.Println("Yes")
		} else {
			fmt.Println("No")
		}
		//QAAAAAQQ
	}

}

package main

import "fmt"

func main() {
	var t, n int
	var log []byte

	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&n)
		fmt.Scan(&log)
		var countQ, countA int = 0, 0
		var cond bool
		for j := 0; j < n; j++ {
			if log[j] == 'A' {
				countA++
			}
			if log[j] == 'Q' {
				countQ++
			}
			if log[j] == 'Q' && j == n-1 {
				cond = true
				break
			}
			//QQQAQAAAAAAAAQQQA
			//QQAQQQQQQQQQQQQQQQQQQQQQQQQQQA
			//3 1
			//1

		}
		var locQ, locA int = 0, 0
		if !cond {
			for i := n - 1; i > 0; i-- {
				if log[i] == 'A' {
					locA++
				}
				if log[i] == 'Q' && log[i-1] == 'Q' {
					locQ++
				}
				if log[i] == 'Q' && log[i-1] == 'A' {
					locQ++
					if locA < locQ {
						cond = true
					}
					break
				}

			}
		}

		if countA > countQ && !cond {
			fmt.Println("Yes")
		} else {
			fmt.Println("No")
		}
		//QAAAAAQQ
	}

}

package main

import "fmt"

func main() {
	var t, n int
	var log []byte

	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&n)
		fmt.Scan(&log)
		var countQ, countA int = 0, 0
		var cond bool
		for j := 0; j < n; j++ {
			if log[j] == 'A' {
				countA++
			}
			if log[j] == 'Q' {
				countQ++
			}
			if log[j] == 'Q' && j == n-1 {
				cond = true
				break
			}
			//QQQAQAAAAAAAAQQQA
			//3 1
			//1

		}
		var locQ, locA int
		for i := n - 1; i > 0; i-- {
			if log[i] == 'A' {
				locA++
			}
			if log[i] == 'Q' && log[i-1] == 'Q' {
				locQ++
			}
			if log[i] == 'Q' && log[i-1] == 'A' {
				locQ++
				if locA < locQ {
					cond = true
				}
				break
			}

		}

		if countA < countQ || cond {
			fmt.Println("No")
		} else if countA > countQ && !cond {
			fmt.Println("Yes")
		}
		//QAAAAAQQ
	}

}

package main

import "fmt"

func main() {
	var t, n int
	var log []byte

	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&n)
		fmt.Scan(&log)
		var countQ, countA int = 0, 0
		var cond bool
		for j := 0; j < n; j++ {
			if log[j] == 'A' {
				countA++
			}
			if log[j] == 'Q' {
				countQ++
			}
			if log[j] == 'Q' && j == n-1 {
				cond = true
				break
			}
			//QQQAQAAAAAAAAQQQA
			//3 1
			//1

		}
		var locQ, locA int
		for i := n - 1; i > 0; i-- {
			if log[i] == 'A' {
				locA++
			}
			if log[i] == 'Q' && log[i-1] == 'Q' {
				locQ++
			}
			if log[i] == 'Q' && log[i-1] == 'A' {
				locQ++
				if locA < locQ {
					cond = true
				}
				break
			}

		}

		if countA < countQ || cond {
			fmt.Println("No")
		} else {
			fmt.Println("Yes")
		}
		//QAAAAAQQ
	}

}

package main

import "fmt"

func main() {
	var t, n int
	var log []byte

	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&n)
		fmt.Scan(&log)
		var countQ, countA int = 0, 0
		var cond bool
		for j := 0; j < n; j++ {
			if log[j] == 'A' {
				countA++
			} else if log[j] == 'Q' {
				countQ++
			}
			if log[j] == 'Q' && j == n-1 {
				cond = true
				break
			}
			//QQQAQQQA

		}
		if countA < countQ || cond {
			fmt.Println("No")
		} else {
			fmt.Println("Yes")
		}
		//QAAAAAQQ
	}

}

package main

import "fmt"

func main() {
	var t, n int
	var log []byte

	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&n)
		fmt.Scan(&log)
		var countQ, countA int = 0, 0
		for j := 0; j < n; j++ {
			if log[j] == 'A' {
				countA++
			} else {
				countQ++
			}
		}
		if countA < countQ {
			fmt.Println("No")
		} else {
			fmt.Println("Yes")
		}
	}

}

package main

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

var (
	t, n int
	str  string
	q, a int
)

func main() {
	defer flush()
	scan(&t)
	for ; t != 0; t-- {
		scan(&n, &str)
		flag := false
		q, a = 0, 0
		for i := 0; i < n; i++ {
			if str[i] == 'Q' {
				flag = true
				q++
			} else {
				flag = false
				a++
			}
		}
		if str[0] == 'A' {
			flag = false
		}
		if flag || q > a {
			println("No")
		} else {
			println("Yes")
		}
	}
}

var (
	ioR *bufio.Reader
	ioW *bufio.Writer
)

func init() {
	ioR = bufio.NewReader(os.Stdin)
	ioW = bufio.NewWriter(os.Stdout)
}

func scan(a ...any) {
	fmt.Fscan(ioR, a...)
}

func println(a ...any) {
	fmt.Fprintln(ioW, a...)
}

func flush() {
	ioW.Flush()
}

package main

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

var (
	t, n int
	str  string
	q, a int
)

func main() {
	defer flush()
	scan(&t)
	for ; t != 0; t-- {
		scan(&n, &str)
		flag := false
		for i := 0; i < n; i++ {
			if str[i] == 'Q' {
				flag = true
				q++
			} else {
				flag = false
				a++
			}
		}
		if str[0] == 'A' {
			flag = false
		}
		if flag || q > a {
			println("No")
		} else {
			println("Yes")
		}
	}
}

var (
	ioR *bufio.Reader
	ioW *bufio.Writer
)

func init() {
	ioR = bufio.NewReader(os.Stdin)
	ioW = bufio.NewWriter(os.Stdout)
}

func scan(a ...any) {
	fmt.Fscan(ioR, a...)
}

func println(a ...any) {
	fmt.Fprintln(ioW, a...)
}

func flush() {
	ioW.Flush()
}

package main

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

var (
	t, n int
	str  string
	q, a int
)

func main() {
	defer flush()
	scan(&t)
	for ; t != 0; t-- {
		scan(&n, &str)
		flag := false
		for i := 0; i < n; i++ {
			if str[i] == 'Q' {
				flag = true
			} else {
				flag = false
			}
		}
		if str[0] == 'A' {
			flag = false
		}
		if flag {
			println("No")
		} else {
			println("Yes")
		}
	}
}

var (
	ioR *bufio.Reader
	ioW *bufio.Writer
)

func init() {
	ioR = bufio.NewReader(os.Stdin)
	ioW = bufio.NewWriter(os.Stdout)
}

func scan(a ...any) {
	fmt.Fscan(ioR, a...)
}

func println(a ...any) {
	fmt.Fprintln(ioW, a...)
}

func flush() {
	ioW.Flush()
}

package main

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

var (
	t, n int
	str  string
	q, a int
)

func main() {
	defer flush()
	scan(&t)
	for ; t != 0; t-- {
		scan(&n, &str)
		flag := false
		for i := 0; i < n; i++ {
			if str[i] == 'Q' {
				flag = true
			} else {
				flag = false
			}
		}
		if str[0] == 'A' {
			flag = false
		}
		if flag {
			println("NO")
		} else {
			println("YES")
		}
	}
}

var (
	ioR *bufio.Reader
	ioW *bufio.Writer
)

func init() {
	ioR = bufio.NewReader(os.Stdin)
	ioW = bufio.NewWriter(os.Stdout)
}

func scan(a ...any) {
	fmt.Fscan(ioR, a...)
}

func println(a ...any) {
	fmt.Fprintln(ioW, a...)
}

func flush() {
	ioW.Flush()
}

package main

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

var (
	t, n int
	str  string
	q, a int
)

func main() {
	defer flush()
	scan(&t)
	for ; t != 0; t-- {
		scan(&n, &str)
		q, a = 0, 0
		flag := false
		for i := 0; i < n; i++ {
			if str[i] == 'Q' {
				q++
				flag = true
			} else {
				flag = false
				a++
			}
		}
		if flag {
			println("NO")
		} else {
			println("YES")
		}
	}
}

var (
	ioR *bufio.Reader
	ioW *bufio.Writer
)

func init() {
	ioR = bufio.NewReader(os.Stdin)
	ioW = bufio.NewWriter(os.Stdout)
}

func scan(a ...any) {
	fmt.Fscan(ioR, a...)
}

func println(a ...any) {
	fmt.Fprintln(ioW, a...)
}

func flush() {
	ioW.Flush()
}

package main

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

var (
	t, n int
	str  string
	q, a int
)

func main() {
	defer flush()
	scan(&t)
	for ; t != 0; t-- {
		scan(&n, &str)
		q, a = 0, 0
		for i := 0; i < n; i++ {
			if str[i] == 'Q' {
				q++
			} else {
				a++
			}
		}
		if q > a {
			println("NO")
		} else {
			println("YES")
		}
	}
}

var (
	ioR *bufio.Reader
	ioW *bufio.Writer
)

func init() {
	ioR = bufio.NewReader(os.Stdin)
	ioW = bufio.NewWriter(os.Stdout)
}

func scan(a ...any) {
	fmt.Fscan(ioR, a...)
}

func println(a ...any) {
	fmt.Fprintln(ioW, a...)
}

func flush() {
	ioW.Flush()
}

package main

import (
	"fmt"
)

func work() {
	var n int
	fmt.Scanln(&n)
	cntQ := 0
	cntA := 0
	var s string
	fmt.Scanln(&s)
	for _, c := range s {
		if c == 'Q' {
			cntQ++
		} else if c == 'A' {
			cntA++
		}
		if cntQ > cntA {
			fmt.Println("No")
			return
		}
	}
	// fmt.Println(s)
	// fmt.Println(cntQ, cntA)
	fmt.Println("Yes")
}

func main() {
	var T int
	fmt.Scanln(&T)
	for i := 0; i < T; i++ {
		work()
	}
}

package main

import (
	"fmt"
)

func work() {
	var n int
	fmt.Scanln(&n)
	cntQ := 0
	cntA := 0
	var s string
	fmt.Scanln(&s)
	for _, c := range s {
		if c == 'Q' {
			cntQ++
		} else if c == 'A' {
			cntA++
		}
	}
	// fmt.Println(s)
	// fmt.Println(cntQ, cntA)
	if cntQ > cntA {
		fmt.Println("No")
	} else {
		fmt.Println("Yes")
	}
}

func main() {
	var T int
	fmt.Scanln(&T)
	for i := 0; i < T; i++ {
		work()
	}
}

package main

import (
	"fmt"
)

func work() {
	var n int
	fmt.Scanf("%d", &n)
	cntQ := 0
	cntA := 0
	var s string
	fmt.Scanln(&s)
	for _, c := range s {
		if c == 'Q' {
			cntQ++
		} else if c == 'A' {
			cntA++
		}
	}
	fmt.Println(s)
	fmt.Println(cntQ, cntA)
	if cntQ > cntA {
		fmt.Println("No")
	} else {
		fmt.Println("Yes")
	}
}

func main() {
	var T int
	fmt.Scanf("%d", &T)
	for i := 0; i < T; i++ {
		work()
	}
}

package main

import (
	"fmt"
)

func work() {
	var n int
	fmt.Scanf("%d", &n)
	cntQ := 0
	cntA := 0
	var s string
	fmt.Scanln(&s)
	for _, c := range s {
		if c == 'Q' {
			cntQ++
		} else if c == 'A' {
			cntA++
		}
	}
	if cntQ > cntA {
		fmt.Println("No")
	} else {
		fmt.Println("Yes")
	}
}

func main() {
	var T int
	fmt.Scanf("%d", &T)
	for i := 0; i < T; i++ {
		work()
	}
}

package main

import (
	"fmt"
)

func work() {
	var n int
	fmt.Scanf("%d", &n)
	cntQ := 0
	cntA := 0
	var s string
	fmt.Scanf("%s", &s)
	for _, c := range s {
		if c == 'Q' {
			cntQ++
		} else if c == 'A' {
			cntA++
		}
	}
	if cntQ > cntA {
		fmt.Println("No")
	} else {
		fmt.Println("Yes")
	}
}

func main() {
	var T int
	fmt.Scanf("%d", &T)
	for i := 0; i < T; i++ {
		work()
	}
}

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Fscan(in,&n)
		var s string
		fmt.Fscan(in,&s)
		a:= make([]int,n)
		for j:=0;j<n;j++{
			if s[j] == 81 {
				a[j] = 1
			} else {
				a[j] = 0
			}
		}
		for j:=0;j<n-1;j++{
			for k:=j+1;k<n;k++{
				if a[k] == 0 {
					a[k] = 2
				}
			}
		}
		var q,solved int
		for j:=0;j<n;j++{
			if a[j] == 1 {
				q++
			} else if a[j] == 2 {
				solved++
			}
		}
		if q > solved || a[n-1] == 1 {
			fmt.Fprintln(out,"No")
		} else {
			fmt.Fprintln(out,"Yes")
		}
	}
}

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n,tally int
		fmt.Fscan(in,&n)
		var s string
		fmt.Fscan(in,&s)
		for j:=0;j<n;j++{
			if s[j] == 65 {
				tally++
			}
		}
		var r bool = false
		if n & 1 == 1 {
			if tally > n/2 {
				r = true
			}
		} else {
			if tally >= n/2 {
				r = true
			}
		}
		if r == true && s[n-1] != 81 {
			fmt.Fprintln(out,"Yes")
		} else {
			fmt.Fprintln(out,"No")
		}
	}
}

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n,tally int
		fmt.Fscan(in,&n)
		var s string
		fmt.Fscan(in,&s)
		for j:=0;j<n;j++{
			if s[j] == 65 {
				tally++
			}
		}
		var r bool = false
		if n & 1 == 1 {
			if tally > n/2 {
				r = true
			}
		} else {
			if tally >= n/2 {
				r = true
			}
		}
		if r == true {
			fmt.Fprintln(out,"Yes")
		} else {
			fmt.Fprintln(out,"No")
		}
	}
}

package main

import "fmt"

func main() {
    var cases, num = 0, 0
    fmt.Scanf("%d\n", &cases)
    for i := 0; i < cases; i++ {
        str := ""
        fmt.Scanf("%d\n", &num)
        fmt.Scanf("%s\n", &str)
        res := 0
        j := 0
        for j = 0; j < num; j++ {
            if str[j] == 'Q' {
                res++
            } else if str[j] == 'A' {
                res--
            }
            if res < 0 {
                break
            }
        }
        if res > 0 || j < num {
            fmt.Println("No")
        } else {
            fmt.Println("Yes")
        }
    }
}

package main

import "fmt"

func main() {
    var cases, num = 0, 0
    fmt.Scanf("%d\n", &cases)
    for i := 0; i < cases; i++ {
        str := ""
        fmt.Scanf("%d\n", &num)
        fmt.Scanf("%s\n", &str)
        res := 0
        for j := 0; j < num; j++ {
            if str[j] == 'Q' {
                res++
            } else if str[j] == 'A' {
                res--
            }
            if res < 0 {
                break
            }
        }
        if res > 0 {
            fmt.Println("No")
        } else {
            fmt.Println("Yes")
        }
    }
}

package main

import "fmt"

func main() {
    var cases, num = 0, 0
    fmt.Scanf("%d", &cases)
    for i := 0; i < cases; i++ {
        str := ""
        fmt.Scanf("%d", &num)
        fmt.Scanf("%s", &str)
        res := 0
        for j := 0; j < num; j++ {
            if str[j] == 'Q' {
                res++
            } else if str[j] == 'A' {
                res--
            }
            if res < 0 {
                break
            }
        }
        if res > 0 {
            fmt.Println("No")
        } else {
            fmt.Println("Yes")
        }
    }
}

package main

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

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

func (in *CF1754A) 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 *CF1754A) load() {
  if len(in.split) <= in.index {
    in.split = strings.Split(in.GetLine(), in.separator)
    in.index = 0
  }
}

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

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

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

/**
Run solve the problem CF1754A
Date: 10/24/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1754/A
Problem: CF1754A
**/
func (in *CF1754A) Run() {
  for t := in.NextInt(); t > 0; t-- {
    n := in.NextInt()
    str := in.NextString()
    q := 0

    for i := 0; i < n; i++ {
      if str[i] == 'Q' {
        q++
      } else {
        q = 0
      }
    }
    if q > 0 {
      fmt.Println("NO")
    } else {
      fmt.Println("YES")
    }
  }
}

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

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

package main

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

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

func (in *CF1754A) 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 *CF1754A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1754A
Date: 10/24/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1754/A
Problem: CF1754A
**/
func (in *CF1754A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		str := in.NextString()
		q := 0
		for i := 0; i < n; i++ {
			if str[i] == 'Q' {
				q++
			}
		}
		if q > n-q || str[n-1] == 'Q' {
			fmt.Println("NO")
		} else {
			fmt.Println("YES")
		}
	}
}

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

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

package main

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

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

func (in *CF1754A) 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 *CF1754A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1754A
Date: 10/24/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1754/A
Problem: CF1754A
**/
func (in *CF1754A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		str := in.NextString()
		q := 0
		for i := 0; i < n; i++ {
			if str[i] == 'Q' {
				q++
			}
		}
		if q > n-q {
			fmt.Println("NO")
		} else {
			fmt.Println("YES")
		}
	}
}

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

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

package main

import "fmt"

func main() {
	var testCase int
	var inputChar int

	fmt.Scan(&testCase)
	for i := 0; i < testCase; i++ {
		fmt.Scan(&inputChar)
		counter := 0
		board := make([]string, inputChar)

		for j := 0; j < inputChar; j++ {
			fmt.Scan(&board[j])
			if board[0] == "A" {
				counter = -1
				break
			}

			if board[j] == "Q" {
				counter--

			} else if board[j] == "A" {
				counter++
				if counter > 0 {
					counter = 0
				}
			}

		}

		if counter >= 0 {
			fmt.Print("Yes\n")

		} else {
			fmt.Print("No\n")
		}
	}

}

package main

import "fmt"

func main() {
	var testCase int
	var inputChar int

	fmt.Scan(&testCase)
	for i := 0; i < testCase; i++ {
		fmt.Scan(&inputChar)
		counter := 0
		board := make([]string, inputChar)

		for j := 0; j < inputChar; j++ {
			fmt.Scan(&board[j])
			if board[0] == "A" {
				counter = -1
				break
			}

			if board[j] == "Q" {
				counter++

			} else if board[j] == "A" {
				counter--

			}

		}

		if counter < 0 {
			fmt.Print("No\n")

		} else {
			fmt.Print("Yes\n")
		}
	}

}

package main

import (
	"fmt"
)

func main() {
	var epochs int
	fmt.Scanf("%d\n", &epochs)
	for ; epochs > 0; epochs-- {
		var num, q, a int
		fmt.Scanf("%d\n", &num)
		var qastring string
		fmt.Scanf("%s\n", &qastring)
		for _, qa := range qastring {
			if qa == 'Q' {
				q++
			} else {
				a++
			}
		}
		if a >= q {
			fmt.Println("Yes")
		} else {
			fmt.Println("No")
		}
	}
}

package main

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

var in = bufio.NewReader(os.Stdin)
var tc = 1
var DEBUG = os.Getenv("DEBUG") != ""

func solve() {
	var n int
	var s string
	scan(&n, &s)
	c := 0
	for _, e := range []rune(s) {
		if e == 'Q' {
			c++
		} else {
			c--
		}
	}
	printf("%s\n", yn(c <= 0))
}

func main() {
	var T int
	scan(&T)
	for ; T > 0; T-- {
		solve()
		tc++
	}
}

func yn(b bool) string      { return map[bool]string{true: "Yes", false: "No"}[b] }
func scan(a ...interface{}) { fmt.Fscan(in, a...) }
func printf(format string, a ...interface{}) {
	if DEBUG {
		format = fmt.Sprintf("Test #%d: %s", tc, format)
	}
	fmt.Printf(format, a...)
}

func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

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

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

package main

import (
	"fmt"
)

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

	for testCases > 0 {
		testCases--
		var message string
		fmt.Scan(&message)
		count := 0
		possible := true
		message = reverse(message)
		for _, value := range message {
			if value == 81 {
				count--
			} else {
				count++
			}
			// fmt.Println(value)
			if count < 0 {
				possible = false
			}
		}
		if possible {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}
func reverse(message string) string {
	var result string
	for _, v := range message {
		result = string(v) + result
	}
	return result
}

package main

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

var reader = bufio.NewReader(os.Stdin)

const N = 52

var n, m int
var (
	a, g, q [N]int
)

func main() {
	var t int

	fmt.Fscan(reader, &t)
testcase:
	for t > 0 {
		t--
		fmt.Fscan(reader, &n)
		var s string
		fmt.Fscan(reader, &s)
		qcnt := 0
		scnt := 0
		for i := 0; i < len(s);  {
			if qcnt > scnt {
				if s[i] == 'Q' {
					qcnt ++
					i ++
				} else {
					j := i
					for ; j < len(s) && s[j] == 'A'; j ++ {

					}
					i = j
					scnt ++
				}
			} else {
				fmt.Println("No")
				continue testcase
			}
		}
		if qcnt == scnt {
			fmt.Println("Yes")
		} else {
			fmt.Println("No")
		}
	}
}

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


package main

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

func main() {
	buf, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		panic(err)
	}
	var a, b [2]int64
	var ans [2]int64
	for i := range buf {
		switch buf[i] {
		case 'a':
			a[i%2]++
			ans[0] += a[(i+1)%2]
			ans[1] += a[i%2]
		case 'b':
			b[i%2]++
			ans[0] += b[(i+1)%2]
			ans[1] += b[i%2]
		}
	}
	fmt.Println(ans[0], ans[1])
}

package main

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

const MAX_N = 100010

var dp [2][2]int64

func main() {
	fsc := NewFastScanner()
	S := fsc.Next()
	N := len(S)
	var res1, res2 int64

	for i := 0; i < N; i++ {
		if S[i] == 'a' {
			dp[i%2][0]++
			//even
			res1 += dp[(i+1)%2][0]
			//odd
			res2 += dp[i%2][0]
		} else {
			dp[i%2][1]++
			//even
			res1 += dp[(i+1)%2][1]
			//odd
			res2 += dp[i%2][1]
		}
	}
	fmt.Println(res1, res2)

}

//template
type FastScanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

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

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

//Max,Min
func IntMax(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func Int64Max(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}
func Float64Max(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

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

func Int64Min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}
func Float64Min(a, b float64) float64 {
	if a > b {
		return b
	}
	return a
}

//Gcd
func IntGcd(a, b int) int {
	if a < b {
		b, a = a, b
	}
	if b == 0 {
		return a
	}
	return IntGcd(b, a%b)
}
func Int64Gcd(a, b int64) int64 {
	if a < b {
		b, a = a, b
	}
	if b == 0 {
		return a
	}
	return Int64Gcd(b, a%b)
}

func IntAbs(a int) int {
	if a < 0 {
		a *= -1
	}
	return a
}

func Int64Abs(a int64) int64 {
	if a < 0 {
		a *= -1
	}
	return a
}