package main

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

const mod = 998244353
const mx = int64(1e9 + 7)

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	arr := make([]string, 8)
	for i := 0; i < 8; i++ {
		Fscan(in, &arr[i])
	}
	var check func(i, j int) bool
	check = func(i, j int) bool {
		v := arr[i][j]
		col := true
		row := true
		for d := 0; d < 8; d++ {
			if arr[i][d] != v {
				col = false
			}
			if arr[d][j] != v {
				row = false
			}
		}
		if v == 'R' {
			return col
		} else {
			return row
		}
	}
	check(3, 4)
	for i := 0; i < 8; i++ {
		for j := 0; j < 8; j++ {
			if arr[i][j] != '.' {
				if check(i, j) {
					Fprintln(out, string(arr[i][j]))
					return
				}
			}
		}
	}
}

func max(i, j int) int {
	if i > j {
		return i
	}
	return j
}

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

func main() {
	stdin := os.Stdin
	stdout := os.Stdout
	in := bufio.NewReader(stdin)
	out := bufio.NewWriter(stdout)
	n := 1
	Fscan(in, &n)
	for i := 0; i < n; i++ {
		solve(in, out)
	}
}

package main

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

var in = bufio.NewReader(os.Stdin)

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

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

	for ; t > 0; t-- {
    var red = "B"
    for i := 0; i < 8; i ++{
      var s string
      fmt.Fscan(in, &s)
      if s == "RRRRRRRR" {
        red = "R"
      }
    }
    fmt.Println(red)
	}
}

package main

import (
	"fmt"
)

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

		// var numArr []int

		var arr [8]string
		for i := 0; i < len(arr); i++ {

			var str string
			fmt.Scan(&str)
			arr[i] = str
		}
		var res string
		res = thri(arr)
		fmt.Println(res)
		// var res string
		// res = sec(numArr)
	}

}
func thri(arr [8]string) string {
	count := 0
	for i := 0; i < len(arr); i++ {
		for j := 0; j < len(arr[i]); j++ {
			if arr[i][j] == 82 {
				count++
			}
		}
		if count != 8 {
			count = 0
		} else {
			return "R"
		}
	}
	if count != 8 {
		return "B"
	} else {
		return "R"
	}
}

package main

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

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

func readInt() int {
	var i int
	fmt.Fscan(reader, &i)
	return i
}

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

func main(){
	defer writer.Flush()
	T := readInt()
	for i := 1; i <= T; i++{
		board := make([]string, 8)
		ans := "B"
		for r := 0; r < 8; r++ {
			board[r] = readString()
		}
		for r := 0; r < 8; r++ {
			allEq := true
			for c := 0; c < 8; c++ {
				if board[r][c] != board[r][0] {
					allEq = false
					break
				}
			}
			if allEq && board[r][0] == 'R'{
				ans = string(board[r][0])
			}
		}
		for c := 0; c < 8; c++ {
			allEq := true
			for r := 0; r < 8; r++ {
				if board[r][c] != board[0][c] {
					allEq = false
					break
				}
			}
			if allEq && board[0][c] == 'B'{
				ans = string(board[0][c])
			}
		}
		fmt.Fprintln(writer, ans)
	}
}

package main

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

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

func match(arr [8][8]byte) byte {
	for i := 0; i < 8; i++ {
		var row []byte
		for j := 0; j < 8; j++ {
			row = append(row, arr[i][j])
		}

		if string(row) == "RRRRRRRR" {
			return 'R'
		}
	}

	return 'B'
}

func main() {
	writer.Flush()
	var test int
	fmt.Fscanf(reader, "%d\n", &test)
	for test > 0 {
		var arr [8][8]byte
		fmt.Fscanf(reader, "\n")
		for i := 0; i < 8; i++ {
			for j := 0; j < 8; j++ {
				fmt.Fscanf(reader, "%c", &arr[i][j])
			}
			fmt.Fscanf(reader, "\n")
		}
		fmt.Printf("%c\n", match(arr))
		test--
	}
}

package main

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

type IO struct {
	in  io.Reader
	out io.Writer
}

func NewIO(in io.Reader, out io.Writer) *IO {
	return &IO{in: bufio.NewReader(in), out: out}
}

func (io *IO) ReadInt() int {
	var res int
	_, _ = fmt.Fscanf(io.in, "%d\n", &res)
	return res
}

func (io *IO) ReadPairInt() (int, int) {
	var a, b int
	_, _ = fmt.Fscanf(io.in, "%d %d\n", &a, &b)
	return a, b
}

func (io *IO) ReadString() string {
	var res string
	_, _ = fmt.Fscanf(io.in, "%s\n", &res)
	return res
}

func (io *IO) ReadIntSlice(n int) sort.IntSlice {
	var t int
	res := make(sort.IntSlice, 0, n)
	for i := 0; i < n; i++ {
		_, _ = fmt.Fscanf(io.in, "%d", &t)
		res = append(res, t)
	}
	_, _ = fmt.Fscanf(io.in, "\n")
	return res
}

func (io *IO) Println(v ...interface{}) {
	_, _ = fmt.Fprintln(io.out, v...)
}

func (io *IO) Print(v ...interface{}) {
	_, _ = fmt.Fprint(io.out, v...)
}

func (io *IO) Printf(format string, v ...interface{}) {
	_, _ = fmt.Fprintf(io.out, format, v...)
}

func (io *IO) PrintIntSlice(s sort.IntSlice) {
	for _, v := range s {
		io.Printf("%d ", v)
	}
}

func (io *IO) PrintlnIntSlice(s sort.IntSlice) {
	io.PrintIntSlice(s)
	io.Printf("\n")
}

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

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

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

func main() {
	io := NewIO(os.Stdin, os.Stdout)

	t := io.ReadInt()
	for i := 0; i < t; i++ {
		io.ReadString()

		done := false
		for j := 0; j < 8; j++ {
			row := io.ReadString()
			if done {
				continue
			}

			oneSizeSet := true
			for _, c := range row {
				if c != 'R' {
					oneSizeSet = false
				}
			}

			if oneSizeSet {
				fmt.Println("R")
				done = true
			}
		}

		if !done {
			fmt.Println("B")
		}
	}
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	var s string
	fmt.Scanln(&s)
	count, _ := strconv.Atoi(s)
	answers := make([]string, 0, count)
	for i := 0; i < count ; i++ {
		scanner.Scan()
		var arr [8][8]string
		for row := 0; row < 8; row++ {
			scanner.Scan()
			line := strings.Split(scanner.Text(), "")
			for column := 0; column < 8; column++ {
				arr[row][column] = line[column]
			}
		}
		if checkArrayByRow(arr) {
			answers = append(answers, "R")
		}
		if checkArrayByColumn(arr) {
			answers = append(answers, "B")
		}
	}
	for _, answer := range answers {
		fmt.Println(answer)
	}
}

func checkArrayByRow(arr [8][8]string) bool {
	outer:
	for row := 0; row < 8; row++ {
		for column := 0; column < 8; column++ {
			if arr[row][column] != "R" {
				continue outer
			}
		}
		return true
	}
	return false
}

func checkArrayByColumn(arr [8][8]string) bool {
	outer:
	for row := 0; row < 8; row++ {
		for column := 0; column < 8; column++ {
			if arr[column][row] != "B" {
				continue outer
			}
		}
		return true
	}
	return false
}

package main

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

func check_row(g []string, i int, c rune) bool {
	r := true
	for _, v := range g[i] {
		if rune(v) != c {
			r = false
			break
		}
	}
	return r
}

func check_column(g []string, i int, c rune) bool {
	r := true
	for j := range g[i] {
		v := g[j][i]
		if rune(v) != c {
			r = false
			break
		}
	}
	return r
}

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-- {

		g := make([]string, 8)
		for i:= range g {
			fmt.Fscan(r, &g[i])
		}

		var res rune
		for i := range g {
			if check_row(g, i, 'R') {
				res = 'R'
				break
			}
			if check_column(g, i, 'B') {
				res = 'B'
				break
			}
		}

		fmt.Fprintln(w, string(res))

	}

}

package main

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

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

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

	var T int
	var s string

loop:
	for Fscan(in, &T); T > 0; T-- {
		g := [8]string{}
		for i := 0; i < 8; i++ {
			Fscan(in, &s)
			g[i] = s
		}

		for i := 0; i < 8; i++ {
			if g[i] == "RRRRRRRR" {
				Fprintln(out, "R")
				continue loop
			}
		}

	lp:
		for j := 0; j < 8; j++ {
			for i := 0; i < 8; i++ {
				if g[i][j] != 'B' {
					continue lp
				}
			}
			Fprintln(out, "B")
			continue loop
		}

		Fprintln(out, "")
	}
}

