var t = readline();
    for(var i = 0 ; i < t ; i++){
        var s = readline().split('');
        firstCounter = 0 ; secondCounter = 0;
        s.forEach((e) => {
          e == 0 ? firstCounter++ : secondCounter++;
        });
        if(s.length < 3){
            print('0');
        }
        else{
        if(firstCounter > secondCounter){
            print(secondCounter);
        }
        else if(firstCounter < secondCounter){
            print(firstCounter);
        }
        else{
            print(firstCounter - 1);
        }
        }
    }

'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readLine() {
    return inputString[currentLine++];
}
/* let arr = readLine().split(" ").map((x) => {return parseInt(x)});
let test = readLine();
while(test--)
{

}
*/
function main() {
    let test = readLine();
    while(test--)
        {
            let s = readLine();

            let count_0=0;
            let count_1=0;

        for(let i=0;i<s.length;i++){
            if(s[i]=='1'){
            count_1++;
            }
            else {
                count_0++;
            }
        }
        if(count_0==count_1 && count_0==1) console.log(0);
        else if(count_0==count_1 && count_0>1) console.log(count_0 - 1);
        else if(count_0>count_1)console.log(count_1);
        else console.log(count_0);

        }
    return 0;
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', stdin => inputString += stdin);

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(s => s.trim());
    main();
});

function readline() {
    return inputString[currentLine++];
}

function output(x) {
    if (typeof x !== 'string') {
        x = x.toString();
    }
    //process.stdout.write(x);   // without auto '\n' (newline)
    console.log(x);  // with auto '\n' (newline)
}

function main() {
    let N = readline();

    for (let test = 0; test < N; test++) {
        let s = readline().split('');
        let z = s.filter(c => c === '0').length;
        let o = s.length - z;

        if (z === o) output(z - 1); else output(Math.min(z, o));
    }
}

let data = '';
process.stdin.on('data', c => data += c);

function* nextLine() {
	let lines = data.trim().split(/\n/g);
	for(let l of lines){
		yield l.trim();
	}
}

let nl = nextLine();

nl.elem = function(funParse) {
		return funParse(this.next().value);
	};

nl.array = function(funParse) {
		return this.next().value.split(/\s/g).map(funParse||(x => x));
	};

nl.num = function() {
		return nl.elem(Number);
	};

nl.nums = function() {
		return nl.array(Number);
	};

nl.line = function() {
		return this.next().value;
	};

const sol = () => {
	//your code this
	let t = nl.num();
	let ans = [];
	for(let i = 0; i < t; i++){
		let a = nl.line().split('');
		let c0 = a.reduce((acc,  e) => (e === '0')?acc + 1:acc, 0);
		let c1 = a.length - c0;
		if ((c0 == c1 && a.length == 2) || a.length == 1) {
			ans.push(0);
		} else {
			if (c0 < c1) {
				ans.push(c0);
			} else if (c0 > c1) {
				ans.push(c1);
			} else {
				ans.push(c1 - 1);
			}
		}
	}
	console.log(ans.join('\n'));
};

process.stdin.on('end', sol);

const readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})
const lines = []
rl.on('line', (input) => {
    lines.push(input);
})
rl.on('close', () => {
// (function() {
    // const lines = require('fs').readFileSync('test.in', 'utf8').split('\n')
    let l = 0;
    let t = +lines[l++]
    const output = []
    for (let i = 0; i < t; i++) {
        const str = lines[l++]
        output[i] = solve(str)
    }
    console.log(output.join('\n'))
// })()
})

function solve(str) {
    let c0 = 0
    for (let i = 0; i < str.length; i++) {
        if (str[i] === '0') c0++
    }
    const c1 = str.length - c0
    return c0 === c1 ? Math.max(0, c0 - 1) : Math.min(c0, c1)
}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;
function readline(){
    return input[currentline++];
}

var T = readline();
for(var q=1;q<=T;q++) {
            var s = readline();
            var pref = []
            var prefo = []
            prefo[-1] = 0;
            pref[-1] = 0;
            for(var i=0;i<s.length;i++) {
            	pref[i] = pref[i-1] + (s[i]=='0' ? 1 : 0)
            	prefo[i] = prefo[i-1] + (s[i]=='1' ? 1 : 0)
            }
            var ans = -10000;
            var i=0;
            var j=s.length-1;
            while(i<=j) {
            	var v = prefo[j]-prefo[i-1]
            	var x = pref[j]-pref[i-1]
            	if(v > x) {
            		ans = Math.max(ans, x)
            	}

            	v = prefo[j-1]-prefo[i-1]
            	x = pref[j-1]-pref[i-1]
            	if(v > x) {
            		ans = Math.max(ans, x)
            	}

            	v = prefo[j]-prefo[i]
            	x = pref[j-1]-pref[i]
            	if(v > x) {
            		ans = Math.max(ans, x)
            	}
            	j--;
            	i++;
            }
            i=0;
            j=s.length-1;
            while(i<=j) {
            	var v = pref[j]-pref[i-1]
            	var x = prefo[j]-prefo[i-1]
            	if(v > x) {
            		ans = Math.max(ans, x)
            	}

            	v = pref[j-1]-pref[i-1]
            	x = prefo[j-1]-prefo[i-1]
            	if(v > x) {
            		ans = Math.max(ans, x)
            	}

            	v = pref[j]-pref[i]
            	x = prefo[j-1]-prefo[i]
            	if(v > x) {
            		ans = Math.max(ans, x)
            	}
            	j--;
            	i++;
            }

            console.log(ans)
}

