package main

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

// github.com/EndlessCheng/codeforces-go
func CF1253D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, m, v, w, ans int
	Fscan(in, &n, &m)
	fa := make([]int, n+1)
	for i := range fa {
		fa[i] = i
	}
	var f func(int) int
	f = func(x int) int {
		if fa[x] != x {
			fa[x] = f(fa[x])
		}
		return fa[x]
	}
	merge := func(from, to int) int {
		ff, ft := f(from), f(to)
		if ff > ft {
			ff, ft = ft, ff
		}
		fa[ff] = ft
		return ft
	}

	for ; m > 0; m-- {
		Fscan(in, &v, &w)
		merge(v, w)
	}
	end := -1
	for i := range fa {
		if i > end {
			end = f(i)
		} else if f(i) != end {
			end = merge(i, end)
			ans++
		}
	}
	Fprint(out, ans)
}

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

package main

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

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	s := 0

	for _, i := range integers {
		s += i
	}

	return s
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		// str := strconv.FormatInt(A[i], 10)  // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var n, m int
var G [200000 + 5][]int
var colors [200000 + 5]int
var left, right int

func main() {
	n, m = ReadInt2()

	for i := 0; i < m; i++ {
		x, y := ReadInt2()
		x--
		y--
		G[x] = append(G[x], y)
		G[y] = append(G[y], x)
	}

	L := make(ComponentList, 0)
	for i := 0; i < n; i++ {
		colors[i] = -1
	}
	for i := 0; i < n; i++ {
		if colors[i] == -1 {
			left, right = i, i
			dfs(i, i)
			L = append(L, &Component{key: left, l: left, r: right})
		}
	}
	// sort.Stable(byKey{L})

	ans := 0
	biggest := L[0].r
	for i := 1; i < len(L); i++ {
		if L[i].l <= biggest {
			ans++
			ChMax(&biggest, L[i].r)
		} else {
			biggest = L[i].r
		}
	}

	fmt.Println(ans)
}

func dfs(i, c int) {
	colors[i] = c

	for _, nid := range G[i] {
		if colors[nid] == -1 {
			ChMin(&left, nid)
			ChMax(&right, nid)
			dfs(nid, c)
		}
	}
}

type Component struct {
	key  int
	l, r int
}
type ComponentList []*Component
type byKey struct {
	ComponentList
}

func (l ComponentList) Len() int {
	return len(l)
}
func (l ComponentList) Swap(i, j int) {
	l[i], l[j] = l[j], l[i]
}

func (l byKey) Less(i, j int) bool {
	return l.ComponentList[i].key < l.ComponentList[j].key
}

// how to use
// L := make(ComponentList, 0, 200000+5)
// L = append(L, &Component{key: intValue})
// sort.Stable(byKey{ L })                // Stable ASC
// sort.Stable(sort.Reverse(byKey{ L }))  // Stable DESC

// MODはとったか？
// 遷移だけじゃなくて最後の最後でちゃんと取れよ？

/*******************************************************************/

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"sort"
	"strconv"
)

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	s := 0

	for _, i := range integers {
		s += i
	}

	return s
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		// str := strconv.FormatInt(A[i], 10)  // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var n, m int
var G [200000 + 5][]int
var colors [200000 + 5]int
var left, right int

func main() {
	n, m = ReadInt2()

	for i := 0; i < m; i++ {
		x, y := ReadInt2()
		x--
		y--
		G[x] = append(G[x], y)
		G[y] = append(G[y], x)
	}

	L := make(ComponentList, 0)
	for i := 0; i < n; i++ {
		colors[i] = -1
	}
	for i := 0; i < n; i++ {
		if colors[i] == -1 {
			left, right = i, i
			dfs(i, i)
			L = append(L, &Component{key: left, l: left, r: right})
		}
	}
	sort.Stable(byKey{L})

	ans := 0
	biggest := L[0].r
	for i := 1; i < len(L); i++ {
		if L[i].l <= biggest {
			ans++
			ChMax(&biggest, L[i].r)
		} else {
			biggest = L[i].r
		}
	}

	fmt.Println(ans)
}

func dfs(i, c int) {
	colors[i] = c

	for _, nid := range G[i] {
		if colors[nid] == -1 {
			ChMin(&left, nid)
			ChMax(&right, nid)
			dfs(nid, c)
		}
	}
}

type Component struct {
	key  int
	l, r int
}
type ComponentList []*Component
type byKey struct {
	ComponentList
}

func (l ComponentList) Len() int {
	return len(l)
}
func (l ComponentList) Swap(i, j int) {
	l[i], l[j] = l[j], l[i]
}

func (l byKey) Less(i, j int) bool {
	return l.ComponentList[i].key < l.ComponentList[j].key
}

// how to use
// L := make(ComponentList, 0, 200000+5)
// L = append(L, &Component{key: intValue})
// sort.Stable(byKey{ L })                // Stable ASC
// sort.Stable(sort.Reverse(byKey{ L }))  // Stable DESC

// MODはとったか？
// 遷移だけじゃなくて最後の最後でちゃんと取れよ？

/*******************************************************************/

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"sort"
	"strconv"
)

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	s := 0

	for _, i := range integers {
		s += i
	}

	return s
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		// str := strconv.FormatInt(A[i], 10)  // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var n, m int

type Component struct {
	key  int
	l, r int
}
type ComponentList []*Component
type byKey struct {
	ComponentList
}

func (l ComponentList) Len() int {
	return len(l)
}
func (l ComponentList) Swap(i, j int) {
	l[i], l[j] = l[j], l[i]
}

func (l byKey) Less(i, j int) bool {
	return l.ComponentList[i].key < l.ComponentList[j].key
}

// how to use
// L := make(ComponentList, 0, 200000+5)
// L = append(L, &Component{key: intValue})
// sort.Stable(byKey{ L })                // Stable ASC
// sort.Stable(sort.Reverse(byKey{ L }))  // Stable DESC

func main() {
	n, m = ReadInt2()

	uf := NewUnionFind(n)
	for i := 0; i < m; i++ {
		x, y := ReadInt2()
		x--
		y--
		uf.Unite(x, y)
	}

	memo := make(map[int]Component)
	for i := 0; i < n; i++ {
		// p := uf.parents[i]
		p := uf.Root(i)
		if _, ok := memo[p]; ok {
			c := memo[p]
			c.r = i
			memo[p] = c
		} else {
			memo[p] = Component{key: i, l: i, r: i}
		}
	}
	// fmt.Println(len(memo))

	L := make(ComponentList, 0)
	for _, v := range memo {
		// fmt.Printf("left: %d, right: %d\n", v.l, v.r)
		newC := &Component{key: v.key, l: v.l, r: v.r}
		// L = append(L, &v)
		L = append(L, newC)
	}
	sort.Stable(byKey{L})
	// for i := 0; i < len(L); i++ {
	// 	fmt.Printf("left: %d, right: %d\n", L[i].l, L[i].r)
	// }

	ans := 0
	biggest := L[0].r
	for i := 1; i < len(L); i++ {
		if L[i].l <= biggest {
			ans++
			ChMax(&biggest, L[i].r)
		} else {
			biggest = L[i].r
		}
	}

	fmt.Println(ans)
}

// UnionFind provides disjoint set algorithm.
// It accepts both 0-based and 1-based setting.
type UnionFind struct {
	parents []int
}

// NewUnionFind returns a pointer of a new instance of UnionFind.
func NewUnionFind(n int) *UnionFind {
	uf := new(UnionFind)
	uf.parents = make([]int, n+1)

	for i := 0; i <= n; i++ {
		uf.parents[i] = -1
	}

	return uf
}

// Root method returns root node of an argument node.
// Root method is a recursive function.
func (uf *UnionFind) Root(x int) int {
	if uf.parents[x] < 0 {
		return x
	}

	// route compression
	uf.parents[x] = uf.Root(uf.parents[x])
	return uf.parents[x]
}

