You're given an undirected graph with $$$n$$$ nodes and $$$m$$$ edges. Nodes are numbered from $$$1$$$ to $$$n$$$.The graph is considered harmonious if and only if the following property holds:  For every triple of integers $$$(l, m, r)$$$ such that $$$1 \le l < m < r \le n$$$, if there exists a path going from node $$$l$$$ to node $$$r$$$, then there exists a path going from node $$$l$$$ to node $$$m$$$. In other words, in a harmonious graph, if from a node $$$l$$$ we can reach a node $$$r$$$ through edges ($$$l < r$$$), then we should able to reach nodes $$$(l+1), (l+2), \ldots, (r-1)$$$ too.What is the minimum number of edges we need to add to make the graph harmonious?

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$3 \le n \le 200\ 000$$$ and $$$1 \le m \le 200\ 000$$$). The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \neq v_i$$$), that mean that there's an edge between nodes $$$u$$$ and $$$v$$$. It is guaranteed that the given graph is simple (there is no self-loop, and there is at most one edge between every pair of nodes).

Print the minimum number of edges we have to add to the graph to make it harmonious.

NoteIn the first example, the given graph is not harmonious (for instance, $$$1 < 6 < 7$$$, node $$$1$$$ can reach node $$$7$$$ through the path $$$1 \rightarrow 2 \rightarrow 7$$$, but node $$$1$$$ can't reach node $$$6$$$). However adding the edge $$$(2, 4)$$$ is sufficient to make it harmonious.In the second example, the given graph is already harmonious.

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"
	"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 (
	"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 (
	. "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)
			}
		}
		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
}


Alice and Bob are playing a game on a sequence $$$a_1, a_2, \dots, a_n$$$ of length $$$n$$$. They move in turns and Alice moves first.In the turn of each player, he or she should select an integer and remove it from the sequence. The game ends when there is no integer left in the sequence. Alice wins if the sum of her selected integers is even; otherwise, Bob wins. Your task is to determine who will win the game, if both players play optimally.

Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The following lines contain the description of each test case. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 100$$$), indicating the length of the sequence. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$), indicating the elements of the sequence.

For each test case, output "Alice" (without quotes) if Alice wins and "Bob" (without quotes) otherwise.

NoteIn the first and second test cases, Alice always selects two odd numbers, so the sum of her selected numbers is always even. Therefore, Alice always wins.In the third test case, Bob has a winning strategy that he always selects a number with the same parity as Alice selects in her last turn. Therefore, Bob always wins.In the fourth test case, Alice always selects two even numbers, so the sum of her selected numbers is always even. Therefore, Alice always wins.

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

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

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


There are $$$n$$$ positive integers $$$a_1, a_2, \dots, a_n$$$. For the one move you can choose any even value $$$c$$$ and divide by two all elements that equal $$$c$$$.For example, if $$$a=[6,8,12,6,3,12]$$$ and you choose $$$c=6$$$, and $$$a$$$ is transformed into $$$a=[3,8,12,3,3,12]$$$ after the move.You need to find the minimal number of moves for transforming $$$a$$$ to an array of only odd integers (each element shouldn't be divisible by $$$2$$$).

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. The first line of a test case contains $$$n$$$ ($$$1 \le n \le 2\cdot10^5$$$) — the number of integers in the sequence $$$a$$$. The second line contains positive integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). The sum of $$$n$$$ for all test cases in the input doesn't exceed $$$2\cdot10^5$$$.

For $$$t$$$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $$$2$$$).

NoteIn the first test case of the example, the optimal sequence of moves can be as follows:  before making moves $$$a=[40, 6, 40, 3, 20, 1]$$$;  choose $$$c=6$$$;  now $$$a=[40, 3, 40, 3, 20, 1]$$$;  choose $$$c=40$$$;  now $$$a=[20, 3, 20, 3, 20, 1]$$$;  choose $$$c=20$$$;  now $$$a=[10, 3, 10, 3, 10, 1]$$$;  choose $$$c=10$$$;  now $$$a=[5, 3, 5, 3, 5, 1]$$$ — all numbers are odd. Thus, all numbers became odd after $$$4$$$ moves. In $$$3$$$ or fewer moves, you cannot make them all odd.

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)

		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, 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 (
	"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
}