package main

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

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

	solve := func(curCase int) {
		a := make([]string, 8)
		for i := range a {
			Fscan(in, &a[i])
		}
		for _, s := range a {
			if  s == strings.Repeat("R", 8) {
				Fprintln(out, "R")
				return
			}
		}
		Fprintln(out, "B")
	}

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

	// 如果是多组数据，请务必加上这段保险 —— 已经无法统计，有多少位竞赛选手在漏读数据上损失大把分数（包括我）
	// 此外，如果不小心写成了 scan(v) 而不是 scan(&v)，由于未传入指针，不会读入任何数据，这样本地运行一次就能立马发现错误
	leftData, _ := ioutil.ReadAll(in)
	if s := strings.TrimSpace(string(leftData)); s != "" {
		panic("有未读入的数据：\n" + s)
	}

}

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

package main

import "fmt"

func main() {
	var t int
	var s string
	fmt.Scan(&t)
	for t > 0 {
		t--
		fmt.Scanf("\n")
		var r bool
		for i := 0; i < 8; i++ {
			fmt.Scan(&s)
			// fmt.Println(s)
			if s == "RRRRRRRR" {
				r = true
			}
		}
		if r {
			fmt.Println("R")
		} else {
			fmt.Println("B")
		}
	}
}

package main

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

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

func solve() {
	m := [8]string{}

	for i := 0; i < 8; i++ {
		var s string
		Fscan(r, &s)
		m[i] = s
	}

	for row := 0; row < 8; row++ {
		if m[row][0] == 'R' && m[row] == "RRRRRRRR" {
			Fprintln(w, "R")
			return
		}
	}
	Fprintln(w, "B")
}

func main() {
	// inputFile, _ := os.OpenFile("input.txt", os.O_RDONLY, os.ModePerm)

	defer w.Flush()

	var t int
	Fscan(r, &t)

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

package main

import (
	"fmt"
)

func main() {
	var T int
	fmt.Scan(&T)
	for Case := 1; Case <= T; Case++ {
		var G = [8]string{}
		for i := 0; i < 8; i++ {
			fmt.Scan(&G[i])
		}
		ans := -1
		for i := 0; i < 8; i++ {
			ok := true
			for j := 0; j < 8; j++ {
				if G[i][j] != 'R' {
					ok = false
				}
			}
			if ok {
				ans = 0
			}
		}
		for j := 0; j < 8; j++ {
			ok := true
			for i := 0; i < 8; i++ {
				if G[i][j] != 'B' {
					ok = false
				}
			}
			if ok {
				ans = 1
			}
		}
		if ans == 0 {
			fmt.Println("R")
		} else {
			fmt.Println("B")
		}
	}
}

package main

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

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

func solve(b []string) string {
	for _, x := range b {
		if x == "RRRRRRRR" {
			return "R"
		}
	}
	return "B"
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		b := make([]string, 8)
		for i := 0; i < 8; i++ {
			fmt.Fscan(reader, &b[i])
		}
		fmt.Println(solve(b))
	}
}

package main

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

const cf = true

func solve() {
	var str string
	ans := "B"
	for i := 0; i < 8; i++ {
		fmt.Fscan(in, &str)
		if !strings.Contains(str, "B") && !strings.Contains(str, ".") {
			ans = "R"
		}
	}
	fmt.Fprintln(out, ans)
}

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

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

var sc = bufio.NewScanner(os.Stdin)

const mod = 998244353
const N = 2e5

func main() {
	sc.Buffer(make([]byte, 500000*1+1), 500000)
	sc.Split(bufio.ScanWords)

	T := nextInt()
	for T > 0 {
		T--
		a := make([]string, 8)
		flag := -1
		for i := 0; i < 8; i++ {
			a[i] = nextString()
			t := true
			for j := 0; j < 8; j++ {
				if a[i][j] != 'R' {
					t = false
				}
			}
			if t {
				flag = 0
			}
		}
		if flag == 0 {
			fmt.Println("R")
		} else {
			fmt.Println("B")
		}
	}
}

func max(args ...int) int {
	res := args[0]
	for _, v := range args {
		if v > res {
			res = v
		}
	}
	return res
}

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

func nextString() string {
	sc.Scan()
	return sc.Text()
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1742C
Date: 10/21/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1742/C
Problem: CF1742C
**/
func (in *CF1742C) Run() {
  for t := in.NextInt(); t > 0; t-- {
    m := make([]string, 8)
    in.GetLine()
    for i := 0; i < 8; i++ {
      m[i] = in.NextString()
    }

    ans := uint8(0)
    for i := 0; i < 8; i++ {
      r := 0
      b := 0

      for j := 0; j < 8; j++ {
        if m[i][j] == 'R' {
          r++
        }
      }
      if r == 8 {
        ans = 'R'
        break
      }

      for j := 0; j < 8; j++ {
        if m[j][i] == 'B' {
          b++
        }
      }

      if b == 8 {
        ans = 'B'
        break
      }

    }

    fmt.Println(string(rune(ans)))
  }
}

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

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

package main

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		aa := make([]string, 8)
		for i := 0; i < 8; i++ {
			fmt.Fscan(in, &aa[i])
		}

		d := 'B'
		for i := range aa {
			var r int
			for j := 0; j < 8; j++ {
				if aa[i][j] == 'R' {
					r++
				}
			}
			if r == 8 {
				d = 'R'
			}
		}
		fmt.Fprintln(out, string(d))
	}
}

package main

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

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

func ans() {
	var n int = 8
	var grid []string = make([]string, n)
	for i := 0; i < n; i++ {
		Fscan(in, &grid[i])
	}
	for row := 0; row < n; row++ {
		var cntR int = 0
		for col := 0; col < n; col++ {
			if grid[row][col] == 'R' {
				cntR++
			}
		}
		if cntR == n {
			Fprintln(out, "R")
			return
		}
	}
	for col := 0; col < n; col++ {
		var cntB int = 0
		for row := 0; row < n; row++ {
			if grid[row][col] == 'B' {
				cntB++
			}
		}
		if cntB == n {
			Fprintln(out, "B")
			return
		}
	}
}

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

package main

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

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

func solve() {
	f := false
	var n int
	fmt.Fscan(in, &n)
	for i := 0; i < 8; i++ {
		var a string
		fmt.Fscanln(in, &a)
		if "RRRRRRRR" == a {
			f = true
		}
	}

	if f {
		fmt.Fprintln(out, "R")
		return
	}

	fmt.Fprintln(out, "B")
}

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

package main

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

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

func solve() {
	f := false
	var n int
	s := "RB"
	fmt.Fscan(in, &n)
	for i := 0; i < 8; i++ {
		var a string
		fmt.Fscanln(in, &a)
		f_t := true
		for j := 0; j < 8; j++ {
			if a[j] != s[0] {
				f_t = false
			}
		}

		if f_t {
			f = true
		}
	}

	if f {
		fmt.Fprintln(out, "R")
		return
	}

	fmt.Fprintln(out, "B")
}

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

package main

import "fmt"

func main() {
	var t int

	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		board := make([]string, 8)
		for j := 0; j < 8; j++ {
			fmt.Scan(&board[j])
		}
		for j := 0; j < 8; j++ {
			flag := true
			for k := 0; k < 8; k++ {
				if board[j][k] != 'R' {
					flag = false
					break
				}
			}
			if flag {
				fmt.Println("R")
				break
			}
		}
		for j := 0; j < 8; j++ {
			flag := true
			for k := 0; k < 8; k++ {
				if board[k][j] != 'B' {
					flag = false
					break
				}
			}
			if flag {
				fmt.Println("B")
				break
			}
		}
	}
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1742C
Date: 10/21/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1742/C
Problem: CF1742C
**/
func (in *CF1742C) Run() {
	for t := in.NextInt(); t > 0; t-- {
		m := make([]string, 8)
		in.GetLine()
		for i := 0; i < 8; i++ {
			m[i] = in.NextString()
		}

		ans := uint8(0)
		for i := 0; i < 8; i++ {
			r := 0
			b := 0

			for j := 0; j < 8; j++ {
				if m[i][j] == 'R' {
					r++
				} else if m[i][j] == 'B' {
					b++
				}
			}
			if r == 8 {
				ans = 'R'
				break
			}

			if b == 8 {
				ans = 'B'
				break
			}

			b, r = 0, 0
			for j := 0; j < 8; j++ {
				if m[j][i] == 'R' {
					r++
				} else if m[j][i] == 'B' {
					b++
				}
			}

			if r == 8 {
				ans = 'R'
				break
			}

			if b == 8 {
				ans = 'B'
				break
			}

		}

		fmt.Println(string(rune(ans)))
	}
}

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

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