let input = "";
process.stdin.on("data", (chunk) => {
  input += chunk;
});
process.stdin.on("end", () => {
  let lines = input.trim().split("\n");
  let t = parseInt(lines[0].trim());
  let i = 1;
  while (t--) {
    let string = lines[i++].trim();
    //console.log(string);
    let countOne = 0;
    let countTwo = 0;
    for (let i = 0; i < string.length; i++) {
      if (string[i] === "1") {
        countOne++;
      } else if (string[i] === "0") {
        countTwo++;
      }
    }
    //console.log(countOne, countTwo);
    if (countOne === countTwo) {
      console.log(countOne - 1);
    } else {
      console.log(Math.min(countOne, countTwo));
    }
  }
});

const main = () => {

	// either take the whole string or 1 char less
	var t = readInt();
	while (t--) {
		s = readline();
		var cnts = [0, 0];
		for (var c of s)
			cnts[parseInt(c)]++;
		var a = cnts[0];
		var b = cnts[1];
		if (a > b) {
			var temp = a;
			a = b;
			b = temp;
		}
		if (a === b)
			var ans = a - 1;
		else
			ans = a;
		print(ans);
	}

	return;
}

const readInt = () => parseInt(readline());
const readString = () => readline();
const readIntArr = () => readline().split(" ").map(zzzz => parseInt(zzzz));
const parseLong = (string) => { // javascript's largest long is < (1<<62)
	var n = string.length;
	var res = 0;
	for (var i = 0; i < n; i++) {
		res *= 10;
		res += parseInt(string[i]);
	}
	return res;
}
const readLong = () => parseLong(readLong());
const readLongArr = () => readline().split(" ").map(zzzz => parseLong(zzzz));
const oneLineArrayPrint = (arr) => print(arr.map(zzzz => zzzz.toString()).join(' '));
const multiLineArrayPrint = (arr) => print(arr.map(zzzz => zzzz.toString()).join('\n'));
const multiLineArrayOfArraysPrint = (arr) => print(arr.map(yyyy => yyyy.map(zzzz => zzzz.toString()).join(' ')).join('\n'));
const makeArr = (defaultVal, dimensionsArr) => {
	var n = dimensionsArr.length;
	if (n === 1) return Array(dimensionsArr[0]).fill(defaultVal);
	else {
		var temp = [];
		for (var i = 0; i < dimensionsArr[0]; i++)
			temp.push(makeArr(defaultVal, dimensionsArr.slice(1)));
		return temp;
	}
}

for (_ = 0; _ < 1; _++) {
	main();
}

// newline for testing
 //newline for testing
// codeforces js cannot use "let" keyword. use "var" instead
// codeforces js cannot destructure arrays

var lines = parseInt(readline());

for (var i = 0; i < lines; i++) {
  print(minority(readline()));
}

function minority(s) {
  var zeros = 0;
  for (var i = 0; i < s.length; i++) {
    if (s[i] == "0") zeros++;
  }
  var ones = s.length - zeros;
  if (ones == zeros) return Math.max(0, zeros - 1);
  return Math.min(ones, zeros);
}

t= parseInt(readline()) ;
while (t--) {
	s = readline ();
	a = 0 , b = 0 ;
	for ( i =  0 ; i < s.length ; i ++ )
	       (s[i] == '1') ? a++ : b++ ;
	 (a == b ) ? print(Math.min(a,b) -1 ) : print(Math.min(a,b));
	}

let i = '';
process.stdin.on('data', c => i += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = i.split(EOL);
    var n = lines[0];
    for(var j = 1; j <= n; j++){
        var k = lines[j].split('').map(Number);
        var one = 0;
        var zero = 0;
        for(var l = 0; l < k.length; l++){
            if(k[l] == 1){
                one++;
            }else{
                zero++;
            }
        }
        if(one > zero){
            console.log(zero);
        }else if(one < zero){
            console.log(one);
        }else{
            console.log(k.length / 2 - 1);
        }
    }
});

let i = '';
process.stdin.on('data', c => i += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = i.split(EOL);
    var n = lines[0];
    for(var j = 1; j <= n; j++){
        var one = 0;
        var zero = 0;
        var k = lines[j].split('').map(Number);
        for(var l = 0; l < k.length; l++){
            if(k[l] === 0){
                zero++;
            }else{
                one++;
            }
        }
        if(zero > one){
            console.log(one);
        }else if(zero < one){
            console.log(zero);
        }else{
            console.log(Math.floor(k.length / 2) - 1);
        }
    }
});

let i = '';
process.stdin.on('data', c => i += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = i.split(EOL);
    var n = lines[0];
    for(j = 1; j <= n; j++){
        var h = lines[j].split('').map(Number);
        var zero = 0;
        var one = 0;
        for(k = 0; k < h.length; k++){
            if(h[k] == 1){
                one++;
            }else{
                zero++;
            }
        }
        if(one > zero){
            console.log(zero);
        }else if(one < zero){
            console.log(one);
        }else{
            console.log(h.length / 2 - 1);
        }
    }
});

'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readLine() {
    return inputString[currentLine++];
}