// Unite method merges a set including x and a set including y.
func (uf *UnionFind) Unite(x, y int) bool {
	xp := uf.Root(x)
	yp := uf.Root(y)

	if xp == yp {
		return false
	}

	// merge: xp -> yp
	// merge larger set to smaller set
	if uf.CcSize(xp) > uf.CcSize(yp) {
		xp, yp = yp, xp
	}
	// update set size
	uf.parents[yp] += uf.parents[xp]
	// finally, merge
	uf.parents[xp] = yp

	return true
}

// Same method returns whether x is in the set including y or not.
func (uf *UnionFind) Same(x, y int) bool {
	return uf.Root(x) == uf.Root(y)
}

// CcSize method returns the size of a set including an argument node.
func (uf *UnionFind) CcSize(x int) int {
	return -uf.parents[uf.Root(x)]
}

// MODはとったか？
// 遷移だけじゃなくて最後の最後でちゃんと取れよ？

/*******************************************************************/

package main

import (
	. "fmt"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	buf := make([]byte, 4096)
	_i := len(buf)
	rc := func() byte {
		if _i == len(buf) {
			os.Stdin.Read(buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	r := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}

	fa := make([]int, r()+1)
	for i := range fa {
		fa[i] = i
	}
	var f func(int) int
	f = func(x int) int {
		if fa[x] != x {
			fa[x] = f(fa[x])
		}
		return fa[x]
	}
	merge := func(from, to int) int {
		ff, ft := f(from), f(to)
		if ff > ft {
			ff, ft = ft, ff
		}
		fa[ff] = ft
		return ft
	}

	for m := r(); m > 0; m-- {
		merge(r(), r())
	}
	ans, end := 0, -1
	for i := range fa {
		if i > end {
			end = f(i)
		} else if f(i) != end {
			end = merge(i, end)
			ans++
		}
	}
	Print(ans)
}

package main

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

var visited map[int]bool
var min, max, cnt int
var edges map[int][]int

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	q := 1
	//fmt.Fscanf(in, "%d\n", &q)
	edges = make(map[int][]int)
	for ii := 0; ii < q; ii++ {
		n, m := 0, 0
		fmt.Fscanf(in, "%d %d\n", &n, &m)
		for i := 0; i < m; i++ {
			a, b := 0, 0
			fmt.Fscanf(in, "%d %d\n", &a, &b)
			edges[a] = append(edges[a], b)
			edges[b] = append(edges[b], a)
		}
		visited = make(map[int]bool)
		ans := 0
		//fmt.Println(edges)
		a, b := []int{}, []int{}
		for i := 0; i <= n; i++ {
			if !visited[i] {
				min = i
				max = i
				cnt = 0
				dfs(i)
				a = append(a, min)
				b = append(b, max)
			}
		}
		curMax := b[0]
		for i := 1; i < len(a); i++ {
			if a[i] < curMax {
				edges[a[i]] = append(edges[a[i]], b[i-1])
				edges[b[i-1]] =append(edges[b[i-1]], a[i])
				ans++
				if b[i] > curMax {
					curMax = b[i]
				}
			} else {
					curMax = b[i]
			}
		}

		visited = make(map[int]bool)
		for i := 0; i <= n; i++ {
			if !visited[i] {
				min = i
				max = i
				cnt = 0
				dfs(i)
				ans += max - min + 1 - cnt
			}
		}

		fmt.Fprintf(out, "%d\n", ans)
	}
	return
}

func dfs(i int) {
	if i < min {
		min = i
	}
	if i > max {
		max = i
	}
	visited[i] = true
	cnt++
	for _, v := range edges[i] {
		if !visited[v] {
			dfs(v)
		}
	}
	return
}

package main

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

var visited map[int]bool
var min, max, cnt int
var edges map[int][]int

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	q := 1
	//fmt.Fscanf(in, "%d\n", &q)
	edges = make(map[int][]int)
	for ii := 0; ii < q; ii++ {
		n, m := 0, 0
		fmt.Fscanf(in, "%d %d\n", &n, &m)
		for i := 0; i < m; i++ {
			a, b := 0, 0
			fmt.Fscanf(in, "%d %d\n", &a, &b)
			edges[a] = append(edges[a], b)
			edges[b] = append(edges[b], a)
		}
		visited = make(map[int]bool)
		ans := 0
		//fmt.Println(edges)
		a, b := []int{}, []int{}
		for i := 0; i <= n; i++ {
			if !visited[i] {
				min = i
				max = i
				cnt = 0
				dfs(i)
				a = append(a, min)
				b = append(b, max)
			}
		}
		for i := 1; i < len(a); i++ {
			if a[i] < b[i-1] {
				edges[a[i]] = append(edges[a[i]], b[i-1])
				edges[b[i-1]] =append(edges[b[i-1]], a[i])
				ans++
			}
		}

		visited = make(map[int]bool)
		for i := 0; i <= n; i++ {
			if !visited[i] {
				min = i
				max = i
				cnt = 0
				dfs(i)
				ans += max - min + 1 - cnt
			}
		}

		fmt.Fprintf(out, "%d\n", ans)
	}
	return
}

func dfs(i int) {
	if i < min {
		min = i
	}
	if i > max {
		max = i
	}
	visited[i] = true
	cnt++
	for _, v := range edges[i] {
		if !visited[v] {
			dfs(v)
		}
	}
	return
}


package main

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

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

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

		var e, o int
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)

			if v%2 == 0 {
				e++
			} else {
				o++
			}
		}

		if o%4 == 2 || (o%4 == 1 && e%2 == 0) {
			fmt.Fprintln(out, "Bob")
		} else {
			fmt.Fprintln(out, "Alice")
		}
	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)

const INF = 1e18
const N = 105

var f [2][2][N][N]int

// u: 		当前是谁的回合，0：Alice，1：Bob
// s: 		当前 Alice 取的数和是奇数还是偶数，0：偶数，1：奇数
// even:	当前剩余的偶数个数
// odd:		当前剩余的奇数个数
// 返回值是对于当前回合的人是否存在必胜状态
func dp(u, s, even, odd int) int {
	if even+odd == 0 {
		if u^s == 1 {
			return 0
		}
		return 1
	}
	if f[u][s][even][odd] != -1 {
		return f[u][s][even][odd]
	}
	if even > 0 {
		if dp(u^1, s, even-1, odd) == 0 {
			f[u][s][even][odd] = 1
			return 1
		}
	}
	if odd > 0 {
		if u == 0 {
			if dp(u^1, s^1, even, odd-1) == 0 {
				f[u][s][even][odd] = 1
				return 1
			}
		} else {
			if dp(u^1, s, even, odd-1) == 0 {
				f[u][s][even][odd] = 1
				return 1
			}
		}
	}
	f[u][s][even][odd] = 0
	return 0
}

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

	for i := 0; i < N; i++ {
		for j := 0; j < N; j++ {
			f[0][0][i][j] = -1
			f[0][1][i][j] = -1
			f[1][0][i][j] = -1
			f[1][1][i][j] = -1
		}
	}
	T := nextInt()
	for ; T > 0; T-- {
		n, cnt := nextInt(), make([]int, 2)
		for i := 0; i < n; i++ {
			x := nextInt()
			cnt[x&1]++
		}
		// fmt.Printf("even: %d, odd: %d\n", cnt[0], cnt[1])
		if dp(0, 0, cnt[0], cnt[1]) == 1 {
			fmt.Println("Alice")
		} else {
			fmt.Println("Bob")
		}
	}
}

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

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"
	"sort"
	"strconv"
)

const cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	even, odd := 0, 0
	var num int
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &num)
		if num%2 == 0 {
			even++
		} else {
			odd++
		}
	}
	dppA := make([][]bool, even+1)
	for i := range dppA {
		dppA[i] = make([]bool, odd+1)
	}
	dppB := make([][]bool, even+1)
	for i := range dppB {
		dppB[i] = make([]bool, odd+1)
	}
	dppA[0][0] = false
	dppB[0][0] = true
	for i := 1; i <= even; i++ {
		dppA[i][0] = false
		dppB[i][0] = true
	}
	for i := 1; i <= odd; i++ {
		if ((i-1)/2)%2 == 0 {
			dppA[0][i] = true
		} else {
			dppA[0][i] = false
		}
		if (i/2)%2 == 0 {
			dppB[0][i] = true
		} else {
			dppB[0][i] = false
		}
	}

	dpA := make([][]bool, even+1)
	for i := range dpA {
		dpA[i] = make([]bool, odd+1)
	}
	dpB := make([][]bool, even+1)
	for i := range dpB {
		dpB[i] = make([]bool, odd+1)
	}
	dpA[0][0] = true
	dpB[0][0] = false
	for i := 1; i <= even; i++ {
		dpA[i][0] = true
		dpB[i][0] = false
	}
	for i := 1; i <= odd; i++ {
		if ((i-1)/2)%2 == 0 {
			dpA[0][i] = false
		} else {
			dpA[0][i] = true
		}
		if (i/2)%2 == 0 {
			dpB[0][i] = false
		} else {
			dpB[0][i] = true
		}
	}
	for i := 1; i <= even; i++ {
		for j := 1; j <= odd; j++ {
			if !dpB[i-1][j] || !dppB[i][j-1] {
				dpA[i][j] = true
			}
			if !dppB[i-1][j] || !dpB[i][j-1] {
				dppA[i][j] = true
			}
			if !dpA[i-1][j] || !dpA[i][j-1] {
				dpB[i][j] = true
			}
			if !dppA[i-1][j] || !dppA[i][j-1] {
				dppB[i][j] = true
			}
		}
	}
	if dpA[even][odd] {
		fmt.Fprintln(out, "Alice")
	} else {
		fmt.Fprintln(out, "Bob")
	}
}

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

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

type set[T 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 := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

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

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

func scan(a ...interface{}) {
	fmt.Fscan(in, a...)
}
func printf(f string, a ...interface{}) {
	fmt.Fprintf(out, f, a...)
}

func solve() {
	var n, x int
	scan(&n)
	// a := make([]int, k)
	o, e := 0, 0
	for i := 0; i < n; i++ {
		scan(&x)
		if x < 0 {
			x = -x
		}
		if x%2 == 1 {
			// printf("ok\n")
			o++
		} else {
			e++
		}
	}
	if o%2 == 0 {
		if o%4 == 0 {
			printf("Alice\n")
		} else {
			printf("Bob\n")
		}
	} else {
		if o%4 == 3 {
			printf("Alice\n")
		} else {
			if e%2 == 1 {
				printf("Alice\n")
			} else {
				printf("Bob\n")
			}
		}
	}

	// printf("%d\n")
}
func main() {
	defer out.Flush()
	n := 0
	scan(&n)
	for i := 0; i < n; i++ {
		solve()
	}
}

package main

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

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

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

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {
		var n, odds int
		fmt.Fscan(r, &n)
		for i := 0; i < n; i++ {
			var t int
			fmt.Fscan(r, &t)
			odds += abs(t) % 2
		}

		if odds%4 == 3 || odds%4 == 0 || (odds%4 == 1 && n%2 == 0) {
			fmt.Fprintln(w, "Alice")
		} else {
			fmt.Fprintln(w, "Bob")
		}
	}
}

package main

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

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

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {
		var n, evens, odds int
		fmt.Fscan(r, &n)
		for i := 0; i < n; i++ {
			var t int
			fmt.Fscan(r, &t)
			odds += t % 2
		}
		evens = n - odds

		if odds%4 == 3 || odds%4 == 0 || (odds%4 == 1 && evens%2 == 1) {
			fmt.Fprintln(w, "Alice")
		} else {
			fmt.Fprintln(w, "Bob")
		}
	}
}

package main

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

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

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {
		var n, evens, odds int
		fmt.Fscan(r, &n)
		for i := 0; i < n; i++ {
			var t int
			fmt.Fscan(r, &t)
			odds += t % 2
		}
		evens = n - odds

		if odds%4 == 3 || odds%4 == 0 || (odds%4 == 1 && evens%2==1) {
			fmt.Fprintln(w, "Alice")
		} else {
			fmt.Fprintln(w, "Bob")
		}
	}
}

package main

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

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

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {
		var n, evens, odds int
		fmt.Fscan(r, &n)
		for i := 0; i < n; i++ {
			var t int
			fmt.Fscan(r, &t)
			odds += t % 2
		}
		evens = n - odds

		if odds%4 == 3 || odds%4 == 0 || (odds%4 == 1 && evens%4==1) {
			fmt.Fprintln(w, "Alice")
		} else {
			fmt.Fprintln(w, "Bob")
		}
	}
}

package main

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

const cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	even, odd := 0, 0
	var num int
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &num)
		if num%2 == 0 {
			even++
		} else {
			odd++
		}
	}

	dpA := make([][]bool, even+1)
	for i := range dpA {
		dpA[i] = make([]bool, odd+1)
	}
	dpB := make([][]bool, even+1)
	for i := range dpB {
		dpB[i] = make([]bool, odd+1)
	}
	dpA[0][0] = true
	dpB[0][0] = false
	for i := 1; i <= even; i++ {
		dpA[i][0] = true
		dpB[i][0] = false
	}
	for i := 1; i <= odd; i++ {
		if ((i-1)/2)%2 == 0 {
			dpA[0][i] = false
		} else {
			dpA[0][i] = true
		}
		if (i/2)%2 == 0 {
			dpB[0][i] = false
		} else {
			dpB[0][i] = true
		}
	}
	for i := 1; i <= even; i++ {
		for j := 1; j <= odd; j++ {
			if !dpB[i-1][j] || dpB[i][j-1] {
				dpA[i][j] = true
			}
			if !dpA[i-1][j] || !dpA[i][j-1] {
				dpB[i][j] = true
			}
		}
	}
	if dpA[even][odd] {
		fmt.Fprintln(out, "Alice")
	} else {
		fmt.Fprintln(out, "Bob")
	}
}

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

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

type set[T 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 := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

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"
	"sort"
	"strconv"
)

const cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	even, odd := 0, 0
	var num int
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &num)
		if num%2 == 0 {
			even++
		} else {
			odd++
		}
	}
	dpA := make([][]bool, even+1)
	for i := range dpA {
		dpA[i] = make([]bool, odd+1)
	}
	dpB := make([][]bool, even+1)
	for i := range dpB {
		dpB[i] = make([]bool, odd+1)
	}
	dpA[0][0] = true
	dpB[0][0] = false
	for i := 1; i <= even; i++ {
		dpA[i][0] = true
		dpB[i][0] = false
	}
	for i := 1; i <= odd; i++ {
		if ((i-1)/2)%2 == 0 {
			dpA[0][i] = false
		} else {
			dpA[0][i] = true
		}
		if (i/2)%2 == 0 {
			dpB[0][i] = false
		} else {
			dpB[0][i] = true
		}
	}
	for i := 1; i <= even; i++ {
		for j := 1; j <= odd; j++ {
			if !dpB[i-1][j] {
				dpA[i][j] = true
			}
			if !dpA[i-1][j] || !dpA[i][j-1] {
				dpB[i][j] = true
			}
		}
	}
	if dpA[even][odd] {
		fmt.Fprintln(out, "Alice")
	} else {
		fmt.Fprintln(out, "Bob")
	}
}

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

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