package main

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

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

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

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

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

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

/**
Run solve the problem CF1742C
Date: 10/21/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1742/C
Problem: CF1742C
**/
func (in *CF1742C) Run() {
	for t := in.NextInt(); t > 0; t-- {
		m := make([]string, 8)
		in.GetLine()
		for i := 0; i < 8; i++ {
			m[i] = in.NextString()
		}

		ans := uint8(0)
		for i := 0; i < 8; i++ {
			aux := uint8(0)
			ok := true
			for j := 0; j < len(m[i]); j++ {
				if m[i][j] == '.' {
					ok = false
					break
				}
				if j == 0 {
					aux = m[i][j]
				} else {
					if aux == m[i][j] {
						ans = aux
						continue
					} else {
						ok = false
						ans = uint8(0)
						break
					}
				}
			}
			if ok {
				break
			}

			ok = true
			aux = uint8(0)
			for j := 0; j < len(m[i]); j++ {
				if m[j][i] == '.' {
					ok = false
					break
				}
				if j == 0 {
					aux = m[j][i]
				} else {
					if aux == m[j][i] {
						ans = aux
						continue
					} else {
						ok = false
						ans = uint8(0)
						break
					}
				}
			}
			if ok {
				break
			}
		}

		fmt.Println(string(rune(ans)))
	}
}

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

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

package main

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

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

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

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

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

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

/**
Run solve the problem CF1742C
Date: 10/21/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1742/C
Problem: CF1742C
**/
func (in *CF1742C) Run() {
	for t := in.NextInt(); t > 0; t-- {
		m := make([]string, 8)
		in.GetLine()
		for i := 0; i < 8; i++ {
			m[i] = in.NextString()
		}

		ans := uint8(0)
		for i := 0; i < 8; i++ {
			aux := uint8(0)
			ok := true
			for j := 0; j < len(m[i]); j++ {
				fmt.Println("m>", m[i][j])
				if m[i][j] == '.' {
					ok = false
					break
				}
				if j == 0 {
					aux = m[i][j]
				} else {
					if aux == m[i][j] {
						ans = aux
						continue
					} else {
						ok = false
						ans = uint8(0)
						break
					}
				}
			}
			if ok {
				break
			}

			ok = true
			aux = uint8(0)
			for j := 0; j < len(m[i]); j++ {
				if m[j][i] == '.' {
					ok = false
					break
				}
				if j == 0 {
					aux = m[j][i]
				} else {
					if aux == m[j][i] {
						ans = aux
						continue
					} else {
						ok = false
						ans = uint8(0)
						break
					}
				}
			}
			if ok {
				break
			}
		}

		fmt.Println(string(rune(ans)))
	}
}

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

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

package main

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

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

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

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

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

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

/**
Run solve the problem CF1742C
Date: 10/21/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1742/C
Problem: CF1742C
**/
func (in *CF1742C) Run() {
	for t := in.NextInt(); t > 0; t-- {
		m := make([]string, 8)
		in.GetLine()
		for i := 0; i < 8; i++ {
			m[i] = in.NextString()
		}

		ans := uint8(0)
		for i := 0; i < 8; i++ {
			aux := uint8(0)
			ok := true
			for j := 0; j < len(m[i]); j++ {
				if m[i][j] == '.' {
					break
				}
				if j == 0 {
					aux = m[i][j]
				} else {
					if aux == m[i][j] {
						ans = aux
						continue
					} else {
						ok = false
						ans = uint8(0)
						break
					}
				}
			}
			if ok {
				break
			}

            ok = true
			aux = uint8(0)
			for j := 0; j < len(m[i]); j++ {
				if m[j][i] == '.' {
					break
				}
				if j == 0 {
					aux = m[j][i]
				} else {
					if aux == m[j][i] {
						ans = aux
						continue
					} else {
						ok = false
						ans = uint8(0)
						break
					}
				}
			}
			if ok {
				break
			}
		}

		fmt.Println(string(rune(ans)))
	}
}

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

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

package main

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

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

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

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

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

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

/**
Run solve the problem CF1742C
Date: 10/21/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1742/C
Problem: CF1742C
**/
func (in *CF1742C) Run() {
	for t := in.NextInt(); t > 0; t-- {
		m := make([]string, 8)
		in.GetLine()
		for i := 0; i < 8; i++ {
			m[i] = in.NextString()
		}

		ans := uint8(0)
		for i := 0; i < 8; i++ {
			aux := uint8(0)
			ok := true
			for j := 0; j < len(m[i]); j++ {
				if m[i][j] == '.' {
					break
				}
				if j == 0 {
					aux = m[i][j]
				} else {
					if aux == m[i][j] {
						ans = aux
						continue
					} else {
						ok = false
						ans = uint8(0)
						break
					}
				}
			}
			if ok {
				break
			}

			aux = uint8(0)
			for j := 0; j < len(m[i]); j++ {
				if m[j][i] == '.' {
					break
				}
				if j == 0 {
					aux = m[j][i]
				} else {
					if aux == m[j][i] {
						ans = aux
						continue
					} else {
						ok = false
						ans = uint8(0)
						break
					}
				}
			}
			if ok {
				break
			}
		}

		fmt.Println(string(rune(ans)))
	}
}

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

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

package main

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

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

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

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

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

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

/**
Run solve the problem CF1742C
Date: 10/21/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1742/C
Problem: CF1742C
**/
func (in *CF1742C) Run() {
	for t := in.NextInt(); t > 0; t-- {
		m := make([]string, 8)
		in.GetLine()
		for i := 0; i < 8; i++ {
			m[i] = in.NextString()
		}

		ans := uint8(0)
		for i := 0; i < 8; i++ {
			aux := uint8(0)
			ok := true
			for j := 0; j < len(m[i]); j++ {
				if j == 0 {
					aux = m[i][j]
				} else {
					if aux == m[i][j] {
						ans = aux
						continue
					} else {
						ok = false
						ans = uint8(0)
						break
					}
				}
			}
			if ok {
				break
			}

			aux = uint8(0)
			for j := 0; j < len(m[i]); j++ {
				if j == 0 {
					aux = m[j][i]
				} else {
					if aux == m[j][i] {
						ans = aux
						continue
					} else {
						ok = false
						ans = uint8(0)
						break
					}
				}
			}
			if ok {
				break
			}
		}

		fmt.Println(string(rune(ans)))
	}
}

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

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

package main

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		aa := make([]string, 8)
		for i := 0; i < 8; i++ {
			fmt.Fscan(in, &aa[i])
		}

		var d rune
		var f bool
		for i := range aa {
			r, b := 0, 0
			for j := 0; j < 8; j++ {
				if aa[i][j] == 'R' {
					r++
				} else if aa[i][j] == 'B' {
					b++
				}
			}
			if r == 8 {
				d = 'R'
				f = true
			} else if b == '8' {
				d = 'B'
				f = true
			}
		}

		if !f {
			for i := range aa {
				r, b := 0, 0
				for j := 0; j < 8; j++ {
					if aa[j][i] == 'R' {
						r++
					} else if aa[j][i] == 'B' {
						b++
					}
				}
				if r == 8 {
					d = 'R'
				} else if b == 8 {
					d = 'B'
				}
			}
		}
		fmt.Fprintln(out, string(d))
	}
}

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++ {
		aa := make([]string, 8)
		for i := 0; i < 8; i++ {
			fmt.Fscan(in, &aa[i])
		}

		var d byte
		for i := range aa {
			r, b := 0, 0
			for j := 0; j < 8; j++ {
				if aa[i][j] == 'R' {
					r++
				} else if aa[i][j] == 'B' {
					b++
				}
			}
			if r == 8 {
				d = 'R'
			} else if b == '8' {
				d = 'B'
			}
		}

		for i := range aa {
			r, b := 0, 0
			for j := 0; j < 8; j++ {
				if aa[j][i] == 'R' {
					r++
				} else if aa[j][i] == 'B' {
					b++
				}
			}
			if r == 8 {
				d = 'R'
			} else if b == 8 {
				d = 'B'
			}
		}
		fmt.Fprintln(out, string(d))
	}
}

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++ {
		aa := make([]string, 8)
		for i := 0; i < 8; i++ {
			fmt.Fscan(in, &aa[i])
		}

		var d byte
		for i := range aa {
			if aa[i][0] == '.' {
				continue
			}

			f := true
			for j := 1; j < 8; j++ {
				if aa[i][j] != aa[i][j-1] {
					f = false
					break
				}
			}
			if f {
				d = aa[i][7]
			}
		}

		for i := range aa {
			if aa[0][i] == '.' {
				continue
			}

			f := true
			for j := 1; j < 8; j++ {
				if aa[j][i] != aa[j-1][i] {
					f = false
					break
				}
			}
			if f {
				d = aa[7][i]
			}
		}
		fmt.Fprintln(out, string(d))
	}
}

