package main

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

func solve() {
    n, k := readInt(), readInt()
    a := readIntArray(n)
    s, ans := a[0], 0
    for i := 1; i < n; i++ {
        d := k - a[i] - s
        if d > 0 {
            a[i] += d
            ans += d
        }
        s = a[i]
    }
    fmt.Println(ans)
    for _, v := range a {
        fmt.Print(v, " ")
    }
    fmt.Println()
}

// Helpers

var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)

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

// Math

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

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

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

// IO

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

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

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

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

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

// Sort

type Ints64 []int64

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

/* Sort tempalte
func (a ) Len() int           { return len(a) }
func (a ) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ) Less(i, j int) bool { return  }
*/

package main

import (
	"fmt"
)

func main() {
	var n, k int
	fmt.Scan(&n, &k)
	need := make([]int, n)
	needCnt := 0
	yesterday := 0
	fmt.Scan(&yesterday)
	need[0] = yesterday
	for i := 1; i < n; i++ {
		var today int
		fmt.Scan(&today)
		if yesterday+today >= k {
			need[i] = today
			yesterday = today
		} else {
			need[i] = k - yesterday
			needCnt += need[i] - today
			yesterday = need[i]
		}
	}
	fmt.Print(needCnt, "\n")
	for i := 0; i < n; i++ {
		fmt.Print(need[i], " ")
	}
}

package main

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

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

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r:rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}

func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}

func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

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

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

	return result
}

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

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}
func main() {
	sc := NewScanner()
	N, K := sc.NextInt(), sc.NextInt()
	A := sc.NextIntArray()
	diff := 0
	for i := 1; i < N; i++ {
		k := A[i - 1] + A[i]
		if K - k > 0 {
			diff += K - k
			A[i] += K - k
		}
	}
	fmt.Println(diff)
	PrintI(A...)
}

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

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

package main

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

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

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

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

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

	res := 0
	for i := 0; i+1 < n; i++ {
		s := a[i] + a[i+1]
		if s < k {
			res += k - s
			a[i+1] += k - s
		}
	}

	fmt.Fprintf(bufout, "%d\n", res)
	for i := 0; i < n; i++ {
		fmt.Fprintf(bufout, "%d ", a[i])
	}
	fmt.Fprintf(bufout, "\n")
}

package main

import "fmt"

const N int = 500

func main() {
    var n, k int
    fmt.Scanf("%d%d\n", &n, &k)
    var a, b [N]int
    for i := 0; i < n; i++ {
        fmt.Scanf("%d", &a[i])
        b[i] = a[i]
    }
    var s int
    for i := 0; i + 1 < n; i++ {
        d := k - (b[i] + b[i + 1])
        if d > 0 {
            s += d
            b[i + 1] += d
        }
    }
    fmt.Printf("%d\n", s)
    for i := 0; i < n; i++ {
        c := ' '
        if i == n - 1 {
            c = '\n'
        }
        fmt.Printf("%d%c", b[i], c)
    }
}

package main

import "fmt"

func main() {
	var n, k int32
	var sum int32
	fmt.Scan(&n, &k)
	a := make([]int32, n)
	for i := int32(0); i < n; i++ {
		fmt.Scan(&a[i])
		if i > 0 && a[i - 1] + a[i] < k {
			x := k - a[i - 1] - a[i]
			sum += x;
			a[i] += x
		}
	}
	fmt.Println(sum)
	for i := int32(0); i < n; i++ {
		if i != 0 {
			fmt.Print(" ")
		}
		fmt.Print(a[i])
	}
	fmt.Println()

}

// http://codeforces.com/problemset/problem/732/B
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

const (
	MOD = 1e9 + 7
)

type (

)

func main () {
	var (
		n, k int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)
	fmt.Fscanln(input, &n, &k)

	pwalks := make([]int, n + 2)

	// first & last days
	pwalks[0] = k
	pwalks[n + 1] = k

	for i := 0; i < n; i++ {
		fmt.Fscan(input, &pwalks[i + 1])
	}

	// go
	overs := 0
	for i := 1; i < n + 1; i++ {
		if (k > pwalks[i] + pwalks[i - 1]) {
			add := k - pwalks[i] - pwalks[i - 1]
			overs += add
			pwalks[i] += add
		}
	}

	fmt.Println(overs)
	for i := 0; i < n; i++ {
		fmt.Print(pwalks[i + 1], " ")
	}
}

package main

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

func CalculateNeededWalks(days int, neededWalks int, walks []int) (int, []int) {
    var todaysNeededWalks int
    var totalAdditionalWalks int

    for i := 1; i < days; i += 1 {
        todaysNeededWalks = neededWalks - walks[i-1]

        if todaysNeededWalks <= 0 || walks[i] > todaysNeededWalks {
            continue
        }

        totalAdditionalWalks += todaysNeededWalks - walks[i]
        walks[i] = todaysNeededWalks
    }

    return totalAdditionalWalks, walks
}

func PrepareInput() (int, int, []int) {
    var days int
    var neededWalks int

    fmt.Fscan(os.Stdin, &days)
    fmt.Fscan(os.Stdin, &neededWalks)

    walks := make([]int, days)

    for i := 0; i < days; i++ {
        fmt.Fscan(os.Stdin, &walks[i])
    }

    return days, neededWalks, walks
}

func PrintResult(totalAdditionalWalks int, days int, walks []int) {
    var walksPrepared []string = make([]string, days)

    for i := 0; i < days; i += 1 {
        walksPrepared[i] = strconv.Itoa(walks[i])
    }

    var walksLine string = strings.Join(walksPrepared, " ")

    fmt.Println(totalAdditionalWalks)
    fmt.Println(walksLine)
}

func main() {
    days, neededWalks, preparedWalks := PrepareInput()

    totalAdditionalWalks, walks := CalculateNeededWalks(days, neededWalks, preparedWalks)

    PrintResult(totalAdditionalWalks, days, walks)
}

package main

import (
	"fmt"
	"strings"
)

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

	a := make([]int, n)
	for i := range a {
		fmt.Scanf("%d", &a[i])
	}

	sum := 0
	for i := 1; i < n; i++ {
		if a[i-1]+a[i] < k {
			diff := k - a[i-1] - a[i]
			sum += diff
			a[i] += diff
		}
	}

	fmt.Printf("%d\n", sum)
	fmt.Println(strings.Trim(fmt.Sprint(a), "[]"))
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var n, k int
	fmt.Fscanln(reader, &n, &k)
	walks := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &walks[i])
	}
	var min_walks, extra_walk int
	for i := 1; i < n; i++ {
		extra_walk = k - (walks[i-1] + walks[i])
		if extra_walk <= 0 {
			continue
		}
		walks[i] += extra_walk
		min_walks += extra_walk
	}
	fmt.Fprintln(writer, min_walks)
	for i := 0; i < n; i++ {
		fmt.Fprintf(writer, "%d ", walks[i])
	}
}

package main

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

var ret []int

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, min int
	fmt.Fscanln(reader, &n, &min)
	var a []int
	a = make([]int, n)
	ret = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i])
		ret[i] = a[i]
	}
	fmt.Fscanln(reader)

	startIndex := 0
	for i := 0; i < n; i++ {
		if a[i] >= min {
			solveSub(ret, startIndex, i, min)
			ret[i] = a[i]
			startIndex = i + 1
		}
	}
	if startIndex < n {
		solveSub(ret, startIndex, n, min)
	}
	retSum := 0
	for i := range ret {
		retSum += ret[i] - a[i]
	}
	fmt.Println(retSum)
	for i := 0; i < len(ret)-1; i++ {
		fmt.Print(ret[i], " ")
	}
	fmt.Println(ret[len(ret)-1])
}

func solveSub(a []int, i int, j int, min int) {
	//fmt.Println(i, j)
	if i >= j {
		return
	}
	for k := i; k < j-1; k++ {
		if a[k]+a[k+1] < min {
			a[k+1] = min - a[k]
		}
	}
}

package main

import "fmt"

const N int = 500

func main() {
    var n, k int
    fmt.Scanf("%d %d", &n, &k)
    var a, b [N]int
    for i := 0; i < n; i++ {
        fmt.Scanf("%d", &a[i])
        b[i] = a[i]
    }
    var s int
    for i := 0; i + 1 < n; i++ {
        d := k - (b[i] + b[i + 1])
        if d > 0 {
            s += d
            b[i + 1] += d
        }
    }
    fmt.Printf("%d\n", s)
    for i := 0; i < n; i++ {
        c := ' '
        if i == n - 1 {
            c = '\n'
        }
        fmt.Printf("%d%c", b[i], c)
    }
}

package main

import "fmt"

const N int = 500