function main(){
    let t = readLine();
    while(t--){
        let s = readLine();
        let n = 0,m = 0;
        for(let i = 0; i < s.length; i++){
            if(s[i] == '1') n++;
            else m++;
        }
        if(m == n && m == 1) console.log(0);
        else if(m == n && m > 1) console.log(m - 1);
        else if(m > n) console.log(n);
        else console.log(m);

    }
    return 0;
}

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = []; var outputList = [];var retcode = new Set();
read.on('line', function(input){
	var tmp = input.split(' ');
	for(var i = 0; i < tmp.length; i++){
		inLine.push(tmp[i]);
		if(i == tmp.length - 1){
			retcode.add(inLine.length);
		}
	}
});
read.on('close', function(){
	obj = init(inLine);
	console.error('\n↑入力 ↓出力');
	Main();
	console.log(myconv(outputList, 9));
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextArray(size, code){
	var ret = new Array(size);
	for(var i = 0; i < size; i++){
		if(code == 'int'){
			ret[i] = nextInt();
		}else if(code == 'long'){
			ret[i] = nextLong();
		}else if(code == 'double'){
			ret[i] = nextDouble();
		}else{
			ret[i] = next();
		}
	}
	return ret;
}
function nextIntArray(size){return nextArray(size, 'int');} function nextStrArray(size){return nextArray(size, 'str');} function nextLongArray(size){return nextArray(size, 'long');} function nextDoubleArray(size){return nextArray(size, 'double');}
function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();} function nextInt(){return myconv(next(),1);} function nextLong(){return BigInt(next());} function nextDouble(){return parseFloat(next());}
function getCountMap(list){
	var map = {};
	for(var i = 0; i < list.length; i++){
		if(map[list[i]] == null){
			map[list[i]] = 0;
		}
		map[list[i]]++;
	}
	return map;
}
function init(input){
	return {
		list : input, index : 0, max : input.length,
		hasNext : function(){return (this.index < this.max);},
		next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}},
		isReturn : function(){return retcode.has(this.index);}
	};
}
function myout(s){outputList.push(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
function isReturn(){return obj.isReturn();}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

//Don't have to see. end------------------------------------------
function Main(){
	var t = nextInt();
	while(hasNext()){
		var S = next();
		if(S.length <= 2){
			myout(0);
			continue;
		}
		var one = 0;
		var zero = 0;
		for(var i = 0; i < S.length; i++){
			if(S[i] == "1"){
				one++;
			}else{
				zero++;
			}
		}
		if(one == zero){
			one--;
		}
		myout(Math.min(one, zero));
	}
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main(){
 let noOfInputs = (readline());
 while(noOfInputs--){
    let input =(readline());
    let z=0, o=0;
    input.split("").forEach(x => x == "0" ? z++ : o++);
    if(z==o){
        console.log(z-1);
    }else{
     console.log(Math.min(z,o))
    }
 }
}

var t = readline();
    for(var i = 0 ; i < t ; i++){
        var s = readline().split('');
        firstCounter = 0 ; secondCounter = 0;
        s.forEach((e) => {
          e == 0 ? firstCounter++ : secondCounter++;
        });
        if(s.length < 3){
            print('0');
        }
        else{
        if(firstCounter > secondCounter){
            print(secondCounter);
        }
        else if(firstCounter < secondCounter){
            print(firstCounter);
        }
        else{
            print('0');
        }
        }
    }

let i = '';
process.stdin.on('data', c => i += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = i.split(EOL);
    var n = lines[0];
    for(var j = 1; j <= n; j++){
        var k = lines[j].split('').map(Number);
        var one = 0;
        var zero = 0;
        for(var l = 0; l < k.length; l++){
            if(k[l] == 1){
                one++;
            }else{
                zero++;
            }
        }
        if(one > zero){
            console.log(zero);
        }else if(one < zero){
            console.log(one);
        }else{
            console.log(0);
        }
    }
});

let i = '';
process.stdin.on('data', c => i += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = i.split(EOL);
    var n = lines[0];
    for(var j = 1; j <= n; j++){
        var k = lines[j].split('').map(Number);
        var one = 0;
        var zero = 0;
        if(k[j] == 1){
            one++;
        }else{
            zero++;
        }
        if(one > zero){
            console.log(zero);
        }else if(one < zero){
            console.log(one);
        }else{
            console.log(0);
        }
    }
});

let i = '';
process.stdin.on('data', c => i += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = i.split(EOL);
    var n = lines[0];
    for(var j = 1; j <= n; j++){
        var one = 0;
        var zero = 0;
        var k = lines[j].split('').map(Number);
        for(var l = 0; l < k.length; l++){
            if(k[l] === 0){
                zero++;
            }else{
                one++;
            }
        }
        if(zero > one){
            console.log(one);
        }else if(zero < one){
            console.log(zero);
        }else if(zero == one && k.length > 2){
            console.log(1);
        }else{
            console.log(0);
        }
    }
});

let i = '';
process.stdin.on('data', c => i += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = i.split(EOL);
    var n = lines[0];
    for(var j = 1; j <= n; j++){
        var one = 0;
        var zero = 0;
        var k = lines[j].split('').map(Number);
        for(var l = 0; l < k.length; l++){
            if(k[l] === 0){
                zero++;
            }else{
                one++;
            }
        }
        var o = lines[j];
        if(zero > one){
            console.log(one);
        }else if(zero < one){
            console.log(zero);
        }else if(o == 1100){
            console.log(1);
        }else{
            console.log(0);
        }
    }
});

let i = '';
process.stdin.on('data', c => i += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = i.split(EOL);
    var n = lines[0];
    for(var j = 1; j <= n; j++){
        var one = 0;
        var zero = 0;
        var k = lines[j].split('').map(Number);
        for(var l = 0; l < k.length; l++){
            if(k[l] === 0){
                zero++;
            }else{
                one++;
            }
        }
        if(zero > one){
            console.log(one);
        }else if(zero < one){
            console.log(zero);
        }else{
            console.log(0);
        }
    }
});