package main

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

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

func ans(cur int) {
	var n int = 8
	var grid []string = make([]string, n)
	for i := 0; i < n; i++ {
		Fscan(in, &grid[i])
	}
	if cur == 9 {
		for i := 0; i < n; i++ {
			Fprintln(out, grid[i])
		}
	}
	for row := 0; row < n; row++ {
		var cntR int = 0
		var cntB int = 0
		for col := 0; col < n; col++ {
			if grid[row][col] == 'R' {
				cntR++
			} else if grid[row][col] == 'B' {
				cntB++
			}
		}
		if cntR == n {
			Fprintln(out, "R")
			return
		}
		if cntB == n {
			Fprintln(out, "B")
			return
		}
	}
	for col := 0; col < n; col++ {
		var cntR int = 0
		var cntB int = 0
		for row := 0; row < n; row++ {
			if grid[row][col] == 'R' {
				cntR++
			} else if grid[row][col] == 'B' {
				cntB++
			}
		}
		if cntR == n {
			Fprintln(out, "R")
			return
		}
		if cntB == n {
			Fprintln(out, "B")
			return
		}
	}
}

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

package main

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

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

func ans() {
	var n int = 8
	var grid []string = make([]string, n)
	for i := 0; i < n; i++ {
		Fscan(in, &grid[i])
	}
	for row := 0; row < n; row++ {
		var cntR int = 0
		var cntB int = 0
		for col := 0; col < n; col++ {
			if grid[row][col] == 'R' {
				cntR++
			} else if grid[row][col] == 'B' {
				cntB++
			}
		}
		if cntR == n {
			Fprintln(out, "R")
			return
		}
		if cntB == n {
			Fprintln(out, "B")
			return
		}
	}
	for col := 0; col < n; col++ {
		var cntR int = 0
		var cntB int = 0
		for row := 0; row < n; row++ {
			if grid[row][col] == 'R' {
				cntR++
			} else if grid[row][col] == 'B' {
				cntB++
			}
		}
		if cntR == n {
			Fprintln(out, "R")
			return
		}
		if cntB == n {
			Fprintln(out, "B")
			return
		}
	}
}

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

package main

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

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

func solve() {
	var n int
	fmt.Fscan(in, &n)
	hashMap := make(map[byte]int)
	for i := 0; i < 8; i++ {
		var a string
		fmt.Fscanln(in, &a)
		for j := 0; j < 8; j++ {
			hashMap[a[j]]++
		}
	}

	s := "RB"
	if hashMap[s[0]] > hashMap[s[1]] {
		fmt.Fprintln(out, "R")
		return
	}

	fmt.Fprintln(out, "B")
}

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

package main

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

var in = bufio.NewReader(os.Stdin)

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

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

	for ; t > 0; t-- {
    a := make([]string, 8,8)
    var skip = false
    for i := 0; i < 8; i ++{
      var s string
      fmt.Fscan(in, &s)
      if s == "RRRRRRRR" {
        fmt.Println("R")
        skip = true
      } else if s == "BBBBBBBB" {
        fmt.Println("B")
        skip = true
      }
      a[i] = s
    }
    if skip {
      continue
    }
    for i := 0; i<8;i++ {
      s := fmt.Sprintf("%c%c%c%c%c%c%c%c",a[0][i],a[1][i],a[2][i],a[3][i],a[4][i],a[5][i],a[6][i],a[7][i])
      if s == "RRRRRRRR" {
        fmt.Println("R")
        break
      } else if s == "BBBBBBBB" {
        fmt.Println("B")
        break
      }
    }
	}
}

package main

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

var in = bufio.NewReader(os.Stdin)

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

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

	for ; t > 0; t-- {
    a := make([]int, 256,256)
    for i := 0; i < 8; i ++{
      var s string
      fmt.Fscan(in, &s)
      for _, c := range s {
        a[int(c)]++
      }
    }
    if a[int('R')] > a[int('B')] {
      fmt.Println("R")
    } else {
      fmt.Println("B")
    }
	}
}

package main

import (
	"fmt"
)

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

		// var numArr []int

		var arr [8]string
		for i := 0; i < len(arr); i++ {

			var str string
			fmt.Scan(&str)
			arr[i] = str
		}
		var res string
		res = thri(arr)
		fmt.Println(res)
		// var res string
		// res = sec(numArr)
	}

}
func thri(arr [8]string) string {
	count := 0
	for i := 0; i < len(arr); i++ {
		for j := 0; j < len(arr[i]); j++ {
			if arr[i][j] == 82 {
				count++
			}
		}
		if count != 8 {
			count = 0
		}
	}
	if count != 8 {
		return "B"
	} else {
		return "R"
	}
}

package main

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

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

func readInt() int {
	var i int
	fmt.Fscan(reader, &i)
	return i
}

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

func main(){
	defer writer.Flush()
	T := readInt()
	for i := 1; i <= T; i++{
		board := make([]string, 8)
		ans := "B"
		for r := 0; r < 8; r++ {
			board[r] = readString()
		}
		for r := 0; r < 8; r++ {
			allEq := true
			for c := 0; c < 8; c++ {
				if board[r][c] != board[r][0] {
					allEq = false
					break
				}
			}
			if allEq && board[r][0] != '.'{
				ans = string(board[r][0])
			}
		}
		for c := 0; c < 8; c++ {
			allEq := true
			for r := 0; r < 8; r++ {
				if board[r][c] != board[0][c] {
					allEq = false
					break
				}
			}
			if allEq && board[0][c] != '.'{
				ans = string(board[0][c])
			}
		}
		fmt.Fprintln(writer, ans)
	}
}

package main

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

const mod = 998244353
const mx = int64(1e9 + 7)

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	arr := make([]string, 8)
	for i := 0; i < 8; i++ {
		Fscan(in, &arr[i])
	}
	var check func(i, j int) bool
	check = func(i, j int) bool {
		v := arr[i][j]
		col := true
		row := true
		for d := 0; d < 8; d++ {
			if arr[i][d] != v {
				col = false
			}
			if arr[d][j] != v {
				row = false
			}
		}
		return col || row
	}
	check(3, 4)
	for i := 0; i < 8; i++ {
		for j := 0; j < 8; j++ {
			if arr[i][j] != '.' {
				if check(i, j) {
					Fprintln(out, string(arr[i][j]))
					return
				}
			}
		}
	}
}

func max(i, j int) int {
	if i > j {
		return i
	}
	return j
}

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

func main() {
	stdin := os.Stdin
	stdout := os.Stdout
	in := bufio.NewReader(stdin)
	out := bufio.NewWriter(stdout)
	n := 1
	Fscan(in, &n)
	for i := 0; i < n; i++ {
		solve(in, out)
	}
}

package main

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

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

func match(arr [8][8]byte) byte {
	for i := 0; i < 8; i++ {
		var row []byte
		var col []byte
		for j := 0; j < 8; j++ {
			row = append(row, arr[i][j])
			col = append(col, arr[j][i])
		}

		if string(row) == "BBBBBBBB" || string(col) == "BBBBBBBB" {
			return 'B'
		}

		if string(row) == "RRRRRRRR" || string(col) == "RRRRRRRR" {
			return 'R'
		}
	}

	return ' '
}

func main() {
	writer.Flush()
	var test int
	fmt.Fscanf(reader, "%d\n", &test)
	for test > 0 {
		var arr [8][8]byte
		fmt.Fscanf(reader, "\n")
		for i := 0; i < 8; i++ {
			for j := 0; j < 8; j++ {
				fmt.Fscanf(reader, "%c", &arr[i][j])
			}
			fmt.Fscanf(reader, "\n")
		}
		fmt.Printf("%c\n", match(arr))
		test--
	}
}

package main

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

type IO struct {
	in  io.Reader
	out io.Writer
}

func NewIO(in io.Reader, out io.Writer) *IO {
	return &IO{in: bufio.NewReader(in), out: out}
}

func (io *IO) ReadInt() int {
	var res int
	_, _ = fmt.Fscanf(io.in, "%d\n", &res)
	return res
}

func (io *IO) ReadPairInt() (int, int) {
	var a, b int
	_, _ = fmt.Fscanf(io.in, "%d %d\n", &a, &b)
	return a, b
}

func (io *IO) ReadString() string {
	var res string
	_, _ = fmt.Fscanf(io.in, "%s\n", &res)
	return res
}