func main() {
    var n, k int
    fmt.Scanf("%d%d", &n, &k)
    var a, b [N]int
    for i := 0; i < n; i++ {
        fmt.Scanf("%d", &a[i])
        b[i] = a[i]
    }
    var s int
    for i := 0; i + 1 < n; i++ {
        d := k - (b[i] + b[i + 1])
        if d > 0 {
            s += d
            b[i + 1] += d
        }
    }
    fmt.Printf("%d\n", s)
    for i := 0; i < n; i++ {
        c := ' '
        if i == n - 1 {
            c = '\n'
        }
        fmt.Printf("%d%c", b[i], c)
    }
}

package main

import "fmt"

const N int = 500

func main() {
    var n, k int
    fmt.Scanf("%d%d", &n, &k)
    fmt.Printf("%d %d", n, k)
    /*
    var a, b [N]int
    for i := 0; i < n; i++ {
        fmt.Scanf("%d", &a[i])
        b[i] = a[i]
    }
    var s int
    for i := 0; i + 1 < n; i++ {
        d := k - (b[i] + b[i + 1])
        if d > 0 {
            s += d
            b[i + 1] += d
        }
    }
    fmt.Printf("%d\n", s)
    for i := 0; i < n; i++ {
        c := ' '
        if i == n - 1 {
            c = '\n'
        }
        fmt.Printf("%d%c", b[i], c)
    }
    */
}

package main

import "fmt"

const N int = 500

func main() {
    var n, k int
    fmt.Scanf("%d%d", &n, &k)
    fmt.Printf("%d %d", n, k)
    var a, b [N]int
    for i := 0; i < n; i++ {
        fmt.Scanf("%d", &a[i])
        b[i] = a[i]
    }
    for i := 0; i < n; i++ {
        fmt.Printf("%d %d\n", a[i], b[i])
    }
    /*
    var s int
    for i := 0; i + 1 < n; i++ {
        d := k - (b[i] + b[i + 1])
        if d > 0 {
            s += d
            b[i + 1] += d
        }
    }
    fmt.Printf("%d\n", s)
    for i := 0; i < n; i++ {
        c := ' '
        if i == n - 1 {
            c = '\n'
        }
        fmt.Printf("%d%c", b[i], c)
    }
    */
}

package main

import "fmt"

const N int = 500

func main() {
    var n, k int
    fmt.Scanf("%d%d", &n, &k)
    fmt.Printf("%d %d\n", n, k)
    var a, b [N]int
    for i := 0; i < n; i++ {
        fmt.Scanf("%d", &a[i])
        b[i] = a[i]
    }
    for i := 0; i < n; i++ {
        fmt.Printf("%d %d\n", a[i], b[i])
    }
    /*
    var s int
    for i := 0; i + 1 < n; i++ {
        d := k - (b[i] + b[i + 1])
        if d > 0 {
            s += d
            b[i + 1] += d
        }
    }
    fmt.Printf("%d\n", s)
    for i := 0; i < n; i++ {
        c := ' '
        if i == n - 1 {
            c = '\n'
        }
        fmt.Printf("%d%c", b[i], c)
    }
    */
}

package main

import "fmt"

const N int = 500

func main() {
    var n, k int
    fmt.Scanf("%d%d\n", &n, &k)
    fmt.Printf("%d %d\n", n, k)
    var a, b [N]int
    for i := 0; i < n; i++ {
        fmt.Scanf("%d", &a[i])
        b[i] = a[i]
    }
    for i := 0; i < n; i++ {
        fmt.Printf("%d %d\n", a[i], b[i])
    }
    /*
    var s int
    for i := 0; i + 1 < n; i++ {
        d := k - (b[i] + b[i + 1])
        if d > 0 {
            s += d
            b[i + 1] += d
        }
    }
    fmt.Printf("%d\n", s)
    for i := 0; i < n; i++ {
        c := ' '
        if i == n - 1 {
            c = '\n'
        }
        fmt.Printf("%d%c", b[i], c)
    }
    */
}

package main

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

func CalculateNeededWalks(days int, neededWalks int, walks []int) (int, []int) {
    var todaysNeededWalks int
    var totalAdditionalWalks int

    for i := 1; i < days; i += 1 {
        todaysNeededWalks = neededWalks - walks[i-1]

        if todaysNeededWalks <= 0 {
            continue
        }

        totalAdditionalWalks += todaysNeededWalks - walks[i]
        walks[i] = todaysNeededWalks
    }

    return totalAdditionalWalks, walks
}

func PrepareInput() (int, int, []int) {
    var days int
    var neededWalks int

    fmt.Fscan(os.Stdin, &days)
    fmt.Fscan(os.Stdin, &neededWalks)

    walks := make([]int, days)

    for i := 0; i < days; i++ {
        fmt.Fscan(os.Stdin, &walks[i])
    }

    return days, neededWalks, walks
}

func PrintResult(totalAdditionalWalks int, days int, walks []int) {
    var walksPrepared []string = make([]string, days)

    for i := 0; i < days; i += 1 {
        walksPrepared[i] = strconv.Itoa(walks[i])
    }

    var walksLine string = strings.Join(walksPrepared, " ")

    fmt.Println(totalAdditionalWalks)
    fmt.Println(walksLine)
}

func main() {
    days, neededWalks, preparedWalks := PrepareInput()

    totalAdditionalWalks, walks := CalculateNeededWalks(days, neededWalks, preparedWalks)

    PrintResult(totalAdditionalWalks, days, walks)
}

package main

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

var ret []int

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, min int
	fmt.Fscanln(reader, &n, &min)
	var a []int
	a = make([]int, n)
	ret = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i])
		ret[i] = a[i]
	}
	fmt.Fscanln(reader)

	startIndex := 0
	for i := 0; i < n; i++ {
		if a[i] >= min {
			solveSub(ret, startIndex, i, min)
			ret[i] = 0
			startIndex = i + 1
		}
	}
	if startIndex < n {
		solveSub(ret, startIndex, n, min)
	}
	retSum := 0
	for i := range ret {
		retSum += ret[i] - a[i]
	}
	fmt.Println(retSum)
	for i := 0; i < len(ret)-1; i++ {
		fmt.Print(ret[i], " ")
	}
	fmt.Println(ret[len(ret)-1])
}

func solveSub(a []int, i int, j int, min int) {
	//fmt.Println(i, j)
	if i >= j {
		return
	}
	for k := i; k < j-1; k++ {
		if a[k]+a[k+1] < min {
			a[k+1] = min - a[k]
		}
	}
}


package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

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

  n, m, k := getI(), getI(), getI()
  icons, index := make([]int, n), make([]int, n+1)
  for i := range icons {
    icons[i] = getI()
    index[icons[i]] = i
  }
  count := int64(0)
  for i := 0; i < m; i++ {
    q := getI()
    pos := index[q]
    count += int64(pos/k) + 1
    if pos != 0 {
      p := icons[pos-1]
      icons[pos-1], icons[pos] = q, p
      index[q] = pos-1
      index[p] = pos
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", count))
}

package main

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

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

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

	n, m, k := nextInt(), nextInt(), nextInt()
	posiciones := make([]int, n)
	iconos := make([]int, n)
	icono, pos := 0, 0
	preIcon, prePos := 0, 0
	gesto := int64(0)

	for i := 0; i < n; i++ {
		icono = nextInt()-1
		posiciones[icono] = i
		iconos[i] = icono
	}

	for ; m > 0; m-- {
		icono = nextInt()-1
		pos = posiciones[icono]
		gesto += int64(pos/k + 1)

		if pos != 0 {
			preIcon = iconos[pos-1]
			prePos = posiciones[preIcon]
			posiciones[icono], posiciones[preIcon] = posiciones[preIcon], posiciones[icono]
			iconos[pos], iconos[prePos] = iconos[prePos], iconos[pos]
		}

	}

	println(gesto)
}

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

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

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

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

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

package main

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

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

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

	n, m, k := nextInt(), nextInt(), nextInt()
	postisions := make([]int, n)
	icons := make([]int, n)
	icon, pos := 0, 0
	preIcon, prePos := 0, 0
	gesture := int64(0)

	for i := 0; i < n; i++ {
		icon = nextInt()-1
		postisions[icon] = i
		icons[i] = icon
	}

	for ; m > 0; m-- {
		icon = nextInt()-1
		pos = postisions[icon]
		gesture += int64(pos/k + 1)

		if pos != 0 {
			preIcon = icons[pos-1]
			prePos = postisions[preIcon]
			postisions[icon], postisions[preIcon] = postisions[preIcon], postisions[icon]
			icons[pos], icons[prePos] = icons[prePos], icons[pos]
		}

		// println(gesture, icon, pos , preIcon, prePos)
		// println(postisions)
		// println(icons)
	}

	println(gesture)
}

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

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

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

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

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

package main

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

