Alice and Bob have received three big piles of candies as a gift. Now they want to divide these candies as fair as possible. To do this, Alice takes one pile of candies, then Bob takes one of the other two piles. The last pile is split between Alice and Bob as they want: for example, it is possible that Alice takes the whole pile, and Bob gets nothing from it.After taking the candies from the piles, if Alice has more candies than Bob, she discards some candies so that the number of candies she has is equal to the number of candies Bob has. Of course, Bob does the same if he has more candies.Alice and Bob want to have as many candies as possible, and they plan the process of dividing candies accordingly. Please calculate the maximum number of candies Alice can have after this division process (of course, Bob will have the same number of candies).You have to answer $$$q$$$ independent queries.Let's see the following example: $$$[1, 3, 4]$$$. Then Alice can choose the third pile, Bob can take the second pile, and then the only candy from the first pile goes to Bob — then Alice has $$$4$$$ candies, and Bob has $$$4$$$ candies.Another example is $$$[1, 10, 100]$$$. Then Alice can choose the second pile, Bob can choose the first pile, and candies from the third pile can be divided in such a way that Bob takes $$$54$$$ candies, and Alice takes $$$46$$$ candies. Now Bob has $$$55$$$ candies, and Alice has $$$56$$$ candies, so she has to discard one candy — and after that, she has $$$55$$$ candies too.

The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 1000$$$) — the number of queries. Then $$$q$$$ queries follow. The only line of the query contains three integers $$$a, b$$$ and $$$c$$$ ($$$1 \le a, b, c \le 10^{16}$$$) — the number of candies in the first, second and third piles correspondingly.

Print $$$q$$$ lines. The $$$i$$$-th line should contain the answer for the $$$i$$$-th query — the maximum number of candies Alice can have after the division, if both Alice and Bob act optimally (of course, Bob will have the same number of candies).

package main

import "fmt"

func main(){
	var n, a, b, c, i int64;
	fmt.Scan(&n);
	for i=0; i < n; i++{
		fmt.Scan(&a, &b, &c);
		fmt.Println((a + b + c) / 2)
	}
}

package main

import (
	"fmt"
)

func main() {
	var q, x int64

	// scr := bufio.NewReader(os.Stdin)
	// ocr := bufio.NewWriter(os.Stdout)

	fmt.Scanln(&q)

	for i := int64(0); i < q; i++ {
		var sum int64
		for j := int64(0); j < 3; j++ {
			fmt.Scanf("%d", &x)
			sum += x
		}
		fmt.Scanln(&x)

		fmt.Println(sum / 2)
	}

}

package main

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

func main() {
	var n, a, b, c int64

	scr := bufio.NewReader(os.Stdin)
	// ocr := bufio.NewWriter(os.Stdout)

	fmt.Fscanln(scr, &n)

	for i := int64(0); i < n; i++ {
		fmt.Fscanln(scr, &a, &b, &c)
		fmt.Println((a + b + c) / 2)
	}

}

package main

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

func main() {
	var t int
	var a, b, c int64
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		aa := make([]int64, 0)
		fmt.Fscan(in, &a, &b, &c)
		aa = append(aa, a)
		aa = append(aa, b)
		aa = append(aa, c)
		sort.Slice(aa, func(i, j int) bool {
			return aa[i] < aa[j]
		})
		left := aa[2] - aa[1] + aa[0]
		fmt.Println(left/2 + aa[1])
	}
}

package main

import "fmt"

func main() {
	var query int
	var pile1, pile2, pile3, sum uint64
	fmt.Scan(&query)
	for i := 0; i < query; i++ {
		fmt.Scan(&pile1)
		fmt.Scan(&pile2)
		fmt.Scan(&pile3)
		sum = pile1 + pile2 + pile3;
		fmt.Println(sum/2)
	}
}

// A. Three Piles of Candies
/*
Alice and Bob have received three big piles of candies as a gift. Now they want to divide these candies as fair as possible. To do this, Alice takes one pile of candies, then Bob takes one of the other two piles. The last pile is split between Alice and Bob as they want: for example, it is possible that Alice takes the whole pile, and Bob gets nothing from it.

After taking the candies from the piles, if Alice has more candies than Bob, she discards some candies so that the number of candies she has is equal to the number of candies Bob has. Of course, Bob does the same if he has more candies.

Alice and Bob want to have as many candies as possible, and they plan the process of dividing candies accordingly. Please calculate the maximum number of candies Alice can have after this division process (of course, Bob will have the same number of candies).

You have to answer 𝑞 independent queries.

Let's see the following example: [1,3,4]. Then Alice can choose the third pile, Bob can take the second pile, and then the only candy from the first pile goes to Bob — then Alice has 4 candies, and Bob has 4 candies.

Another example is [1,10,100]. Then Alice can choose the second pile, Bob can choose the first pile, and candies from the third pile can be divided in such a way that Bob takes 54 candies, and Alice takes 46 candies. Now Bob has 55 candies, and Alice has 56 candies, so she has to discard one candy — and after that, she has 55 candies too.

Input
The first line of the input contains one integer 𝑞 (1≤𝑞≤1000) — the number of queries. Then 𝑞 queries follow.

The only line of the query contains three integers 𝑎,𝑏 and 𝑐 (1≤𝑎,𝑏,𝑐≤1016) — the number of candies in the first, second and third piles correspondingly.

Output
Print 𝑞 lines. The 𝑖-th line should contain the answer for the 𝑖 -th query — the maximum number of candies Alice can have after the division, if both Alice and Bob act optimally (of course, Bob will have the same number of candies).

Example
Input
4
1 3 4
1 10 100
10000000000000000 10000000000000000 10000000000000000
23 34 45

Output
4
55
15000000000000000
51
*/
package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	input1, _ := reader.ReadString('\n')
	q, _ := strconv.Atoi(strings.TrimSpace(input1))

	for ; q > 0; q-- {
		input2, _ := reader.ReadString('\n')
		arr := strings.Split(strings.TrimSpace(input2), " ")

		a, _ := strconv.ParseInt(arr[0], 10, 64)
		b, _ := strconv.ParseInt(arr[1], 10, 64)
		c, _ := strconv.ParseInt(arr[2], 10, 64)

		fmt.Println((a + b + c) / 2)
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var q int
	fmt.Scan(&q)
	for ; q > 0; q-- {
		a := make([]uint64, 3, 3)
		fmt.Scan(&a[0], &a[1], &a[2])
		sort.Slice(a, func(i, j int) bool {
			return a[i] < a[j]
		})
		fmt.Println(a[1] + (a[2] - a[1] + a[0]) / 2)
	}
}