func (io *IO) ReadIntSlice(n int) sort.IntSlice {
	var t int
	res := make(sort.IntSlice, 0, n)
	for i := 0; i < n; i++ {
		_, _ = fmt.Fscanf(io.in, "%d", &t)
		res = append(res, t)
	}
	_, _ = fmt.Fscanf(io.in, "\n")
	return res
}

func (io *IO) Println(v ...interface{}) {
	_, _ = fmt.Fprintln(io.out, v...)
}

func (io *IO) Print(v ...interface{}) {
	_, _ = fmt.Fprint(io.out, v...)
}

func (io *IO) Printf(format string, v ...interface{}) {
	_, _ = fmt.Fprintf(io.out, format, v...)
}

func (io *IO) PrintIntSlice(s sort.IntSlice) {
	for _, v := range s {
		io.Printf("%d ", v)
	}
}

func (io *IO) PrintlnIntSlice(s sort.IntSlice) {
	io.PrintIntSlice(s)
	io.Printf("\n")
}

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

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

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

func main() {
	io := NewIO(os.Stdin, os.Stdout)

	t := io.ReadInt()
	for i := 0; i < t; i++ {
		io.ReadString()

		done := false
		for j := 0; j < 8; j++ {
			row := io.ReadString()
			if done {
				continue
			}

			oneSizeSet := true
			for _, c := range row {
				if c != int32(row[0]) || c == '.' {
					oneSizeSet = false
				}
			}

			if oneSizeSet {
				fmt.Println("R")
				done = true
			}
		}

		if !done {
			fmt.Println("B")
		}
	}
}

package main

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

func check_row(g []string, i int, c rune) bool {
	r := true
	for _, v := range g[i] {
		if rune(v) != c {
			r = false
			break
		}
	}
	return r
}

func check_column(g []string, i int, c rune) bool {
	r := true
	for j := range g[i] {
		v := g[j][i]
		if rune(v) != c {
			r = false
			break
		}
	}
	return r
}

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-- {

		g := make([]string, 8)
		for i:= range g {
			fmt.Fscan(r, &g[i])
		}

		var res rune
		for i := range g {
			o := []rune{'R', 'B'}
			for _, col := range o {
				if check_row(g, i, col) || check_column(g, i, col) {
					res = col
					break
				}
			}
		}

		fmt.Fprintln(w, string(res))

	}

}

package main

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

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

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

	var T int
	var s string

loop:
	for Fscan(in, &T); T > 0; T-- {
		g := [8]string{}
		for i := 0; i < 8; i++ {
			Fscan(in, &s)
			g[i] = s
		}

	l1:
		for i := 0; i < 8; i++ {
			if g[i][0] == '.' {
				continue
			}
			for j := 1; j < 8; j++ {
				if g[i][j] != g[i][0] {
					continue l1
				}
			}
			Fprintln(out, string(g[i][0]))
			continue loop
		}

	l2:
		for j := 0; j < 8; j++ {
			if g[0][j] == '.' {
				continue
			}
			for i := 1; i < 8; i++ {
				if g[i][j] != g[0][j] {
					continue l2
				}
			}
			Fprintln(out, string(g[0][j]))
			continue loop
		}

		Fprintln(out, "")
	}
}

package main

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

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

	solve := func(curCase int) {
		a := make([]string, 8)
		ok := false
		for i := range a {
			Fscan(in, &a[i])
			if ok {
				continue
			}
			if a[i][0] == '.' {
				continue
			}
			if strings.Count(a[i], a[i][:1]) == 8 {
				Fprintln(out, a[i][:1])
				ok = true
			}
		}
		if ok {
			return
		}
		for j := 0; j < 8; j++ {
			if a[0][j] == '.' {
				continue
			}
			c := 0
			for i := 0; i < 8; i++ {
				if a[i][j] == a[0][j] {
					c++
				}
			}
			if c == 8 {
				Fprintln(out, string(a[0][j]))
				return
			}
		}
	}

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

	// 如果是多组数据，请务必加上这段保险 —— 已经无法统计，有多少位竞赛选手在漏读数据上损失大把分数（包括我）
	// 此外，如果不小心写成了 scan(v) 而不是 scan(&v)，由于未传入指针，不会读入任何数据，这样本地运行一次就能立马发现错误
	leftData, _ := ioutil.ReadAll(in)
	if s := strings.TrimSpace(string(leftData)); s != "" {
		panic("有未读入的数据：\n" + s)
	}

}

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


package main

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

var s string
var n int
var zeroes int
var ones int

func f(budget int) bool {
	r := 0
	ones_eaten := ones
	zeroes_left := 0
	left_ones_eaten := 0
	for l := -1; l < n; l++ {
		if l != -1 {
			if s[l] == '1' {
				ones_eaten++
				left_ones_eaten++
			} else {
				zeroes_left--
			}
		}
		if left_ones_eaten > budget {
			break
		}
		for r < n && (r <= l || ones_eaten > budget) {
			if s[r] == '1' {
				ones_eaten--
			} else {
				zeroes_left++
			}
			r++
		}
		if zeroes_left <= budget {
			// fmt.Println(ones_eaten, budget)
			return true
		}
	}
	return false
}

func main() {
	var reader *bufio.Reader = bufio.NewReader(os.Stdin)
	var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var T int
	fmt.Scan(&T)
	for t := 0; t < T; t++ {
		fmt.Fscan(reader, &s)
		n = len(s)
		zeroes = 0
		for i := 0; i < n; i++ {
			if s[i] == '0' {
				zeroes++
			}
		}
		ones = n - zeroes
		// ahhhhh can't figure out how to do it in O(n) so let's do O(n log n) w/ binary search?
		// or w/ a log(n) structure like in LCS
		// don't want to do that so let's do binary search
		l := -1
		r := ones
		// fmt.Println(l, r)
		for r-l > 1 {
			m := (l + r + 1) / 2
			can := f(m)
			if can {
				r = m
			} else {
				l = m
			}
		}
		fmt.Fprintln(writer, r)
	}
}

package main

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

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

	var T int
	var s string
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &s)
		n := len(s)
		s1 := make([]int, n+1)
		for i, b := range s {
			s1[i+1] = s1[i] + int(b&1)
		}
		Fprintln(out, sort.Search(n, func(mx int) bool {
			c, l := 0, 0
			for r, b := range s {
				c += int(b&1 ^ 1)
				for c > mx {
					c -= int(s[l]&1 ^ 1)
					l++
				}
				if s1[n]-(s1[r+1]-s1[l]) <= mx {
					return true
				}
			}
			return false
		}))
	}
}

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

package main

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

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

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

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

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

		s = strings.Trim(s, "0")
		var k0, k1 int
		for _, ch := range s {
			if ch == '1' {
				k1++
			}
		}

		tot := k1
		for i, j := 0, 0; i < len(s); i++ {
			for j < len(s) && (k0 < k1 || j < i) {
				if s[j] == '1' {
					k1--
				} else {
					k0++
				}
				j++
			}

			tot = min(tot, max(k0, k1))
			if j > i {
				if s[j-1] == '0' {
					tot = min(tot, max(k0-1, k1))
				} else {
					tot = min(tot, max(k0, k1+1))
				}
			}
			if s[i] == '1' {
				k1++
			} else {
				k0--
			}
		}
		fmt.Fprintln(out, tot)
	}
}

package main

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

func acc(op func(int64, int64) int64, c ...int64) int64 {
	res := c[0]
	for _, i := range c {
		res = op(res, i)
	}
	return res
}

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

func min(c ...int64) int64 {
	return acc(func(a int64, b int64) int64 {
		if a > b {
			return b
		}
		return a
	}, c...)
}

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

func solve(inout io.ReadWriter) {
	scan := func(inp ...interface{}) {
		fmt.Fscan(inout, inp...)
	}
	printf := func(format string, outp ...interface{}) {
		fmt.Fprintf(inout, format, outp...)
	}

	var s string
	scan(&s)

	y := 0
	for _, c := range s {
		if c == '1' {
			y++
		}
	}

	a := 0
	for i := 0; i < y; i++ {
		if s[i] == '0' {
			a++
		}
	}
	ans := a
	for i := y; i < len(s); i++ {
		if s[i] == '0' {
			a++
		}
		if s[i-y] == '0' {
			a--
		}
		if a < ans {
			ans = a
		}
	}
	printf("%d\n", ans)

}

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

	var T int = 1
	fmt.Fscan(inout, &T)
	for ; T > 0; T-- {
		solve(inout)
	}
}

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)

		pre := make([]int, len(s)+1)

		f := make([]int, 0, len(s))

		for i, ch := range s {
			pre[i+1] = pre[i]
			if ch == '0' {
				pre[i+1] += 1
			} else {
				f = append(f, pre[i+1])
			}
		}

		l := 0
		r := len(f) - 1

		for l <= r {
			tmpAns := (l + r) >> 1
			flag := false
			for i := 0; i <= tmpAns; i++ {
				if f[len(f)-1-(tmpAns-i)]-f[i] <= tmpAns {
					flag = true
				}
			}
			if flag {
				r = tmpAns - 1
			} else {
				l = tmpAns + 1
			}
		}

		Fprintln(out, r+1)

	}

}

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

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

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

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

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

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

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

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

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