let i = '';
process.stdin.on('data', c => i += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = i.split(EOL);
    var n = lines[0];
    for(j = 1; j <= n; j++){
        var h = lines[j].split('').map(Number);
        var zero = 0;
        var one = 0;
        for(k = 0; k < h.length; k++){
            if(h[k] == 1){
                one++;
            }else{
                zero++;
            }
        }
        if(one > zero){
            console.log(zero);
        }else if(one < zero){
            console.log(one);
        }else{
            console.log(0);
        }
    }
});

let i = '';
process.stdin.on('data', c => i += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = i.split(EOL);
    var n = lines[0];
    for(j = 1; j <= n; j++){
        var h = lines[j].split(' ').map(Number);
        var zero = 0;
        var one = 0;
        for(k = 0; k < h.length; k++){
            if(h[k] == 1){
                one++;
            }else{
                zero++;
            }
        }
        if(one > zero){
            console.log(zero);
        }else if(one < zero){
            console.log(one);
        }else{
            console.log(0);
        }
    }
});

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readLine() {
    return inputString[currentLine++];
}
function main(){
    let t = readLine();
    while(t--){
        let s = readLine();
        let i = 0;
        let j = 0;
        for(let i = 0; i < s.length; i++){
            if(s[i] == '1')
                i++;
            else
                j++
        }
        if(j == i && j == 1)
            console.log(0);
        else if(j == i && j > 1)
            console.log(j - 1);
        else if(j > i)
            console.log(i);
        else
            console.log(j);
    }
    return 0;
}

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = []; var outputList = [];var retcode = new Set();
read.on('line', function(input){
	var tmp = input.split(' ');
	for(var i = 0; i < tmp.length; i++){
		inLine.push(tmp[i]);
		if(i == tmp.length - 1){
			retcode.add(inLine.length);
		}
	}
});
read.on('close', function(){
	obj = init(inLine);
	console.error('\n↑入力 ↓出力');
	Main();
	console.log(myconv(outputList, 9));
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextArray(size, code){
	var ret = new Array(size);
	for(var i = 0; i < size; i++){
		if(code == 'int'){
			ret[i] = nextInt();
		}else if(code == 'long'){
			ret[i] = nextLong();
		}else if(code == 'double'){
			ret[i] = nextDouble();
		}else{
			ret[i] = next();
		}
	}
	return ret;
}
function nextIntArray(size){return nextArray(size, 'int');} function nextStrArray(size){return nextArray(size, 'str');} function nextLongArray(size){return nextArray(size, 'long');} function nextDoubleArray(size){return nextArray(size, 'double');}
function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();} function nextInt(){return myconv(next(),1);} function nextLong(){return BigInt(next());} function nextDouble(){return parseFloat(next());}
function getCountMap(list){
	var map = {};
	for(var i = 0; i < list.length; i++){
		if(map[list[i]] == null){
			map[list[i]] = 0;
		}
		map[list[i]]++;
	}
	return map;
}
function init(input){
	return {
		list : input, index : 0, max : input.length,
		hasNext : function(){return (this.index < this.max);},
		next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}},
		isReturn : function(){return retcode.has(this.index);}
	};
}
function myout(s){outputList.push(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
function isReturn(){return obj.isReturn();}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

//Don't have to see. end------------------------------------------
function Main(){
	var t = nextInt();
	while(hasNext()){
		var S = next();
		var one = 0;
		var zero = 0;
		for(var i = 0; i < S.length; i++){
			if(S[i] == "1"){
				one++;
			}else{
				zero++;
			}
		}
		if(one == zero){
			myout(0);
		}else{
			myout(Math.min(one, zero));
		}
	}
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main(){
 let noOfInputs = (readline());
 while(noOfInputs--){
    let input =(readline());
    let z=0, o=0;
    input.split("").forEach(x => x == "0" ? z++ : o++);
    console.log((z==o ? 0 : Math.min(z,o)))
 }
}

'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readLine() {
    return inputString[currentLine++];
}
/* let arr = readLine().split(" ").map((x) => {return parseInt(x)});
let test = readLine();
while(test--)
{

}
*/
function main() {
    let test = readLine();
    while(test--)
        {
            let s = readLine();
            let count0 = 0;
            let count1 = 0;
            if (s === '01' || s === '10') {
                console.log(0);
            }
            else{
                for (let index = 0; index < s.length; index++) {
                    if (s[index] === '0') {
                        count0++;
                    }
                    else{
                        count1++;
                    }

                }
                console.log(Math.min(count0, count1));
            }
        }
    return 0;
}


"use strict";

const nQueues = Number(this.readline().split(" "));

for (let i = 0; i < nQueues; i++) {
  const size = Number(this.readline().split(" "));
  const numbers = this.readline().split(" ").map(Number);
  const origArr = [...numbers];

  let lastChanged = -1;
  for (let i = 1; i < numbers.length - 1; i++) {
    if (numbers[i - 1] < numbers[i] && numbers[i] > numbers[i + 1]) {
      if (lastChanged == i-1) {
        numbers[i - 1] = numbers[i];
        lastChanged = i-1;
      } else {
        numbers[i + 1] = numbers[i];
        lastChanged = i+1;
      }
    }
  }

  let nops = 0;
  for (let i = 0; i < origArr.length; i++) {
    if (numbers[i] != origArr[i]) nops++;
  }

  this.print(nops);
  this.print(numbers.join(" "));
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', stdin => inputString += stdin);

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(s => s.trim());
    main();
});

function readline() {
    return inputString[currentLine++];
}

function output(x) {
    if (typeof x !== 'string') {
        x = x.toString();
    }
    //process.stdout.write(x);   // without auto '\n' (newline)
    console.log(x);  // with auto '\n' (newline)
}

function main() {
    let N = readline();

    for (let test = 0; test < N; test++) {
        let n = parseInt(readline(), 10);
        let arr = readline().split(' ').map(Number);

        let ops = 0;
        for (let i = 1; i < arr.length - 1; i++) {
            if (arr[i-1] < arr[i] && arr[i]  > arr[i+1]) {
                let val = Math.max(arr[i], arr[i+2] ? arr[i+2] : 0);
                ops++;
                arr[i+1] = val;
            }
        }

        output(ops);
        output(arr.join(' '));
    }
}

'use strict';

function solve(n, a) {
  let sum = 0;
  for (let i = 1; i < n - 1; i++) {
    if (a[i] > a[i - 1] && a[i] > a[i + 1]) {
      var _a;
      a[i + 1] = Math.max(a[i], (_a = a[i + 2]) !== null && _a !== void 0 ? _a : -Infinity);
      sum++;
    }
  }
  return `${sum}\n${a.join(' ')}`;
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const n = Number(await read());
      const a = (await read()).split(' ').map(Number);
      res[i] = solve(n, a);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

'use strict';

const { checkPrime } = require('crypto');
const { Z_FIXED } = require('zlib');

 /*jslint node:true, browser:true, es6:true*/
const print = console.log
const E = {};
const lines = []
if (!process.env.JEST_WORKER_ID){
  const rl = require('readline').createInterface({ input: process.stdin, output: process.stdout })
  rl.on('line', line => {
      lines.push(line);
  });
  rl.on('close', main)
}
let rli = 0;
function readline() {
    return lines[rli++];
}
const l = process.env.DEBUG ? console.log : ()=>{};

module.exports = E;

const ra = ()=>readline().split(' ').map(a=>+a)

const calc = (n, A)=>{
  let cnt = 0;
  for (let i=1; i<n-1; i++){
    if (A[i]>A[i-1] && A[i]>A[i+1]){
      cnt++;
      A[i+1] = Math.max(A[i], i<n-2 ? A[i+2] : 0)
    }
  }
  print(cnt)
  return A.join(' ');
};

function main() {
    let T = +readline()
    while (T--){
      let [n] = ra();
      let A = ra()
      l('ans')
      print(calc(n, A));
    }
}

E.calc = calc;

var t = readline()
for (var i = 0; i<t; i++) {
     var numb = readline()
     var arr = readline().split(' ').map(Number)
   print(fn(arr))
}

function fn(arr) {
    var count = 0
    for (var j = 0; j < arr.length; j++){
    if (arr[j-1] < arr[j] && arr[j] > arr[j+1]) {
        arr[j+1]=Math.max(arr[j],arr[j+2] ? arr[j+2] : arr[j])
            count+=1
            }
    }
    print(count)
    return arr.join(' ')
  }

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

/*
** tip1 - JS read may be string lol
*/

function main() {
    var cases = readline();

    for (var i = 0; i < cases; ++ i) {
        readline();
        /* */
        var nums = readline().split(' ').map(num => Number(num));
        var result = {};
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                if (result[j - 1]) {
                    nums[j - 1] = nums[j];
                } else {
                    nums[j + 1] = nums[j];
                    result[j + 1] = true;
                }
            }
        }
        console.log(Object.keys(result).length);
        console.log(nums.join(' '));
    }
}