type set[T 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 := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

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

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

func scan(a ...interface{}) {
	fmt.Fscan(in, a...)
}
func printf(f string, a ...interface{}) {
	fmt.Fprintf(out, f, a...)
}

func solve() {
	var n, x int
	scan(&n)
	// a := make([]int, k)
	o, e := 0, 0
	for i := 0; i < n; i++ {
		scan(&x)
		if x%2 == 1 {
			o++
		} else {
			e++
		}
	}
	if o%2 == 0 {
		if o%4 == 0 {
			printf("Alice\n")
		} else {
			printf("Bob\n")
		}
	} else {
		if o%4 == 3 {
			printf("Alice\n")
		} else {
			if e%2 == 1 {
				printf("Alice\n")
			} else {
				printf("Bob\n")
			}
		}
	}

	// printf("%d\n")
}
func main() {
	defer out.Flush()
	n := 0
	scan(&n)
	for i := 0; i < n; i++ {
		solve()
	}
}


package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		// input
		n := 0
		fmt.Fscanf(in, "%d\n", &n)

		freqMap := make(map[int]int, n)

		for j := 0; j < n; j++ {
			num := 0
			fmt.Fscanf(in, "%d", &num)
			if num%2 == 1 {
				continue
			}

			factor2 := getCLZ(num)
			factorOther := num >> uint(factor2)

			if freqMap[factorOther] < factor2 {
				freqMap[factorOther] = factor2
			}

		}
		fmt.Fscanf(in, "\n")

		sum := 0
		for _, v := range freqMap {
			sum += v
		}

		fmt.Fprintln(out, sum)
	}
}

func makeThemOdd(arr []int) int {

	set := make(map[int]bool)
	for i := range arr {
		for arr[i]%2 == 0 {
			set[arr[i]] = true
			arr[i] /= 2
		}
	}
	return len(set)
}

func getCLZ(x int) int {
	t := 1
	r := 0
	for (x & t) == 0 {
		t <<= 1
		r++
	}

	return r
}

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		// input
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		arr := make([]int, n)

		for j := 0; j < n; j++ {
			fmt.Fscanf(in, "%d", &arr[j])
		}
		fmt.Fscanf(in, "\n")

		fmt.Fprintln(out, makeThemOddV2(arr))
	}
}

func makeThemOdd(arr []int) int {

	set := make(map[int]bool)
	for i := range arr {
		for arr[i]%2 == 0 {
			set[arr[i]] = true
			arr[i] /= 2
		}
	}
	return len(set)
}

func makeThemOddV2(arr []int) int {
	freqMap := make(map[int]int, len(arr))

	for i := range arr {
		if arr[i]%2 != 0 {
			continue
		}

		factor2 := getCLZ(arr[i])

		factorOther := arr[i] / (1 << uint(factor2))
		if freqMap[factorOther] < factor2 {
			freqMap[factorOther] = factor2
		}
	}

	sum := 0
	for _, v := range freqMap {
		sum += v
	}
	return sum
}

func getCLZ(x int) int {
	t := 1
	r := 0
	for (x & t) == 0 {
		t <<= 1
		r++
	}

	return r
}

package main

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

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var t int
var n int
var A []int

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		n = ReadInt()
		A = ReadIntSlice(n)

		solve()
	}
}

func solve() {
	cnts := make([]int, n)
	odds := make([]int, n)

	for i := 0; i < n; i++ {
		a := A[i]
		c := 0
		for a%2 == 0 {
			c++
			a /= 2
		}
		cnts[i], odds[i] = c, a
	}

	memo := make(map[int]int)
	for i := 0; i < n; i++ {
		memo[odds[i]] = 0
	}
	for i := 0; i < n; i++ {
		memo[odds[i]] = Max(memo[odds[i]], cnts[i])
	}

	ans := 0
	for _, v := range memo {
		ans += v
	}
	fmt.Println(ans)
}

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

/*
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
- いきなりオーバーフローはしていませんか？
*/

/*******************************************************************/

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		// input
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		arr := make([]int, n)

		for j := 0; j < n; j++ {
			fmt.Fscanf(in, "%d", &arr[j])
		}
		fmt.Fscanf(in, "\n")

		fmt.Fprintln(out, makeThemOddV2(arr))
	}
}

func makeThemOdd(arr []int) int {

	set := make(map[int]bool)
	for i := range arr {
		for arr[i]%2 == 0 {
			set[arr[i]] = true
			arr[i] /= 2
		}
	}
	return len(set)
}

func makeThemOddV2(arr []int) int {
	freqMap := make(map[int]int, len(arr))

	for i := range arr {
		if arr[i]%2 != 0 {
			continue
		}

		factor2 := getCLZ(arr[i])
		factorOther := arr[i] >> uint(factor2)

		if freqMap[factorOther] < factor2 {
			freqMap[factorOther] = factor2
		}
	}

	sum := 0
	for _, v := range freqMap {
		sum += v
	}
	return sum
}

func getCLZ(x int) int {
	t := 1
	r := 0
	for (x & t) == 0 {
		t <<= 1
		r++
	}

	return r
}

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		// input
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		arr := make([]int, n)

		for j := 0; j < n; j++ {
			fmt.Fscanf(in, "%d", &arr[j])
		}
		fmt.Fscanf(in, "\n")

		fmt.Fprintln(out, makeThemOdd(arr))
	}
}

func makeThemOdd(arr []int) int {

	set := make(map[int]bool)
	for i := range arr {
		for arr[i]%2 == 0 {
			set[arr[i]] = true
			arr[i] /= 2
		}
	}
	return len(set)
}


package main

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

type state struct {
  seen bool
  color bool
  pj int
  pk int
  pl int
}

var dp [41][41][41][41]state

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

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

  var t int
  for fmt.Fscan(reader, &t); t > 0; t-- {
    var n, A, B int
    fmt.Fscan(reader, &n, &A, &B)
    var s string;
    fmt.Fscan(reader, &s)

    dp[0][0][0][0].seen = true
    for i := 0; i < n; i++ {
      v := int(s[i]-'0')
      for j := 0; j <= i; j++ {
        for k := 0; k < A; k++ {
          for l := 0; l < B; l++ {
            if dp[i][j][k][l].seen {
              dp[i+1][j+1][(10*k+v)%A][l] = state{true, true, j, k, l}
              dp[i+1][j][k][(10*l+v)%B] = state{true, false, j, k, l}
            }
          }
        }
      }
    }

    best := 500
    var bestj int
    for j := 1; j < n; j++ {
      if dp[n][j][0][0].seen && abs(2*j-n) < best {
        bestj = j
        best = abs(2*j-n)
      }
    }

    if best != 500 {
      ans := make([]bool, n)
      sj := bestj
      sk := 0
      sl := 0
      for i := 0; i < n; i++ {
        state := dp[n-i][sj][sk][sl]
        ans[n-1-i] = state.color
        sj, sk, sl = state.pj, state.pk, state.pl
      }
      var z string
      for i := 0; i < n; i++ {
        if ans[i] {
          z += "R"
        } else {
          z += "B"
        }
      }
      fmt.Fprint(writer, z, "\n")
    } else {
      fmt.Fprint(writer, "-1\n")
    }

    for i := 1; i <= n; i++ {
      for j := 0; j <= i; j++ {
        for k := 0; k < A; k++ {
          for l := 0; l < B; l++ {
            dp[i][j][k][l] = state{false, false, 0, 0, 0}
          }
        }
      }
    }
  }
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1593F(in io.Reader, out io.Writer) {
	var T, n, ma, mb int
	var s string
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &ma, &mb, &s)
		minD, ans := n, "-1"
		t := make([]byte, n)
		vis := [40][40][40][41]bool{}
		var f func(i, x, y, c int)
		f = func(i, x, y, c int) {
			if i == n {
				if x == 0 && y == 0 && abs(n-c*2) < minD {
					minD = abs(n - c*2)
					ans = string(t)
				}
				return
			}
			if vis[i][x][y][c] {
				return
			}
			vis[i][x][y][c] = true
			t[i] = 'R'
			f(i+1, (x*10+int(s[i]&15))%ma, y, c+1)
			t[i] = 'B'
			f(i+1, x, (y*10+int(s[i]&15))%mb, c)
		}
		f(0, 0, 0, 0)
		Fprintln(out, ans)
	}
}

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

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

package main

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