func doNothing(m Int) Int {
	return m
}

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

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

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

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

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

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

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

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

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

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

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

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

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

package main

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

func main_edu_128_C() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		var s string
		Fscan(in, &s)
		var n = len(s)
		var wins, c1 int
		for i := 0; i < n; i++ {
			if s[i] == '1' {
				wins++
			}
		}
		var ans = int(1e9)
		for i, j := 0, 0; i < n; i++ {
			if s[i] == '1' {
				c1++
			}
			for i-j+1 > wins {
				if s[j] == '1' {
					c1--
				}
				j++
			}
			if i-j+1 == wins {
				if wins-c1 < ans {
					ans = wins - c1
				}
			}
		}
		Fprintln(out, ans)
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() {
	main_edu_128_C()
}

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 main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var niter int
	fmt.Fscan(in, &niter)
	for iii := 0; iii < niter; iii++ {
		var s string
		fmt.Fscan(in, &s)
		b := []byte(s)
		n := len(s)
		del1l := make([]int, n+1)
		del0l := make([]int, n+1)
		del1r := make([]int, n+1)
		del0r := make([]int, n+1)
		c0, c1 := 0, 0
		for i := 0; i < n; i++ {
			if b[i] == '1' {
				c1++
			} else {
				c0++
			}
			del0l[i+1] = c0
			del1l[i+1] = c1
		}
		c0, c1 = 0, 0
		for i := n - 1; i >= 0; i-- {
			if b[i] == '1' {
				c1++
			} else {
				c0++
			}
			del0r[n-i] = c0
			del1r[n-i] = c1
		}
		cs := del0r[n] // cs
		//bs := make([]int, n+1)
		m := 999999
		for i := 0; i <= n; i++ {
			c1 := del1l[i]
			c2 := del0l[i]
			j := cs - c1 - c2
			if i+j <= n && j >= 0 {
				m = min(m, max(del1l[i]+del1r[j], cs-del0l[i]-del0r[j]))
			}
			// if j >= 0 {
			// 	fmt.Println("!!!!", i, j, cs, c1, c2)
			// 	fmt.Println("!!!!", del1l[i]+del1r[j], cs-del0l[i]-del0r[j])
			// 	fmt.Println("!!", del0l, del0r, del1l, del1r)
			// }
		}
		fmt.Fprintln(out, m)
	}
}

package main

import "fmt"

func main() {
	var test int
	fmt.Scanln(&test)

	for test > 0 {
		test--

		var str string
		fmt.Scanln(&str)

		fmt.Println(solve(str))
	}
}

func solve(str string) int {
	ones := []int{}
	for i, ch := range str {
		if ch == '1' {
			ones = append(ones, i)
		}
	}

	res := len(ones)
	l := 0
	r := len(ones)
	for l <= r {
		m := l + (r-l)/2
		if possible(&ones, m) {
			res = m
			r = m - 1
		} else {
			l = m + 1
		}
	}

	return res
}

func possible(ones *[]int, k int) bool {
	if len(*ones) == 0 {
		return true
	}

	oneLeft := len(*ones) - k
	for i := 0; i < len(*ones)-oneLeft+1; i++ {
		zeroInRange := (*ones)[i+oneLeft-1] - (*ones)[i] - oneLeft + 1
		if zeroInRange <= k {
			return true
		}
	}
	return false
}

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)

		for len(s) > 0 && s[len(s)-1] == '0' {
			s = s[0 : len(s)-1]
		}

		for len(s) > 0 && s[0] == '0' {
			s = s[1:]
		}

		left := L0
		for _, ch := range s {
			if ch == '0' {
				left++
			}
		}
		ans := left

		a := make([]Int, len(s)+10)
		b := make([]Int, len(s)+10)
		c := make([]Int, len(s)+10)

		r := Int(len(s) - 1)
		add := L0
		del := L0
		cnt := L0

		a[cnt] = add
		b[cnt] = del
		c[cnt] = r
		cnt++

		for r >= 0 {
			for r >= 0 && s[r] == '1' {
				add++
				r--
			}
			for r >= 0 && s[r] == '0' {
				del++
				r--
			}
			a[cnt] = add
			b[cnt] = del
			c[cnt] = r
			cnt++
		}

		l := L0
		tmpDel := L0
		r = L0

		for r+1 < cnt && Max(tmpDel+a[r+1], left-b[r+1]) < left {
			r++
		}

		ans = Min(ans, Max(tmpDel+a[r], left-b[r]))

		for l < Int(len(s)) {
			for l < Int(len(s)) && s[l] == '1' {
				l++
				tmpDel++
			}
			for l < Int(len(s)) && s[l] == '0' {
				l++
				left--
			}

			for r > 0 && c[r] < l {
				r--
			}

			for r > 0 && Max(tmpDel+a[r-1], left-b[r-1]) <= Max(tmpDel+a[r], left-b[r]) {
				r--
			}
			ans = Min(ans, Max(tmpDel+a[r], left-b[r]))

			if tmpDel > left {
				break
			}

		}

		Fprintln(out, ans)

	}

}

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

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

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

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

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

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

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

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

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

func doNothing(m Int) Int {
	return m
}

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

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

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

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

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

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

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

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

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

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

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

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

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

package main

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

type Int int64

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

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

	var T int
	Fscan(in, &T)

	for tt := 0; tt < T; tt++ {
		var s string
		Fscan(in, &s)

		for len(s) > 0 && s[len(s)-1] == '0' {
			s = s[0 : len(s)-1]
		}

		for len(s) > 0 && s[0] == '0' {
			s = s[1:]
		}

		left := L0
		for _, ch := range s {
			if ch == '0' {
				left++
			}
		}
		ans := left

		a := make([]Int, len(s)+10)
		b := make([]Int, len(s)+10)
		c := make([]Int, len(s)+10)

		r := Int(len(s) - 1)
		add := L0
		del := L0
		cnt := L0

		a[cnt] = add
		b[cnt] = del
		c[cnt] = r
		cnt++

		for r >= 0 {
			for r >= 0 && s[r] == '1' {
				add++
				r--
			}
			for r >= 0 && s[r] == '0' {
				del++
				r--
			}
			a[cnt] = add
			b[cnt] = del
			c[cnt] = r
			cnt++
		}

		l := L0
		tmpDel := L0
		r = L0

		for r+1 < cnt && tmpDel+a[r+1] <= left-b[r+1] {
			r++
		}

		ans = Min(ans, Max(tmpDel+a[r], left-b[r]))

		for l < Int(len(s)) {
			for l < Int(len(s)) && s[l] == '1' {
				l++
				tmpDel++
			}
			for l < Int(len(s)) && s[l] == '0' {
				l++
				left--
			}

			for r > 0 && c[r] < l {
				r--
			}

			for r > 0 && tmpDel+a[r] > left-b[r] {
				r--
			}
			ans = Min(ans, Max(tmpDel+a[r], left-b[r]))

			if tmpDel > left {
				break
			}

		}

		Fprintln(out, ans)

	}

}

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

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

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

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

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

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

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

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

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

func doNothing(m Int) Int {
	return m
}

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

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

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

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

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

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

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

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

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

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

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

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

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

package main

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

type Int int64

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

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

	var T int
	Fscan(in, &T)

	for tt := 0; tt < T; tt++ {
		var s string
		Fscan(in, &s)

		for len(s) > 0 && s[len(s)-1] == '0' {
			s = s[0 : len(s)-1]
		}

		for len(s) > 0 && s[0] == '0' {
			s = s[1:]
		}

		left := L0
		for _, ch := range s {
			if ch == '0' {
				left++
			}
		}
		ans := left

		a := make([]Int, len(s)+10)
		b := make([]Int, len(s)+10)
		c := make([]Int, len(s)+10)

		r := Int(len(s) - 1)
		add := L0
		del := L0
		cnt := L0

		a[cnt] = add
		b[cnt] = del
		c[cnt] = r
		cnt++

		for r >= 0 {
			for r >= 0 && s[r] == '1' {
				add++
				r--
			}
			for r >= 0 && s[r] == '0' {
				del++
				r--
			}
			a[cnt] = add
			b[cnt] = del
			c[cnt] = r
			cnt++
		}

		l := L0

		tmpDel := L0

		r = L0
		for r+1 < cnt && tmpDel+a[r+1] <= left-b[r] {
			r++
		}

		ans = Min(ans, Max(tmpDel+a[r], left-b[r]))

		for l < Int(len(s)) {
			for l < Int(len(s)) && s[l] == '1' {
				l++
				tmpDel++
			}
			for l < Int(len(s)) && s[l] == '0' {
				l++
				left--
			}

			for r > 0 && c[r] < l {
				r--
			}

			for r > 0 && tmpDel+a[r] > left-b[r] {
				r--
			}
			ans = Min(ans, Max(tmpDel+a[r], left-b[r]))

			if tmpDel > left {
				break
			}

		}

		Fprintln(out, ans)

	}

}

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

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

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

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

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

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

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

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

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