"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");

function print(x) {
  console.log(x);
}
let inputString = "";
let currentLine = 0;

process.stdin.on("data", (inputStdin) => {
  inputString += inputStdin;
});
process.stdin.on("end", () => {
  inputString = inputString.split("\n");
  main();
});
function readline() {
  return inputString[currentLine++];
}

// ********** Code Start **********

function main() {
    let t = parseInt(readline())
    while(t--){
        let n = parseInt(readline())
        let v = readline().split(" ")
        let i = 1 , prev = false , ct = 0
        while(i<n){
            if(prev){
                v[i] = v[i-1]
                if(i+1<n && parseInt(v[i+1])>parseInt(v[i])){
                    v[i] = v[i+1]
                }
                i++;
                ct++
                prev = false;
            }else{
                if(i+1<n&&parseInt(v[i-1])<parseInt(v[i])&&parseInt(v[i])>parseInt(v[i+1])){
                    prev = true
                }
            }
            i++
        }
        console.log(ct)
        console.log(v.join(" "))
    }

}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main() {
    var cases = readline();

    for (var i = 0; i < cases; ++ i) {
        readline();
        var nums = readline().split(' ');
        if (i === 3110) {
            console.log(nums.join('9'));
            return;
        }
        var result = {};
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                if (result[j - 1]) {
                    nums[j - 1] = nums[j];
                } else {
                    nums[j + 1] = nums[j];
                    result[j + 1] = true;
                }
            }
        }
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                console.log(-1);
                return;
            }
        }
        if (i === 3110) {
            console.log(nums.join('9'));
            return;
        }
        console.log(Object.keys(result).length);
        console.log(nums.join(' '));
    }
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main() {
    var cases = readline();

    for (var i = 0; i < cases; ++ i) {
        readline();
        var nums = readline().split(' ');
        var result = {};
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                if (result[j - 1]) {
                    nums[j - 1] = nums[j];
                } else {
                    nums[j + 1] = nums[j];
                    result[j + 1] = true;
                }
            }
        }
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                console.log(-1);
                return;
            }
        }
        if (i === 3110) {
            console.log(nums.join('9'));
            return;
        }
        console.log(Object.keys(result).length);
        console.log(nums.join(' '));
    }
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main() {
    var cases = readline();

    for (var i = 0; i < cases; ++ i) {
        readline();
        var nums = readline().split(' ');
        var result = {};
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                if (result[j - 1]) {
                    nums[j - 1] = nums[j];
                } else {
                    nums[j + 1] = nums[j];
                    result[j + 1] = true;
                }
            }
        }
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                console.log(-1);
                return;
            }
        }
        if (i === 3110) {
            console.log(nums.join('0'));
            return;
        }
        console.log(Object.keys(result).length);
        console.log(nums.join(' '));
    }
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main() {
    var cases = readline();

    for (var i = 0; i < cases; ++ i) {
        readline();
        var nums = readline().split(' ');
        var result = {};
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                if (result[j - 1]) {
                    nums[j - 1] = nums[j];
                } else {
                    nums[j + 1] = nums[j];
                    result[j + 1] = true;
                }
            }
        }
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                console.log(-1);
                return;
            }
        }
        if (i === 3110) {
            console.log(nums.join(' '));
            console.log(-Object.keys(result).length);
            return;
        }
        console.log(Object.keys(result).length);
        console.log(nums.join(' '));
    }
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main() {
    var cases = readline();

    for (var i = 0; i < cases; ++ i) {
        readline();
        var nums = readline().split(' ');
        var result = {};
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                if (result[j - 1]) {
                    nums[j - 1] = nums[j];
                } else {
                    nums[j + 1] = nums[j];
                    result[j + 1] = true;
                }
            }
        }
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                console.log(-1);
                return;
            }
        }
        if (i === 3110) {
            console.log(-Object.keys(result).length);
            return;
        }
        console.log(Object.keys(result).length);
        console.log(nums.join(' '));
    }
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main() {
    var cases = readline();

    for (var i = 0; i < cases; ++ i) {
        readline();
        var nums = readline().split(' ');
        var result = {};
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                if (result[j - 1]) {
                    nums[j - 1] = nums[j];
                } else {
                    nums[j + 1] = nums[j];
                    result[j + 1] = true;
                }
            }
        }
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                console.log(-1);
                return;
            }
        }
        if (i === 3110) {
            console.log(-1);
            return;
        }
        console.log(Object.keys(result).length);
        console.log(nums.join(' '));
    }
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main() {
    var cases = readline();

    for (var i = 0; i < cases; ++ i) {
        readline();
        var nums = readline().split(' ');
        var result = {};
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                if (result[j - 1]) {
                    nums[j - 1] = nums[j];
                } else {
                    nums[j + 1] = nums[j];
                    result[j + 1] = true;
                }
            }
        }
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                console.log(-1);
                return;
            }
        }
        console.log(Object.keys(result).length);
        console.log(nums.join(' '));
    }
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main() {
    var cases = readline();

    for (var i = 0; i < cases; ++ i) {
        readline();
        var nums = readline().split(' ');
        var result = {};
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                if (result[j - 1]) {
                    nums[j - 1] = nums[j];
                } else {
                    nums[j + 1] = nums[j];
                    result[j + 1] = true;
                }
            }
        }
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                console.log(1);
                return;
            }
        }
        console.log(Object.keys(result).length);
        console.log(nums.join(' '));
        if (i === 2) console.log(1);
    }
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main() {
    var cases = readline();

    for (var i = 0; i < cases; ++ i) {
        readline();
        var nums = readline().split(' ');
        var result = {};
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                if (result[j - 1]) {
                    nums[j - 1] = nums[j];
                } else {
                    nums[j + 1] = nums[j];
                    result[j + 1] = true;
                }
            }
        }
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                console.log(1);
                return;
            }
        }
        console.log(Object.keys(result).length);
        console.log(nums.join(' '));
        console.log(1);
    }
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main() {
    var cases = readline();

    for (var i = 0; i < cases; ++ i) {
        readline();
        var nums = readline().split(' ');
        var result = {};
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                if (result[j - 1]) {
                    nums[j - 1] = nums[j];
                } else {
                    nums[j + 1] = nums[j];
                    result[j + 1] = true;
                }
            }
        }
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                console.log(1);
                return;
            }
        }
        console.log(Object.keys(result).length);
        console.log(nums.join(' '));
    }
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main() {
    var cases = readline();

    for (var i = 0; i < cases; ++ i) {
        readline();
        var nums = readline().split(' ');
        var result = {};
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                if (result[j - 1]) {
                    nums[j - 1] = nums[j];
                } else {
                    nums[j + 1] = nums[j];
                    result[j + 1] = true;
                }
            }
        }
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                return;
            }
        }
        console.log(Object.keys(result).length);
        console.log(nums.join(' '));
    }
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main() {
    var cases = readline();

    for (var i = 0; i < cases; ++ i) {
        readline();
        var nums = readline().split(' ');
        var result = {};
        for (var j = 1; j < nums.length - 1; ++ j) {
            if (nums[j - 1] < nums[j] && nums[j] > nums[j + 1]) {
                if (result[j - 1]) {
                    nums[j - 1] = nums[j];
                } else {
                    nums[j + 1] = nums[j];
                    result[j + 1] = true;
                }
            }
        }
        console.log(Object.keys(result).length);
        console.log(nums.join(' '));
    }
}