func Solve(n int, m int, k int, a []int, b []int) int64 {
	screen := make([]int64, n+1)
	inv := make([]int, n+1)
	for i, elem := range a {
		screen[elem] = int64((i + k) / k)
		inv[elem] = i
	}
	ans := int64(0)
	for _, elem := range b {
		ans += screen[elem]
		j := inv[elem]
		if j-1 >= 0 {
			screen[elem], screen[a[j-1]] = screen[a[j-1]], screen[elem]
			inv[elem], inv[a[j-1]] = inv[a[j-1]], inv[elem]
			a[j], a[j-1] = a[j-1], a[j]
		}
	}
	return ans
}

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

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

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

package main

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

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

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

	n, m, k := nextInt(), nextInt(), nextInt()
	postisions := make([]int, n)
	icons := make([]int, n)
	icon, pos := 0, 0
	preIcon, prePos := 0, 0
	gesture := 0

	for i := 0; i < n; i++ {
		icon = nextInt()-1
		postisions[icon] = i
		icons[i] = icon
	}

	for ; m > 0; m-- {
		icon = nextInt()-1
		pos = postisions[icon]
		gesture += pos/k + 1

		if pos != 0 {
			preIcon = icons[pos-1]
			postisions[icon], postisions[preIcon] = postisions[preIcon], postisions[icon]
			prePos = postisions[preIcon]
			icons[pos], icons[prePos] = icons[prePos], icons[pos]
		}

		// println(gesture, icon, pos , preIcon, prePos)
		// println(postisions)
		// println(icons)
	}

	println(gesture)
}

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

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

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

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

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

package main

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

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

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

	n, m, k := nextInt(), nextInt(), nextInt()
	postisions := make([]int, n)
	icons := make([]int, n)
	icon, pos := 0, 0
	preIcon, prePos := 0, 0
	gesture := 0

	for i := 0; i < n; i++ {
		icon = nextInt()-1
		postisions[icon] = i
		icons[i] = icon
	}

	for ; m > 0; m-- {
		icon = nextInt()-1
		pos = postisions[icon]
		gesture += pos/k + 1

		if pos != 0 {
			preIcon = icons[pos-1]
			prePos = postisions[preIcon]
			postisions[icon], postisions[preIcon] = postisions[preIcon], postisions[icon]
			icons[pos], icons[prePos] = icons[prePos], icons[pos]
		}

		// println(gesture, icon, pos , preIcon, prePos)
		// println(postisions)
		// println(icons)
	}

	println(gesture)
}

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

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

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

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

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

package main

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

func Solve(n int, m int, k int, a []int, b []int) int {
	screen := make([]int, n+1)
	inv := make([]int, n+1)
	for i, elem := range a {
		screen[elem] = (i + k) / k
		inv[elem] = i
	}
	ans := 0
	for _, elem := range b {
		ans += screen[elem]
		j := inv[elem]
		if j-1 >= 0 {
			screen[elem], screen[a[j-1]] = screen[a[j-1]], screen[elem]
			inv[elem], inv[a[j-1]] = inv[a[j-1]], inv[elem]
			a[j], a[j-1] = a[j-1], a[j]
		}
	}
	return ans
}

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

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

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


package main

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

// github.com/EndlessCheng/codeforces-go
func CF1187C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type fact struct{ l, r int }

	var n, m, t, l, r int
	Fscan(in, &n, &m)
	fa := make([]int, 2*n)
	for i := range fa {
		fa[i] = i
	}
	var find func(int) int
	find = func(x int) int {
		if fa[x] != x {
			fa[x] = find(fa[x])
		}
		return fa[x]
	}
	merge := func(from, to int) { fa[find(from)] = find(to) }
	fs := []fact{}
	for ; m > 0; m-- {
		Fscan(in, &t, &l, &r)
		l--
		if t > 0 {
			tar := n + l
			for i := l; i < r; i++ {
				if find(i) != i {
					tar = find(i)
					break
				}
			}
			for i := l; i < r; i++ {
				merge(i, tar)
			}
		} else {
			fs = append(fs, fact{l, r})
		}
	}
o:
	for _, f := range fs {
		for i := f.l; i < f.r; i++ {
			if find(i) != find(f.l) {
				continue o
			}
		}
		Fprint(out, "NO")
		return
	}
	Fprintln(out, "YES")
	for i := 0; i < n; {
		st := i
		for ; i < n && find(i) == find(st); i++ {
			Fprint(out, 1e4+i-2*st, " ")
		}
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1187C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type fact struct{ l, r int }

	var n, m, t, l, r int
	Fscan(in, &n, &m)
	fa := make([]int, 2*n)
	for i := range fa {
		fa[i] = i
	}
	var find func(int) int
	find = func(x int) int {
		if fa[x] != x {
			fa[x] = find(fa[x])
		}
		return fa[x]
	}
	merge := func(from, to int) { fa[find(from)] = find(to) }
	fs := []fact{}
	for ; m > 0; m-- {
		Fscan(in, &t, &l, &r)
		l--
		if t > 0 {
			tar := n + l
			for i := l; i < r; i++ {
				if find(i) != i {
					tar = find(i)
					break
				}
			}
			for i := l; i < r; i++ {
				merge(i, tar)
			}
		} else {
			fs = append(fs, fact{l, r})
		}
	}
o:
	for _, f := range fs {
		for i := f.l; i < f.r; i++ {
			if find(i) != find(f.l) {
				continue o
			}
		}
		Fprint(out, "NO")
		return
	}
	Fprintln(out, "YES")
	for i := 0; i < n; {
		st := i
		for ; i < n && find(i) == find(st); i++ {
			Fprint(out, n-st, " ")
		}
	}
}

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

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/1187/C

package main

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

type Fact struct {
	T int
	L int
	R int
}

type Solution struct {
	Exists bool
	Array  []int
}

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

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

func mergeFacts(facts []Fact) []Fact {
	sort.Slice(facts, func(i, j int) bool { return facts[i].L < facts[j].L })
	mergedFacts := []Fact{}
	for len(facts) > 0 {
		var currentFact Fact
		currentFact, facts = facts[0], facts[1:]
		if len(mergedFacts) == 0 || currentFact.L > mergedFacts[len(mergedFacts)-1].R {
			mergedFacts = append(mergedFacts, currentFact)
		} else {
			lastMergedFact := mergedFacts[len(mergedFacts)-1]
			if lastMergedFact.L <= currentFact.L && currentFact.L <= lastMergedFact.R {
				mergedFacts[len(mergedFacts)-1].L = min(lastMergedFact.L, currentFact.L)
				mergedFacts[len(mergedFacts)-1].R = max(lastMergedFact.R, currentFact.R)
			}
		}
	}
	return mergedFacts
}

func splitFacts(facts []Fact) ([]Fact, []Fact) {
	t1Facts := []Fact{}
	t0Facts := []Fact{}
	for _, fact := range facts {
		if fact.T == 0 {
			t0Facts = append(t0Facts, fact)
		} else if fact.T == 1 {
			t1Facts = append(t1Facts, fact)
		}
	}
	return t0Facts, mergeFacts(t1Facts)
}

func constructB(n int, t1Facts []Fact) []int {
	b := make([]int, n-1)
	for i := 0; i < n-1; i++ {
		b[i] = -1
	}
	for _, fact := range t1Facts {
		for i := fact.L; i < fact.R; i++ {
			b[i-1] = 0
		}
	}
	return b
}

func constructA(n int, b []int) []int {
	a := make([]int, n)
	a[0] = n
	for i := 1; i < n; i++ {
		a[i] = b[i-1] + a[i-1]
	}
	return a
}

func Solve(n int, m int, facts []Fact) Solution {
	t0Facts, t1Facts := splitFacts(facts)
	b := constructB(n, t1Facts)
	a := constructA(n, b)
	for _, fact := range t0Facts {
		factVerified := false
		for i := fact.L; i < fact.R; i++ {
			if a[i-1] > a[i] {
				factVerified = true
			}
		}
		if !factVerified {
			return Solution{Exists: false}
		}
	}
	return Solution{Exists: true, Array: a}
}

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

	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	facts := make([]Fact, m)
	for i := 0; i < m; i++ {
		fmt.Fscanf(reader, "%d %d %d\n", &facts[i].T, &facts[i].L, &facts[i].R)
	}

	solution := Solve(n, m, facts)
	if !solution.Exists {
		fmt.Fprintf(writer, "NO\n")
	} else {
		fmt.Fprintf(writer, "YES\n")
		for _, elem := range solution.Array {
			fmt.Fprintf(writer, "%d ", elem)
		}
		fmt.Fprintf(writer, "\n")
	}
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/1187/C

package main

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

type Fact struct {
	T int
	L int
	R int
}

type Solution struct {
	Exists bool
	Array  []int
}

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

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