func doNothing(m Int) Int {
	return m
}

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

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

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

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

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

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

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

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

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

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

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

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

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

package main

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

type Int int64

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

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

	var T int
	Fscan(in, &T)

	for tt := 0; tt < T; tt++ {
		var s string
		Fscan(in, &s)

		for len(s) > 0 && s[len(s)-1] == '0' {
			s = s[0 : len(s)-1]
		}

		for len(s) > 0 && s[0] == '0' {
			s = s[1:]
		}

		left := L0
		for _, ch := range s {
			if ch == '0' {
				left++
			}
		}
		ans := left

		a := make([]Int, 0, 100000)
		b := make([]Int, 0, 100000)

		r := Int(len(s) - 1)
		add := L0
		del := L0

		a = append(a, add)
		b = append(b, del)

		for r >= 0 {
			for r >= 0 && s[r] == '1' {
				add++
				r--
			}
			for r >= 0 && s[r] == '0' {
				del++
				r--
			}
			a = append(a, add)
			b = append(b, del)

		}

		l := L0

		tmpDel := L0

		r = L0
		for r+1 < Int(len(a)) && tmpDel+a[r+1] <= left-b[r] {
			r++
		}

		ans = Min(ans, Max(tmpDel+a[r], left-b[r]))

		for l < Int(len(s)) {
			for l < Int(len(s)) && s[l] == '1' {
				l++
				tmpDel++
			}
			for l < Int(len(s)) && s[l] == '0' {
				l++
				left--
			}

			for r > 0 && tmpDel+a[r] > left-b[r] {
				r--
			}
			ans = Min(ans, Max(tmpDel+a[r], left-b[r]))

			if tmpDel > left {
				break
			}

		}

		Fprintln(out, ans)

	}

}

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

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

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

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

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

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

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

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

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

func doNothing(m Int) Int {
	return m
}

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

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

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

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

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

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

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

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

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

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

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

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

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

package main

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

type Int int64

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

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

	var T int
	Fscan(in, &T)

	for tt := 0; tt < T; tt++ {
		var s string
		Fscan(in, &s)

		for len(s) > 0 && s[len(s)-1] == '0' {
			s = s[0 : len(s)-1]
		}

		for len(s) > 0 && s[0] == '0' {
			s = s[1:]
		}

		left := L0
		for _, ch := range s {
			if ch == '0' {
				left++
			}
		}
		ans := left

		a := make([]Int, 0, 100000)
		b := make([]Int, 0, 100000)

		r := Int(len(s) - 1)
		add := L0
		del := L0

		a = append(a, add)
		b = append(b, del)

		for r >= 0 {
			for r >= 0 && s[r] == '1' {
				add++
				r--
			}
			for r >= 0 && s[r] == '0' {
				del++
				r--
			}
			a = append(a, add)
			b = append(b, del)

		}

		l := L0

		tmpDel := L0

		r = L0
		for r+1 < Int(len(a)) && tmpDel+a[r+1] <= left-b[r] {
			r++
		}

		ans = Min(ans, Max(tmpDel+a[r], left-b[r]))

		for l < Int(len(s)) {
			for l < Int(len(s)) && s[l] == '1' {
				l++
				tmpDel++
			}
			for l < Int(len(s)) && s[l] == '0' {
				l++
				left--
			}

			for r > 0 && tmpDel+a[r] > left-b[r] {
				r--
			}
			ans = Min(ans, Max(tmpDel+a[r], left-b[r]))

		}

		Fprintln(out, ans)

	}

}

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

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

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

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

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

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

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

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

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

func doNothing(m Int) Int {
	return m
}

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

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

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

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

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

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

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

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

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

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

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

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

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

package main

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

type Int int64

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

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

	var T int
	Fscan(in, &T)

	for tt := 0; tt < T; tt++ {
		var s string
		Fscan(in, &s)

		for len(s) > 0 && s[len(s)-1] == '0' {
			s = s[0 : len(s)-1]
		}

		for len(s) > 0 && s[0] == '0' {
			s = s[1:]
		}

		left := 0
		for _, ch := range s {
			if ch == '0' {
				left++
			}
		}

		l := 0
		ans1 := 0
		left1 := left
		for l < len(s) {
			if s[l] == '1' {
				if ans1 < left1 {
					ans1++
					l++
				} else {
					break
				}
			}
			for l < len(s) && s[l] == '0' {
				l++
				left1--
			}
		}

		ans2 := 0
		left2 := left
		r := len(s) - 1
		for r >= 0 {
			if s[r] == '1' {
				if ans2 < left2 {
					ans2++
					r--
				} else {
					break
				}
			}
			for r >= 0 && s[r] == '0' {
				r--
				left2--
			}
		}

		Fprintln(out, Min(Max(Int(ans1), Int(left1)), Max(Int(ans2), Int(left2))))

	}

}

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

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

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

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

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

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

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

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

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

func doNothing(m Int) Int {
	return m
}

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

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

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

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

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

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

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

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

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

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

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

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

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

package main

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

type Int int64

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

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

	var T int
	Fscan(in, &T)

	for tt := 0; tt < T; tt++ {
		var s string
		Fscan(in, &s)

		for len(s) > 0 && s[len(s)-1] == '0' {
			s = s[0 : len(s)-1]
		}

		for len(s) > 0 && s[0] == '0' {
			s = s[1:]
		}

		left := 0
		for _, ch := range s {
			if ch == '0' {
				left++
			}
		}

		l := 0
		ans1 := 0
		left1 := left
		for l < len(s) {
			if s[l] == '1' {
				if ans1 < left1 {
					ans1++
					l++
				} else {
					break
				}
			}
			for l < len(s) && s[l] == '0' {
				l++
				left1--
			}
		}

		ans2 := 0
		left2 := left
		r := len(s) - 1
		for r >= 0 {
			if s[r] == '1' {
				if ans1 < left2 {
					ans2++
					r--
				} else {
					break
				}
			}
			for r >= 0 && s[r] == '0' {
				r--
				left2--
			}
		}

		Fprintln(out, Min(Max(Int(ans1), Int(left1)), Max(Int(ans2), Int(left2))))

	}

}

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

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

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

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

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

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

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

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

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

func doNothing(m Int) Int {
	return m
}

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

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

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

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

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

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

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

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

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

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

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

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

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

package main

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

type Int int64

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

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

	var T int
	Fscan(in, &T)

	for tt := 0; tt < T; tt++ {
		var s string
		Fscan(in, &s)

		for len(s) > 0 && s[len(s)-1] == '0' {
			s = s[0 : len(s)-1]
		}

		for len(s) > 0 && s[0] == '0' {
			s = s[1:]
		}

		ans := 0

		left := 0
		for _, ch := range s {
			if ch == '0' {
				left++
			}
		}

		l := 0
		for l < len(s) {
			if s[l] == '1' {
				if ans < left {
					ans++
					l++
				} else {
					break
				}
			}
			for l < len(s) && s[l] == '0' {
				l++
				left--
			}
		}
		Fprintln(out, Max(Int(ans), Int(left)))

	}

}

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

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

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

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

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

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

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

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

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

func doNothing(m Int) Int {
	return m
}

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

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

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

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

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

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

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

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

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

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

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

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

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

package main

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