package main

import (
	"fmt"
)

func main() {
	var q int
	fmt.Scan(&q)
	for ; q > 0; q-- {
		var a, b, c int64
		fmt.Scan(&a, &b, &c)
		fmt.Println((a + b + c) / 2)
	}
}

package main

import "fmt"

func main(){
    var n, a, b, c, i int64;
    fmt.Scan(&n);
    for ; i < n; i++{
        fmt.Scan(&a, &b, &c);
        fmt.Println((a + b + c) / 2)
    }
}

package main

import "fmt"

func main() {
	var testCase int

	if _, err := fmt.Scan(&testCase); err != nil {

	}

	for testCase > 0 {
		testCase -= 1
		var (
			p1 int64
			p2 int64
			p3 int64
		)
		if _, err := fmt.Scan(&p1, &p2, &p3); err != nil {

		}

		sum := p1 + p2 + p3
		if sum%2 == 0 {
			fmt.Println(sum / 2)
		} else {
			fmt.Println((sum - 1) / 2)
		}
	}
}

package main

import (
	"fmt"
)

func main()  {
	var requests int
	fmt.Scan(&requests)
	for i:=0;i< requests;i++ {
		ledges := make([]uint64, 3)
		for k:=0;k<3;k++ {
			fmt.Scan(&ledges[k])
		}
		var count uint64
		for _,j:= range ledges{
			count += j
		}
		fmt.Printf("%d\n", count/2)
	}
}

package main

import "fmt"
import "sort"

func main() {
	var n int
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		var arr []int64 = make([]int64, 3)
		for i, _ := range arr {
			fmt.Scan(&arr[i])
		}
		sort.Slice(arr, func(i, j int) bool {
			return arr[i] < arr[j]
		})
		A := arr[0]
		B := arr[1]
		C := arr[2] - (B - A)
		fmt.Println(B + C/2)
	}
}

package main
import "fmt"

func main(){
    var q int
    fmt.Scan(&q)
    for i:=0;i<q;i++ {
        var a,b,c int64
        fmt.Scan(&a,&b,&c)
        fmt.Println((a+b+c)/2)
    }
}

package main

import (
	"fmt"
)

func main() {
	var n int
	var a, b, c, result int64
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a, &b, &c)
		result = (a + b + c) / 2
		fmt.Println(result)
	}
}

package main
import "fmt"
func main(){
    var x int
    fmt.Scan(&x)
    for i:=0; i<x; i++{
        var a uint64
        var b uint64
        var c uint64
        fmt.Scan(&a,&b,&c)
        fmt.Println((a+b+c)/2)
    }
}

package main

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

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

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

	for i := 0; i < n; i++ {
		as := make([]int64, 3)
		fmt.Fscanf(input, "%d %d %d\n", &as[0], &as[1], &as[2])

		var sum int64
		for j := range as {
			sum += as[j]
		}
		fmt.Println(sum / 2)
	}
}

package main

import "fmt"

type Triple struct{ a, b, c int64 }

func main() {
	var q int // query
	fmt.Scan(&q)
	t := make([]Triple, q) // triples
	for i := 0; i < q; i++ {
		fmt.Scan(&t[i].a, &t[i].b, &t[i].c)
		res := (t[i].a + t[i].b + t[i].c) / 2
		fmt.Println(res)
	}
}

package main

import "fmt"

func main() {
    var a,b,c,q int64
    fmt.Scan(&q)
    for q>0 {
        fmt.Scan(&a, &b, &c)
        a = (a+b+c)>>1
        fmt.Println(a)
        q-=1
    }
}

package main

import "fmt"

func main() {
	var num int

	_, err := fmt.Scan(&num)
	if err != nil {
	}

	for num > 0 {
		num -= 1
		var candy int64
		var result int64 = 0
		for i := 0; i < 3; i++ {
			_, err := fmt.Scan(&candy)
			if err != nil {
			}
			result += candy
		}
		fmt.Println(result / 2)
	}

}

package main

import "fmt"

func main() {
	var q, a, b, c int64
	fmt.Scan(&q)
	for ; q > 0; q-- {
		fmt.Scan(&a, &b, &c)
		fmt.Println((a + b + c) / 2)
	}
}

package main

import "fmt"

func main() {
	var n int
	var a, b, c int64
	fmt.Scanln(&n)
	for i := 0; i < n;  i++{
		fmt.Scanln(&a, &b, &c)
		fmt.Println((a + b + c)/2)
	}
}

package main

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

