You are given a string $$$s$$$. You have to determine whether it is possible to build the string $$$s$$$ out of strings aa, aaa, bb and/or bbb by concatenating them. You can use the strings aa, aaa, bb and/or bbb any number of times and in any order.For example:  aaaabbb can be built as aa $$$+$$$ aa $$$+$$$ bbb;  bbaaaaabbb can be built as bb $$$+$$$ aaa $$$+$$$ aa $$$+$$$ bbb;  aaaaaa can be built as aa $$$+$$$ aa $$$+$$$ aa;  abab cannot be built from aa, aaa, bb and/or bbb.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 50$$$), consisting of characters a and/or b.

For each test case, print YES if it is possible to build the string $$$s$$$. Otherwise, print NO. You may print each letter in any case (for example, YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).

8

aaaabbb

bbaaaaabbb

aaaaaa

abab

a

b

aaaab

bbaaa

NoteThe first four test cases of the example are described in the statement.

let buf='';

process.stdin.on('readable',function(){
    let chunk=process.stdin.read();
    if(chunk) buf+=chunk.toString();
})

process.stdin.on('end',function(){
    const {EOL}=require('os');
    let line=buf.split(EOL);
    let t=parseInt(line[0]);
    for(let i=1;i<=t;i++)
    {
        let s=line[i];
        flag=true;
        let c1=0,c2=0;
        for(let i=0;i<s.length;i++)
        {
            if(s[i]==='a')
            {
                c1++;
                if(c2>0)
                {
                    if(c2===1) flag=false;
                    c2=0;
                }
            }
            else
            {
                c2++;
                if(c1>0)
                {
                    if(c1===1) flag=false;
                    c1=0;
                }
            }
        }
        if(c1===1||c2===1) flag=false;
        if(flag) console.log("YES");
        else console.log("NO");
    }
})

'use-strict'

process.stdin.resume();
process.stdin.setEncoding('utf8');

let data = '';
let currentLine = 0;

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

process.stdin.on('end', () =>{
    data = data.replace(/\s*$/, '')
    .split('\n')
    .map(str => str.replace(/\s*$/, ''));

    let testCases = parseInt(data.shift());

    while(testCases--) {

        const a = readLine();

        const res = main(a);

        console.log(res)
    }
});

function get_ints() {
    return data.shift().split(' ').map(Number);
}

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

function main(a){
    if(a.length <= 1) return 'NO';
   let count = 0;
   let prev = 'c'
   for (let index = 0; index < a.length; index++) {
       const element = a[index];
        if(prev !== element){
            if(count === 1) return 'NO';
            count = 1;
            prev = element;
        } else {
            count++;
        }
   }
   if(count === 1) return 'NO';
    return 'YES';
}

module.exports = main;

const fs = require("fs");

const input = fs.readFileSync(0, "utf8").trim().split("\n");

const testCases = [];
let T;

function parseInput() {
  T = parseInt(input[0]);
  let index = 0;

  for (let i = 0; i < T; i++) {
    index++;
    const s = input[index].trim();
    const testCase = {
      s,
    };

    testCases.push(testCase);
  }
}

function solution(testCase, index) {
  const { s } = testCase;

  let result = "YES";
  let run = 0;
  let last = undefined;
  for (let i = 0; i < s.length; i++) {
    const c = s[i];
    if (c != last) {
      if (run == 1) {
        result = "NO";
        break;
      }
      run = 1;
      last = c;
    } else {
      run++;
    }
  }
  if (run == 1) {
    result = "NO";
  }

  console.log(result);
}

parseInput();

testCases.forEach(solution);

'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();

        let a = 0;
        let b = 0;

        let no = false;
        if (s === 'a' || s === 'b') {
            output('NO');
            continue;
        }
        for (let i = 0; i < s.length; i++) {
            if (s[i] === 'a') {
                if (b === 1) {
                    no = true;
                    break;
                } else {
                    a++;
                    b = 0;
                }
            } else {
                if (a === 1) {
                    no = true;
                    break;
                } else {
                    b++;
                    a = 0;
                }
            }
        }

        if (b === 1 || a === 1) {
            no = true;
        }

        output(no ? 'NO' : 'YES');
    }
}

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];
    var e = 0;
    for(j = 1; j <= n; j++){
        var k = lines[j].split('');
        var a = false;
        for(l = 0; l < k.length; l++){
            if(k[l - 1] != k[l] && k[l] != k[l + 1]){
                a = true;
            }
        }
        if(a === true){
           console.log('NO');
        }else{
            console.log('YES');
        }
    }
});

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) ? 'YES' : 'NO'
    }
    console.log(output.join('\n'))