func mergeFacts(facts []Fact) []Fact {
	sort.Slice(facts, func(i, j int) bool { return facts[i].L < facts[j].L })
	mergedFacts := []Fact{}
	for len(facts) > 0 {
		var currentFact Fact
		currentFact, facts = facts[0], facts[1:]
		if len(mergedFacts) == 0 || currentFact.L > mergedFacts[len(mergedFacts)-1].R {
			mergedFacts = append(mergedFacts, currentFact)
		} else {
			lastMergedFact := mergedFacts[len(mergedFacts)-1]
			if lastMergedFact.L <= currentFact.L && currentFact.L <= lastMergedFact.R {
				mergedFacts[len(mergedFacts)-1].L = min(lastMergedFact.L, currentFact.L)
				mergedFacts[len(mergedFacts)-1].R = max(lastMergedFact.R, currentFact.R)
			}
		}
	}
	return mergedFacts
}

func splitFacts(facts []Fact) ([]Fact, []Fact) {
	t1Facts := []Fact{}
	t0Facts := []Fact{}
	for _, fact := range facts {
		if fact.T == 0 {
			t0Facts = append(t0Facts, fact)
		} else if fact.T == 1 {
			t1Facts = append(t1Facts, fact)
		}
	}
	return mergeFacts(t0Facts), mergeFacts(t1Facts)
}

func Solve(n int, m int, facts []Fact) Solution {
	t0Facts, t1Facts := splitFacts(facts)
	b := make([]int, n-1)
	for _, fact := range t0Facts {
		for i := fact.L; i < fact.R; i++ {
			b[i-1] = -1
		}
	}
	a := make([]int, n)
	a[0] = n
	for i := 1; i < n; i++ {
		a[i] = b[i-1] + a[i-1]
	}
	for _, fact := range t1Facts {
		for i := fact.L; i < fact.R; i++ {
			if a[i-1] > a[i] {
				return Solution{Exists: false}
			}
		}
	}
	return Solution{Exists: true, Array: a}
}

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

	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	facts := make([]Fact, m)
	for i := 0; i < m; i++ {
		fmt.Fscanf(reader, "%d %d %d\n", &facts[i].T, &facts[i].L, &facts[i].R)
	}

	solution := Solve(n, m, facts)
	if !solution.Exists {
		fmt.Fprintf(writer, "NO\n")
	} else {
		fmt.Fprintf(writer, "YES\n")
		for _, elem := range solution.Array {
			fmt.Fprintf(writer, "%d ", elem)
		}
		fmt.Fprintf(writer, "\n")
	}
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/1187/C

package main

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

type Fact struct {
	T int
	L int
	R int
}

type Solution struct {
	Exists bool
	Array  []int
}

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

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

func mergeFacts(facts []Fact) []Fact {
	sort.Slice(facts, func(i, j int) bool { return facts[i].L < facts[j].L })
	mergedFacts := []Fact{}
	for len(facts) > 0 {
		var currentFact Fact
		currentFact, facts = facts[0], facts[1:]
		if len(mergedFacts) == 0 || currentFact.L > mergedFacts[len(mergedFacts)-1].R {
			mergedFacts = append(mergedFacts, currentFact)
		} else {
			lastMergedFact := mergedFacts[len(mergedFacts)-1]
			if lastMergedFact.L <= currentFact.L && currentFact.L <= lastMergedFact.R {
				mergedFacts[len(mergedFacts)-1].L = min(lastMergedFact.L, currentFact.L)
				mergedFacts[len(mergedFacts)-1].R = max(lastMergedFact.R, currentFact.R)
			}
		}
	}
	return mergedFacts
}

func splitFacts(facts []Fact) ([]Fact, []Fact) {
	t1Facts := []Fact{}
	t0Facts := []Fact{}
	for _, fact := range facts {
		if fact.T == 0 {
			t0Facts = append(t0Facts, fact)
		} else if fact.T == 1 {
			t1Facts = append(t1Facts, fact)
		}
	}
	return mergeFacts(t0Facts), mergeFacts(t1Facts)
}

func constructB(n int, t1Facts []Fact) []int {
	b := make([]int, n-1)
	for i := 0; i < n-1; i++ {
		b[i] = -1
	}
	for _, fact := range t1Facts {
		for i := fact.L; i < fact.R; i++ {
			b[i-1] = 0
		}
	}
	return b
}

func constructA(n int, b []int) []int {
	a := make([]int, n)
	a[0] = n
	for i := 1; i < n; i++ {
		a[i] = b[i-1] + a[i-1]
	}
	return a
}

func Solve(n int, m int, facts []Fact) Solution {
	t0Facts, t1Facts := splitFacts(facts)
	b := constructB(n, t1Facts)
	a := constructA(n, b)
	for _, fact := range t0Facts {
		factVerified := false
		for i := fact.L; i < fact.R; i++ {
			if a[i-1] > a[i] {
				factVerified = true
			}
		}
		if !factVerified {
			return Solution{Exists: false}
		}
	}
	return Solution{Exists: true, Array: a}
}

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

	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	facts := make([]Fact, m)
	for i := 0; i < m; i++ {
		fmt.Fscanf(reader, "%d %d %d\n", &facts[i].T, &facts[i].L, &facts[i].R)
	}

	solution := Solve(n, m, facts)
	if !solution.Exists {
		fmt.Fprintf(writer, "NO\n")
	} else {
		fmt.Fprintf(writer, "YES\n")
		for _, elem := range solution.Array {
			fmt.Fprintf(writer, "%d ", elem)
		}
		fmt.Fprintf(writer, "\n")
	}
}


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

		if m > n {
			n, m = m, n
		}

		if n == 1 && m == 1 {
			n = 0
		}
		fmt.Fprintln(out, (m-1)*2+n)
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var t, m, n int
	fmt.Scanln(&t)
	for i := 0; i < t; i++ {
		fmt.Scanln(&m, &n)
		if m == n && m == 1 {
			fmt.Println(0)
		} else {
			nn := math.Max(float64(m), float64(n))
			n2 := int(nn - 2)
			ans := (m+n-2)*2 - n2
			fmt.Println(ans)
		}

	}

}

package main

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

const MOD int64 = 1000000007

var n, m 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, &m)

		fmt.Println(solve())
	}
}

type pair struct {
	v int
	idx []int
}

func solve() int {
	if n == 1 && m == 1 {
		return 0
	}
	if n == 1 || m == 1 {
		return n + m - 1
	}
	return (n + m - 2) + min(n, m)
}

//IQ = 87

//........105....99...87..87..87..87.87.......87....

//            86

func min(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"
	"math"
	"os"
)

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

	return b
}

func main() {
	// f, _ := os.Open("input.txt")
	var reader = bufio.NewReader(os.Stdin)
	// var reader = bufio.NewReader(f)
	var tests int
	fmt.Fscan(reader, &tests)
	for t := 0; t < tests; t++ {
		var n, m int
		fmt.Fscan(reader, &n)
		fmt.Fscan(reader, &m)
		if n == 1 && m == 1 {
			fmt.Println(0)
			continue
		}

		if n == 1 {
			fmt.Println(m)
			continue
		}

		if m == 1 {
			fmt.Println(n)
			continue
		}

		var answ1 int = math.MaxInt
		var answ2 int = math.MaxInt
		if n % 2 == 1 {
			answ1 = n / 2 + m - 1 + n / 2 + n / 2 + 1 + n / 2
		}

		if m % 2 == 1 {
			answ2 = m / 2 + n - 1 + m / 2 + m / 2 + 1 + m / 2
		}

		var answ3 int = math.MaxInt
		var answ4 int = math.MaxInt
		if m % 2 == 0 {
			answ3 = m / 2 - 1 + n - 1 + m/2 + m/2 + m/2
		}
		if n % 2 == 0 {
			answ4 = n / 2 - 1 + m - 1 + n/2 + n/2 + n/2
		}

		fmt.Println(min(answ1, min(answ2, min(answ3, answ4))))
	}
}

package main

import "fmt"

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

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

package main

import (
	"fmt"
	"math"
)

func minimum(nums ...int) int {
	ret := math.MaxInt
	for _, num := range nums {
		if num < ret {
			ret = num
		}
	}
	return ret
}

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

	for ; t > 0; t-- {
		var row, col int
		fmt.Scanf("%d %d\n", &row, &col)

		res := row - 1 + col - 1
		res += minimum(row-1, col-1)
		if res > 0 {
			res++
		}
		fmt.Println(res)
	}
}

package main

import (
	"fmt"
)

func main() {
	var TC int
	fmt.Scanf("%d\n", &TC)
	for tc := 0; tc < TC; tc += 1 {
		var n, m int
		fmt.Scanf("%d %d\n", &n, &m)

		out := n - 1 + m - 1
		if n < m {
			out += n - 1
			if m > 1 {
				out += 1
			}
		} else {
			out += m - 1
			if n > 1 {
				out += 1
			}
		}

		fmt.Printf("%v\n", out)
	}
}