func main() {

	//stdin, err := os.Open("test.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("test.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)

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

	for i := 0; i < n; i++ {
		var a, b, c int64
		fmt.Fscanf(reader, "%d %d %d\n", &a, &b, &c)
		fmt.Fprintf(writer, "%d\n", (a+b+c)/2)
	}

	writer.Flush()
}

// Author: sighduck
// URL: https://codeforces.com/contest/1196/problem/A

package main

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

func Solve(a, b, c int64) int64 {
	arr := []int64{a, b, c}
	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
	return (arr[2]-arr[1]+arr[0])/2 + arr[1]
}

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

	var q int
	fmt.Fscanf(reader, "%d\n", &q)
	for i := 0; i < q; i++ {
		var a, b, c int64
		fmt.Fscanf(reader, "%d %d %d\n", &a, &b, &c)
		fmt.Fprintf(writer, "%d\n", Solve(a, b, c))
	}
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	for ; n > 0; n-- {
		var a, b, c uint64
		fmt.Scanf("%d %d %d\n", &a, &b, &c)
		fmt.Println((a + b + c) / 2)
	}
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	for ; n > 0; n-- {
		var a, b, c int64
		fmt.Scanf("%d %d %d\n", &a, &b, &c)
		fmt.Println((a + b + c) / 2)
	}
}

package main

import (
	"fmt"
)

func main() {
	var n, q, x int

	// scr := bufio.NewReader(os.Stdin)
	// ocr := bufio.NewWriter(os.Stdout)

	fmt.Scanln(&q)

	for i := 0; i < q; i++ {
		fmt.Scanln(&n)
		sum := 0
		for j := 0; j < n; j++ {
			fmt.Scanf("%d", &x)
			sum += x
		}
		fmt.Scanln(&x)

		fmt.Println(x / 2)
	}

}

package main

import (
	"fmt"
)

func main() {
	var q, x int

	// scr := bufio.NewReader(os.Stdin)
	// ocr := bufio.NewWriter(os.Stdout)

	fmt.Scanln(&q)

	for i := 0; i < q; i++ {
		sum := 0
		for j := 0; j < 3; j++ {
			fmt.Scanf("%d", &x)
			sum += x
		}
		fmt.Scanln(&x)

		fmt.Println(sum / 2)
	}

}

package main

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

func main() {
	var n, a, b, c int

	scr := bufio.NewReader(os.Stdin)
	// ocr := bufio.NewWriter(os.Stdout)

	fmt.Fscanln(scr, &n)

	for i := 0; i < n; i++ {
		fmt.Fscanln(scr, &a, &b, &c)
		fmt.Println((a + b + c) / 2)
	}

}

package main

import (
	"fmt"
)

func main() {
	var q, x int

	// scr := bufio.NewReader(os.Stdin)
	// ocr := bufio.NewWriter(os.Stdout)

	fmt.Scanln(&q)

	for i := 0; i < q; i++ {
		sum := 0
		for j := 0; j < 3; j++ {
			fmt.Scanf("%d", &x)
			sum += x
		}
		fmt.Scanln(&x)

		fmt.Println(x / 2)
	}

}

package main

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

func main() {
	var t, a, b, c int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		aa := make([]int, 0)
		fmt.Fscan(in, &a, &b, &c)
		aa = append(aa, a)
		aa = append(aa, b)
		aa = append(aa, c)
		sort.Slice(aa, func(i, j int) bool {
			return aa[i] < aa[j]
		})
		left := aa[2] - aa[1] + aa[0]
		fmt.Println(left/2 + aa[1])
	}
}

// A. Three Piles of Candies
/*
Alice and Bob have received three big piles of candies as a gift. Now they want to divide these candies as fair as possible. To do this, Alice takes one pile of candies, then Bob takes one of the other two piles. The last pile is split between Alice and Bob as they want: for example, it is possible that Alice takes the whole pile, and Bob gets nothing from it.

After taking the candies from the piles, if Alice has more candies than Bob, she discards some candies so that the number of candies she has is equal to the number of candies Bob has. Of course, Bob does the same if he has more candies.

Alice and Bob want to have as many candies as possible, and they plan the process of dividing candies accordingly. Please calculate the maximum number of candies Alice can have after this division process (of course, Bob will have the same number of candies).

You have to answer 𝑞 independent queries.

Let's see the following example: [1,3,4]. Then Alice can choose the third pile, Bob can take the second pile, and then the only candy from the first pile goes to Bob — then Alice has 4 candies, and Bob has 4 candies.

Another example is [1,10,100]. Then Alice can choose the second pile, Bob can choose the first pile, and candies from the third pile can be divided in such a way that Bob takes 54 candies, and Alice takes 46 candies. Now Bob has 55 candies, and Alice has 56 candies, so she has to discard one candy — and after that, she has 55 candies too.

Input
The first line of the input contains one integer 𝑞 (1≤𝑞≤1000) — the number of queries. Then 𝑞 queries follow.

The only line of the query contains three integers 𝑎,𝑏 and 𝑐 (1≤𝑎,𝑏,𝑐≤1016) — the number of candies in the first, second and third piles correspondingly.

Output
Print 𝑞 lines. The 𝑖-th line should contain the answer for the 𝑖 -th query — the maximum number of candies Alice can have after the division, if both Alice and Bob act optimally (of course, Bob will have the same number of candies).

Example
Input
4
1 3 4
1 10 100
10000000000000000 10000000000000000 10000000000000000
23 34 45

Output
4
55
15000000000000000
51
*/
package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	input1, _ := reader.ReadString('\n')
	q, _ := strconv.Atoi(strings.TrimSpace(input1))

	for ; q > 0; q-- {
		input2, _ := reader.ReadString('\n')
		arr := strings.Split(strings.TrimSpace(input2), " ")

		a, _ := strconv.Atoi(arr[0])
		b, _ := strconv.Atoi(arr[1])
		c, _ := strconv.Atoi(arr[2])

		fmt.Println((a + b + c) / 2)
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var q int
	fmt.Scan(&q)
	for ; q > 0; q-- {
		a := make([]int, 3, 3)
		fmt.Scan(&a[0], &a[1], &a[2])
		sort.Ints(a)
		fmt.Println(a[1] + ((a[2] - (a[1] - a[0])) / 2))
	}
}