// })()
})

function solve(str) {
    let p, c
    for (let i = 0; i < str.length; i++) {
        const x = str[i]
        if (x === p) {
            c++
        } else {
            if (c === 1) return false
            c = 1
            p = x
        }
    }
            if (c === 1) return false
    return true
}

'use strict';

var readline = require('readline');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve(s) {
  let cur = '';
  for (let i = 0; i <= s.length; i++) {
    if (cur[0] !== s[i]) {
      if (cur.length > 0) {
        if (cur.length < 2) {
          console.log('NO');
          return;
        }
      }
      cur = '';
    }
    cur += s[i];
  }
  console.log('YES');
}

async function main(read) {
  let t = Number(await read());
  while (t--) {
    const s = await read();
    solve(s);
  }
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();

const lines = [];
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 ra = ()=>readline().split(' ').map(a=>+a);

function div(val, by){
    return (val - val % by) / by;
}

const cnk = (n, k) => {
    let res = 1;
    for (let i = 1; i <= k; i++)
        res = res * (n - k + i) / i;
    return Math.round(res + 0.01);
}

const calc = (s)=>{
    let prev = 'c';
    let count = 0;
    for (let i = 0; i < s.length; i++) {
        if (s[i] !== prev) {
            if (count === 1) return 'NO';
            count = 1;
            prev = s[i];
        } else {
            count++;
        }
    }
    if (count === 1) return 'NO';

    return 'YES';
};

function main() {
    let T = +readline();
    for (let t = 1; t <= T; t++){
        // let n = +readline();
        let s = readline();
        console.log(`${calc(s)}`);
    }
}

'use strict';
 /*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 = (s)=>{
  for (let i=0; i<s.length;){
    let j = i;
    while (s[i]==s[j] && j<s.length) j++;
    if (j-i<2)
      return false;
    i = j;
  }
  return true;
};

function main() {
    let T = +readline()
    while (T--){
      //let [n] = ra();
      let s = readline();
      L('Ans:')
      print(calc(s) ? 'yes' : 'no');
    }
}

E.calc = calc;

//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 N = S.length;
		var c = 1;
		var ok = true;
		for(var i = 1; i < N; i++){
			if(S[i - 1] != S[i]){
				if(c == 1){
					ok = false;
				}else{
					c = 1;
				}
			}else{
				c++;
			}
		}
		if(c == 1){
			ok = false;
		}
		myout((ok) ? "YES" : "NO");
	}
}

var num=parseInt(readline());
var arr=[];
for(var i=0;i<num;i++){
    arr.push(readline().split(""));
}
var sum;
for(var i=0;i<num;i++){
    sum=0;
    for(var x=0;x<arr[i].length;x++){
        if(arr[i][x]===arr[i][x+1]){
            ++sum;
        }
        else{
            if(sum>=1){
                sum=0;
            }
            else{
                sum=-1;
                break;
            }
        }
    }
    if(sum===0){
        print("YES");
    }
    else if(sum===-1){
        print("NO");
    }
}

let buf='';

process.stdin.on('readable',function(){
    let chunk=process.stdin.read();
    if(chunk) buf+=chunk.toString();
})

process.stdin.on('end',function(){
    console.log();
})

'use-strict'

process.stdin.resume();
process.stdin.setEncoding('utf8');

let data = '';
let currentLine = 0;

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

process.stdin.on('end', () =>{
    data = data.replace(/\s*$/, '')
    .split('\n')
    .map(str => str.replace(/\s*$/, ''));

    let testCases = parseInt(data.shift());

    while(testCases--) {

        const a = readLine();

        const res = main(a);

        console.log(res)
    }
});

function get_ints() {
    return data.shift().split(' ').map(Number);
}

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

function main(a){
    if(a.length <= 1) return 'NO';
   let countA = 0;
   let countB = 0;
   let countAllA = 0;
   let countAllB = 0;
   for (let index = 0; index < a.length; index++) {
       const element = a[index];
       if(element == 'a') {
           countA++;
           countAllA++;
           if(countB == 1) return 'NO';
           countB = 0;
       }
       if(element == 'b') {
           countB++;
           countAllB++;
           if(countA == 1) return 'NO';
           countA = 0;
       }
   }
   if(countAllA == 1 || countAllB == 1) return 'NO';
    return 'YES';
}

module.exports = main;

const fs = require("fs");

const input = fs.readFileSync(0, "utf8").trim().split("\n");

const testCases = [];
let T;

function parseInput() {
  T = parseInt(input[0]);
  let index = 0;

  for (let i = 0; i < T; i++) {
    index++;
    const s = input[index];
    const testCase = {
      s,
    };

    testCases.push(testCase);
  }
}

function solution(testCase, index) {
  const { s } = testCase;

  let result = "YES";
  let run = 0;
  let last = undefined;
  for (let i = 0; i < s.length; i++) {
    const c = s[i];
    if (c != last) {
      if (run == 1) {
        result = "NO";
        break;
      }
      run = 1;
      last = c;
    } else {
      run++;
    }
  }
  if (run == 1) {
    result = "NO";
  }

  console.log(result);
}

parseInput();

testCases.forEach(solution);


You are given a string $$$s$$$, consisting only of characters '0' and '1'.You have to choose a contiguous substring of $$$s$$$ and remove all occurrences of the character, which is a strict minority in it, from the substring.That is, if the amount of '0's in the substring is strictly smaller than the amount of '1's, remove all occurrences of '0' from the substring. If the amount of '1's is strictly smaller than the amount of '0's, remove all occurrences of '1'. If the amounts are the same, do nothing.You have to apply the operation exactly once. What is the maximum amount of characters that can be removed?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The only line of each testcase contains a non-empty string $$$s$$$, consisting only of characters '0' and '1'. The length of $$$s$$$ doesn't exceed $$$2 \cdot 10^5$$$. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$.

For each testcase, print a single integer — the maximum amount of characters that can be removed after applying the operation exactly once.

NoteIn the first testcase, you can choose substrings "0", "1" or "01". In "0" the amount of '0' is $$$1$$$, the amount of '1' is $$$0$$$. '1' is a strict minority, thus all occurrences of it are removed from the substring. However, since there were $$$0$$$ of them, nothing changes. Same for "1". And in "01" neither of '0' or '1' is a strict minority. Thus, nothing changes. So there is no way to remove any characters.In the second testcase, you can choose substring "10101010101". It contains $$$5$$$ characters '0' and $$$6$$$ characters '1'. '0' is a strict minority. Thus, you can remove all its occurrences. There exist other substrings that produce the same answer.In the third testcase, you can choose substring "011000100". It contains $$$6$$$ characters '0' and $$$3$$$ characters '1'. '1' is a strict minority. Thus, you can remove all its occurrences.

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

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

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

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


You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i > a_{i - 1}$$$ and $$$a_i > a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.

Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \leq t \leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.

NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums.

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

}

'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;

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

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++];
}

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


There are $$$n$$$ emotes in very popular digital collectible card game (the game is pretty famous so we won't say its name). The $$$i$$$-th emote increases the opponent's happiness by $$$a_i$$$ units (we all know that emotes in this game are used to make opponents happy).You have time to use some emotes only $$$m$$$ times. You are allowed to use any emotion once, more than once, or not use it at all. The only restriction is that you cannot use the same emote more than $$$k$$$ times in a row (otherwise the opponent will think that you're trolling him).Note that two emotes $$$i$$$ and $$$j$$$ ($$$i \ne j$$$) such that $$$a_i = a_j$$$ are considered different.You have to make your opponent as happy as possible. Find the maximum possible opponent's happiness.

The first line of the input contains three integers $$$n, m$$$ and $$$k$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le k \le m \le 2 \cdot 10^9$$$) — the number of emotes, the number of times you can use emotes and the maximum number of times you may use the same emote in a row. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is value of the happiness of the $$$i$$$-th emote.

Print one integer — the maximum opponent's happiness if you use emotes in a way satisfying the problem statement.

NoteIn the first example you may use emotes in the following sequence: $$$4, 4, 5, 4, 4, 5, 4, 4, 5$$$.

// stuck due to very large numbers
process.stdin.resume()
process.stdin.setEncoding('utf-8')

var arr = ''
process.stdin.on('data', (i) => {
  arr += i
})

process.stdin.on('end', () => {
  arr = arr.split('\n')
  const l1 = arr[0].split(' ')
  const n = parseInt(l1[0])
  const m = parseInt(l1[1])
  const k = parseInt(l1[2])

  var a = arr[1].split(' ')
  a = a.sort((a, b) => b - a)

  var first = parseInt(a[0])
  var sec = parseInt(a[1])

  if(m == k) console.log(first * k)
  else {
    var sum = 0
    // console.log(first, sec)
    var k1 = k + 1
    var mo = m % k1
    mo = mo
    var d = Math.floor(m / k1)
    sum = add(multiply(add(multiply(first.toString(), k.toString()), sec.toString()), d.toString()), sum.toString())
    // console.log(sum)
    sum = add(multiply(first.toString(), mo.toString()), sum.toString())
    // console.log(sum)
    console.log(sum)
  }
})

function multiply(a, b) {
  if ((a | 0) == 0 || (b | 0) == 0) {
      return '0';
  }

  a = a.split('').reverse();
  b = b.split('').reverse();
  var result = [];

  for (var i = 0; a[i] >= 0; i++) {
      for (var j = 0; b[j] >= 0; j++) {
          if (!result[i + j]) {
              result[i + j] = 0;
          }

          result[i + j] += a[i] * b[j];
      }
  }

  for (var i = 0; result[i] >= 0; i++) {
      if (result[i] >= 10) {
          if (!result[i + 1]) {
              result[i + 1] = 0;
          }

          result[i + 1] += parseInt(result[i] / 10);
          result[i] %= 10;
      }
  }

  return result.reverse().join('');
}

function add(a, b) {
  if ((a | 0) == 0 && (b | 0) == 0) {
      return '0';
  }

  a = a.split('').reverse();
  b = b.split('').reverse();
  var result = [];

  for (var i = 0; (a[i] >= 0) || (b[i] >= 0); i++) {
      var sum = (parseInt(a[i]) || 0) + (parseInt(b[i]) || 0);

      if (!result[i]) {
          result[i] = 0;
      }

      var next = ((result[i] + sum) / 10) | 0;
      result[i] = (result[i] + sum) % 10;

      if (next) {
          result[i + 1] = next;
      }
  }

  return result.reverse().join('');
}

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

var arr = ''
process.stdin.on('data', (i) => {
  arr += i
})

process.stdin.on('end', () => {
  arr = arr.split('\n')
  const l1 = arr[0].split(' ')
  const n = parseInt(l1[0])
  const m = parseInt(l1[1])
  const k = parseInt(l1[2])

  var a = arr[1].split(' ')
  a = a.sort((a, b) => b - a)

  var first = parseInt(a[0])
  var sec = parseInt(a[1])

  if(m == k) console.log(first * k)
  else {
    var sum = 0
    // console.log(first, sec)
    if(n == 200000) console.log(m, k, first, sec)
    sum += (first * k + sec) * Math.floor(m / (k + 1))
    // console.log(sum)
    sum += (first) * (m % (k + 1))
    console.log(sum)
  }
})

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

var arr = ''
process.stdin.on('data', (i) => {
  arr += i
})

process.stdin.on('end', () => {
  arr = arr.split('\n')
  const l1 = arr[0].split(' ')
  const n = parseInt(l1[0])
  const m = parseInt(l1[1])
  const k = parseInt(l1[2])

  var a = arr[1].split(' ')
  a = a.sort((a, b) => b - a)

  var first = parseInt(a[0])
  var sec = parseInt(a[1])

  if(m == k) console.log(first * k)
  else {
    var sum = 0
    // console.log(first, sec)
    if(n == 200000 && m == 1987654321 && k == 330000) console.log(m, k, first, sec)
    sum += (first * k + sec) * Math.floor(m / (k + 1))
    // console.log(sum)
    sum += (first) * (m % (k + 1))
    console.log(sum)
  }
})

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

var arr = ''
process.stdin.on('data', (i) => {
  arr += i
})

process.stdin.on('end', () => {
  arr = arr.split('\n')
  const l1 = arr[0].split(' ')
  const n = parseInt(l1[0])
  const m = parseInt(l1[1])
  const k = parseInt(l1[2])

  var a = arr[1].split(' ')
  a = a.sort((a, b) => b - a)

  var first = parseInt(a[0])
  var sec = parseInt(a[1])

  if(m == k) console.log(first * k)
  else {
    var sum = 0
    // console.log(first, sec)
    sum += (first * k + sec) * Math.floor(m / (k + 1))
    // console.log(sum)
    sum += (first) * (m % (k + 1))
    console.log(sum)
  }
})


You are given an array $$$a$$$ of length $$$n$$$.Let's define the eversion operation. Let $$$x = a_n$$$. Then array $$$a$$$ is partitioned into two parts: left and right. The left part contains the elements of $$$a$$$ that are not greater than $$$x$$$ ($$$\le x$$$). The right part contains the elements of $$$a$$$ that are strictly greater than $$$x$$$ ($$$> x$$$). The order of elements in each part is kept the same as before the operation, i. e. the partition is stable. Then the array is replaced with the concatenation of the left and the right parts.For example, if the array $$$a$$$ is $$$[2, 4, 1, 5, 3]$$$, the eversion goes like this: $$$[2, 4, 1, 5, 3] \to [2, 1, 3], [4, 5] \to [2, 1, 3, 4, 5]$$$.We start with the array $$$a$$$ and perform eversions on this array. We can prove that after several eversions the array $$$a$$$ stops changing. Output the minimum number $$$k$$$ such that the array stops changing after $$$k$$$ eversions.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). 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 $$$k$$$ — the number of eversions after which the array stops changing.

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

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

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

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

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


During the programming classes Vasya was assigned a difficult problem. However, he doesn't know how to code and was unable to find the solution in the Internet, so he asks you to help.You are given a sequence $$$a$$$, consisting of $$$n$$$ distinct integers, that is used to construct the binary search tree. Below is the formal description of the construction process.  First element $$$a_1$$$ becomes the root of the tree.  Elements $$$a_2, a_3, \ldots, a_n$$$ are added one by one. To add element $$$a_i$$$ one needs to traverse the tree starting from the root and using the following rules:   The pointer to the current node is set to the root.  If $$$a_i$$$ is greater than the value in the current node, then its right child becomes the current node. Otherwise, the left child of the current node becomes the new current node.  If at some point there is no required child, the new node is created, it is assigned value $$$a_i$$$ and becomes the corresponding child of the current node.

The first line of the input contains a single integer $$$n$$$ ($$$2 \leq n \leq 100\,000$$$) — the length of the sequence $$$a$$$. The second line contains $$$n$$$ distinct integers $$$a_i$$$ ($$$1 \leq a_i \leq 10^9$$$) — the sequence $$$a$$$ itself.

Output $$$n - 1$$$ integers. For all $$$i > 1$$$ print the value written in the node that is the parent of the node with value $$$a_i$$$ in it.

(function () {
	var n = parseInt(readline());
	var arr = readline().split(' ').map(function (ele){
		return parseInt(ele);
	});
	var disArr = [];
	arr.forEach(function (ele, idx) {
		disArr.push({
			idx: idx,
			v: ele
		});
	});
	disArr.sort(function (a,b) {
		return a.v - b.v;
	});
	disArr.forEach(function (ele, idx) {
		arr[ele.idx] = idx + 1;
	});
	var ans = [];
	for(var i=0;i<n;++i) {
		ans.push({
			left: -1,
			right: -1,
			father: -1
		});
	}
	var tr = (function (n) {
		var arr = [];
		for(var i=0;i<n;++i) {
			arr.push(0);
		}
		function add (p) {
			//print(p);
			for(;p<n;p+=p&(-p))
				++arr[p];
		}
		function find (p) {
			var ret = 0;
			//print(p);
			for(;p>0;p-=p&(-p))
				ret+=arr[p];
			return ret;
		}
		function lfind (v,l,r) {
			var mid;
			while(l<r) {
				mid = Math.floor((l+r)/2);
				if(find(mid)<v) {
					l=mid+1;
				} else {
					r=mid;
				}
				//print('l'+l);
				//print('r'+r);
			}
			return l;
		}
		function rfind (v,l,r) {
			var mid;
			while(l<r) {
				mid = Math.floor((l+r)/2);
				if(find(mid)>v) {
					r=mid;
				} else {
					l=mid+1;
				}
			}
			return l;
		}
		return {
			'add': add,
			'find': find,
			'lfind': lfind,
			'rfind': rfind
		}
	})(n+1);

	arr.forEach(function (ele, idx) {
		//print(ele);
		if(idx === 0) {
			tr.add(ele);
			return;
		}
		var x = tr.lfind(tr.find(ele),0,ele),
			y = tr.rfind(tr.find(ele),ele,n);
		if(x!=0)
			x = disArr[x-1].idx;
		else
			x = -1;
		y = disArr[y-1].idx;
		if(x!=-1&&ans[x].right === -1) {
			ans[x].right = idx;
			ans[idx].father = x;
		} else {
			ans[y].left = idx;
			ans[idx].father = y;
		}
		tr.add(ele);
	});
	// ans.forEach(function (ele) {
	// 	print(JSON.stringify(ele));
	// });
	var pt = [];
	for(i=1;i<n;++i) {
		pt.push(disArr[arr[ans[i].father]-1].v);
	}
	print(pt.join(' '));
})();