func die(v ...interface{}) {
	_, _, line, _ := runtime.Caller(1)
	fmt.Printf("die line %d: %v\n", line, v)
	os.Exit(-1)
}

func check(b bool) {
	if !b {
		_, _, line, _ := runtime.Caller(1)
		fmt.Printf("check failed line %d\n", line)
		os.Exit(-1)
	}
}

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

const (
	INIT = -1
	INF  = int(1e9)
	MAX  = 42
	MAX2 = MAX * MAX
	MAX3 = MAX * MAX * MAX
)

var (
	n, a, b int
	dig     []int
	dp      []int
	p       []int
)

func at(i, j, arem, brem int) int {
	return i*MAX3 + j*MAX2 + arem*MAX + brem
}

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

	dp = make([]int, MAX*MAX*MAX*MAX)
	p = make([]int, MAX*MAX*MAX*MAX)

	var tc int
	fmt.Fscan(r, &tc)
	for ; tc > 0; tc-- {
		var s string
		fmt.Fscan(r, &n, &a, &b, &s)

		dig = make([]int, len(s))
		for i := 0; i < len(s); i++ {
			dig[i] = int(s[i] - '0')
		}

		for i := 0; i < len(dp); i++ {
			dp[i] = INIT
		}

		dp[at(0, 0, 0, 0)] = 0
		for i := 0; i < n; i++ {
			for j := 0; j+i < n; j++ {
				for k := 0; k < a; k++ {
					for l := 0; l < b; l++ {

						if dp[at(i, j, k, l)] == INIT {
							continue
						}

						arem := (k*10 + dig[i+j]) % a
						brem := (l*10 + dig[i+j]) % b

						dp[at(i+1, j, arem, l)] = 0
						dp[at(i, j+1, k, brem)] = 0

						// fmt.Printf("R %3d %3d %3d %3d\n", i+1, j, arem, l)
						// fmt.Printf("B %3d %3d %3d %3d\n", i, j+1, k, brem)

						// if positive, direction is R
						// else direction is B.
						p[at(i+1, j, arem, l)] = at(i, j, k, l)
						p[at(i, j+1, k, brem)] = -(at(i, j, k, l) + 1)

					}
				}
			}
		}

		ans := INF
		t := INIT
		for i := 1; i < n; i++ {
			j := n - i
			check(i+j == n)
			check(i > 0)
			check(j > 0)
			rv := abs(i - j)

			if dp[at(i, j, 0, 0)] == 0 {
				if ans > rv {
					ans = rv
					t = at(i, j, 0, 0)
				}
			}
		}

		if ans > MAX {
			fmt.Fprintln(w, -1)
			continue
		}

		u := t
		path := make([]byte, n)
		for i := 0; i < n; i++ {
			// fmt.Printf("u %d p[u] %d\n", u, p[u])
			if p[u] < 0 {
				path[n-1-i] = 'B'
				u = -(p[u] + 1)
			} else {
				path[n-1-i] = 'R'
				u = p[u]
			}
		}

		fmt.Fprintln(w, string(path))

	}
}

package main

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

func die(v ...interface{}) {
	_, _, line, _ := runtime.Caller(1)
	fmt.Printf("die line %d: %v\n", line, v)
	os.Exit(-1)
}

func check(b bool) {
	if !b {
		_, _, line, _ := runtime.Caller(1)
		fmt.Printf("check failed line %d\n", line)
		os.Exit(-1)
	}
}

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

type scanner struct {
	sc  *bufio.Scanner
	buf []byte
	max int
}

func newScanner() *scanner {
	rv := &scanner{max: bufio.MaxScanTokenSize}
	rv.sc = bufio.NewScanner(os.Stdin)
	rv.sc.Split(bufio.ScanWords)
	rv.buf = make([]byte, rv.max)
	rv.sc.Buffer(rv.buf, rv.max)
	return rv
}

func (s *scanner) nextInt() int {
	if !s.sc.Scan() {
		die(io.ErrUnexpectedEOF)
	}
	rv, err := strconv.Atoi(s.sc.Text())
	if err != nil {
		die(err)
	}
	return rv
}

func (s *scanner) nextInt64() int64 {
	if !s.sc.Scan() {
		die(io.ErrUnexpectedEOF)
	}
	rv, err := strconv.ParseInt(s.sc.Text(), 10, 64)
	if err != nil {
		die(err)
	}
	return rv
}

func (s *scanner) nextStr() string {
	if !s.sc.Scan() {
		die(io.ErrUnexpectedEOF)
	}
	return s.sc.Text()
}

const (
	INIT = -1
	INF  = int(1e9)
	MAX  = 42
	MAX2 = MAX * MAX
	MAX3 = MAX * MAX * MAX
)

var (
	n, a, b int
	dig     []int
	dp      []int
	p       []int
)

func at(i, j, arem, brem int) int {
	return i*MAX3 + j*MAX2 + arem*MAX + brem
}

func main() {
	sc := newScanner()
	tc := sc.nextInt()

	dp = make([]int, MAX*MAX*MAX*MAX)
	p = make([]int, MAX*MAX*MAX*MAX)

	for ; tc > 0; tc-- {
		n = sc.nextInt()
		a = sc.nextInt()
		b = sc.nextInt()

		s := sc.nextStr()
		dig = make([]int, len(s))
		for i := 0; i < len(s); i++ {
			dig[i] = int(s[i] - '0')
		}

		for i := 0; i < len(dp); i++ {
			dp[i] = INIT
		}

		dp[at(0, 0, 0, 0)] = 0
		for i := 0; i < n; i++ {
			for j := 0; j+i < n; j++ {
				for k := 0; k < a; k++ {
					for l := 0; l < b; l++ {

						if dp[at(i, j, k, l)] == INIT {
							continue
						}

						arem := (k*10 + dig[i+j]) % a
						brem := (l*10 + dig[i+j]) % b

						dp[at(i+1, j, arem, l)] = 0
						dp[at(i, j+1, k, brem)] = 0

						// fmt.Printf("R %3d %3d %3d %3d\n", i+1, j, arem, l)
						// fmt.Printf("B %3d %3d %3d %3d\n", i, j+1, k, brem)

						// if positive, direction is R
						// else direction is B.
						p[at(i+1, j, arem, l)] = at(i, j, k, l)
						p[at(i, j+1, k, brem)] = -(at(i, j, k, l) + 1)

					}
				}
			}
		}

		ans := INF
		t := INIT
		for i := 1; i < n; i++ {
			j := n - i
			check(i+j == n)
			check(i > 0)
			check(j > 0)
			rv := abs(i - j)

			if dp[at(i, j, 0, 0)] == 0 {
				if ans > rv {
					ans = rv
					t = at(i, j, 0, 0)
				}
			}
		}

		if ans > MAX {
			fmt.Println(-1)
			continue
		}

		u := t
		path := make([]byte, n)
		for i := 0; i < n; i++ {
			// fmt.Printf("u %d p[u] %d\n", u, p[u])
			if p[u] < 0 {
				path[n-1-i] = 'B'
				u = -(p[u] + 1)
			} else {
				path[n-1-i] = 'R'
				u = p[u]
			}
		}

		fmt.Println(string(path))

	}
}

package main

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

type before struct {
	cRed, vRed, vBlack int
}

var empty before = before{-1, 0, 0}

var dp[41][41][41][41]before

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

	var A, B, n, t int
	var s string

	fmt.Fscan(r, &t)
	for ; t > 0; t-- {
		fmt.Fscan(r, &n, &A, &B, &s)

		cleanDP(n, A, B)
		dp[0][0][0][0] = before{0, 0, 0};

		calcDP(n, A, B, s)
		fmt.Fprintln(w, getRes(n, A, B))
	}
}

func cleanDP(n, A, B int) {
	for i := 0; i <= n; i++ {
		for j := 0; j <= n; j++ {
			for k := 0; k < A; k++ {
				for l := 0; l < B; l++ {
					dp[i][j][k][l] = empty
				}
			}
		}
	}
}