package main

import (
	"fmt"
)

func main()  {
	var requests int
	fmt.Scan(&requests)
	for i:=0;i< requests;i++ {
		ledges := make([]int, 3)
		for k:=0;k<3;k++ {
			fmt.Scan(&ledges[k])
		}
		count := 0
		for _,j:= range ledges{
			count += j
		}
		fmt.Printf("%d\n", count/2)
	}

}

package main

import (
	"fmt"
)

func main()  {
	var requests int
	fmt.Scan(&requests)
	for i:=0;i< requests;i++ {
		ledges := make([]int, 3)
		for k:=0;k<3;k++ {
			fmt.Scan(&ledges[k])
		}
		count := 0
		for _,j:= range ledges{
			count += j
		}
		fmt.Println(count/2)
	}

}

package main

import "fmt"
import "sort"

func main() {
	var n int
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		var arr []int = make([]int, 3)
		for i, _ := range arr {
			fmt.Scan(&arr[i])
		}
		sort.Ints(arr)
		A := arr[0]
		B := arr[1]
		C := arr[2] - (B - A)
		fmt.Println(B + C/2)
	}
}

package main
import "fmt"

func main(){
    var q int
    fmt.Scan(&q)
    for i:=0;i<q;i++ {
        var a,b,c int
        fmt.Scan(&a,&b,&c)
        fmt.Println("%d",(a+b+c)/2)
    }
}

package main
import "fmt"

func main(){
    var q int
    fmt.Scan(&q)
    for i:=0;i<q;i++ {
        var a,b,c int
        fmt.Scan(&a,&b,&c)
        fmt.Println((a+b+c)/2)
    }
}

package main

import (
	"fmt"
)

func main() {
	var q int // query
	fmt.Scan(&q)

	for i := 0; i < q; i++ {
		var a, b, c int
		fmt.Scan(&a, &b, &c)
		res := (a + b + c) / 2
		fmt.Println(res)
	}
}

package main

import (
	"fmt"
)

type Triple struct{ a, b, c int }

func main() {
	var q int // query
	fmt.Scan(&q)
	t := make([]Triple, q) // triples
	for i := 0; i < q; i++ {
		fmt.Scan(&t[i].a, &t[i].b, &t[i].c)
	}

	for i := 0; i < q; i++ {
		res := (t[i].a + t[i].b + t[i].c) / 2
		fmt.Println(res)
	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)

func ri() (n int) {
	sc.Scan()
	for _, v := range sc.Bytes() {
		n = n*10 + int(v-48)
	}
	return
}

func main() {
	sc.Split(bufio.ScanWords)
	q := ri() // query

	for i := 0; i < q; i++ {
		a, b, c := ri(), ri(), ri()
		res := (a + b + c) / 2
		fmt.Println(res)
	}
}

package main

import "fmt"

type Triple struct{ a, b, c int }

func main() {
	var q int // query
	fmt.Scan(&q)
	t := make([]Triple, q) // triples
	for i := 0; i < q; i++ {
		fmt.Scan(&t[i].a, &t[i].b, &t[i].c)
		res := (t[i].a + t[i].b + t[i].c) / 2
		fmt.Println(res)
	}
}

// 15000000000000000

// 2147483647
// 9223372036854775807

package main

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

func main() {

	//stdin, err := os.Open("test.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("test.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)

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

	for i := 0; i < n; i++ {
		var a, b, c int
		fmt.Fscanf(reader, "%d %d %d\n", &a, &b, &c)
		fmt.Fprintf(writer, "%d\n", (a+b+c)/2)
	}

	writer.Flush()
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	for ; n > 0; n-- {
		var a, b, c int
		fmt.Scan(&a, &b, &c)
		fmt.Println((a + b + c) / 2)
	}
}

package main

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

func solve(s string) int {
	p := strings.Split(s, " ")
	a, _ := strconv.Atoi(p[0])
	b, _ := strconv.Atoi(p[1])
	c, _ := strconv.Atoi(p[2])
	return (a + b + c) / 2
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		scanner.Scan()
		fmt.Println(solve(scanner.Text()))
	}
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	for ; n > 0; n-- {
		var a, b, c int
		fmt.Scanf("%d %d %d\n", &a, &b, &c)
		fmt.Println((a + b + c) / 2)
	}
}

package main

import (
	"fmt"
)

func main() {
	var n, a, b, c int
	fmt.Scan(&n)
	for ; n > 0; n-- {
		fmt.Scan(&a, &b, &c)
		fmt.Println((a + b + c) / 2)
	}
}