func main_edu_128_A() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		/* var l1, l2, r1, r2 int
		Fscan(in, &l1, &r1, &l2, &r2)
		if l1 > l2 {
			l1, r1, l2, r2 = l2, r2, l1, r1
		}
		if l1 == l2 {
			Fprintln(out, l1)
			return
		}
		if r1 < l2 {
			Fprintln(out, l1+l2)
			return
		}
		Fprintln(out, l2) */
		/* var n, m, col int
		col = -1
		Fscan(in, &n, &m)
		a := make([]string, n)
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
		}
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				if a[i][j] == 'R' {
					col = j
					break
				}
			}
			if col != -1{
				break
			}
		}
		for i := 0; i < n; i++ {
			for j := 0; j < col; j++ {
				if a[i][j] == 'R' {
				//	Fprintln(out, ":", i, j, col)
					Fprintln(out, "NO")
					return
				}
			}
		}
		Fprintln(out, "YES") */
		var s string
		Fscan(in, &s)
		var n = len(s)
		var zero, one int
		for i := 0; i < n; i++ {
			if s[i] == '0' {
				zero++
			}
		}

		/* var max func(a, b int) int
		max = func(a, b int) int {
			if a > b {
				return a
			}
			return b
		} */
		l, r := 0, n-1
		for l <= r && zero > one {
			for l <= r && s[l] == '0' && zero > one {
				l++
				zero--
			}
			for l <= r && s[r] == '0' && zero > one {
				r--
				zero--
			}
			if one >= zero {
				break
			}
			if l <= r {
				L, R := 0, 0
				for i := l + 1; i <= r && s[i] == '0'; i++ {
					L++
				}
				for j := r - 1; j >= l && s[j] == '0'; j-- {
					R++
				}
				if L >= R {
					l++
				} else {
					r--
				}
				one++
			}
		}
		Fprintln(out, zero)
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() {
	main_edu_128_A()
}

package main

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

func main_edu_128_A() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		/* var l1, l2, r1, r2 int
		Fscan(in, &l1, &r1, &l2, &r2)
		if l1 > l2 {
			l1, r1, l2, r2 = l2, r2, l1, r1
		}
		if l1 == l2 {
			Fprintln(out, l1)
			return
		}
		if r1 < l2 {
			Fprintln(out, l1+l2)
			return
		}
		Fprintln(out, l2) */
		/* var n, m, col int
		col = -1
		Fscan(in, &n, &m)
		a := make([]string, n)
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
		}
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				if a[i][j] == 'R' {
					col = j
					break
				}
			}
			if col != -1{
				break
			}
		}
		for i := 0; i < n; i++ {
			for j := 0; j < col; j++ {
				if a[i][j] == 'R' {
				//	Fprintln(out, ":", i, j, col)
					Fprintln(out, "NO")
					return
				}
			}
		}
		Fprintln(out, "YES") */
		var s string
		Fscan(in, &s)
		var n = len(s)
		var zero, one int
		for i := 0; i < n; i++ {
			if s[i] == '0' {
				zero++
			}
		}

		var max func(a, b int) int
		max = func(a, b int) int {
			if a > b {
				return a
			}
			return b
		}
		l, r := 0, n-1
		for l <= r {
			for l <= r && s[l] == '0' && zero > one {
				l++
				zero--
			}
			for l <= r && s[r] == '0' && zero > one {
				r--
				zero--
			}
			if one >= zero {
				break
			}
			if l <= r {
				L, R := 0, 0
				for i := l + 1; s[i] == '0'; i++ {
					L++
				}
				for j := r - 1; s[j] == '0'; j-- {
					R++
				}
				if L >= R {
					l++
				} else {
					r--
				}
				one++
			}
		}
		Fprintln(out, max(zero, one))
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() {
	main_edu_128_A()
}

package main

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

func main_edu_128_A() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		/* var l1, l2, r1, r2 int
		Fscan(in, &l1, &r1, &l2, &r2)
		if l1 > l2 {
			l1, r1, l2, r2 = l2, r2, l1, r1
		}
		if l1 == l2 {
			Fprintln(out, l1)
			return
		}
		if r1 < l2 {
			Fprintln(out, l1+l2)
			return
		}
		Fprintln(out, l2) */
		/* var n, m, col int
		col = -1
		Fscan(in, &n, &m)
		a := make([]string, n)
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
		}
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				if a[i][j] == 'R' {
					col = j
					break
				}
			}
			if col != -1{
				break
			}
		}
		for i := 0; i < n; i++ {
			for j := 0; j < col; j++ {
				if a[i][j] == 'R' {
				//	Fprintln(out, ":", i, j, col)
					Fprintln(out, "NO")
					return
				}
			}
		}
		Fprintln(out, "YES") */
		var s string
		Fscan(in, &s)
		var n = len(s)
		var zero, one int
		for i := 0; i < n; i++ {
			if s[i] == '0' {
				zero++
			}
		}

		var max func(a, b int) int
		max = func(a, b int) int {
			if a > b {
				return a
			}
			return b
		}
		l, r := 0, n-1
		for l <= r {
			for l <= r && s[l] == '0' && zero > one {
				l++
				zero--
			}
			for l <= r && s[r] == '0' && zero > one {
				r--
				zero--
			}
			if one >= zero {
				break
			}
			if l <= r {
				L, R := 0, 0
				for i := l + 1; s[i] == '0'; i++ {
					L++
				}
				for j := r - 1; s[j] == '-'; j-- {
					R++
				}
				if L >= R {
					l++
				} else {
					r--
				}
				one++
			}
		}
		Fprintln(out, max(zero, one))
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() {
	main_edu_128_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 main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var niter int
	fmt.Fscan(in, &niter)
	for iii := 0; iii < niter; iii++ {
		var s string
		fmt.Fscan(in, &s)
		b := []byte(s)
		n := len(s)
		del1l := make([]int, n+1)
		del0l := make([]int, n+1)
		del1r := make([]int, n+1)
		del0r := make([]int, n+1)
		c0, c1 := 0, 0
		for i := 0; i < n; i++ {
			if b[i] == '1' {
				c1++
			} else {
				c0++
			}
			del0l[i+1] = c0
			del1l[i+1] = c1
		}
		c0, c1 = 0, 0
		for i := n - 1; i >= 0; i-- {
			if b[i] == '1' {
				c1++
			} else {
				c0++
			}
			del0r[n-i] = c0
			del1r[n-i] = c1
		}
		cs := del0r[n] // cs
		//bs := make([]int, n+1)
		m := 999999
		for i := 0; i <= n; i++ {
			c1 := del1l[i]
			c2 := del0l[i]
			j := cs - c1 - c2
			if i+j <= n && j >= 0 {
				m = min(m, max(del1l[i]+del1r[j], j))
			}
			//fmt.Println("!!!!", cs, i, j, i+j)
		}
		fmt.Fprintln(out, m)
	}
}

package main

import "fmt"

func main() {
	var test int
	fmt.Scanln(&test)

	for test > 0 {
		test--

		var str string
		fmt.Scanln(&str)

		fmt.Println(solve(str))
	}
}

func solve(str string) int {
	ones := []int{}
	for i, ch := range str {
		if ch == '1' {
			ones = append(ones, i)
		}
	}

	res := len(ones)
	l := 0
	r := len(ones)
	for l <= r {
		m := l + (r-l)/2
		if possible(&ones, m) {
			res = m
			r = m - 1
		} else {
			l = m + 1
		}
	}

	return res
}

func possible(ones *[]int, k int) bool {
	if len(*ones) == 0 {
		return true
	}

	oneLeft := len(*ones) - k
	for i := 0; i < len(*ones)-oneLeft+1; i++ {
		zeroInRange := (*ones)[i+oneLeft-1] - (*ones)[i] - oneLeft
		if zeroInRange <= k {
			return true
		}
	}
	return false
}

package main

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

var s string
var n int
var zeroes int
var ones int

func f(budget int) bool {
	if zeroes <= budget {
		return true
	}
	r := 0
	ones_eaten := ones
	zeroes_left := 0
	left_ones_eaten := 0
	for l := 0; l < n; l++ {
		if s[l] == '1' {
			ones_eaten++
			left_ones_eaten++
		} else {
			zeroes_left--
		}
		if left_ones_eaten > budget {
			break
		}
		for r < n && (r <= l || ones_eaten > budget) {
			if s[r] == '1' {
				ones_eaten--
			} else {
				zeroes_left++
			}
			r++
		}
		if zeroes_left <= budget {
			// fmt.Println(ones_eaten, budget)
			return true
		}
	}
	return false
}

func main() {
	var reader *bufio.Reader = bufio.NewReader(os.Stdin)
	var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var T int
	fmt.Scan(&T)
	for t := 0; t < T; t++ {
		fmt.Fscan(reader, &s)
		n = len(s)
		zeroes = 0
		for i := 0; i < n; i++ {
			if s[i] == '0' {
				zeroes++
			}
		}
		ones = n - zeroes
		// ahhhhh can't figure out how to do it in O(n) so let's do O(n log n) w/ binary search?
		// or w/ a log(n) structure like in LCS
		// don't want to do that so let's do binary search
		l := -1
		r := ones
		// fmt.Println(l, r)
		for r-l > 1 {
			m := (l + r + 1) / 2
			can := f(m)
			if can {
				r = m
			} else {
				l = m
			}
		}
		fmt.Fprintln(writer, r)
	}
}