It is given a non-negative integer $$$x$$$, the decimal representation of which contains $$$n$$$ digits. You need to color each its digit in red or black, so that the number formed by the red digits is divisible by $$$A$$$, and the number formed by the black digits is divisible by $$$B$$$.At least one digit must be colored in each of two colors. Consider, the count of digits colored in red is $$$r$$$ and the count of digits colored in black is $$$b$$$. Among all possible colorings of the given number $$$x$$$, you need to output any such that the value of $$$|r - b|$$$ is the minimum possible.Note that the number $$$x$$$ and the numbers formed by digits of each color, may contain leading zeros.  Example of painting a number for $$$A = 3$$$ and $$$B = 13$$$ The figure above shows an example of painting the number $$$x = 02165$$$ of $$$n = 5$$$ digits for $$$A = 3$$$ and $$$B = 13$$$. The red digits form the number $$$015$$$, which is divisible by $$$3$$$, and the black ones — $$$26$$$, which is divisible by $$$13$$$. Note that the absolute value of the difference between the counts of red and black digits is $$$1$$$, it is impossible to achieve a smaller value.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of two lines. The first line contains three integers $$$n$$$, $$$A$$$, $$$B$$$ ($$$2 \le n \le 40$$$, $$$1 \le A, B \le 40$$$). The second line contains a non-negative integer $$$x$$$ containing exactly $$$n$$$ digits and probably containing leading zeroes.

For each test case, output in a separate line:   -1 if the desired coloring does not exist;  a string $$$s$$$ of $$$n$$$ characters, each of them is a letter 'R' or 'B'. If the $$$i$$$-th digit of the number $$$x$$$ is colored in red, then the $$$i$$$-th character of the string $$$s$$$ must be the letter 'R', otherwise the letter 'B'.  The number formed by digits colored red should divisible by $$$A$$$. The number formed by digits colored black should divisible by $$$B$$$. The value $$$|r-b|$$$ should be minimal, where $$$r$$$ is the count of red digits, $$$b$$$ is the count of black digits. If there are many possible answers, print any of them.

NoteThe first test case is considered in the statement.In the second test case, there are no even digits, so it is impossible to form a number from its digits that is divisible by $$$2$$$.In the third test case, each coloring containing at least one red and one black digit is possible, so you can color $$$4$$$ digits in red and $$$4$$$ in black ($$$|4 - 4| = 0$$$, it is impossible to improve the result).In the fourth test case, there is a single desired coloring.

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 (
  "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
}


Professor GukiZ doesn't accept string as they are. He likes to swap some letters in string to obtain a new one.GukiZ has strings a, b, and c. He wants to obtain string k by swapping some letters in a, so that k should contain as many non-overlapping substrings equal either to b or c as possible. Substring of string x is a string formed by consecutive segment of characters from x. Two substrings of string x overlap if there is position i in string x occupied by both of them.GukiZ was disappointed because none of his students managed to solve the problem. Can you help them and find one of possible strings k?

The first line contains string a, the second line contains string b, and the third line contains string c (1 ≤ |a|, |b|, |c| ≤ 105, where |s| denotes the length of string s). All three strings consist only of lowercase English letters.  It is possible that b and c coincide.

Find one of possible strings k, as described in the problem statement. If there are multiple possible answers, print any of them.

NoteIn the third sample, this optimal solutions has three non-overlaping substrings equal to either b or c on positions 1 – 2 (ab), 3 – 4 (ab), 5 – 7 (aca). In this sample, there exist many other optimal solutions, one of them would be acaababbcc.

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 (
  "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']++
	}

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