package main

import "fmt"

func calc(n, m int) int {
    var res int
    res += m + n - 1
    if n > m {
        res += m - 1
    } else {
        res += n - 1
    }
    return res
}

func main() {
    var testCase, n, m int
    fmt.Scanf("%d\n", &testCase)
    for testCase > 0 {
        fmt.Scanf("%d %d\n", &n, &m)
        if n == 1 && m == 1 {
            fmt.Println("0")
            testCase--
            continue
        }
        fmt.Println(calc(n, m))
        testCase--
    }
}

package main
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 y,x int
		fmt.Fscan(in,&y,&x)
		if x==1 && y==1 {
			fmt.Fprintln(out,0)
		} else {
			fmt.Fprintln(out,(y-1) + (x-1) + min(x,y))
		}
	}
}

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

package main

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

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

func qwq() {
	n, m := 0, 0
	fmt.Fscan(in, &n, &m)
	if n < m {
		n, m = m, n
	}
	if n == 1 {
		fmt.Fprintln(out, 0)
	} else {
		fmt.Fprintln(out, n+m-2+m)
	}
}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1715A
Date: 8/30/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1715/A
Problem: CF1715A
**/
func (in *CF1715A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, m := in.NextInt(), in.NextInt()
		if n == 1 && m == 1 {
			fmt.Println(0)
		} else {
			fmt.Println(n + m - 2 + Min(n, m))
		}

	}
}

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

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

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

package main

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

func Min(a, b int) int {
	return int(math.Min(float64(a), float64(b)))
}

func Max(a, b int) int {
	return int(math.Max(float64(a), float64(b)))
}

func solution(reader io.Reader, writer io.Writer) {
	var t int
	fmt.Fscan(reader, &t)

	for i := 0; i < t; i++ {
		var n, m int
		fmt.Fscan(reader, &n, &m)
		if n+m == 2 {
			fmt.Fprintln(writer, 0)
		} else {
			fmt.Fprintln(writer, Max(n, m)+2*Min(n, m)-2)
		}
	}
}

func timeTrack(start time.Time) {
	elapsed := time.Since(start)
	fmt.Printf("Execution time %f", elapsed.Seconds())
}

func main() {
	var stdin *os.File
	var stdout *os.File

	LOCAL, _ := strconv.ParseBool(os.Getenv("LOCAL"))

	if LOCAL {
		defer timeTrack(time.Now())

		stdin, _ = os.Open("file.in")
		defer stdin.Close()

		stdout, _ = os.Create("file.out")
		defer stdout.Close()
	} else {
		stdin = os.Stdin
		stdout = os.Stdout
	}

	reader := bufio.NewReaderSize(stdin, 64*1024)
	writer := bufio.NewWriterSize(stdout, 64*1024)
	defer writer.Flush()

	solution(reader, writer)
}

package main

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

var (
	reader bufio.Reader
	writer bufio.Writer
)

func main() {
	inp, err := os.Open("in.txt")
	if err == nil {
		os.Stdin = inp
		outp, _ := os.Create("out.txt")
		os.Stdout = outp
		defer outp.Close()
		defer inp.Close()
	}
	reader = *bufio.NewReader(os.Stdin)
	writer = *bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var T int
	scan(&T)

	for ; T > 0; T-- {
		var n, m int
		scan(&n, &m)

		if n < m {
			n, m = m, n
		}

		if n == 1 && m == 1 {
			printf("0\n")
		} else {
			printf("%v\n", n + m - 2 + 1 + m - 1)
		}
	}
}

func scan(a ...interface{}) (n int, err error) {
	return fmt.Fscan(&reader, a...)
}

func printf(format string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(&writer, format, a...)
}

package main

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

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

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

// func min3(a, b, c int) int {
// 	return min(min(a, b), c)
// }

// func maxSquare(a int) int {
// 	tmp := int(math.Sqrt(float64(a)))
// 	if tmp*tmp == a {
// 		return tmp * tmp
// 	} else {
// 		return (tmp + 1) * (tmp + 1)
// 	}
// }

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var niter int
	fmt.Fscan(in, &niter)
	for iiter := 0; iiter < niter; iiter++ {
		var n, m int
		fmt.Fscan(in, &m, &n)
		m--
		n--
		if m > n {
			n, m = m, n
		}
		// n > m
		res := m + m + n + min(n, 1)
		fmt.Fprintln(out, res)
	}
}

package main

import (
	"fmt"
)

func main() {
	var t, n, m int
	fmt.Scanln(&t)
	for t > 0 {
		fmt.Scanln(&n, &m)
		if n < m {
			n, m = m, n
		}
		ans := 2*m+n-2
		if m == 1 && n == 1{
		    ans = 0
		}
		fmt.Println(ans)
		t--
	}
}

package main

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

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 Solve(reader *bufio.Reader) {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < t; i++ {
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())

		if n == 1 && m == 1 {
			wr.WriteString(strconv.Itoa(0) + "\n")
		} else {
			wr.WriteString(strconv.Itoa(n+m-2+min(n, m)) + "\n")
		}
	}

}

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

package main

import (
	"fmt"
)

func main() {
	var t, n, m int
	fmt.Scanln(&t)
	for t > 0 {
		fmt.Scanln(&n, &m)
		if n < m {
			n, m = m, n
		}
		ans := 2*m+n-2
		if m == 1 {
		    ans = 0
		}
		fmt.Println(ans)
		t--
	}
}

package main

import (
	"fmt"
)

func main() {
	var t, n, m int
	fmt.Scanln(&t)
	for t > 0 {
		fmt.Scanln(&n, &m)
		if n < m {
			n, m = m, n
		}
		fmt.Println(2*m+n-2)
		t--
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var t, n, m int
	fmt.Scanln(&t)
	for t > 0 {
		fmt.Scanln(&n, &m)
		if n < m {
			n, m = m, n
		}
		fmt.Println(int(math.Max(0, float64(2*m+n-2))))
		t--
	}
}

package main

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

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 Solve(reader *bufio.Reader) {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < t; i++ {
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())

		if n == 1 && m == 1 {
			wr.WriteString(strconv.Itoa(0) + "\n")
      return
		}

		wr.WriteString(strconv.Itoa(n+m-2+min(n, m)) + "\n")
	}

}

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

package main

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

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 Solve(reader *bufio.Reader) {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < t; i++ {
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())

		if n == 1 && m == 1 {
			wr.WriteString(strconv.Itoa(0) + "\n")
		}

		wr.WriteString(strconv.Itoa(n+m-2+min(n, m)) + "\n")
	}

}

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

package main

import (
	"fmt"
	"math"
)

func main() {
	var t, m, n int
	fmt.Scanln(&t)
	for i := 0; i <= t; i++ {
		fmt.Scanln(&m, &n)
		if m == n && m == 1 {
			fmt.Println(0)
		} else {
			nn := math.Max(float64(m), float64(n))
			n2 := int(nn - 2)
			ans := (m+n-2)*2 - n2
			fmt.Println(ans)
		}

	}

}

package main

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

const MOD int64 = 1000000007

var n, m 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, &m)

		fmt.Println(solve())
	}
}

type pair struct {
	v int
	idx []int
}

func solve() int {
	if n == 1 || m == 1 {
		return n + m - 2
	}
	return (n + m - 2) + min(n, m)
}

//IQ = 87

//........105....99...87..87..87..87.87.......87....

//            86

func min(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"
	"math"
	"os"
)

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

	return b
}

func main() {
	// f, _ := os.Open("input.txt")
	var reader = bufio.NewReader(os.Stdin)
	// var reader = bufio.NewReader(f)
	var tests int
	fmt.Fscan(reader, &tests)
	for t := 0; t < tests; t++ {
		var n, m int
		fmt.Fscan(reader, &n)
		fmt.Fscan(reader, &m)
		if n == 1 && m == 1 {
			fmt.Println(0)
			continue
		}

		if n == 1 {
			fmt.Println(m)
			continue
		}

		if m == 1 {
			fmt.Println(n)
			continue
		}

		var answ1 int = math.MaxInt
		var answ2 int = math.MaxInt
		if n % 2 == 1 {
			answ1 = n / 2 + m - 1 + n / 2 + n / 2 + 1 + n / 2
		}

		if m % 2 == 1 {
			answ2 = m / 2 + n - 1 + m / 2 + m / 2 + 1 + m / 2
		}

		var answ3 int = math.MaxInt
		if n % 2 == 0 && m % 2 == 0 {
			answ3 = m / 2 - 1 + n - 1 + m/2 + m/2 + m/2
		}

		fmt.Println(min(answ1, min(answ2, answ3)))
	}
}

package main

import "fmt"

func main() {
	var t, n, m int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&n, &m)
		ans := m + m - 1 + n - 1
		fmt.Println(ans)
	}
}

package main

import (
	"fmt"
	"math"
)