Shubham has a binary string $$$s$$$. A binary string is a string containing only characters "0" and "1".He can perform the following operation on the string any amount of times:   Select an index of the string, and flip the character at that index. This means, if the character was "0", it becomes "1", and vice versa. A string is called good if it does not contain "010" or "101" as a subsequence  — for instance, "1001" contains "101" as a subsequence, hence it is not a good string, while "1000" doesn't contain neither "010" nor "101" as subsequences, so it is a good string.What is the minimum number of operations he will have to perform, so that the string becomes good? It can be shown that with these operations we can make any string good.A string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters.

The first line of the input contains a single integer $$$t$$$ $$$(1\le t \le 100)$$$ — the number of test cases. Each of the next $$$t$$$ lines contains a binary string $$$s$$$ $$$(1 \le |s| \le 1000)$$$.

For every string, output the minimum number of operations required to make it good.

NoteIn test cases $$$1$$$, $$$2$$$, $$$5$$$, $$$6$$$ no operations are required since they are already good strings.For the $$$3$$$rd test case: "001" can be achieved by flipping the first character  — and is one of the possible ways to get a good string.For the $$$4$$$th test case: "000" can be achieved by flipping the second character  — and is one of the possible ways to get a good string.For the $$$7$$$th test case: "000000" can be achieved by flipping the third and fourth characters  — and is one of the possible ways to get a good string.

package main

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

func readline(r *bufio.Reader) string {
	l, _, err := r.ReadLine()
	if err != nil {
		panic(err)
	}
	return string(l)
}

func main() {
	var t int
	_, err := fmt.Scanf("%d\n", &t)
	if err != nil {
		panic(err)
	}

	r := bufio.NewReader(os.Stdin)
	for i := 0; i < t; i++ {
		s := readline(r)
		n := len(s)

		ks := make([]int, n+1)
		ls := make([]int, n+1)

		var k, l int
		for i := 1; i <= n; i++ {
			ks[i] = ks[i-1]
			ls[i] = ls[i-1]

			isZero := s[i-1] == '0'
			if isZero {
				ks[i]++
				k++
			} else {
				ls[i]++
				l++
			}
		}

		ans := math.MaxInt32
		for i := 0; i <= n; i++ {
			c := ls[i] + (k - ks[i])
			if c < ans {
				ans = c
			}

			c = ks[i] + (l - ls[i])
			if c < ans {
				ans = c
			}
		}
		fmt.Printf("%d\n", ans)
	}
}

package main

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

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 main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)

	for tc > 0 {
		tc--
		s, _ := reader.ReadString('\n')
		res := solve(s[:len(s)-1])
		fmt.Printf("%d\n", res)
	}
}

func solve(s string) int {
	//fmt.Fprintln(os.Stderr, s)
	// all 0, all 1,
	// half 0, half 1
	var one int
	n := len(s)
	cnt := make([]int, n)

	for i := 0; i < n; i++ {
		if s[i] != '0' && s[i] != '1' {
			n = i
			break
		}
		one += int(s[i] - '0')
		cnt[i] = one
	}

	ans := min(cnt[n-1], n-cnt[n-1])

	for i := n - 2; i >= 0; i-- {
		right := cnt[n-1] - cnt[i]
		left := cnt[i]
		// 01
		ans = min(ans, n-1-i-right+left)
		// or 10
		ans = min(ans, right+(i+1)-left)
	}

	return ans
}

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		s := ""
		fmt.Fscanf(in, "%s\n", &s)
		n := len(s)
		a := make([]int, n)
		if s[0] == '0' {
			a[0] = 1
		}
		for i := 1; i < n; i++ {
			a[i] = a[i-1]
			if s[i] == '0' {
				a[i]++
			}
		}
		ans := n
		allZero := a[n-1]
		allOne := n - a[n-1]
		for i := 0; i <= n; i++ {
			// before all 0
			beforeZero := 0
			if i == 0 {
				beforeZero = 0
			} else {
				beforeZero = a[i-1]
			}
			beforeOne := i - beforeZero
			afterZero := allZero - beforeZero
			afterOne := allOne - beforeOne
			tmpAns := beforeOne + afterZero
			if tmpAns < ans {
				ans = tmpAns
			}
			tmpAns = beforeZero + afterOne
			if tmpAns < ans {
				ans = tmpAns
			}
			// before all 1
		}
		fmt.Fprintf(out, "%d\n", ans)
	}
	return
}

package main

import (
	"fmt"
	"math"
)

var (
	n     int
	input string
)

func main() {
	fmt.Scanln(&n)
	for i := 0; i < n; i++ {
		fmt.Scanln(&input)
		ans := len(input)
		one, zero := 0, 0
		for _, c := range input {
			if c == '1' {
				one++
			} else {
				zero++
			}
		}
		x0, x1 := 0, 0
		for _, c := range input {
			if c == '1' {
				x1++
			} else {
				x0++
			}
			r1 := x0 + (one - x1)
			r2 := x1 + (zero - x0)
			res := math.Min(float64(r1), float64(r2))
			ans = int(math.Min(res, float64(ans)))
		}
		fmt.Println(ans)
	}
}

/*
URL:
https://codeforces.com/problemset/problem/1363/B
*/

package main

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