func calcDP(n, A, B int, s string) {
	for i := 1; i <= n; i++ {
		val := int(s[i - 1] - '0')

		for cRed := 0; cRed <= i - 1; cRed++ {
			for vRed := 0; vRed < A; vRed++ {
				for vBlack := 0; vBlack < B; vBlack++ {
					if dp[i - 1][cRed][vRed][vBlack] != empty {
						nvRed 	:= (10 * vRed 	+ val) % A
						nvBlack := (10 * vBlack + val) % B

						dp[i][cRed + 1][nvRed][vBlack] = before{cRed, vRed, vBlack};
						dp[i][cRed][vRed][nvBlack] 	   = before{cRed, vRed, vBlack};
					}
				}
			}
		}
	}
}

func getRes(n, A, B int) string {
	best := n
	for cRed := 1; cRed < n; cRed++ {
		if dp[n][cRed][0][0] != empty {
			if abs(cRed - (n - cRed)) < abs(best - (n - best)) {
				best = cRed
			}

			}
		}

	if best == n {
		return "-1"
	}

	return recoverDP(n, A, B, best, 0, 0)
}

func recoverDP(n, A, B, cRed, vRed, vBlack int) string {
	resultBytes := make([]byte, n)

	for i := n; i > 0; i-- {
		b := dp[i][cRed][vRed][vBlack]
		if cRed != b.cRed {
			resultBytes[i - 1] = 'R'
		} else {
			resultBytes[i - 1] = 'B'
		}

		cRed = b.cRed
		vRed = b.vRed
		vBlack = b.vBlack
	}

	return string(resultBytes)
}

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

	return a
}


package main

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

func getCount(s string) map[rune]int {
  count := map[rune]int{}
  for _, ch := range s {
    count[ch]++
  }
  return count
}

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

  line, _ := reader.ReadString('\n')
  a := strings.TrimSpace(line)
  line, _ = reader.ReadString('\n')
  b := strings.TrimSpace(line)
  line, _ = reader.ReadString('\n')
  c := strings.TrimSpace(line)
  fmt.Fprintf(os.Stderr, "%s %s %s\n", a, b, c)
  countA := getCount(a)
  countB := getCount(b)
  countC := getCount(c)
  feasible := true
  bestSum, bestB, bestC := -1, -1, -1
  for tryB := 0; ; tryB++ {
    tryC := -1
    for ch, count := range countC {
      x := countA[ch]/count
      if tryC == -1 || x < tryC {
        tryC = x
      }
    }
    if tryB+tryC > bestSum {
      bestSum, bestB, bestC = tryB+tryC, tryB, tryC
    }
    for ch, count := range countB {
      if countA[ch] < count {
        feasible = false
      }
    }
    if !feasible {
      break
    }
    for ch, count := range countB {
      countA[ch] -= count
    }
  }
  fmt.Fprintf(os.Stderr, "%d %d\n", bestB, bestC)
  result := []byte{}
  countA = getCount(a)
  for i := 0; i < bestB; i++ {
    result = append(result, []byte(b)...)
    for ch, count := range countB {
      countA[ch] -= count
    }
  }
  for i := 0; i < bestC; i++ {
    result = append(result, []byte(c)...)
    for ch, count := range countC {
      countA[ch] -= count
    }
  }
  //result = append(result, ' ')
  for ch, count := range countA {
    for i := 0; i < count; i++ {
      result = append(result, byte(ch))
    }
  }
  writer.WriteString(fmt.Sprintf("%s\n", result))
}

package main

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

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

const n = 26

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

	var sa, sb, sc string
	a, b, c := make([]int, n), make([]int, n), make([]int, n)
	cntc, cntb := 0, 0

	scan(&sa)
	for i := 0; i < len(sa); i++ {
		a[sa[i]-'a']++
	}

	scan(&sb)
	for i := 0; i < len(sb); i++ {
		b[sb[i]-'a']++
	}

	scan(&sc)
	for i := 0; i < len(sc); i++ {
		c[sc[i]-'a']++
	}

	for {
		cntb, cntc = cntSubString(a, b), cntSubString(a, c)

		if cntb == 0 && cntc == 0 {
			break
		}

		if cntb >= cntc {
			print(sb)

			for i := range a {
				a[i] -= b[i]
			}
		} else {
			print(sc)

			for i := range a {
				a[i] -= c[i]
			}
		}
	}

	for i := range a {
		for a[i] > 0 {
			print(string(i + 'a'))
			a[i]--
		}
	}

	println()
}

func cntSubString(a, b []int) int {
	min := 1000000

	for i := range a {
		if b[i] != 0 && a[i]/b[i] < min {
			min = a[i] / b[i]
		}
	}

	return min
}

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

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

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

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 print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

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

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

const n = 26

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

	var sa, sb, sc string
	a, b, c := make([]int, n), make([]int, n), make([]int, n)
	cntc, cntb := 0, 0

	scan(&sa)
	for i := 0; i < len(sa); i++ {
		a[sa[i]-'a']++
	}

	scan(&sb)
	for i := 0; i < len(sb); i++ {
		b[sb[i]-'a']++
	}

	scan(&sc)
	for i := 0; i < len(sc); i++ {
		c[sc[i]-'a']++
	}

	cntb, cntc = cntSubString(a, b), cntSubString(a, c)

	if cntb < cntc {
		sb, sc = sc, sb
		b, c = c, b
		cntb, cntc = cntc, cntSubString(a, c)
	}

	for i := range a {
		a[i] -= cntb * b[i]
	}
	// println(a)

	for i := 0; i < cntb; i++ {
		print(sb)
	}

	for i := 0; i < cntc; i++ {
		print(sc)
	}

	for i := range a {
		for a[i] > 0 {
			print(string(i + 'a'))
			a[i]--
		}
	}

	println()
}

func cntSubString(a, b []int) int {
	min := 1000000

	for i := range a {
		if b[i] != 0 && a[i]/b[i] < min {
			min = a[i] / b[i]
		}
	}

	return min
}

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

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

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

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 print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

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

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

const n = 26

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

	var sa, sb, sc string
	a, b, c := make([]int, n), make([]int, n), make([]int, n)
	cntc, cntb := 0, 0

	scan(&sa)
	for i := 0; i < len(sa); i++ {
		a[sa[i]-'a']++
	}

	scan(&sb)
	for i := 0; i < len(sb); i++ {
		b[sb[i]-'a']++
	}

	scan(&sc)
	for i := 0; i < len(sc); i++ {
		c[sc[i]-'a']++
	}

	cntb, cntc = cntSubString(a, b), cntSubString(a, c)

	if cntb < cntc {
		sb, sc = sc, sb
		b, c = c, b
		cntb, cntc = cntc, cntb
	}

	for i := range a {
		a[i] -= cntb * b[i]
	}

	cntc = cntSubString(a, c)
	// println(a)

	for i := 0; i < cntb; i++ {
		print(sb)
	}

	for i := 0; i < cntc; i++ {
		print(sc)
	}

	for i := range a {
		for a[i] > 0 {
			print(string(i + 'a'))
			a[i]--
		}
	}

	println()
}

func cntSubString(a, b []int) int {
	min := 1000000

	for i := range a {
		if b[i] != 0 && a[i]/b[i] < min {
			min = a[i] / b[i]
		}
	}

	return min
}

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

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

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

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 print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

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

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

const n = 26

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

	var sa, sb, sc string
	a, b, c := make([]int, n), make([]int, n), make([]int, n)
	cntc, cntb := 0, 0

	scan(&sa)
	for i := 0; i < len(sa); i++ {
		a[sa[i]-'a']++
	}

	scan(&sb)
	for i := 0; i < len(sb); i++ {
		b[sb[i]-'a']++
	}

	scan(&sc)
	for i := 0; i < len(sc); i++ {
		c[sc[i]-'a']++
	}

	cntb, cntc = cntSubString(a, b), cntSubString(a, c)

	// println(b, c)
	if cntb < cntc {
		sb, sc = sc, sb
		b, c = c, b
		cntb, cntc = cntc, cntb
	}
	// println(b, c)
	// println(a)
	for i := range a {
		a[i] -= cntb * b[i]
	}

	cntc = cntSubString(a, c)
	// println(a)

	for i := range a {
		a[i] -= cntc * c[i]
	}

	for i := 0; i < cntb; i++ {
		print(sb)
	}

	for i := 0; i < cntc; i++ {
		print(sc)
	}

	for i := range a {
		for a[i] > 0 {
			print(string(i + 'a'))
			a[i]--
		}
	}

	println()
}