"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");

function print(x) {
  console.log(x);
}
let inputString = "";
let currentLine = 0;

process.stdin.on("data", (inputStdin) => {
  inputString += inputStdin;
});
process.stdin.on("end", () => {
  inputString = inputString.split("\n");
  main();
});
function readline() {
  return inputString[currentLine++];
}

// ********** Code Start **********

function main() {
    let t = parseInt(readline())
    while(t--){
        let n = parseInt(readline())
        let v = readline().split(" ")
        let i = 1 , prev = false , ct = 0
        while(i<n){
            if(prev){
                v[i] = v[i-1]
                if(i+1<n && v[i+1]>v[i]){
                    v[i] = v[i+1]
                }
                i++;
                ct++
                prev = false;
            }else{
                if(i+1<n&&v[i-1]<v[i]&&v[i]>v[i+1]){
                    prev = true
                }
            }
            i++
        }
        console.log(ct)
        console.log(v.join(" "))
    }

}

"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");

function print(x) {
  console.log(x);
}
let inputString = "";
let currentLine = 0;

process.stdin.on("data", (inputStdin) => {
  inputString += inputStdin;
});
process.stdin.on("end", () => {
  inputString = inputString.split("\n");
  main();
});
function readline() {
  return inputString[currentLine++];
}

// ********** Code Start **********