func minimum(nums ...int) int {
	ret := math.MaxInt
	for _, num := range nums {
		if num < ret {
			ret = num
		}
	}
	return ret
}

func main() {
	var t int
	fmt.Scanf("%d", &t)

	for ; t > 0; t-- {
		var row, col int
		fmt.Scanf("%d %d", &row, &col)

		res := row - 1 + col - 1
		res += minimum(row-1, col-1)
		if res > 0 {
			res++
		}
		fmt.Println(res)
	}
}

package main

import (
	"fmt"
)

func main() {
	var TC int
	fmt.Scanf("%d", &TC)
	for tc := 0; tc < TC; tc += 1 {
		var n, m int
		fmt.Scanf("%d %d", &n, &m)

		out := n - 1 + m - 1
		if n < m {
			out += n - 1
			if m > 1 {
				out += 1
			}
		} else {
			out += m - 1
			if n > 1 {
				out += 1
			}
		}

		fmt.Printf("%v\n", out)
	}
}

package main

import "fmt"

func calc(n, m int) int {
    var res int
    res += m + n - 1
    if n > m {
        res += m - 1
    } else {
        res += n - 1
    }
    return res
}

func main() {
    var testCase, n, m int
    fmt.Scanf("%d", &testCase)
    for testCase > 0 {
        fmt.Scanf("%d %d", &n, &m)
        if n == 1 && m == 1 {
            fmt.Println("0")
            testCase--
            continue
        }
        fmt.Println(calc(n, m))
        testCase--
    }
}


package main

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

/*
7
ABABABAB
BABABABAAB
ABABA
AB
AABBABAABBB
AABAAAAABABABABAB
AABAAABAAABBABABAAAB
---
YES
NO
NO
YES
NO
YES
YES
======
5
AABABAAABAAABB
AAABB
AAAABBBB
ABABB
ABB
---
YES
YES
YES
NO
NO
======
1
AABABB
---
YES

*/

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

	if len(s) == 1 {
		fmt.Fprintln(writer, "NO")
		return
	}

	if s[0] == 'B' {
		fmt.Fprintln(writer, "NO")
		return
	}

	if s[len(s)-1] != 'B' {
		fmt.Fprintln(writer, "NO")
		return
	}

	aCnt := make([]int, len(s))
	bCnt := make([]int, len(s))

	for i, r := range s {
		if r == 'A' {
			aCnt[i]++
		} else {
			bCnt[i]++
		}

		if i == 0 {
			continue
		}

		aCnt[i] += aCnt[i-1]
		bCnt[i] += bCnt[i-1]

		if aCnt[i] < bCnt[i] {
			fmt.Fprintln(writer, "NO")
			return
		}
	}

	fmt.Fprintln(writer, "YES")
}

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

//goland:noinspection GoUnusedFunction
func factorize(num int) []int {
	factors := []int{1}

	sqrtA := int(math.Sqrt(float64(num)))

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

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

	return factors
}

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

		sum := 0
		lastB := false
		for _, ch := range s {
			if ch == 'A' {
				sum += 1
				lastB = false
			}
			if ch == 'B' {
				sum -= 1
				lastB = true
			}

			if sum < 0 {
				break
			}
		}
		if sum >= 0 && lastB {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}

}

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
}

package main

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

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

	defer writer.Flush()

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

	for ; t > 0; t-- {
		var text string
		fmt.Fscanf(in, "%s\n", &text)

		if text[len(text) - 1] != 'B' {
			fmt.Fprintln(writer, "NO")
			continue
		}

		good := true
		cnt := 0
		for _, ch := range text {
			if ch == 'A' {
				cnt++
			} else {
				cnt--
				if cnt < 0 {
					good = false
					break
				}
			}
		}

		if good {
			fmt.Fprintln(writer, "YES")
		} else {
			fmt.Fprintln(writer, "NO")
		}
	}
}