func cntSubString(a, b []int) int {
	min := 1000000

	for i := range a {
		if b[i] != 0 && a[i]/b[i] < min {
			min = a[i] / b[i]
		}
	}

	return min
}

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

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

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

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 print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

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

func getCount(s string) map[rune]int {
  count := map[rune]int{}
  for _, ch := range s {
    count[ch]++
  }
  return count
}

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

  line, _ := reader.ReadString('\n')
  a := strings.TrimSpace(line)
  line, _ = reader.ReadString('\n')
  b := strings.TrimSpace(line)
  line, _ = reader.ReadString('\n')
  c := strings.TrimSpace(line)
  countA := getCount(a)
  countB := getCount(b)
  countC := getCount(c)
  feasible := true
  bestSum, bestB, bestC := -1, -1, -1
  for tryB := 0; ; tryB++ {
    tryC := -1
    for ch, count := range countC {
      x := countA[ch]/count
      if tryC == -1 || x < tryC {
        tryC = x
      }
    }
    if tryB+tryC > bestSum {
      bestSum, bestB, bestC = tryB+tryC, tryB, tryC
    }
    for ch, count := range countB {
      if countA[ch] < count {
        feasible = false
      }
    }
    if !feasible {
      break
    }
    for ch, count := range countB {
      countA[ch] -= count
    }
  }
  fmt.Fprintf(os.Stderr, "%d %d\n", bestB, bestC)
  result := []byte{}
  for i := 0; i < bestB; i++ {
    result = append(result, []byte(b)...)
  }
  for i := 0; i < bestC; i++ {
    result = append(result, []byte(c)...)
  }
  for ch, count := range countA {
    for i := 0; i < count; i++ {
      result = append(result, byte(ch))
    }
  }
  writer.WriteString(fmt.Sprintf("%s\n", result))
}

package main

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

func getCount(s string) map[rune]int {
  count := map[rune]int{}
  for _, ch := range s {
    count[ch]++
  }
  return count
}

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

  line, _ := reader.ReadString('\n')
  a := strings.TrimSpace(line)
  line, _ = reader.ReadString('\n')
  b := strings.TrimSpace(line)
  line, _ = reader.ReadString('\n')
  c := strings.TrimSpace(line)
  countA := getCount(a)
  countB := getCount(b)
  countC := getCount(c)
  feasible := true
  bestSum, bestB, bestC := -1, -1, -1
  for tryB := 0; ; tryB++ {
    tryC := -1
    for ch, count := range countC {
      x := countA[ch]/count
      if tryC == -1 || x < tryC {
        tryC = x
      }
    }
    if tryB+tryC > bestSum {
      bestSum, bestB, bestC = tryB+tryC, tryB, tryC
    }
    for ch, count := range countB {
      if countA[ch] < count {
        feasible = false
      }
    }
    if !feasible {
      break
    }
    for ch, count := range countB {
      countA[ch] -= count
    }
  }
  fmt.Fprintf(os.Stderr, "%d %d\n", bestB, bestC)
  result := []byte{}
  for i := 0; i < bestB; i++ {
    result = append(result, []byte(b)...)
  }
  for i := 0; i < bestC; i++ {
    result = append(result, []byte(c)...)
    for ch, count := range countC {
      countA[ch] -= count
    }
  }
  //result = append(result, ' ')
  for ch, count := range countA {
    for i := 0; i < count; i++ {
      result = append(result, byte(ch))
    }
  }
  writer.WriteString(fmt.Sprintf("%s\n", result))
}


package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var T, n int64
	for fmt.Fscan(in, &T); T > 0; T-- {
		fmt.Fscan(in, &n)
		a := make([]int64, n)
		for i := range a {
			fmt.Fscan(in, &a[i])
		}

		fmt.Println(PathWithMinCost(n, a))
	}

}

func PathWithMinCost(n int64, costs []int64) int64 {

	var countOdd int64 = 1
	sumOdd := costs[0]
	var countEven int64 = 1
	sumEven := costs[1]
	minOdd := costs[0]
	minEven := costs[1]

	minCost := n * (minEven + minOdd)

	for i := 2; i < len(costs); i++ {
		if i % 2 == 0 {
			countOdd++
			sumOdd += costs[i]
			if minOdd > costs[i] {
				minOdd = costs[i]
			}
		} else {
			countEven++
			sumEven += costs[i]
			if minEven > costs[i] {
				minEven = costs[i]
			}
		}

		cost := sumOdd + minOdd * (n - countOdd) + sumEven + minEven * (n - countEven)
		if minCost > cost {
			minCost = cost
		}
	}

	return minCost
}

package main

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

var ret *big.Int

func isPrime(x int) bool {
	for i := 2; i < x; i++ {
		if x % i == 0 {
			return false
		}
	}
	return true
}

func dfs(dep, maxDep, n int, a, primeArr []int32, used []bool) {
	if dep == maxDep {
		cur := big.NewInt(1)
		for i := 0; i < n; i++ {
			curFlag := false
			for j := 0; j < maxDep; j++ {
				if used[j] && a[i] % primeArr[j] == 0 {
					curFlag = true
				}
			}
			if !curFlag {
				return
			}
		}
		for i := 0; i < maxDep; i++ {
			if used[i] {
				cur.Mul(cur, big.NewInt(int64(primeArr[i])))
			}
		}
		//fmt.Println(cur)
		if ret.Cmp(big.NewInt(0)) == 0 || -1 == cur.Cmp(ret) {
			ret = cur
		}
		return
	}
	used[dep] = true
	dfs(dep + 1, maxDep, n, a, primeArr, used)
	used[dep] = false
	dfs(dep + 1, maxDep, n, a, primeArr, used)
}

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

func printf(f string, a ...interface{}) (n int, err error) { return fmt.Fprintf(out, f, a...) }
func println(a ...interface{}) (n int, err error)          { return fmt.Fprintln(out, a...) }

func nextNum() int {
	in.Scan()
	var r, f int
	f = 1
	for _, c := range in.Bytes() {
		if c == '-' {
			f = -1
			continue
		}
		r *= 10
		r += int(c - '0')
	}
	return r * f
}

func main() {
	input()
}

func input() {
	defer out.Flush()
	in.Split(bufio.ScanWords)
	in.Buffer(make([]byte, 1024), int(1e+9))
	var T int
	T = nextNum()
	for i := 0; i < T; i++ {
		var n int
		n = nextNum()
		a := make([]int, n)
		for j := 0; j < n; j++ {
			a[j] = nextNum()
		}
		fmt.Println(answer(n, a))
	}
}

func answer(n int, a []int) int64 {
	ans := int64(n) * int64(a[0] + a[1])
	min0 := a[0]
	min1 := a[1]
	num0 := n
	num1 := n
	cur := ans
	for i := 2; i < n; i++ {
		if i % 2 == 0 {
			if a[i] >= min0 {
				cur = cur - int64(min0) + int64(a[i])
				num0--
			} else {
				cur = cur - int64(min0) * int64(num0 - 1) + int64(num0 - 1) * int64(a[i])
				min0 = a[i]
				num0--
			}
		} else {
			if a[i] >= min1 {
				cur = cur - int64(min1) + int64(a[i])
				num1--
			} else {
				cur = cur - int64(min1) * int64(num1 - 1) + int64(num1 - 1) * int64(a[i])
				min1 = a[i]
				num1--
			}
		}
		ans = min(ans, cur)
	}
	return ans
}

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

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