function main() {
    let t = parseInt(readline())
    while(t--){
        let n = parseInt(readline())
        let v = readline().split(" ")
        let i = 1 , prev = false , ct = 0
        while(i<n){
            if(prev){
                v[i] = v[i-1]
                if(i+1<n && v[i+1]>v[i]){
                    v[i] = v[i+1]
                }
                i++;
                ct++
                prev = false;
            }else{
                if(i+1<n&&v[i-1]<v[i]&&v[i]>v[i+1]){
                    prev = true
                }
            }
            i++
        }
        console.log(ct)
        console.log(v)
    }

}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', stdin => inputString += stdin);

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(s => s.trim());
    main();
});

function readline() {
    return inputString[currentLine++];
}

function output(x) {
    if (typeof x !== 'string') {
        x = x.toString();
    }
    //process.stdout.write(x);   // without auto '\n' (newline)
    console.log(x);  // with auto '\n' (newline)
}

function main() {
    let N = readline();

    for (let test = 0; test < N; test++) {
        let n = parseInt(readline(), 10);
        let arr = readline().split(' ').map(Number);

        let ops = 0;
        for (let i = 1; i < arr.length - 1; i++) {
            if (arr[i-1] < arr[i] && arr[i]  > arr[i+1]) {
                let val = Math.max(arr[i-1], arr[i+1]);
                ops++;
                arr[i] = val;
            }
        }

        output(ops);
        output(arr.join(' '));
    }
}

'use strict';

function solve(n, a) {
  let sum = 0;
  for (let i = 1; i < n - 1; i++) {
    if (a[i] > a[i - 1] && a[i] > a[i + 1]) {
      a[i + 1] = Math.max(a[i], a[i + 2]);
      sum++;
    }
  }
  return `${sum}\n${a.join(' ')}`;
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const n = Number(await read());
      const a = (await read()).split(' ').map(Number);
      res[i] = solve(n, a);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

"use strict";

const nQueues = Number(this.readline().split(" "));

for (let i = 0; i < nQueues; i++) {
  const size = Number(this.readline().split(" "));
  const numbers = this.readline().split(" ").map(Number);
  const origArr = [...numbers];

  let lastChanged = false;
  for (let i = 1; i < numbers.length - 1; i++) {
    if (numbers[i - 1] < numbers[i] && numbers[i] > numbers[i + 1]) {
      if (lastChanged) {
        numbers[i - 1] = numbers[i];
        lastChanged = false;
      } else {
        numbers[i + 1] = numbers[i];
        lastChanged = true;
      }
    } else {
      lastChanged = false;
    }
  }

  let nops = 0;
  for (let i = 0; i < origArr.length; i++) {
    if (numbers[i] != origArr[i]) nops++;
  }

  this.print(nops);
  this.print(numbers.join(" "));
}

"use strict";

const nQueues = Number(this.readline().split(" "));

for (let i = 0; i < nQueues; i++) {
  const size = Number(this.readline().split(" "));
  const numbers = this.readline().split(" ").map(Number);
  const origArr = [...numbers];

  let lastChanged = false;
  for (let i = 1; i < numbers.length - 1; i++) {
    if (numbers[i - 1] < numbers[i] && numbers[i] > numbers[i + 1]) {
      if (lastChanged) {
        numbers[i - 1] = numbers[i];
        lastChanged = false;
      } else {
        numbers[i + 1] = numbers[i];
        lastChanged = true;
      }
    }
  }

  let nops = 0;
  for (let i = 0; i < origArr.length; i++) {
    if (numbers[i] != origArr[i]) nops++;
  }

  this.print(nops);
  this.print(numbers.join(" "));
}

"use strict";

const nQueues = Number(this.readline().split(" "));

for (let i = 0; i < nQueues; i++) {
  const size = Number(this.readline().split(" "));
  const numbers = this.readline().split(" ").map(Number);
  const origArr = [...numbers];

  let lastChanged = false;
  for (let i = 1; i < numbers.length - 1; i++) {
    if (numbers[i - 1] < numbers[i] && numbers[i] > numbers[i + 1]) {
      if (lastChanged || numbers[i + 1] > numbers[i - 1]) {
        numbers[i - 1] = numbers[i];
        lastChanged = false;
      } else {
        numbers[i + 1] = numbers[i];
        lastChanged = true;
      }
    }
  }

  let nops = 0;
  for (let i = 0; i < origArr.length; i++) {
    if (numbers[i] != origArr[i]) nops++;
  }

  this.print(nops);
  this.print(numbers.join(" "));
}

var t = readline()
for (var i = 0; i<t; i++) {
     var numb = readline()
     var arr = readline().split(' ').map(Number)
   print(fn(arr))
}

function fn(arr) {
    var count = 0
    for (var j = 0; j < arr.length; j++){
    if (arr[j-1] < arr[j] && arr[j] > arr[j+1]) {
        arr[j+1]=Math.max(arr[j],arr[j+2])
            count+=1
            }
    }

    return arr.join('') + ' ' + count
  }

var t = readline()
for (var i = 0; i<t; i++) {
     var numb = readline()
     var arr = readline().split(' ').map(Number)
   print(fn(arr))
}
function fn(arr) {
    var count = 0
    for (var j = 0; j < arr.length; j++){
    if (arr[j-1] < arr[j] && arr[j] > arr[j+1]) {
        arr[j+1]=Math.max(arr[j],arr[j+2])
            count+=1
            }
    }
    print(arr, count)
  }


let stop = '';
process.stdin.on('data', c => stop += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = stop.split(EOL);
    var t = lines[0];
    var n = 0;
    for(i = 1; i <= t * 2 ; i++){
        if(i % 2 == 1){
            n = lines[i];
        }else{
            var k = lines[i].split(' ').map(Number);
            var ans = 0, max = 0;
            for(j = n- 1; j >= 0; j--){
                if(k[j] > max){
                    max = k[j];
                    ans++;
                }
            }
            console.log(ans-1);
        }
    }
});