package main

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

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

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

	for ; t > 0; t-- {
		var text string
		fmt.Fscanf(in, "%s\n", &text)

		//text, _ := reader.ReadString('\n')
		//text = strings.TrimSpace(text)

		if text[len(text) - 1] != 'B' {
			fmt.Println("NO")
			continue
		}

		good := true
		cnt := 0
		for _, ch := range text {
			if ch == 'A' {
				cnt++
			} else {
				cnt--
				if cnt < 0 {
					good = false
					break
				}
			}
		}

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

package main

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

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

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

	for ; t > 0; t-- {
		var text string
		fmt.Fscan(in, &text)

		//text, _ := reader.ReadString('\n')
		//text = strings.TrimSpace(text)

		if text[len(text) - 1] != 'B' {
			fmt.Println("NO")
			continue
		}

		good := true
		cnt := 0
		for _, ch := range text {
			if ch == 'A' {
				cnt++
			} else {
				cnt--
				if cnt < 0 {
					good = false
					break
				}
			}
		}

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

package main

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

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

	text, _ := reader.ReadString('\n')
	text = strings.TrimSpace(text)
	t, _ := strconv.Atoi(text)

	for j := 0; j < t; j++ {
		text, _ := reader.ReadString('\n')
		text = strings.TrimSpace(text)

		if text[len(text) - 1] != 'B' {
			fmt.Println("NO")
			continue
		}

		good := true
		cnt := 0
		for _, ch := range text {
			if ch == 'A' {
				cnt++
			} else {
				cnt--
				if cnt < 0 {
					good = false
					break
				}
			}
		}

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

package main

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

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

	text, _ := reader.ReadString('\n')
	text = strings.TrimSpace(text)
	t, _ := strconv.Atoi(text)

	for j := 0; j < t; j++ {
		text, _ := reader.ReadString('\n')
		text = strings.TrimRight(text, "\r\n")

		if text[len(text) - 1] != 'B' {
			fmt.Println("NO")
			continue
		}

		good := true
		cnt := 0
		for _, ch := range text {
			if ch == 'A' {
				cnt++
			} else {
				cnt--
				if cnt < 0 {
					good = false
					break
				}
			}
		}

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

package main

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

func main() {
	var t int
	fmt.Scanf("%d", &t)

	reader := bufio.NewReader(os.Stdin)
	reader.ReadString('\n')
	for j := 0; j < t; j++ {
		text, _ := reader.ReadString('\n')
		text = strings.TrimRight(text, "\r\n")

		if text[len(text) - 1] != 'B' {
			fmt.Println("NO")
			continue
		}

		good := true
		cnt := 0
		for _, ch := range text {
			if ch == 'A' {
				cnt++
			} else {
				cnt--
				if cnt < 0 {
					good = false
					break
				}
			}
		}

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

package main

import (
	"fmt"
)

func main() {
	var b int
	var text string
	fmt.Scanf("%d\n", &b)
	for j := 0; j < b; j++ {
		fmt.Scanf("%s\n", &text)

		if text[len(text) - 1] != 'B' {
			fmt.Println("NO")
			continue
		}

		good := true
		cnt := 0
		for _, ch := range text {
			if ch == 'A' {
				cnt++
			} else {
				cnt--
				if cnt < 0 {
					good = false
					break
				}
			}
		}

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

package main

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

func main() {
	var b int
	fmt.Scanf("%d", &b)

	reader := bufio.NewReader(os.Stdin)
	reader.ReadString('\n')
	for j := 0; j < b; j++ {
		text, _ := reader.ReadString('\n')
		text = strings.TrimRight(text, "\r\n")

		if text[len(text) - 1] != 'B' {
			fmt.Println("NO")
			continue
		}

		good := true
		cnt := 0
		for _, ch := range text {
			if ch == 'A' {
				cnt++
			} else {
				cnt--
				if cnt < 0 {
					good = false
					break
				}
			}
		}

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

package main

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

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

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

		f := true
		var a, b int64
		for _, ch := range s {
			if ch == 'A' {
				a++
			} else {
				b++
			}

			if b > a {
				f = false
				break
			}
		}

		if f && b > 0 && s[len(s)-1] == 'B' {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

const MOD int64 = 1000000007

var s string

// 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, &s)

		a := solve()

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

type pair struct {
	v int
	idx []int
}

//a b c d

//20 12 -- 4
//4  2  -- 4

func solve() bool  {
	var cntA int
	var cntB int

	for i := 0; i < len(s); i++ {
		if s[i] == 'A' {
			cntA++
		}
		if s[i] == 'B' {
			cntB++
			if cntB > cntA {
				return false
			}
		}
	}

	if s[len(s) - 1] == 'A' {
		return false
	}

	if cntB == 0 {
		return false
	}

	return true

}

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

package main
import ("fmt"
        "bufio"
        "os")

func solve(reader *bufio.Reader) {
    var str string
    fmt.Fscan(reader, &str)
    s := []rune(str)
    n := len(s)
    a := 0
    for i := 0; i < n; i++ {
        if(s[i] == 'A') {
            a++
        } else {
            a--
        }
        if(a < 0) {
            fmt.Println("NO")
            return
        }
    }
    if(s[n - 1] != 'B') {
        fmt.Println("NO")
    } else {
        fmt.Println("YES")
    }
}

func main() {
    reader := bufio.NewReader(os.Stdin)
    tc := 1
    fmt.Fscan(reader, &tc)
    for i := 0; i < tc; i++ {
        solve(reader)
    }
}

package main

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

var in = bufio.NewReader(os.Stdin)

func main() {
	var n int
	fmt.Fscan(in, &n)

	for i := 0; i < n; i += 1 {
		var line string
		fmt.Fscan(in, &line)

		if good(line) {
			fmt.Printf("YES\n")
		} else {
			fmt.Printf("NO\n")
		}
	}
}

func good(s string) bool {
	if !strings.HasSuffix(s, "B") {
		return false
	}
	as := 0
	for i := 0; i < len(s); i += 1 {
		u := s[i]
		if u == 'A' {
			as += 1
		} else if u == 'B' {
			as -= 1
			if as < 0 {
				return false
			}
		}
	}

	return true
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1672B
Date: 27/07/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1672/B
Problem: CF1672B
**/
func (in *CF1672B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		str := in.NextString()
		ans := true

		if len(str) < 2 {
			ans = false
		}

		b := 0
		a := 0
		for i := 0; i < len(str) && ans; i++ {
			if str[i] == 'B' {
				b++
			} else {
				a++
			}
			if b > a {
				ans = false
			}
		}

		if b > a {
			ans = false
		}

		if ans && str[0] == 'B' {
			ans = false
		}

		if ans && str[len(str)-1] == 'A' {
			ans = false
		}

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

}

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

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

package main

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

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

func solve() string {
	var s string
	fmt.Fscan(in, &s)

	var cntA int = 0
	var lastChar rune

	for _, c := range s {
		if c == 'B' {
			if cntA == 0 {
				return "NO"
			}
			cntA -= 1
		} else {
			cntA += 1
		}
		lastChar = c
	}

	if lastChar != 'B' {
		return "NO"
	} else {
		return "YES"
	}
}

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

package main

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

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

func main() {
	defer w.Flush()

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

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

func solve() {
	var s string
	fmt.Fscan(r, &s)

	if len(s) < 2 || s[len(s)-1] != 'B' {
		fmt.Fprintln(w, "NO")
		return
	}

	a := 0
	b := 0

	for _, c := range s {
		if c == 'A' {
			a++
		} else {
			b++
		}

		if b > a {
			fmt.Fprintln(w, "NO")
			return
		}
	}

	fmt.Fprintln(w, "YES")
}

package main

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

func solve(input string) string {
	if (input[len(input)-1]) != 'B' {
		return "NO"
	}
	sum := 0
	for _, val := range input {
		if val == 'B' {
			sum -= 1
		} else {
			sum += 1
		}
		if sum < 0 {
			return "NO"
		}
	}
	return "YES"
}
func main() {
	in := bufio.NewReader(os.Stdin)
	t := readInt(in)
	for i := 0; i < t; i++ {
		input, _ := in.ReadString('\n')
		input = strings.TrimSpace(input)
		fmt.Println(solve(input))
	}
}

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.TrimSpace(nStr)
	n, _ := strconv.Atoi(nStr)
	return n
}

package main

import (
	"fmt"
)

func main() {

	var testcase int
	fmt.Scan(&testcase)

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

		var inputString string
		fmt.Scan(&inputString)

		numberOfA := 0
		numberOfB := 0

		if len(inputString) < 2 {
			fmt.Println("NO")
			continue
		}

		raisedFlag := false
		for _, char := range inputString {

			if string(char) == "A" {
				numberOfA += 1
			}
			if string(char) == "B" {
				numberOfB += 1
			}

			if numberOfB > numberOfA {
				fmt.Println("NO")
				raisedFlag = true
				break
			}

		}
		if raisedFlag == true {
			continue
		}

		if string(inputString[len(inputString)-1:]) == "B" {
			fmt.Println("YES")
		} else {
			fmt.Println("No")
		}
	}

}

package main

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

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

func solve(s string) string {
	l := len(s)
	if s[0] == 'B' || s[l-1] == 'A' {
		return "NO"
	}
	c := 0
	for i := range s {
		if s[i] == 'A' {
			c++
		} else {
			c--
		}
		if c < 0 {
			return "NO"
		}
	}
	return "YES"
}

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

package main

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

func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)

	for ii := 0; ii < t; ii++ {
		var s string
		fmt.Fscan(in, &s)
		a := 0
		can := true
		b := 0
		for i := 0; i < len(s); i++ {
			if s[i] == 'A' {
				a++
				b = 0
			} else {
				if a == 0 {
					can = false
					break
				}
				a--
				b++
			}
		}
		if can && (a == 0 || b > 0) {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

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

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

	for ; t > 0; t-- {
		var text string
		fmt.Fscanf(in, "%s\n", &text)

		//text, _ := reader.ReadString('\n')
		//text = strings.TrimSpace(text)

		if text[len(text) - 1] != 'B' {
			fmt.Fprintln(writer, "NO")
			continue
		}

		good := true
		cnt := 0
		for _, ch := range text {
			if ch == 'A' {
				cnt++
			} else {
				cnt--
				if cnt < 0 {
					good = false
					break
				}
			}
		}

		if good {
			fmt.Fprintln(writer, "YES")
		} else {
			fmt.Fprintln(writer, "NO")
		}
	}
}

package main

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

func main() {
	var b int
	fmt.Scanf("%d", &b)

	reader := bufio.NewReader(os.Stdin)
	reader.ReadString('\n')
	for j := 0; j < b; j++ {
		text, _ := reader.ReadString('\n')
		//text = strings.TrimRight(text, "\r\n")

		if text[len(text) - 1] != 'B' {
			fmt.Println("NO")
			continue
		}

		good := true
		cnt := 0
		for _, ch := range text {
			if ch == 'A' {
				cnt++
			} else {
				cnt--
				if cnt < 0 {
					good = false
					break
				}
			}
		}

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

package main

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

func main() {
	var b int
	fmt.Scanf("%d", &b)

	reader := bufio.NewReader(os.Stdin)
	reader.ReadString('\n')
	for j := 0; j < b; j++ {
		text, _ := reader.ReadString('\n')
		text = strings.TrimRight(text, "\r\n")

		good := true
		cnt := 0
		for _, ch := range text {
			if ch == 'A' {
				cnt++
			} else {
				cnt--
				if cnt < 0 {
					good = false
					break
				}
			}
		}

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

package main

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

func main() {
	var b int
	fmt.Scanf("%d", &b)

	reader := bufio.NewReader(os.Stdin)
	reader.ReadString('\n')
	for j := 0; j < b; j++ {
		text, _ := reader.ReadString('\n')
		text = strings.TrimRight(text, "\r\n")

		if text[len(text) - 1] != 'B' {
			fmt.Println("NO")
			continue
		}

		good := true
		cnt := 0
		for _, ch := range text {
			if ch != 'A' {
				if cnt == 0 {
					good = false
					break
				}
				cnt = 0
			} else {
				cnt++
			}
		}

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

package main

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

func main() {
	var b int
	fmt.Scanf("%d", &b)

	reader := bufio.NewReader(os.Stdin)
	reader.ReadString('\n')
	for j := 0; j < b; j++ {
		text, _ := reader.ReadString('\n')
		text = strings.TrimRight(text, "\r\n")

		good := true
		cnt := 0
		for _, ch := range text {
			if ch != 'A' {
				if cnt == 0 {
					good = false
					break
				}
				cnt = 0
			} else {
				cnt++
			}
		}

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

package main

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

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

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

		f := true
		var a, b int
		for _, ch := range s {
			if ch == 'A' {
				a++
			} else {
				b++
			}

			if b > a {
				f = false
				break
			}
		}

		if f && b > 0 {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

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

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

		var f bool
		var b int
		for i := 0; i < len(s); i++ {
			if i < len(s)-1 && s[i:i+2] == "BB" {
				f = false
				break
			}

			if s[i] == 'B' {
				b++
			}
			f = true
		}

		if f && b > 0 && s[0] == 'A' {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

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

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

		var f bool
		for i := 0; i < len(s)-1; i++ {
			if s[i:i+2] == "BB" {
				f = false
				break
			}
			f = true
		}

		if f {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

const MOD int64 = 1000000007

var s string

// 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, &s)

		a := solve()

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

type pair struct {
	v int
	idx []int
}

//a b c d

//20 12 -- 4
//4  2  -- 4

func solve() bool  {
	var cntA int
	var cntB int

	for i := 0; i < len(s); i++ {
		if s[i] == 'A' {
			cntA++
		}
		if s[i] == 'B' {
			cntB++
			if cntB > cntA {
				return false
			}
		}
	}

	if cntB == 0 {
		return false
	}

	return true

}

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

package main

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

const MOD int64 = 1000000007

var s string

// 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, &s)

		a := solve()

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

type pair struct {
	v int
	idx []int
}

//a b c d

//20 12 -- 4
//4  2  -- 4

func solve() bool  {
	var cntA int
	var cntB int
	for i := 0; i < len(s); i ++ {
		if s[i] == 'A' {
			cntA++
		} else {
			cntB++
		}
	}

	if cntB == 0 {
		return false
	}

	if cntA >= cntB {
		return true
	}

	return false
}

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

package main

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

const MOD int64 = 1000000007

var s string

// 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, &s)

		a := solve()

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

type pair struct {
	v int
	idx []int
}

//a b c d

//20 12 -- 4
//4  2  -- 4

func solve() bool  {
	var cntA int
	var cntB int
	for i := 0; i < len(s); i ++ {
		if s[i] == 'B' {
			cntB++

			var j int
			for j = i + 1; j < len(s); j++ {
				if s[j] != 'B' {
					break
				}
				cntB++
			}
			i = j - 1

			if cntA < cntB {
				return false
			}
			cntA = 0
			cntB = 0

		} else {
			cntA++
		}
	}

	if cntA > 0 {
		return false
	}

	return true
}

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

package main
import ("fmt"
        "bufio"
        "os")

func solve(reader *bufio.Reader) {
    var str string
    fmt.Fscan(reader, &str)
    s := []rune(str)
    n := len(s)
    a := 0
    for i := 0; i < n; i++ {
        if(s[i] == 'A') {
            a++
        } else {
            a--
        }
        if(a < 0) {
            fmt.Println("NO")
            return
        }
    }
    if(a == n) {
        fmt.Println("NO")
    } else {
        fmt.Println("YES")
    }
}

func main() {
    reader := bufio.NewReader(os.Stdin)
    tc := 1
    fmt.Fscan(reader, &tc)
    for i := 0; i < tc; i++ {
        solve(reader)
    }
}

package main
import ("fmt"
        "bufio"
        "os")

func solve(reader *bufio.Reader) {
    var str string
    fmt.Fscan(reader, &str)
    s := []rune(str)
    n := len(s)
    a, b := 0, 0
    for i := 0; i < n; i++ {
        if(s[i] == 'A') {
            a++
        } else {
            b++
        }
        if(b > a) {
            fmt.Println("NO")
            return
        }
    }
    if(b == 0) {
        fmt.Println("NO")
    } else {
        fmt.Println("YES")
    }
}

func main() {
    reader := bufio.NewReader(os.Stdin)
    tc := 1
    fmt.Fscan(reader, &tc)
    for i := 0; i < tc; i++ {
        solve(reader)
    }
}

package main

import (
	"fmt"
)

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

	for i := 0; i < n; i += 1 {
		var line string
		_, _ = fmt.Scanln(&line)

		if good(line) {
			fmt.Printf("YES\n")
		} else {
			fmt.Printf("NO\n")
		}
	}
}

func good(s string) bool {
	as := 0
	b := false
	for i := 0; i < len(s); i += 1 {
		u := s[i]
		if u == 'A' {
			as += 1
		} else if u == 'B' {
			b = true
			as -= 1
			if as < 0 {
				return false
			}
		}
	}

	return b
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1672B
Date: 27/07/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1672/B
Problem: CF1672B
**/
func (in *CF1672B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		str := in.NextString()
		ans := true

		if len(str) < 2 {
			ans = false
		}

		for i := 1; i < len(str) && ans; i++ {
			if str[i-1] == 'B' && str[i] == 'B' {
				ans = false
			}
		}

		if ans && str[0] == 'B' {
			ans = false
		}

		if ans && str[len(str)-1] == 'A' {
			ans = false
		}

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

}

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

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

package main

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

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

func solve() string {
	var s string
	fmt.Fscan(in, &s)

	if len(s) == 0 {
		return "YES"
	}

	if len(s) < 2 {
		return "NO"
	}

	var haveB bool = false
	for ind, c := range s {
		if c == 'B' {
			if ind < 1 {
				return "NO"
			}
			if s[ind-1] != 'A' {
				return "NO"
			}
			haveB = true
		}
	}
	if haveB {
		return "YES"
	} else {
		return "NO"
	}
}

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

package main

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

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

func main() {
	defer w.Flush()

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

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

func solve() {
	var s string
	fmt.Fscan(r, &s)

	if len(s) < 2 || s[len(s)-1] != 'B' {
		fmt.Fprintln(w, "NO")
		return
	}

	hasA := false

	for _, c := range s {
		if c == 'A' {
			hasA = true
		} else {
			if !hasA {
				fmt.Fprintln(w, "NO")
				return
			}

			hasA = false
		}
	}

	fmt.Fprintln(w, "YES")
}

package main

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

func solve(input string) string {
	if input[len(input)-1] != 'B' {
		return "NO"
	}
	for index, val := range input {
		if val == 'B' {
			if index == 0 {
				return "NO"
			}
			if input[index-1] == 'B' {
				return "NO"
			}
		}
	}
	return "YES"
}
func main() {
	in := bufio.NewReader(os.Stdin)
	t := readInt(in)
	for i := 0; i < t; i++ {
		input, _ := in.ReadString('\n')
		input = strings.TrimSpace(input)
		fmt.Println(solve(input))
	}
}

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.TrimSpace(nStr)
	n, _ := strconv.Atoi(nStr)
	return n
}

package main

import (
	"fmt"
)

func main() {

	var testcase int
	fmt.Scan(&testcase)

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

		var inputString string
		fmt.Scan(&inputString)

		fmt.Println(inputString)

		numberOfA := 0
		numberOfB := 0

		if len(inputString) < 2 {
			fmt.Println("NO")
			continue
		}

		raisedFlag := false
		for _, char := range inputString {

			if string(char) == "A" {
				numberOfA += 1
			}
			if string(char) == "B" {
				numberOfB += 1
			}

			if numberOfB > numberOfA {
				fmt.Println("NO")
				raisedFlag = true
				break
			}

		}
		if raisedFlag == true {
			continue
		}

		if string(inputString[len(inputString)-1:]) == "B" {
			fmt.Println("YES")
		} else {
			fmt.Println("No")
		}
	}

}

package main

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

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

func solve(s string) string {
	l := len(s)
	if s[0] == 'B' || s[l-1] == 'A' {
		return "NO"
	}
	for i := 1; i < len(s); i++ {
		if s[i] == 'B' && s[i-1] == 'B' {
			return "NO"
		}
	}
	return "YES"
}

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

package main

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

/*
7
ABABABAB
BABABABAAB
ABABA
AB
AABBABAABBB
AABAAAAABABABABAB
AABAAABAAABBABABAAAB
---
YES
NO
NO
YES
NO
YES
YES
======
5
AABABAAABAAABB
AAABB
AAAABBBB
ABABB
ABB
---
YES
YES
YES
NO
NO
*/

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

	if len(s) == 1 {
		fmt.Fprintln(writer, "NO")
		return
	}

	if s[0] == 'B' {
		fmt.Fprintln(writer, "NO")
		return
	}

	if s[len(s)-1] != 'B' {
		fmt.Fprintln(writer, "NO")
		return
	}

	rev := reversed(s)

	bCnt := 0

	for i := 0; i < len(rev); i++ {
		r := rev[i]
		if r == 'B' {
			bCnt++
		} else {
			if i+bCnt-1 >= len(rev) {
				fmt.Fprintln(writer, "NO")
				return
			}

			for j := i; j < len(rev) && bCnt > 0; j++ {
				if rev[j] == 'A' {
					bCnt--
				} else {
					break
				}
			}

			if bCnt > 0 {
				fmt.Fprintln(writer, "NO")
				return
			}
		}
	}

	fmt.Fprintln(writer, "YES")
}

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

//goland:noinspection GoUnusedFunction
func factorize(num int) []int {
	factors := []int{1}

	sqrtA := int(math.Sqrt(float64(num)))

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

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

	return factors
}

package main

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

func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)

	for ii := 0; ii < t; ii++ {
		var s string
		fmt.Fscan(in, &s)
		a := 0
		can := true
		b := 0
		for i := 0; i < len(s); i++ {
			if s[i] == 'A' {
				a++
			} else {
				if a == 0 {
					can = false
					break
				}
				a--
				b++
			}
		}
		if can && b > 0 {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

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

		sum := 0
		hasB := false
		for _, ch := range s {
			if ch == 'A' {
				sum += 1
			}
			if ch == 'B' {
				sum -= 1
				hasB = true
			}

			if sum < 0 {
				break
			}
		}
		if sum >= 0 && hasB {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}

}

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
}