package main

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

var in *bufio.Reader

func solve() {
	var n int64
	fmt.Fscan(in, &n)

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

	evenMin, oddMin := c[0], c[1]
	evenAcc, oddAcc := int64(evenMin), int64(oddMin)
	evenSum, oddSum := int64(evenMin)*n, int64(oddMin)*n
	result := evenSum + oddSum
	for i := int64(2); i < n; i++ {
		if i%2 == 0 {
			evenAcc += int64(c[i])
			if c[i] < evenMin {
				evenMin = c[i]
			}
			evenSum = evenAcc + (n-(i/2+1))*int64(evenMin)
		} else {
			oddAcc += int64(c[i])
			if c[i] < oddMin {
				oddMin = c[i]
			}
			oddSum = oddAcc + (n-(i/2+1))*int64(oddMin)
		}
		cur := evenSum + oddSum
		if cur < result {
			result = cur
		}
	}
	fmt.Println(result)
}

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

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

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

/*
* Filename: C/main.go
* Timestamp: 07-06-2021 14:24:44
* Author: Fadhil Musaad
 */

package main

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

var (
	reader *bufio.Reader
	writer *bufio.Writer
)

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

func solve(_r io.Reader, _w io.Writer) {
	reader = bufio.NewReader(_r)
	writer = bufio.NewWriter(_w)
	defer writer.Flush()

	var t int
	scanln(&t)
	for ; t > 0; t-- {
		var n int64
		scanln(&n)
		var odd, even int64 = 1000000005, 1000000005
		sum := int64(0)
		x, y := int64(0), int64(0)

		min := func(a, b int64) int64 {
			if a < b {
				return a
			}
			return b
		}
		ans := int64(1000000000000000000)
		for i := int64(0); i < n; i++ {
			var a int64
			scan(&a)
			if i%2 == 1 {
				x++
				odd = min(odd, a)
			} else {
				y++
				even = min(even, a)
			}
			sum += a
			cur := sum + odd*(n-x) + even*(n-y)
			ans = min(ans, cur)
		}
		scanln()
		println(ans)
	}
}

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

package main

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

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

	tc := readNum(reader)

	var buf bytes.Buffer

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

	fmt.Print(buf.String())
}

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

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

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

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

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

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

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

	return i
}

func solve(n int, C []int) int64 {
	/**
	分成两部分偶数和奇数部分；
	偶数部分用于垂直移动，奇数部分用于水平移动；
	那么其中个部分的最小值应该移动最多n - m + 1, 其他的部分移动m-1
	*/
	x, y := C[0], C[1]
	N := int64(n)
	best := N * (int64(x) + int64(y))
	sh := int64(x)
	sv := int64(y)
	for i := 2; i < n; {
		x = min(x, C[i])
		sh += int64(C[i])
		// if take i segs, and i is horizontal
		m := i/2 + 1
		// horiontal part
		tmp := int64(n-m+1)*int64(x) + sh - int64(x)
		m--
		// vertical part
		tmp += int64(n-m+1)*int64(y) + sv - int64(y)
		best = min2(best, tmp)
		i++
		if i < n {
			y = min(y, C[i])
			sv += int64(C[i])
			// if i == 3, then m = 2
			m = i/2 + 1

			tmp = int64(n-m+1)*int64(x) + sh - int64(x)
			tmp += int64(n-m+1)*int64(y) + sv - int64(y)
			best = min2(best, tmp)
			i++
		}
	}
	return best
}

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

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

package main

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

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

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

	// single test case
	var tt int
	Fscan(in, &tt)

	for tt > 0 {
		tt--

		var nn int64
		Fscan(in, &nn)

		mn := []int64{1e9, 1e9}
		rem := []int64{int64(nn), int64(nn)}
		sum := int64(0)
		ans := int64(1e18)

		a := make([]int64, nn)
		for i := int64(0); i < nn; i++ {
			Fscan(in, &a[i])
		}

		for k := range a {
			mn[k%2] = min(mn[k%2], a[k])
			rem[k%2]--
			sum += a[k]
			if k > 0 {
				cur := sum + rem[0]*mn[0] + rem[1]*mn[1]
				ans = min(ans, cur)
			}
		}

		Fprint(out, ans, "\n")
	}

	// 입력 에러 방지
	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("읽지않은 데이터 발견：\n" + _s)
	}
}

// LETYLETYLETY
func main() { CF106c(os.Stdin, os.Stdout) }

// YTELYTELYTEL

package main

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

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

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

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

func main() {
	defer writer.Flush()
	//redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

// --------------------------------------------------------------------- //

func solve() {
	var n int64
	scan(&n)
	a := make([]int64, n)
	for i := range a {
		scan(&a[i])
	}

	var ans int64 = math.MaxInt64
	var cntEven, cntOdd int64 = 1, 0
	var sumEven, sumOdd int64 = a[0], 0
	var minEven, minOdd int64 = a[0], 1e9 + 1

	f := func() int64 {
		reven := (sumEven - minEven) + minEven*(n-(cntEven-1))
		rodd := (sumOdd - minOdd) + minOdd*(n-(cntOdd-1))
		return reven + rodd
	}

	for i := int64(1); i < n; i++ {
		if i%2 == 0 {
			sumEven += a[i]
			if minEven > a[i] {
				minEven = a[i]
			}
			cntEven++
		} else {
			sumOdd += a[i]
			if minOdd > a[i] {
				minOdd = a[i]
			}
			cntOdd++
		}

		res := f()
		if res < ans {
			ans = res
		}
	}

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

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	var T, n int64
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int64, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		m := a[:2]
		s := []int64{a[0], a[1]}
		c := []int64{1, 1}
		ans := (a[0] + a[1]) * n
		for i, v := range a[2:] {
			i &= 1
			s[i] += v
			if v < m[i] {
				m[i] = v
			}
			c[i]++
			if r := s[0]+m[0]*(n-c[0])+s[1]+m[1]*(n-c[1]); r < ans {
				ans = r
			}
		}
		Println(ans)
	}
}

package main

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

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

	solve := func(Case int) {
		var n int
		Fscan(in, &n)
		a := make([]int64, n)
		for i := range a {
			Fscan(in, &a[i])
		}

		mi0, mi1 := a[0], a[1]
		ans := (mi0 + mi1) * int64(n)
		if n == 2 {
			Fprintln(out, ans)
			return
		}
		c0, c1 := 1, 1
		s0, s1 := mi0, mi1
		for i := 2; i < n; i++ {
			if i&1 == 0 {
				s0 += a[i]
				mi0 = min(mi0, a[i])
				c0++
			} else {
				s1 += a[i]
				mi1 = min(mi1, a[i])
				c1++
			}
			res := (s0 - mi0) + mi0*int64(n+1-c0)
			res += (s1 - mi1) + mi1*int64(n+1-c1)
			ans = min(ans, res)
		}

		Fprintln(out, ans)
	}

	var t int
	Fscan(in, &t)
	for Case := 1; Case <= t; Case++ {
		solve(Case)
	}

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

func main() { run(os.Stdin, os.Stdout) }
func min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

package main

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

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

	solve := func(Case int) {
		var n int
		var ans ,v int64
		Fscan(in, &n)
		a := make([][]int64, 2)
		for i := range a {
			a[i] = make([]int64,0, n/2)
		}
		for i := 0; i < n; i++ {
			Fscan(in, &v)
			a[i&1] = append(a[i&1], v)
		}

		for _, a := range a {
			mi := int64(1e18)
			s := int64(0)
			for i, v := range a {
				mi = min(mi, s+int64(n-i)*v)
				s+=v
			}
			ans+=mi
		}

		Fprintln(out, ans)
	}

	var t int
	Fscan(in, &t)
	for Case := 1; Case <= t; Case++ {
		solve(Case)
	}

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

func main() { run(os.Stdin, os.Stdout) }
func min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}