/********** 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 (
	// General purpose
	MOD          = 1000000000 + 7
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	ReadString = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

var (
	t int
	S []rune
	n int
)

func main() {
	t = ReadInt()
	for i := 0; i < t; i++ {
		S = ReadRuneSlice()
		n = len(S)

		solve()
	}
}

func solve() {
	if n <= 2 {
		fmt.Println(0)
		return
	}

	ans := 100000
	A, B := make([]rune, n), make([]rune, n)
	for i := 0; i < n; i++ {
		A[i] = '0'
		B[i] = '1'
	}

	ChMin(&ans, diff(S, A))
	ChMin(&ans, diff(S, B))
	for i := 0; i < n; i++ {
		A[i] = '1'
		B[i] = '0'
		ChMin(&ans, diff(S, A))
		ChMin(&ans, diff(S, B))
	}
	fmt.Println(ans)
}

func diff(S, T []rune) int {
	res := 0
	for i := 0; i < n; i++ {
		if S[i] != T[i] {
			res++
		}
	}
	return res
}

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

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

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

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

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

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

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

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

package main

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

var nTest int
var s string

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

	fmt.Fscan(in, &nTest)
	for t := 0; t < nTest; t++ {
		fmt.Fscan(in, &s)
		l := len(s)
		cnt0 := make([]int, l+2)
		cnt1 := make([]int, l+2)

		for i := 0; i < l; i++ {
			if s[i] == '0' {
				cnt0[i+1] = cnt0[i] + 1
				cnt1[i+1] = cnt1[i]
			} else {
				cnt1[i+1] = cnt1[i] + 1
				cnt0[i+1] = cnt0[i]
			}
		}
		cnt1[l+1] = cnt1[l]
		cnt0[l+1] = cnt0[l]

		res := math.MaxInt32
		for i := 1; i <= l; i++ {
			res01 := cnt0[i] - cnt0[0] + cnt1[l] - cnt1[i+1]
			res10 := cnt1[i] - cnt1[0] + cnt0[l] - cnt0[i+1]

			if res > res01 {
				res = res01
			}
			if res > res10 {
				res = res10
			}
		}

		fmt.Fprintln(out, res)
	}

	out.Flush()
}

package main

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

var nTest int
var s string

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

	fmt.Fscan(in, &nTest)
	for t := 0; t < nTest; t++ {
		fmt.Fscan(in, &s)
		l := len(s)
		cnt0 := make([]int, l+1)
		cnt1 := make([]int, l+1)

		for i := 1; i <= l; i++ {
			if s[i-1] == '0' {
				cnt0[i] = cnt0[i-1] + 1
				cnt1[i] = cnt1[i-1]
			} else {
				cnt1[i] = cnt1[i-1] + 1
				cnt0[i] = cnt0[i-1]
			}
		}

		res := math.MaxInt32
		for i := 0; i < l; i++ {
			res01 := cnt0[i] + cnt1[l] - cnt1[i+1]
			res10 := cnt1[i] + cnt0[l] - cnt0[i+1]

			if res > res01 {
				res = res01
			}
			if res > res10 {
				res = res10
			}
		}

		fmt.Fprintln(out, res)
	}

	out.Flush()
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1363B(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}

	var t int
	var S []byte
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &S)
		n := len(S)
		s := make([][2]int, n+1)
		for i, b := range S {
			s[i+1] = s[i]
			s[i+1][b-'0']++
		}
		ans := n
		for _, p := range s {
			ans = min(ans, min(p[1]+s[n][0]-p[0], p[0]+s[n][1]-p[1]))
		}
		Fprintln(out, ans)
	}
}

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

package main

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

func min(vars ...int) int {
	res := vars[0]
	for _, value := range vars {
		if value < res {
			res = value
		}
	}
	return res
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	scan := func(x ...interface{}) (n int, err error) {
		return fmt.Fscan(in, x...)
	}
	println := func(x ...interface{}) (n int, err error) {
		return fmt.Fprintln(out, x...)
	}

	var t int
	for scan(&t); t > 0; t-- {
		var s string
		scan(&s)
		prefSums := make([]int, len(s))
		for i := 0; i < len(s); i++ {
			if i > 0 {
				prefSums[i] = prefSums[i-1]
			}
			prefSums[i] += int(s[i] - '0')
		}
		ans := len(s)
		wholeSum := prefSums[len(prefSums)-1]
		for i, prefSum := range prefSums {
			sufSum := wholeSum - prefSum
			prefLen, sufLen := i+1, len(prefSums)-i-1
			cost := min(
				prefSum+sufSum,
				prefSum+(sufLen-sufSum),
				(prefLen-prefSum)+sufSum,
				(prefLen-prefSum)+(sufLen-sufSum))
			if cost < ans {
				ans = cost
			}
		}
		println(ans)
	}
}

package main

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

func min(a ...int) int {
	ret := a[0]
	for i := 1; i < len(a); i++ {
		if ret > a[i] {
			ret = a[i]
		}
	}
	return ret
}

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var t int
	fmt.Fscan(r, &t)
	for ; t > 0; t-- {
		var s string
		fmt.Fscan(r, &s)
		ans := len(s)
		for i := 0; i < len(s); i++ {
			var l0, l1, r0, r1 int
			for j := 0; j < i; j++ {
				if s[j] == '0' {
					l1++
				} else {
					l0++
				}
			}
			for j := i; j < len(s); j++ {
				if s[j] == '0' {
					r1++
				} else {
					r0++
				}
			}
			ans = min(ans, l0+r0, l0+r1, l1+r0, l1+r1)
		}
		fmt.Fprintln(w, ans)
	}
}

package main

import (
	"fmt"
)

func main() {
	var tt int
	fmt.Scanln(&tt)
	for t := 0; t < tt; t++ {
		var s string
		fmt.Scanln(&s)
		n := len(s)
		var count1, count0 int = 0, 0
		one := make([]int, n)
		zero := make([]int, n)
		for i := 0; i < n; i++ {
			if s[i] == '1' {
				count1++
			} else {
				count0++
			}
			one[i] = count1
			zero[i] = count0
		}

		var ans int = 1005
		for i := 0; i < n; i++ {
			var c1, c2 int
			c1 = one[i] + (count0 - zero[i])
			c2 = (count1 - one[i]) + zero[i]

			if ans > c1 {
				ans = c1
			}
			if ans > c2 {
				ans = c2
			}
		}
		fmt.Println(ans)
	}
}

package main

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

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 main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)

	for tc > 0 {
		tc--
		s, _ := reader.ReadString('\n')
		fmt.Println(solve(s[:len(s)-1]))
	}
}

func solve(s string) int {
	fmt.Fprintln(os.Stderr, s)
	// all 0, all 1,
	// half 0, half 1
	var one int
	n := len(s)
	cnt := make([]int, n)

	for i := 0; i < n; i++ {
		one += int(s[i] - '0')
		cnt[i] = one
	}

	ans := min(cnt[n-1], n-cnt[n-1])

	for i := n - 2; i >= 0; i-- {
		right := cnt[n-1] - cnt[i]
		left := cnt[i]
		// 01
		ans = min(ans, n-1-i-right+left)
		// or 10
		ans = min(ans, right+(i+1)-left)
	}

	return ans
}

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

package main

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

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 main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)

	for tc > 0 {
		tc--
		s, _ := reader.ReadString('\n')
		res := solve(s[:len(s)-1])
		fmt.Printf("%d\n", res)
	}
}

func solve(s string) int {
	//fmt.Fprintln(os.Stderr, s)
	// all 0, all 1,
	// half 0, half 1
	var one int
	n := len(s)
	cnt := make([]int, n)

	for i := 0; i < n; i++ {
		one += int(s[i] - '0')
		cnt[i] = one
	}

	ans := min(cnt[n-1], n-cnt[n-1])

	for i := n - 2; i >= 0; i-- {
		right := cnt[n-1] - cnt[i]
		left := cnt[i]
		// 01
		ans = min(ans, n-1-i-right+left)
		// or 10
		ans = min(ans, right+(i+1)-left)
	}

	return ans
}

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

package main

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

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 main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)

	for tc > 0 {
		tc--
		s, _ := reader.ReadString('\n')
		fmt.Println(solve(s[:len(s)-1]))
	}
}

func solve(s string) int {
	// all 0, all 1,
	// half 0, half 1
	var one int
	n := len(s)
	cnt := make([]int, n)

	for i := 0; i < n; i++ {
		one += int(s[i] - '0')
		cnt[i] = one
	}

	ans := min(cnt[n-1], n-cnt[n-1])

	for i := n - 2; i >= 0; i-- {
		right := cnt[n-1] - cnt[i]
		left := cnt[i]
		// 01
		ans = min(ans, n-1-i-right+left)
		// or 10
		ans = min(ans, right+(i+1)-left)
	}

	return ans
}

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

package main

import (
	"fmt"
	"math"
	"strings"
)

var (
	n     int
	input string
)

func main() {
	fmt.Scanln(&n)
	for i := 0; i < n; i++ {
		fmt.Scanln(&input)
		ans := 0
		//01
		tmp := strings.TrimLeft(input, "0")
		tmp = strings.TrimRight(tmp, "1")
		a, b := 0, 0
		for _, c := range tmp {
			if c == '0' {
				a++
			} else {
				b++
			}
		}
		ans = int(math.Min(float64(a), float64(b)))
		//10
		tmp = strings.TrimLeft(input, "1")
		tmp = strings.TrimRight(tmp, "0")
		a, b = 0, 0
		for _, c := range tmp {
			if c == '0' {
				a++
			} else {
				b++
			}
		}
		res := int(math.Min(float64(a), float64(b)))
		ans = int(math.Min(float64(res), float64(ans)))
		fmt.Println(ans)
	}
}

package main

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

func min(vars ...int) int {
	res := vars[0]
	for _, value := range vars {
		if value < res {
			res = value
		}
	}
	return res
}

func main() {
	in := bufio.NewReader(os.Stdin)
	scan := func(x ...interface{}) (n int, err error) {
		return fmt.Fscan(in, x...)
	}

	var t int
	for scan(&t); t > 0; t-- {
		var s string
		scan(&s)
		prefSums := make([]int, len(s))
		for i := 0; i < len(s); i++ {
			if i > 0 {
				prefSums[i] = prefSums[i-1]
			}
			prefSums[i] += int(s[i] - '0')
		}
		ans := len(s)
		wholeSum := prefSums[len(prefSums)-1]
		for i, prefSum := range prefSums {
			sufSum := wholeSum - prefSum
			prefLen, sufLen := i+1, len(prefSums)-i-1
			cost := min(
				prefSum+sufSum,
				prefSum+(sufLen-sufSum),
				(prefLen-prefSum)+sufSum,
				(prefLen-prefSum)+(sufLen-sufSum))
			if cost < ans {
				ans = cost
			}
		}
		println(ans)
	}
}


Qpwoeirut has taken up architecture and ambitiously decided to remodel his city.Qpwoeirut's city can be described as a row of $$$n$$$ buildings, the $$$i$$$-th ($$$1 \le i \le n$$$) of which is $$$h_i$$$ floors high. You can assume that the height of every floor in this problem is equal. Therefore, building $$$i$$$ is taller than the building $$$j$$$ if and only if the number of floors $$$h_i$$$ in building $$$i$$$ is larger than the number of floors $$$h_j$$$ in building $$$j$$$.Building $$$i$$$ is cool if it is taller than both building $$$i-1$$$ and building $$$i+1$$$ (and both of them exist). Note that neither the $$$1$$$-st nor the $$$n$$$-th building can be cool.To remodel the city, Qpwoeirut needs to maximize the number of cool buildings. To do this, Qpwoeirut can build additional floors on top of any of the buildings to make them taller. Note that he cannot remove already existing floors.Since building new floors is expensive, Qpwoeirut wants to minimize the number of floors he builds. Find the minimum number of floors Qpwoeirut needs to build in order to maximize the number of cool buildings.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$3 \le n \le 10^5$$$) — the number of buildings in Qpwoeirut's city. The second line of each test case contains $$$n$$$ integers $$$h_1, h_2, \ldots, h_n$$$ ($$$1 \le h_i \le 10^9$$$) — the number of floors in each of the buildings of the city. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, print a single integer — the minimum number of additional floors Qpwoeirut needs to build in order to maximize the number of cool buildings.

NoteIn the first test case, it is optimal for Qpwoeirut to make the second building cool by building $$$2$$$ additional floors on top of it, making it taller than both of its adjacent buildings. The final heights of buildings will be $$$[2, \underline{3}, 2]$$$.  In the second test case, the number of cool buildings is already maximized, so Qpwoeirut does not need to do anything.In the third test case, it is optimal for Qpwoeirut to make the third and fifth buildings cool by building $$$2$$$ additional floors onto the third building and $$$1$$$ additional floor onto the fifth building. The final heights of buildings will be $$$[3, 1, \underline{6}, 5, \underline{6}, 2]$$$.  It can be shown that it is impossible to make more than $$$2$$$ of the buildings cool, or to make $$$2$$$ buildings cool using fewer than $$$3$$$ additional floors.In the fourth test case, Qpwoeirut can either make the second building cool, or he can make the third building cool. Either way, he will be building $$$3$$$ additional floors and maximizing the number of cool buildings. The final heights of buildings will be $$$[4, 2, \underline{4}, 3, 5, 3, 6, 1]$$$ or $$$[4, \underline{5}, 1, 3, 5, 3, 6, 1]$$$.

package main

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

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

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

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

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

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

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

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

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

package main

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

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

	}
	return y

}

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

	}
	return y

}

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

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

package main

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

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

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

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

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

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

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

	}
}

package main

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

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

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

// 00
package main

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

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

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

	}
}

package main

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

var cf = true

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

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

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

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

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

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

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

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

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

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

var exists = struct{}{}

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

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

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

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

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

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

type Ordered interface {
	Integer | Float | ~string
}

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

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

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

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

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

package main

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

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

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

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

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

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

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

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

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

	for test > 0 {
		test--

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

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

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

	out.Flush()
}

package main

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

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

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

	var T int
	var n int
	var x int

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

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

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

		Fprintln(out, ans)
	}
}

package main

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

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

	var buf bytes.Buffer

	tc := readNum(reader)

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

	fmt.Print(buf.String())
}

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

func normalize(s string) string {

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

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

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

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

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

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

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

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

	return i
}

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

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

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

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

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

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

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

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

	return res
}

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

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

package main

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

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

func main() {
	defer out.Flush()

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

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

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

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

		// fmt.Println(i, tmp)

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

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

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

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

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

package main

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

const MAX =  1e5+10

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

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

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

package main

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

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

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

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

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

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

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

package main

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

const MOD int64 = 1000000007

var n int

var h[]int

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

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

func main() {

	read := bufio.NewReader(os.Stdin)

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

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

		fmt.Fscan(read, &n)

		h = make([]int, n)

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

		fmt.Println(solve())
	}
}

type pair struct {
	v int
	idx []int
}

func solve() int64 {

	if n % 2 == 1 {

		var cnt int64

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

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

			cur := h[i]

			m := max(prev, next)

			if cur == m {
				cnt++

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

		return cnt

	} else {

		var ans int64 = 1e18

		var left int64 = 0

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

		ans = min(ans, left)

		var right int64

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

			left-=int64(v)

			v2 := getForIndex(k + 2)

			right += int64(v2)

			ans = min(ans, left + right)
		}

		return ans
	}
}

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

	cur := h[x]

	m := max(prev, next)

	var ans int

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

	return ans
}

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

	return b
}

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

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

package main

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

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

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

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

	need := make([]int64, n)

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

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

	min := sumR
	sumL := int64(0)

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

	put64ln(min)
}

const (
	once       = false
	maxBufSize = 10000000
)

var (
	splitFunc = bufio.ScanWords
)

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

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

	if once {
		solve()
		return
	}

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

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

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

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

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

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

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

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

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

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

func scan() {
	in.Scan()
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

package main

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

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

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

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

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

package main

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

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

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

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

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

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

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

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

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

package main

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

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

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

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

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

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

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

		fmt.Fprintln(w, res)
	}
}

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

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

package main

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

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

	}
	return y

}

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

	}
	return y

}

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

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

package main

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

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

	}
	return y

}

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

	}
	return y

}

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

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

package main

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

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

	}
	return y

}

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

	}
	return y

}

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

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

package main

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

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

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

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

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

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

	}
}

package main

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

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

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

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

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

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

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

package main

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

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

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

package main

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

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

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

// 00
package main

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

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

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

	}
}

package main

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

var cf = true

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

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

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

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

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

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

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

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

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

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

var exists = struct{}{}

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

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

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

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

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

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

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

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

package main

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

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

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

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

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

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

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

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

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

	for test > 0 {
		test--

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

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

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

	out.Flush()
}

package main

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

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

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

	var T int
	var n int
	var x int

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

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

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

		Fprintln(out, ans)
	}
}