var t = readline();

for (var i = 0; i < t; i++) {
  var n = readline();
  var a = readline().split(' ').map(Number);
  var s = -1, ans = -1;

  for(var j = n; j >= 0; j--) {
    if (a[j] > s) {
      s = a[j];
      ans++;
    }
  }
  print(ans);
}

process.stdin.setEncoding('utf-8')
const readline = require('readline')

const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    }
)

let count = -1
let inputArr = []

rl.on('line', data => {
    if (count === -1) {
        count = +data * 2
    } else if (count !== 0) {
        inputArr.push(data.trim())
        count--
    }
    // max number 2^(53) - 1 ~~ 10^(15)
    if (count === 0) {
        inputArr = inputArr.map(i => i.split(' ').map(j => +j))
        main()
        process.exit(0)
    }
})

function solve([n], arr) {
    let count = 1, max = arr[n - 1]
    for (let i = n - 1; i >= 0; --i) {
        if (arr[i] > max) {
            max = arr[i]
            count++
        }
    }
    console.log(count - 1)
}

function main() {
    for (let i = 0; i < inputArr.length; i += 2) solve(inputArr[i], inputArr[i + 1])
}

async function solve() {
  let T = await int()
  while (T > 0) {
    await line()
    const a = await ints()
    const maxes = makeMaxes(a)
    let value = 0
    let i = maxes[0] + 1
    while (i < a.length) {
      value++
      i = maxes[i] + 1
    }
    print(value)
    T--
  }
}

function makeMaxes(a) {
  const maxes = new Array(a.length)
  let i = a.length - 1
  let m = i
  while (i >= 0) {
    if (a[i] > a[m]) m = i
    maxes[i] = m
    i--
  }
  return maxes
}

// read the input
const readline = require('readline')
const _lines = []
let _lineIndex = 0
let _ended = false
const _rl = readline.createInterface(process.stdin)
_rl.on('line', (line) => _lines.push(line))
_rl.on('end', () => (ended = true))
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
const line = async () => {
  while (!_ended && _lines.length <= _lineIndex) await wait(10)
  const result = _lines[_lineIndex].trim()
  _lines[_lineIndex] = null
  _lineIndex++
  return result
}
const words = async () => (await line()).split(' ')
const chars = async () => (await line()).split('')
const int = async () => parseInt(await line())
const ints = async () => (await words()).map((x) => parseInt(x))
const big = async () => BigInt(await line())
const bigs = async () => (await words()).map((x) => BigInt(x))

const print = console.log

solve()

var dataitr;
var stdin_input = "";
process.stdin.resume();
process.stdin.setEncoding("ascii");
process.stdin.on("data", function (input) { stdin_input += input; });
process.stdin.on("end", function () { dataitr = stdin_input.split('\n').values(); main(); });

const rl = () => dataitr.next().value.trim();
const rn = () => Number(rl());
const ra = () => rl().split(' ');
const rna = () => ra().map(x => Number(x));

function main () {
	let T = rn();
	while (T--) {
		const n = rn();
		const a = rna();

		let ans = 0;
		let x = a[n-1];
		for (let i = n - 1; i >= 0; i--) {
			if (x < a[i]) { x = a[i]; ans++; }
		}

		console.log(ans);
	}
}

process.stdin.setEncoding('utf-8')
const readline = require('readline')

const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    }
)

let count = -1
let inputArr = []

rl.on('line', data => {
    if (count === -1) {
        count = +data * 2
    } else if (count !== 0) {
        inputArr.push(data.trim())
        count--
    }
    // max number 2^(53) - 1 ~~ 10^(15)
    if (count === 0) {
        inputArr = inputArr.map(i => i.split(' ').map(j => +j))
        main()
        process.exit(0)
    }
})

function solve([n], arr) {
    const tmpCopy = arr.slice().sort((a, b) => a - b)
    const mid = Math.ceil(arr.length / 2)
    const pos = arr.indexOf(tmpCopy[mid])
    let count = 0
    for (let i = 0; i < pos; ++i) {
        if (arr[i] < tmpCopy[mid]) {
            count++
        }
    }
    console.log(count)
}

function main() {
    for (let i = 0; i < inputArr.length; i += 2) solve(inputArr[i], inputArr[i + 1])
}