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

// Make a Snippet for the code above this and then write your logic in main();

function main() {
  const x = readline();
  var answer = []

  var array = []

  array[0] = 0
  array[1] = 1
  array[2] = 3
  array[3] = 2
  var index = 0
  for (var i = 0; i < 100000; i++) {
    if (index > 10000000) break
    index = index + i
    array[index] = i
  }

  Array(Number(x)).fill(1).map((t, i) => {

    var line = readline();
    var x = Number(line)
    var xx = x
    var value1 = 0
    var value2 = 0
    if (array[x]) return console.log(array[x])
    if (array[x+1]) return console.log(array[x+1]+1)
    while (!array[x]) {
      x -= 1
    }
    console.log(array[x] +1)

    // console.log(array[x] + 1)
  })
  // console.log(array)
  // array.map((v, i) => {
  //   console.log({v, i})
  // })
}

var tc = parseInt(readline());
for (; tc--;) {
    var Ar = readline().split(' ').map(x => +x);
    var n = Ar[0];
    var res = 0;
    var c = 0, x = 1;
    while (res < n) {
        res += x;
        x++, c++;
    }
    print(n == res - 1 ? c + 1 : c);
}

(()=>{"use strict";var t={675:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),Array.numbersMatrix=(t,e,r)=>{let n=[];null==t&&(e=t),null==r&&(r=0);for(let s=0;s<t;++s){let t=[];for(let n=0;n<e;++n)t.push(r);n.push(t)}return n},Array.prototype.sortAsc=function(){return this.sort(((t,e)=>t-e))},Array.prototype.sortDesc=function(){return this.sort(((t,e)=>e-t))},Array.prototype.max=function(){if(0==this.length)return 0;let t=this[0];for(let e=1;e<this.length;++e)t=Math.max(t,this[e]);return t},Array.prototype.min=function(){if(0==this.length)return 0;let t=this[0];for(let e=1;e<this.length;++e)t=Math.min(t,this[e]);return t},Array.prototype.sum=function(){let t=0;for(let e=0;e<this.length;++e)t+=this[e];return t},Array.prototype.modSum=function(t){let e=0;for(let r=0;r<this.length;++r)e+=this[r],e%=t;return e},Array.prototype.avg=function(){return this.sum()/this.length},Array.prototype.groupBy=function(t){let e=new Map;for(let r=0;r<this.length;++r){const n=t(this[r]);e.has(n)?e.get(n).push(this[r]):e.set(n,[this[r]])}return e},Array.prototype.deepCopy=function(){let t=[];if(0==this.length)return t;let e=!1;Array.isArray(this[0])&&(e=!0);for(let r=0;r<this.length;++r)e?t.push(this[r].deepCopy()):t.push(this[r]);return t},Array.prototype.transpose=function(){let t=[];if(0==this.length)return[[]];for(let e=0;e<this[0].length;++e){let r=[];for(let t=0;t<this.length;++t)r.push(this[t][e]);t.push(r)}return t},Array.prototype.toString=function(){if(0==this.length)return"";if(!Array.isArray(this[0]))return this.join(" ");let t="";for(let e=0;e<this.length;++e)t+=this[e].join(" "),e!=this.length-1&&(t+="\n");return t}},312:function(t,e,r){var n=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0});const s=n(r(747));let i="",o=[],u=0,l="";const p="local"===process.argv[2],a=t=>{if(p){if(o=s.default.readFileSync("input.txt").toString().trim().split("\n").map((t=>t.trim())),t(),s.default.writeFileSync("output.txt",l),console.log(l),s.default.readFileSync("expected.txt").toString().trim()==l.trim()){const{exec:t}=r(129);t("osascript -e 'display notification \"✅ AC!!\"'")}}else process.stdin.resume(),process.stdin.setEncoding("utf-8"),process.stdin.on("data",(t=>{i+=t})),process.stdin.on("end",(e=>{o=i.trim().split("\n").map((t=>t.trim())),t(),console.log(l)}))};function h(){return o[u++]}function f(){return h().split(" ").map((t=>parseFloat(t)))}function c(){return h().split("")}e.default={runMain:a,runEachTest:t=>{a((()=>{let[e]=f();for(;e>0;)t(),e--}))},readline:h,nextNumbers:f,nextBigNumbers:function(){return h().split(" ").map((t=>BigInt(t)))},nextNumbersMatrix:function(t){let e=[];for(let r=0;r<t;++r)e.push(f());return e},nextCharacters:c,nextCharacterMatrix:function(t){let e=[];for(let r=0;r<t;++r)e.push(c());return e},nextIntervalArray:function(t){let e=[];for(let r=0;r<t;++r){const[t,r]=f();e.push({a:t,b:r})}return e},put:function(t){l+=t},puts:function(t){l+=t+"\n"},debug:function(t){p&&console.log(t)}}},965:function(t,e,r){var n=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0}),r(675);const s=n(r(312));s.default.runEachTest((function(){let[t]=s.default.nextNumbers(),e=1,r=1;for(;e<t;)r++,e+=r;e!=t?e-1!=t?s.default.puts(r):s.default.puts(r+1):s.default.puts(r)}))},129:t=>{t.exports=require("child_process")},747:t=>{t.exports=require("fs")}},e={};!function r(n){if(e[n])return e[n].exports;var s=e[n]={exports:{}};return t[n].call(s.exports,s,s.exports,r),s.exports}(965)})();

// Solution written in typescript and compiled via webpack. Original source code below
// Happy Hacking 😜

// import './array'
//
// import io from './io'
//
// function main() {
//   let [n] = io.nextNumbers()
//
//   let a = 1
//
//   let i = 1
//   while (a < n) {
//     i++
//     a += i
//   }
//
//   if (a == n) {
//     io.puts(i)
//     return
//   }
//
//   if (a - 1 == n) {
//     io.puts(i + 1)
//     return
//   }
//
//   io.puts(i)
// }
//
// io.runEachTest(main)
// // io.runMain(main)

const solve = (x) => {
    let jump = 0;
    while (x > 0) {
        jump++;
        x -= jump;
    }
    if (x == -1) jump++;
    console.log(jump);
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(Number(line));
    });
    rl.on('close', () => {
        let t = input[0];
        let i = 1;
        while (t > 0) {
            solve(input[i]);
            t--;
            i++;
        }
    });
};

main()

let inputString = "";
let currentLine = 0;
process.stdin.on("data", (data) => {
  inputString += data;
});
process.stdin.on("end", function () {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());
  main();
});
function readLine() {
  return inputString[currentLine++];
}

// utils
const MOD = 1e9 + 7;
const mod = (n) => ((n % MOD) + MOD) % MOD;
const add = (a, b) => mod(mod(a) + mod(b));
const sub = (a, b) => mod(mod(a) - mod(b));
function mul() {
  for (var a = arguments, r = a[0], i = a.length; --i; )
    r = ((((r >> 16) * a[i]) % MOD) * 65536 + (r & 65535) * a[i]) % MOD;
  return r;
}
function inv(b) {
  for (var a = MOD, u = 0, v = 1, t; b; v = t)
    (t = (a / b) | 0), (a -= t * b), (u -= t * v), (t = a), (a = b), (b = t), (t = u), (u = v);
  u %= MOD;
  return u < 0 ? u + MOD : u;
}
function pow(base, n) {
  if (n === 0) return 1;
  let p = pow(base, Math.floor(n / 2));
  let res = mul(mod(p), mod(p));
  if (n % 2 === 1) res = mul(mod(res), base);
  return res;
}
function highestOneBit(i) {
  i |= i >> 1;
  i |= i >> 2;
  i |= i >> 4;
  i |= i >> 8;
  i |= i >> 16;
  return i - (i >>> 1);
}
function permutation(arr, len, store = "", permutations = []) {
  if (store.length === len) {
    permutations.push(store);
    return;
  }
  for (let i = 0; i < arr.length; i++) {
    const newArr = [...arr];
    const newChar = newArr.splice(i, 1);
    permutation(newArr, len, store.concat(newChar), permutations);
  }
  return permutations;
}

function main() {
  let t = +readLine();
  while (t--) {
    const n = +readLine();
    let [i, count] = [1, 0];
    while (count < n) {
      count += i;
      i++;
    }
    if (count === n) console.log(i - 1);
    else if (count - n > 1) console.log(i - 1);
    else console.log(i);
  }
}

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = []; var outputList = [];
read.on('line', function(input){
	var tmp = input.split(' ');
	for(var i = 0; i < tmp.length; i++){
		inLine.push(tmp[i]);
	}
});
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{
			ret[i] = next();
		}
	}
	return ret;
}
function nextIntArray(size){return nextArray(size, 'int');} function nextStrArray(size){return nextArray(size, 'str');}
function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();} function nextInt(){return myconv(next(),1);}
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';}}
	};
}
function myout(s){outputList.push(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
//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();
	for(var i = 0; i < t; i++){
		var N = nextInt();

		var index = 0;
		var count = 1;
		while(N > index){
			index += count;
			count++;
		}
		count--;

		if(index - 1 == N){
			count++;
		}
		myout(count);
	}
}

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

  let i = 1;
  let t = 0;
  readline.on('line', line => {
    if (i == 1) {
        // number of test case
        t = parseInt(line)
        i++;
    } else if (i <= t) {
        solve(line)
        i++;
    } else {
        solve(line)
        readline.close()
    }
  });

  function solve(input) {
    // your code here
    let x = parseInt(input);
    let i1 = 1
    let x1 = 1
    while (x1 < x) {
        i1 += 1
        x1 += i1
    }

    if (x1 - x == 1) console.log(i1 + 1)
    else console.log(i1)
  }

'use strict'

let t = readline();

while(t--)
{
    let x = readline(), s = 0, a = [], c = 0;
    for(let i = 1; s < x; i++)
    {
        s+=i;
        c++;
    }
    if(s!=x)
        {
            if(!(s - x - 1))while(s!=x){s--; c++;}
        }
    print(c);
}

/*
 * File Created: Monday, 30th November 2020 3:31:00 pm
 * Author: Lukas Rimkus
 */

'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 n = parseInt(data.shift());

        const res = b(n);

        console.log(res)
    }
});

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

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

function a(num)
{
    return num.length ;
}

function b(goal)
{

    function jump1(y,k)
    {
        return y + k;
    }
    function jump2(y)
    {
        return y - 1;
    }

    let sum = 0;

    let jumps = 0;

    let arr = [];

    while(sum < goal)
    {
        jumps++;

        arr.push(jumps);

        sum = sum + jumps;
    }

    if(sum === goal)
    {
        return arr.length;
    }
    else
    {
        let current_pos = sum;

        // that's how many steps back;
        let diference = current_pos - goal;

        // console.log(arr, arr.length, sum, diference)

        for(let i = 0 ; i < arr.length; i++)
        {
            if((arr[i] + 1) === diference)
            {
                return arr.length;
            }
        }
       return arr.length + 1;
    }
}

module.exports = b;

var t = +readline();

for (var i = 0; i < t; i++) {
  var l = +readline();
  var y = 0;
  var res = 0;

  for (var k = 1; k <= l; k++) {
    y += k;
    res++;

    if (y == l) {
      break;
    } else if (y > l) {
      if (y - l == 1) {
        res++;
      }
      break;
    }
  }

  print(res);
}

'use strict'

let t = readline();

while(t--)
{
    let x = readline(), s = 0, a = [], c = 0;
    for(let i = 1; s < x; i++)
    {
        s+=i;
        c++;
    }
    for(let i = 1; s - i > x; i++)
    {
        s-=i;
        c--;
    }
    while(s>x)
    {
        s--;
        c++;
    }
    print(c);
}

var t = +readline();

for (var i = 0; i < t; i++) {
  var l = +readline();
  var y = 0;
  var res = 0;

  for (var k = 1; k <= l; k++) {
    y += k;
    res++;

    if (y == l) {
      break;
    } else if (l - y == 1) {
      res += 2;
      break;
    } else {
      res = l - y;
      break;
    }
  }

  print(res);
}

var t = +readline();

for (var i = 0; i < t; i++) {
  var l = +readline();
  var p = 0;
  var res = 0;

  if (l == 1) {
    print(l);
  } else if (l == 2) {
    print(l+1);
  } else {
    print(l-1);
  }
}

const solve = (x) => {
    if (x == 1) {
        console.log(1);
        return;
    }
    if (x == 2) {
        console.log(3);
        return;
    }
    console.log(x - 1);
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(Number(line));
    });
    rl.on('close', () => {
        let t = input[0];
        let i = 1;
        while (t > 0) {
            solve(input[i]);
            t--;
            i++;
        }
    });
};

main()

let inputString = "";
let currentLine = 0;
process.stdin.on("data", (data) => {
  inputString += data;
});
process.stdin.on("end", function () {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());
  main();
});
function readLine() {
  return inputString[currentLine++];
}

// utils
const MOD = 1e9 + 7;
const mod = (n) => ((n % MOD) + MOD) % MOD;
const add = (a, b) => mod(mod(a) + mod(b));
const sub = (a, b) => mod(mod(a) - mod(b));
function mul() {
  for (var a = arguments, r = a[0], i = a.length; --i; )
    r = ((((r >> 16) * a[i]) % MOD) * 65536 + (r & 65535) * a[i]) % MOD;
  return r;
}
function inv(b) {
  for (var a = MOD, u = 0, v = 1, t; b; v = t)
    (t = (a / b) | 0), (a -= t * b), (u -= t * v), (t = a), (a = b), (b = t), (t = u), (u = v);
  u %= MOD;
  return u < 0 ? u + MOD : u;
}
function pow(base, n) {
  if (n === 0) return 1;
  let p = pow(base, Math.floor(n / 2));
  let res = mul(mod(p), mod(p));
  if (n % 2 === 1) res = mul(mod(res), base);
  return res;
}
function highestOneBit(i) {
  i |= i >> 1;
  i |= i >> 2;
  i |= i >> 4;
  i |= i >> 8;
  i |= i >> 16;
  return i - (i >>> 1);
}
function permutation(arr, len, store = "", permutations = []) {
  if (store.length === len) {
    permutations.push(store);
    return;
  }
  for (let i = 0; i < arr.length; i++) {
    const newArr = [...arr];
    const newChar = newArr.splice(i, 1);
    permutation(newArr, len, store.concat(newChar), permutations);
  }
  return permutations;
}

function main() {
  let t = +readLine();
  while (t--) {
    const n = +readLine();
    if (n === 1) console.log(1);
    else if (n === 2) console.log(3);
    else {
      let [i, count, found] = [1, 0, false];
      while (count <= n) {
        count += i;
        if (count === n) {
          console.log(i);
          found = true;
          break;
        }
        if (count > n) break;
        i++;
      }
      if (!found) {
        const prev = count - i;
        const [prevDiff, nextDiff] = [Math.abs(prev - n), Math.abs(count - n)];
        if (prevDiff <= nextDiff) {
          console.log(i - 1 + prevDiff);
        } else {
          console.log(i + nextDiff);
        }
      }
    }
  }
}

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = []; var outputList = [];
read.on('line', function(input){
	var tmp = input.split(' ');
	for(var i = 0; i < tmp.length; i++){
		inLine.push(tmp[i]);
	}
});
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{
			ret[i] = next();
		}
	}
	return ret;
}
function nextIntArray(size){return nextArray(size, 'int');} function nextStrArray(size){return nextArray(size, 'str');}
function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();} function nextInt(){return myconv(next(),1);}
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';}}
	};
}
function myout(s){outputList.push(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
//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();
	for(var i = 0; i < t; i++){
		var N = nextInt();
		var index = 0;
		var count = 1;
		while(N > index){
			index += count;
			count++;
		}
		count--;
		if(N != index && N % 2 != index % 2){
			count++;
		}

		myout(count);
	}
}

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = []; var outputList = [];
read.on('line', function(input){
	var tmp = input.split(' ');
	for(var i = 0; i < tmp.length; i++){
		inLine.push(tmp[i]);
	}
});
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{
			ret[i] = next();
		}
	}
	return ret;
}
function nextIntArray(size){return nextArray(size, 'int');} function nextStrArray(size){return nextArray(size, 'str');}
function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();} function nextInt(){return myconv(next(),1);}
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';}}
	};
}
function myout(s){outputList.push(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
//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 max = 1000007;
	var dp = new Array(max);
	dp[0] = 0;
	var index = 0;
	var count = 1;
	while(index < max){
		index += count;
		dp[index] = count;
		count++;
	}
	var add = 1;
	while(true){
		var index = 1;
		var count = 1 + add;
		while(index < max){
			index += count;
			if(dp[index] == null){
				dp[index] = count;
			}else{
				dp[index] = Math.min(count, dp[index]);
			}
			count++;
		}
		add++;
		if(add > max){
			break;
		}
	}
	for(var i = max; i >= 1; i--){
		if(dp[i] != null){
			if(dp[i - 1] == null){
				dp[i - 1] = dp[i] + 1;
			}else{
				dp[i - 1] = Math.min(dp[i] + 1, dp[i - 1]);
			}
		}
	}
	var t = nextInt();
	for(var i = 0; i < t; i++){
		var N = nextInt();
		myout(dp[N]);
	}
}

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = []; var outputList = [];
read.on('line', function(input){
	var tmp = input.split(' ');
	for(var i = 0; i < tmp.length; i++){
		inLine.push(tmp[i]);
	}
});
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{
			ret[i] = next();
		}
	}
	return ret;
}
function nextIntArray(size){return nextArray(size, 'int');} function nextStrArray(size){return nextArray(size, 'str');}
function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();} function nextInt(){return myconv(next(),1);}
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';}}
	};
}
function myout(s){outputList.push(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
//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 max = 1500007;
	var dp = new Array(max);
	dp[0] = 0;
	var index = 0;
	var count = 1;
	while(index < max){
		index += count;
		dp[index] = count;
		count++;
	}
	var add = 1;
	while(true){
		var index = 1;
		var count = 1 + add;
		while(index < max){
			index += count;
			if(dp[index] == null){
				dp[index] = count;
			}else{
				dp[index] = Math.min(count, dp[index]);
			}
			count++;
		}
		add++;
		if(add > max){
			break;
		}
	}
	for(var i = max; i >= 1; i--){
		if(dp[i] != null){
			if(dp[i - 1] == null){
				dp[i - 1] = dp[i] + 1;
			}else{
				dp[i - 1] = Math.min(dp[i] + 1, dp[i - 1]);
			}
		}
	}
	var t = nextInt();
	for(var i = 0; i < t; i++){
		var N = nextInt();
		myout(dp[N]);
	}
}

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = []; var outputList = [];
read.on('line', function(input){
	var tmp = input.split(' ');
	for(var i = 0; i < tmp.length; i++){
		inLine.push(tmp[i]);
	}
});
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{
			ret[i] = next();
		}
	}
	return ret;
}
function nextIntArray(size){return nextArray(size, 'int');} function nextStrArray(size){return nextArray(size, 'str');}
function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();} function nextInt(){return myconv(next(),1);}
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';}}
	};
}
function myout(s){outputList.push(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
//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 dp = new Array(1000000);
	dp[0] = 0;
	var index = 0;
	var count = 1;
	while(index < 1000000){
		index += count;
		dp[index] = count;
		count++;
	}
	var add = 1;
	while(true){
		var index = 1;
		var count = 1 + add;
		while(index < 1000000){
			index += count;
			if(dp[index] == null){
				dp[index] = count;
			}else{
				dp[index] = Math.min(count, dp[index]);
			}
			count++;
		}
		add++;
		if(add > 1000000){
			break;
		}
	}

	var t = nextInt();
	for(var i = 0; i < t; i++){
		var N = nextInt();
		if(dp[N] == null){
			myout(dp[N + 1] + 1);
		}else{
			myout(dp[N]);
		}

	}
}

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

// Make a Snippet for the code above this and then write your logic in main();

function main() {
  const x = readline();
  var answer = []

  var array = []

  array[0] = 0
  array[1] = 1
  array[2] = 3
  array[3] = 2
  var index = 0
  for (var i = 0; i < 100000; i++) {
    if (index > 10000000) break
    index = index + i
    array[index] = i
  }

  Array(Number(x)).fill(1).map((t, i) => {

    var line = readline();
    var x = Number(line)
    var xx = x
    var value1 = 0
    var value2 = 0
    if (array[x]) return console.log(array[x])
    while (!array[x]) {
      x -= 1
      value1 += 1
    }
    while (!array[xx]) {
      xx += 1
      value2 += 1
    }
    console.log(Math.min(array[x] + value1, array[xx] + value2 ))

    // console.log(array[x] + 1)
  })
  // console.log(array)
  // array.map((v, i) => {
  //   console.log({v, i})
  // })
}


function dustSweeper() {
  n = +r();
  a = r().split` `.map(Number);
  let firstNumberIndex = a.indexOf(a.find(d => d > 0));
  if (firstNumberIndex === -1) {
    log(0);
    return;
  }
  let sum = 0;
  for (let i = firstNumberIndex; i < a.length - 1; i++) {
    if (a[i] === 0) sum++;
    else sum += a[i];
  }
  log(sum);
}

function main() {
  tt = +r();
  for (; tt; tt--) dustSweeper();
}

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

r = readline;
log = console.log;

'use strict';
/*
3
2 0 0
5
0 2 0 2 0
6
2 0 3 0 4 6
4
0 0 0 10
*/
const main = () => {
  const n = parseInt(readline());

  for (let i = 0; i < n; ++i) {
    const len = parseInt(readline());
    const arr = readline().split(' ').map(Number);
    let operations = 0;
    let sum = 0;
    let seenNonZero = false;
    for (let i = 0; i < len - 1; ++i) {
      if (arr[i] === 0 && seenNonZero) {
        ++operations;
      } else if (arr[i] > 0) {
        seenNonZero = true;
      }
      sum += arr[i];
    }
    console.log(operations + sum);
  }
};

const readline = () => {
  return inputString[currentLine++];
};

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

"use strict";
const fs = require("fs");
//let filename = "0.txt";
let filename = 0;
const input = fs.readFileSync(filename, "utf8").trim().split("\n");
let line = 0;
function readline() {
  return input[line++];
}
const solve = () => {
  let t = parseInt(readline());
  while (t--) {
    let n = parseInt(readline());
    let arr = readline()
      .trim()
      .split(" ")
      .map((cur) => parseInt(cur));
    let flag = false,
      zc = 0;
    for (let i = 0; i < n - 1; i++) {
      if (arr[i] === 0 && flag) zc++;
      else if (arr[i] > 0) flag = true;
    }
    if (!flag) console.log(0);
    else {
      let sum = 0;
      for (let i = 0; i < n - 1; i++) sum += arr[i];
      console.log(sum + zc);
    }
  }
};
solve();

var tests = parseInt(readline());
    for (var t=0; t < tests; t++) {
        var n = parseInt(readline());
        var arr = readline().split(' ').map(x=>parseInt(x));
        var sum = 0;
        var tail = 0;
        while (arr[tail] != undefined && arr[tail] == 0) {
            tail++;
        }
        for (var i = tail+1; i < n-1; i++) {
            if (arr[i] == 0) {
                arr[tail]--;
                arr[i]++;
                if (arr[tail] == 0) {
                    tail++;
                }
                sum++;
            }
        }
        for (var i = 0; i < n-1; i++) {
            sum += arr[i];
        }
        print(sum);
    }

function dustSweeper() {
  n = +r();
  a = r().split` `.map(Number);
  let firstNumberIndex = a.indexOf(a.find(d => d > 0));
  let sum = 0;
  for (let i = firstNumberIndex; i < a.length - 1; i++) {
    if (a[i] === 0) sum++;
    else sum += a[i];
  }
  log(sum);

  // function firstZeroIndex(i) {
  //   let firstZeroIndex = a.slice(i + 1).indexOf(0);
  //   if (firstZeroIndex === -1) return -1;
  //   return firstZeroIndex + i + 1;
  // }

  // for (let i = 0; i < a.length && firstZeroIndex(i) !== -1; i++) {
  //   while (a[i] > 0 || firstZeroIndex(i) !== -1) {
  //     // log(`before: i:${i}, a=${a}`);
  //     i0 = firstZeroIndex(i);
  //     a[i0]++;
  //     a[i]--;
  //     count++;
  //     // log(`after: i:${i}, a=${a}`);
  //   }
  // }
}

function main() {
  tt = +r();
  for (; tt; tt--) dustSweeper();
}

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

r = readline;
log = console.log;

function dustSweeper() {
  n = +r();
  a = r().split` `.map(Number);
  let firstNumberIndex = a.indexOf(a.find(d => d > 0));
  // a = a.slice(firstNumberIndex);
  let count = 0;
  for (let i = firstNumberIndex; i < a.length - 1; i++) {
    if (a[i] === 0) count++;
  }
  log(count + a.reduce((p, c, i) => (i < a.length - 1 ? p + c : p)));

  // function firstZeroIndex(i) {
  //   let firstZeroIndex = a.slice(i + 1).indexOf(0);
  //   if (firstZeroIndex === -1) return -1;
  //   return firstZeroIndex + i + 1;
  // }

  // for (let i = 0; i < a.length && firstZeroIndex(i) !== -1; i++) {
  //   while (a[i] > 0 || firstZeroIndex(i) !== -1) {
  //     // log(`before: i:${i}, a=${a}`);
  //     i0 = firstZeroIndex(i);
  //     a[i0]++;
  //     a[i]--;
  //     count++;
  //     // log(`after: i:${i}, a=${a}`);
  //   }
  // }
}

function main() {
  tt = +r();
  for (; tt; tt--) dustSweeper();
}

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

r = readline;
log = console.log;


"use strict";
const fs = require("fs");
//let filename = "0.txt";
let filename = 0;
const input = fs.readFileSync(filename, "utf8").trim().split("\n");
let line = 0;
function readline() {
  return input[line++];
}
function iInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => parseInt(cur));
}
function BInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => BigInt(cur));
}
function string() {
  return readline().trim();
}
function stringArr() {
  return readline().trim().split("");
}

function stringArr1() {
  return readline().trim().split(" ");
}
function getValue() {
  return parseInt(readline());
}
let mod = 1e9 + 7;
mod = BigInt(mod);
const pow = (num, exp, mod) => {
  if (exp === BigInt(0)) return BigInt(1);
  let x = pow(num, exp / BigInt(2), mod);
  if (exp % BigInt(2)) return (((x * x) % mod) * num) % mod;
  else return (x * x) % mod;
};
const inverse = (num, exp) => {
  return pow(BigInt(num), BigInt(exp), BigInt(mod));
};
const solve = () => {
  let t = getValue();
  while (t--) {
    let [n, m] = iInpArr();
    let arr = iInpArr();
    let pair = [],
      obj = {};
    for (let i = 0; i < n; i++) obj[i + 1] = 0;
    for (let i = 0; i < m; i++) {
      let j = iInpArr();
      pair.push(j);
      obj[j[0]] += 1;
      obj[j[1]] += 1;
    }
    if (m === 0 || m % 2 === 0) {
      console.log(0);
      continue;
    }
    let ans = Infinity;
    for (let i = 0; i < n; i++) {
      if (obj[i + 1] & 1) ans = Math.min(ans, arr[i]);
    }
    for (let i = 0; i < m; i++) {
      if ((obj[pair[i][0]] & 1) === 0 && (obj[pair[i][1]] & 1) === 0)
        ans = Math.min(ans, arr[pair[i][0] - 1] + arr[pair[i][1] - 1]);
    }
    console.log(ans);
  }
};
solve();

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

function readNumbersLine() {
    return readline().split(" ").map(el => +el);
}

function solve() {
    let arr = {};
    let arr2 = []

    let [n, m] = readNumbersLine()
    let A = readNumbersLine()

    for (let j = 0; j < m; j++) {
        let [x, y] = readNumbersLine()
        x -= 1
        y -= 1
        if (arr[x]) {
            arr[x].push(y);
        } else {
            arr[x] = [y]
        }

        if (arr[y]) {
            arr[y].push(x);
        } else {
            arr[y] = [x]
        }
        arr2.push(A[x] + A[y]);
    }

    if (m % 2 === 0) {
        print(0)
        return;
    }
    A = A.map((value, index) => [value, index]);
    A.sort((a, b) => a[0] - b[0])
    let condition = true;
    let i = 0
    arr2.sort((a, b) => a - b);
    // console.log(arr2)
    // console.log(arr)
    while (condition) {
        let index = A[i][1]
        if (arr[index] && arr[index].length % 2 === 1) {
            console.log(Math.min(A[i][0], arr2[0]));
            return;
        }
        i += 1
        if (i >= A.length) {
            condition = false
        }
    }

    console.log(arr2[0]);
    return;
}

function main() {
    let k = +readline()
    for (let i = 0; i < k; i++) {
        solve();
    }

}

var tests = parseInt(readline());
    for (var t=0; t < tests; t++) {
        var nm = readline().split(' ').map(x=>parseInt(x));
        var n = nm[0];
        var m = nm[1];
        var unh = readline().split(' ').map(x=>parseInt(x));
        var graph = {};
        var pairs = [];
        for (var i = 0; i < m; i++) {
            var pair = readline().split(' ').map(x=>parseInt(x));
            pairs.push(pair);
            if (!graph[pair[0]]) {
                graph[pair[0]] = 0;
            }
            graph[pair[0]]++;
            if (!graph[pair[1]]) {
                graph[pair[1]] = 0;
            }
            graph[pair[1]]++;
        }
        //console.log(pairs);
        if (!(m & 1) || n == 1) {
            print(0);
        } else {
            var toCheck = new Set();
            var min = Number.POSITIVE_INFINITY;
            Object.keys(graph).forEach((k) => {
                if (graph[k] & 1) {
                    min = Math.min(unh[k-1], min);
                } else {
                    toCheck.add(parseInt(k));
                }
            })
            //console.log(toCheck);
            for (var i = 0; i < pairs.length; i++) {
                if (toCheck.has(pairs[i][0]) && toCheck.has(pairs[i][1])) {
                    min = Math.min(min, unh[pairs[i][0]-1]+unh[pairs[i][1]-1]);
                }
            }

            print(min);
        }
    }

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

function readNumbersLine() {
    return readline().split(" ").map(el => +el);
}

function solve() {
    let arr = {};
    let arr2 = []

    let [n, m] = readNumbersLine()
    let A = readNumbersLine()
    const sum = A.reduce((partialSum, a) => partialSum + a, 0);

    for (let j = 0; j < m; j++) {
        let [x, y] = readNumbersLine()
        x -= 1
        y -= 1
        if (arr[x]) {
            arr[x].push(y);
        } else {
            arr[x] = [y]
        }

        if (arr[y]) {
            arr[y].push(x);
        } else {
            arr[y] = [x]
        }
        arr2.push(A[x] + A[y]);
    }

    if (m % 2 === 0) {
        print(0)
        return;
    }
    A = A.map((value, index) => [value, index]);
    A.sort((a, b) => a[0] - b[0])
    let condition = true;
    let i = 0
    arr2.sort();
    // console.log(A)
    // console.log(arr)
    while (condition) {
        let index = A[i][1]
        if (arr[index] && arr[index].length % 2 === 1) {
            console.log(Math.min(A[i][0], arr2[0]));
            return;
        }
        i += 1
        if (i >= A.length) {
            condition = false
        }
    }

    console.log(arr2[0]);
    return;
}

function main() {
    let k = +readline()
    for (let i = 0; i < k; i++) {
        solve();
    }

}


function calc(n, a, b) {
  if (a <= b / 2) {
    return a * n;
  } else {
    return b * Math.floor(n / 2) + a * (n % 2);
  }
}

var q = parseInt(readline());
// print(q);
var nab;
for (var i = 0; i < q; i++) {
  nab = readline().split(" ").map(el => parseInt(el));
  // print(nab);
  print(calc(nab[0], nab[1], nab[2]));
}

var n=parseInt(readline());
for(var i=0;i<n;i++)
{
  var s=readline().split(' ');
  if(s[0]%2==0)
  print(Math.min(s[0]*s[1],(s[0]/2)*s[2]));
  else
  print(Math.min(s[0]*s[1],Math.floor(s[0]/2)*s[2]+parseInt(s[1])));
}

var q = readline().split(" ").map(function(x) { return parseInt(x); })[0];
while (q-->0) {
	var numbers = readline().split(" ").map(function(x) { return parseInt(x); });
	var n = numbers[0], a = numbers[1], b = numbers[2];
	var down = 0, up = Math.floor(n/2) + 1;
	while (up > down + 5) {
		var t1 = down + Math.floor((up-down)/3);
		var cost1 = t1 * b + Math.max(0, n-t1*2) * a;
		var t2 = down + 2*Math.floor((up-down)/3);
		var cost2 = t2 * b + Math.max(0, n-t2*2) * a;
		if (cost1 < cost2) up = t2;
		else down = t1;
	}
	var mn = n*a;
	for (var t=down-50; t<=down+50; t++)
		if (t >= 0 && t*2 <= n) {
			var cost1 = t * b + Math.max(0, n-t*2) * a;
			mn = Math.min(mn, cost1);
		}
	write(mn + "\n");
}

var q = parseInt(readline())

for(var t=0;t<q;t++) {

    var nab = readline().split(' ').map((v)=>(parseInt(v)))
    var n = nab[0];
    var a = nab[1];
    var b = nab[2];

    var aa = (n%2) * a + parseInt(n/2) * b;
    var bb = n*a

    print( Math.min(aa, bb) )
}

var n = parseInt(readline());
for (var i = 0; i < n; i++) {
    var s = readline().split(" ");
    print(foo(s[0], s[1], s[2]));
}

function foo(n, a, b) {
    if (a*2 <= b) return n*a;
    return Math.trunc(n / 2) * b + n % 2* a;
}

function repeat(n, fn) {
    var result = [];
    for (var i = 0; i < n; i++)
        result.push(fn(i));
    return result;
}
function readInput(_a) {
    var line = _a.line, int = _a.int, ints = _a.ints, strings = _a.strings;
    var n = int();
    return { n: n, queries: repeat(n, function () { var _a = ints(), n = _a[0], a = _a[1], b = _a[2]; return { n: n, a: a, b: b }; }) };
}
function printOutput(result, _a) {
    var line = _a.line;
    result.forEach(function (r) { return line(r); });
}
function show(input) {
    console.log(input, '-->', solve(input));
}
function inputFunctions(line) {
    return {
        line: line,
        int: function () { return parseInt(line()); },
        strings: function () { return line().split(' '); },
        ints: function () { return line().split(' ').map(function (v) { return parseInt(v); }); }
    };
}
function outputFunctions(line) {
    return { line: line };
}
function sampleInput(input) {
    var lines = input.split('\n');
    var index = 0;
    var line = function () { return lines[index++]; };
    return inputFunctions(line);
}
function sample(input) {
    show(readInput(sampleInput(input)));
}
function run() {
    var input = readInput(inputFunctions(readline));
    var output = solve(input);
    printOutput(output, outputFunctions(print));
}
function solve(_a) {
    // implementation
    var n = _a.n, queries = _a.queries;
    return queries.map(function (_a) {
        var n = _a.n, a = _a.a, b = _a.b;
        if (a * 2 < b)
            return n * a;
        return n % 2 * a + (n - n % 2) / 2 * b;
    });
}
// sample(`4
// 10 1 3
// 7 3 2
// 1 1000 1
// 1000000000000 42 88`)
run();

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

  let tmp=0, T=0, triplets=[];

  rl.on('line', (input) => {
      if (tmp==0) {T=Number(input); tmp++;}
      else {
          if (tmp<=T){
          let str=input.split(' ').map(a=>{return Number(a);});
            triplets.push(str);
            tmp++;}
          }
    if (tmp>T){ rl.close(); therest();}
    return 0;}
);

let therest=function() {
    for (let i=0; i<T; i++) {
        let n=triplets[i][0], a=triplets[i][1], b=triplets[i][2];
       // console.log(n, a, b);

        if (2*a<=b) console.log(a*n);
        else if (n%2==0) console.log(b*n/2);
        else console.log(a+b*(n-1)/2);

    }
//console.log(T, pairs)
}
//console.log(lngth, str);}

  /*
  rl.question('What do you think of Node.js? ', (answer) => {
    // TODO: Log the answer in a database
    console.log(`Thank you for your valuable feedback: ${answer}`);

    rl.close();
  });*/

var q=parseInt(readline());
for(var i=0;i<q;i++)
{
  var n=readline().split(' ');
  if(n[1]*2<=n[2])
  print(n[0]*n[1]);
  else
  {
    if(n[0]%2==0)
    print(n[2]*Math.floor((n[0]/2)));
    else
    {
      print(n[2]*Math.floor((n[0]/2))+parseInt(n[1]));
    }
  }
}

function main() {
    for(var i = 1; i < data.length; i++){
        var x = data[i].split(" ").map(x=>parseInt(x));
        console.log(Math.min(x[1]*2, x[2])*Math.floor(x[0]/2)+x[0]%2*x[1])
    }
}
const readline = require('readline');
const rl = readline.createInterface({input: process.stdin});
var data = [];
rl.on('line', function (line) {data.push(line);});
rl.on('close', main);

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
let c = 0;

rl.on('line', (d) => {
  if (c === 0) {
    c++;
    return;
  }

  let [n, a, b] = d.split(' ').map(Number);
  let ans = 0;

  if (n === 1) {
    ans += a;
  }
  else {
    if (a * 2 < b) {
      ans += n * a;
    }
    else {
      ans += Math.floor(n / 2) * b;
      if (n % 2 === 1) {
        ans += a;
      }
    }
  }

  console.log(ans);
  c++;
});

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
let c = 0;

rl.on('line', (d) => {
  if (c === 0) {
    c++;
    return;
  }

  let [n, a, b] = d.split(' ').map(Number);
  const ans = Math.floor(n / 2) * Math.min(2*a, b) + n % 2 * a;

  // if (n === 1) {
  //   ans += a;
  // }
  // else {
  //   if (a * 2 < b) {
  //     ans += n * a;
  //   }
  //   else {
  //     ans += Math.floor(n / 2) * b;
  //     if (n % 2 === 1) {
  //       ans += a;
  //     }
  //   }
  // }

  console.log(ans);
  c++;
});

(function() {
    var read = {
        number: function() {
            return +readline();
        },
        arr: function(divider) {
            return readline().split(divider ? divider : '');
        },
        arrNumber: function(divider) {
            return readline()
                .split(divider ? divider : '')
                .map(item => +item);
        }
    }

    var q = read.number();

    for(var i = 0; i < q; i++) {
        var n = read.arrNumber(' ');

        if(n[0] === 1) {
            print(n[1]);
        }
        else if(n[1] * 2 > n[2]) {
            var h2 = Math.floor(n[0] / 2);
            print(h2 * n[2] + (n[0] - (h2 * 2)) * n[1]);
        }
        else {
            print(n[1] * n[0]);
        }
    }
}());

var n=parseInt(readline());
for(var i=0;i<n;i++)
{
  var s=readline().split(' ');
  if(s[0]%2==0)
  print(Math.min(s[0]*s[1],(s[0]/2)*s[2]));
  else
  print(Math.min(s[0]*s[1],Math.floor(s[0]/2)*s[2]+s[1]));
}

var n=parseInt(readline());
for(var i=0;i<n;i++)
{
  var s=readline().split(' ');
  if(s[0]%2===0)
  print(Math.min(s[0]*s[1],(s[0]/2)*s[2]));
  else
  print(Math.min(s[0]*s[1],Math.floor(s[0]/2)*s[2]+s[1]));
}

var n=parseInt(readline());
for(var i=0;i<n;i++)
{
  var s=readline().split(' ');
  if(s[0]%2==0)
  print(Math.min(s[0]*s[1],(s[0]/2)*s[2]));
  else
  {
    print(Math.min(s[0]*s[1],Math.floor(s[0]/2)*s[2]+s[1]));
    print(s[0]*s[1],Math.floor(s[0]/2));
  }
}

var n=parseInt(readline());
for(var i=0;i<n;i++)
{
  var s=readline().split(' ');
  if(parseInt(s[0])%2==0)
  print(Math.min(s[0]*s[1],(s[0]/2)*s[2]));
  else
  print(Math.min(s[0]*s[1],(Math.floor(s[0]/2)*s[2]+s[1])));
}

var n=parseInt(readline());
for(var i=0;i<n;i++)
{
  var s=readline().split(' ');
  if(parseInt(s[0])%2==0)
  print(Math.min(s[0]*s[1],(s[0]/2)*s[2]));
  else
  print(Math.min(s[0]*s[1],Math.floor(s[0]/2)*s[2]+s[1]));
}

var n=parseInt(readline());
for(var i=0;i<n;i++)
{
  var s=readline().split(' ');
  if(s[0]%2==0)
  print(Math.min(s[0]*s[1],(s[0]/2)*s[2]));
  else
  {
    print(Math.min(s[0]*s[1],Math.floor(s[0]/2)*s[2]+s[1]));
    print(2);
  }
}

var n = parseInt(readline());
for (var i = 0; i < n; i++) {
    var s = readline().split(" ");
    print(foo(s[0], s[1], s[2]));
}

function foo(n, a, b) {
    if (a*2 <= b) return n*a;
    return Math.ceil(n / 2) * b + (n % 2) * a;
}


function solve(values) {
    var indexes = [];
    for (var i = 1; i < values.length - 1; i++) {
        if (values[i] === 0 && values[i - 1] === 1 && values[i + 1] === 1) {
            indexes.push(i);
        }
    }

    var counter = 0;
    for (var _i = 0; _i < indexes.length; _i++) {
        if (indexes[_i + 1] != null && indexes[_i] + 2 === indexes[_i + 1]) {
            counter++;
            _i += 1;
            continue;
        }
        counter++;
    }

    return counter;
}

readline()
var numbers = readline().split(" ").map(function(x) { return parseInt(x); });
write(solve(numbers))

readline();
var rooms = readline().split(' ');
var cnt = 0;
for(var i = 2; i < rooms.length; i++) {
    if(rooms[i] - rooms[i - 1] === 1 && rooms[i - 2] === '1') {
        rooms[i++] = '0';
        cnt++;
    }
}
print(cnt);

readline();
var arr = readline().split(" ");
var disturbed = 0;
for(var i = 1; i < arr.length - 1; i++){
    if(arr[i - 1] == 1 && arr[i + 1] == 1 && arr[i] == 0){
        disturbed++;
        arr[i - 1] = arr[i + 1] = 0;
    }
}
print(disturbed);


let inputString = "";
let currentLine = 0;
process.stdin.on("data", (data) => {
  inputString += data;
});
process.stdin.on("end", function () {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());
  main();
});
function readLine() {
  return inputString[currentLine++];
}
function main() {
  let t = +readLine();
  let count = 0;
  const arr = [];
  while (t--) {
    let [x, y] = readLine()
      .split(" ")
      .map((n) => parseInt(n));
    arr.push([x, y]);
  }

  for (let i = 0; i < arr.length; i++) {
    const [x, y] = arr[i];
    let [left, right, top, bottom] = [0, 0, 0, 0];
    for (let j = 0; j < arr.length; j++) {
      if (i !== j) {
        const [cx, cy] = arr[j];
        if (cx < x && cy === y) left++;
        if (cx > x && cy === y) right++;
        if (cx === x && cy > y) top++;
        if (cx === x && cy < y) bottom++;
      }
    }
    if (left > 0 && right > 0 && top > 0 && bottom > 0) count++;
  }
  console.log(count);
}

let inputString = '';
let currentLine = 0;
process.stdin.on('data', data => {
	inputString += data;
});
process.stdin.on('end', function () {
	inputString = inputString
		.trim()
		.split('\n')
		.map(str => str.trim());
	main();
});

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

function isPrime(x) {
	let s = Math.sqrt(x);
	let i = 2;
	while (i <= s) {
		if (x % i === 0) {
			return false;
		}
		i++;
	}
	return true;
}

function isSuperCentral(cord, xs, ys) {
	let [x, y] = cord;
	let minY = Math.min(...xs[x]),
		maxY = Math.max(...xs[x]),
		minX = Math.min(...ys[y]),
		maxX = Math.max(...ys[y]);
	return minY < y && maxY > y && minX < x && maxX > x;
}

function main() {
	let n = readLine() >> 0;
	let chart = [];
	let xs = {},
		ys = {};
	while (n--) {
		let [x, y] = readLine()
			.split(' ')
			.map(x => x >> 0);
		xs[x] = xs[x] || [];
		xs[x].push(y);
		ys[y] = ys[y] || [];
		ys[y].push(x);
		chart.push([x, y]);
	}
	let c = 0;
	for (let i = 0; i < chart.length; ++i)
		if (isSuperCentral(chart[i], xs, ys)) c++;

	console.log(c);
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', (inputStd) => {
    inputString += inputStd;
});
process.stdin.on('end', function () {
    inputString = inputString
        .trim()
        .split('\n')
        .map((str) => {
            return str.trim();
        });
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    let n = parseInt(readLine());
    let Allx = [];
    let Ally = [];
    for (let i = 0; i < n; i++) {
        let [x, y] = readLine().split(' ').map(Number);
        Allx.push(x);
        Ally.push(y);
    }

    let up = [];
    let down = [];
    let left = [];
    let right = [];

    for (let i = 0; i < Allx.length; i++) {
        for (let j = 0; j < n; j++) {
            if (Allx[i] === Allx[j] && Ally[i] > Ally[j]) {
                down[i] = true;
            } else if (Allx[i] === Allx[j] && Ally[i] < Ally[j]) {
                up[i] = true;
            } else if (Allx[i] > Allx[j] && Ally[i] === Ally[j]) {
                left[i] = true;
            } else if (Allx[i] < Allx[j] && Ally[i] === Ally[j]) {
                right[i] = true;
            }
        }
    }
    let count = 0;
    for (let i = 0; i < n; i++) {
        if (
            up[i] === true &&
            left[i] === true &&
            right[i] === true &&
            down[i] === true
        ) {
            count++;
        }
    }
    console.log(count);
}

;(function () {

	var n = +readline();

	var p = [];
	for (var i = 0; i < n; i++) {
		var e = readline().split(' ');
		p.push( {x: +e[0], y: +e[1]} );
	}

	var s = 0;
	for (var i = 0; i < n; i++) {
		var f = false;

		for (var j = 0; j < n; j++)
			if (p[i].x == p[j].x && p[i].y > p[j].y) {
				f = true;
				break;
			}

		if (f) {
			f = false;
			for (var j = 0; j < n; j++)
				if (p[i].x == p[j].x && p[i].y < p[j].y) {
					f = true;
					break;
				}

			if (f) {
				f = false;
				for (var j = 0; j < n; j++)
					if (p[i].y == p[j].y && p[i].x > p[j].x) {
						f = true;
						break;
					}

				if (f) {
					f = false;
					for (var j = 0; j < n; j++)
						if (p[i].y == p[j].y && p[i].x < p[j].x) {
							f = true;
							break;
						}

					if (f) s++;
				}
			}
		}
	}

	print(s);

}).call(this);


"use strict";
const fs = require("fs");
//let filename = "0.txt";
let filename = 0;
const input = fs.readFileSync(filename, "utf8").trim().split("\n");
let line = 0;
function readline() {
  return input[line++];
}
function iInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => parseInt(cur));
}
function BInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => BigInt(cur));
}
function string() {
  return readline().trim();
}
function stringArr() {
  return readline().trim().split("");
}

function stringArr1() {
  return readline().trim().split(" ");
}
function getValue() {
  return parseInt(readline());
}
function iInpArr1() {
  return readline()
    .trim()
    .split("")
    .map((cur) => parseInt(cur));
}
let mod = 1e9 + 7;
mod = BigInt(mod);
const pow = (num, exp, mod) => {
  if (exp === BigInt(0)) return BigInt(1);
  let x = pow(num, exp / BigInt(2), mod);
  if (exp % BigInt(2)) return (((x * x) % mod) * num) % mod;
  else return (x * x) % mod;
};
const inverse = (num, exp) => {
  return pow(BigInt(num), BigInt(exp), BigInt(mod));
};
const sqrt = (n) => {
  let low = 1n,
    high = n,
    ans;
  while (low <= high) {
    let mid = (low + high) / 2n;
    if (mid * mid <= n) {
      ans = mid;
      low = mid + 1n;
    } else high = mid - 1n;
  }
  return ans;
};
let p2 = [];
p2[0] = 1;
p2[1] = 2;
for (let i = 2; i < 70; i++) p2[i] = p2[i - 1] * 2;
for (let i = 0; i < 70; i++) {
  let j = p2[i],
    arr = [];
  while (j > 0) {
    arr.push(j % 10);
    j = Math.floor(j / 10);
  }
  p2[i] = arr.reverse();
}
const solve = () => {
  let t = getValue();
  while (t--) {
    let n = getValue();
    let arr = [];
    while (n > 0) {
      arr.push(n % 10);
      n = Math.floor(n / 10);
    }
    arr.reverse();
    const helper = (arr1) => {
      let k = 0,
        op = 0,
        i = 0;
      while (k < arr.length && i < arr1.length) {
        if (arr[k] === arr1[i]) {
          op++;
          i++;
        }
        k++;
      }
      return arr.length - op + (arr1.length - op);
    };
    let ans = Infinity;
    for (let i = 0; i < p2.length; i++) {
      ans = Math.min(ans, helper(p2[i]));
    }
    console.log(ans);
  }
};
solve();

'use strict'
Object.defineProperty(exports, '__esModule', { value: !0 })
var os = require('os')
const stdin = process.stdin,
  stdout = process.stdout,
  integerRegex = /-?\d+/g,
  decimalRegex = /-?\d+(?:\.\d+)?/g
class InputAndOutput {
  lineIndex = 0
  lines = []
  dried() {
    return this.lineIndex >= this.lines.length
  }
  readLine() {
    const e = this.lines[this.lineIndex]
    return (this.lineIndex += 1), e
  }
  readLineAsNumber() {
    return Number(this.readLine())
  }
  readIntegersOfLine() {
    const e = this.readLine().match(integerRegex)
    return null == e ? [] : e.map(e => Number(e))
  }
  readNumsOfLine() {
    const e = this.readLine().match(decimalRegex)
    return null == e ? [] : e.map(e => Number(e))
  }
  print(e) {
    stdout.write(String(e))
  }
  println(e) {
    stdout.write(String(e)), stdout.write('\n')
  }
  async init() {
    const e = await this.readLines()
    this.lines = e
  }
  readLines() {
    const e = []
    return new Promise(n => {
      stdin.on('data', n => e.push(n)),
        stdin.on('end', function () {
          const t = e.join('').split(os.EOL)
          n(t)
        })
    })
  }
}
async function __main__(e) {
  const n = new InputAndOutput()
  await n.init(), e(n)
}
const pow2 = [1n]
for (let e = 1; e < 80; ++e) pow2[e] = 2n * pow2[e - 1]
const pow2s = pow2.map(e => String(e))
function solve(e) {
  let n = 1e4
  const t = String(e)
  for (const e of pow2s) n = Math.min(n, s(t, e))
  return n
  function s(e, n) {
    let t = 0,
      s = 0,
      i = 0
    for (; t < e.length && s < n.length; )
      e[t] !== n[s] ? ((t += 1), (i += 1)) : ((t += 1), (s += 1))
    return (
      t < e.length && (i += e.length - t),
      s < n.length && (i += n.length - s),
      i
    )
  }
}
__main__(function (e) {
  const n = e.readLineAsNumber()
  for (let t = 1; t <= n; ++t) {
    const n = solve(e.readLineAsNumber())
    e.print(n + '\n')
  }
})

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

const len = n => n.toString().length;

function sbsql (a, n) {
	a = a.toString();
	n = n.toString();

	let i = 0, j = 0;
	while (i < a.length && j < n.length) {
		if (n[j] == a[i]) i++;
		j++;
	}
	return i;
}

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

		let ans = len(n) + 1;
		let k = 1n;
		while (k < 1e18) {
			if (l = sbsql(k, n)) {
				ans = Math.min(ans, len(n) - l + len(k) - l);
			}
			k = k << 1n;
		}

		console.log(ans);
	}
}

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

var maxN = 2 * 1e5 + 10
var mod = BigInt(1e9 + 7)

function main() {
  const xx = readline();
  var array = []
  var res = 1n
  var i =0
  while (i < 200) {
    array.push(res.toString())
    res = res* 2n
    i++
  }
  // console.log(array)
  Array(Number(xx)).fill(1).map((t, i) => {
    var string = readline();
    //
    // var [a, b, c] = readline().split(' ').map((x, iii) => {
    //   return parseInt(x)
    // })
    // console.log(string)
    var min = 10
    for (let j = 0; j < array.length; j++) {

      var same = 0
      var add = 0
      var result = true
      var exist = false
      for (let k = 0; k < array[j].length; k++) {
        exist = false
        for (let i = k + add; i < string.length; i++) {
          if (string[i] === array[j][k]) {
            same++;
            exist = true
            add = i - k
            break
          }
        }
        if (!exist) break
      }
      min = Math.min((string.length +array[j].length) - 2*same, min)
      // console.log(same, array[j], string, min)
    }
    +
    console.log(min)
  })
}

const readline = require("readline");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
const input = [];
const P2LIM = BigInt(2e18);

rl.on("line", (line) => {
  input.push(line);
});
rl.on("close", () => {
  const list = input.slice(1);
  const ts = [];
  for (let p2 = 1n; p2 <= P2LIM; p2 *= 2n) ts.push(String(p2));
  for (let e of list) {
    let res = e.length + 1;
    for (let p2 of ts) {
      res = Math.min(res, solve(e, p2));
    }
    console.log(res);
  }
});

function solve(s, t) {
  let tp = 0;
  let sp = 0;
  let taken = 0;

  while (sp < s.length && tp < t.length) {
    if (s[sp] === t[tp]) {
      taken++;
      tp++;
    }
    sp++;
  }
  return s.length - taken + t.length - taken;
}

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++]
    for (let i = 0; i < t; i++) {
        const n = +lines[l++]
        console.log(solve(n))
    }
// })()
})

function solve(n) {
    let target = 1n
    let min = 11
    for (let i = 0; i <= 70; i++) {
        const cur = cal(n, target)
        min = Math.min(min, cur)
        target += target
    }
    return min
}
function cal(n, target) {
    const a = String(n)
    const b = String(target)

    let i = 0
    let j = 0
    while (i < a.length && j < b.length) {
        if (a[i] === b[j]) {
            i++
            j++
        } else {
            i++
        }
    }
    const k = j
    const used = a.length - k
    return a.length - used + used - k + b.length - k

    const limit = Math.pow(2, a.length)
    let min = a.length + b.length
    for (let i = 0; i < limit; i++) {
        const r = []
        let used = 0
        let mask = 1
        for (let j = 0; j < a.length; j++) {
            if (i & mask) {
                used++
                r.push(a[j])
            }
            mask += mask
        }
        const p = r.join('')
        let k = 0
        for (; k < p.length; k++) {
            if (p[k] !== b[k]) break
        }
        min = Math.min(min, a.length - used + used - k + b.length - k)
    // console.log(p, b, used, k, a.length - used + b.length - k)
    }
    // console.log(a, b, min)
    // console.log('--')
    return min
}

let inputs = []

function read() {
    return inputs.pop();
}

function readInt() {
    return parseInt(read());
}

function solve() {
    let test = readInt();
    while (test--) {
        let n = read();
        let res = 100;
        let p2 = BigInt(1);
        for (let i = 0; i < 100; i++) {
            let s2 = p2.toString();
            let cost = 0;
            let ptr = 0;
            for (let i = 0; i < s2.length; i++) {
                while (ptr < n.length && n[ptr] != s2[i]) {
                    cost++;
                    ptr++;
                }
                if (ptr < n.length && n[ptr] == s2[i]) {
                    ptr++;
                }
                else {
                    cost++;
                }
            }
            cost += n.length - ptr;
            res = Math.min(res, cost);
            p2 = p2 * BigInt(2);
        }
        console.log(res);
    }
}

function main() {
    inputs = inputString.trim().split(/\n| /).map((string) => string.trim());
    inputs.reverse();
    solve();
}

let inputString = '';

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

process.stdin.on('end', (_) => { main(); });

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

const len = n => n.toString().length;

function sbsq (a, n) {
	a = a.toString();
	n = n.toString();

	let i = 0, j = 0;
	while (i < a.length && j < n.length) {
		if (n[j] == a[i]) i++;
		j++;
	}

	return i == a.length;
}

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

		let ans = len(n) + 1;
		let k = 1n;
		while (k < 1e18) {
			let cur = k;
			while (cur > 0) {
				if (sbsq(cur, n)) {
					ans = Math.min(ans, len(n) - len(cur) + len(k) - len(cur));
				}
				cur /= 10n;
				//cur = BigInt(cur.toString().slice(0,-1));
			}
			k <<= 2n;
		}

		console.log(ans);
	}
}

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

const len = n => n.toString().length;

function sbsq (a, n) {
	a = a.toString();
	n = n.toString();

	let i = 0, j = 0;
	while (i < a.length && j < n.length) {
		if (n[j] == a[i]) i++;
		j++;
	}

	return i == a.length;
}

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

		let ans = len(n) + 1;
		let k = 1n;
		while (k < 1e18 && len(k) < 2*len(n)) {
			let cur = k;
			while (cur > 0) {
				if (sbsq(cur, n)) {
					ans = Math.min(ans, len(n) - len(cur) + len(k) - len(cur));
				}
				cur /= 10n;
			}
			k *= 2n;
		}

		console.log(ans);
	}
}

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

const len = n => n.toString().length;

function sbsq (a, n) {
	a = a.toString();
	n = n.toString();

	let i = 0, j = 0;
	while (i < a.length && j < n.length) {
		if (n[j] == a[i]) i++;
		j++;
	}

	return i == a.length;
}

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

		let ans = 11;
		let k = 1n;
		while (k < 1e18) {
			let cur = k;
			while (cur > 0) {
				if (sbsq(cur, n)) {
					ans = Math.min(ans, len(n) - len(cur) + len(k) - len(cur));
				}
				cur /= 10n;
			}
			k *= 2n;
		}

		console.log(ans);
	}
}

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

const len = n => n.toString().length;

function sbsq (a, n) {
	a = a.toString();
	n = n.toString();

	let i = 0, j = 0;
	while (i < a.length && j < n.length) {
		if (n[j] == a[i]) i++;
		j++;
	}

	return i == a.length;
}

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

		let ans = 11;
		let k = 1;
		while (k < 1e18) {
			let cur = k;
			while (cur > 0) {
				if (sbsq(cur, n)) {
					ans = Math.min(ans, len(n) - len(cur) + len(k) - len(cur));
				}
				cur = Math.floor(cur/10);
			}
			k *= 2;
		}

		console.log(ans);
	}
}

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

var maxN = 2 * 1e5 + 10
var mod = BigInt(1e9 + 7)

function main() {
  const xx = readline();
  var array = []
  var res = 1n
  var i =0
  while (i < 200) {
    array.push(res.toString())
    res = res* 2n
    i++
  }
  // console.log(array)
  Array(Number(xx)).fill(1).map((t, i) => {
    var string = readline();
    //
    // var [a, b, c] = readline().split(' ').map((x, iii) => {
    //   return parseInt(x)
    // })
    // console.log(string)
    var min = 10
    for (let j = 0; j < array.length; j++) {

      var same = 0
      var add = 0
      var result = true
      var exist = false
      for (let k = 0; k < array[j].length; k++) {
        exist = false
        for (let i = k + add; i < string.length; i++) {
          if (string[i] === array[j][k]) {
            same++;
            exist = true
            add = i - k
            break
          }
        }
        if (!exist && k === 0) result = false
      }
      if(!result) same = 0
      min = Math.min(Math.abs(string.length - same + (array[j].length - same)), min)
      // console.log(same, array[j], string, min)
    }
    console.log(min)
  })
}

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

var maxN = 2 * 1e5 + 10
var mod = BigInt(1e9 + 7)

function main() {
  const xx = readline();
  var array = []
  var res = 1
  while (res <= 10e10) {
    array.push(res.toString())
    res *= 2
  }
  // console.log(array)
  // console.log(array)
  Array(Number(xx)).fill(1).map((t, i) => {
    var string = readline();
    //
    // var [a, b, c] = readline().split(' ').map((x, iii) => {
    //   return parseInt(x)
    // })
    // console.log(string)
    var min = 10
    for (let j = 0; j < array.length; j++) {

      var same = 0
      var add = 0
      var result = true
      var exist = false
      for (let k = 0; k < array[j].length; k++) {
        exist = false
        for (let i = k + add; i < string.length; i++) {
          if (string[i] === array[j][k]) {
            same++;
            exist = true
            add = i - k
            break
          }
        }
        if (!exist && k === 0) result = false
      }
      if(!result) same = 0
      min = Math.min(Math.abs(string.length - same + (array[j].length - same)), min)
      // console.log(same, array[j], string, min)
    }
    console.log(min)
  })
}

"use strict";
const fs = require("fs");
//let filename = "0.txt";
let filename = 0;
const input = fs.readFileSync(filename, "utf8").trim().split("\n");
let line = 0;
function readline() {
  return input[line++];
}
function iInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => parseInt(cur));
}
function BInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => BigInt(cur));
}
function string() {
  return readline().trim();
}
function stringArr() {
  return readline().trim().split("");
}

function stringArr1() {
  return readline().trim().split(" ");
}
function getValue() {
  return parseInt(readline());
}
function iInpArr1() {
  return readline()
    .trim()
    .split("")
    .map((cur) => parseInt(cur));
}
let mod = 1e9 + 7;
mod = BigInt(mod);
const pow = (num, exp, mod) => {
  if (exp === BigInt(0)) return BigInt(1);
  let x = pow(num, exp / BigInt(2), mod);
  if (exp % BigInt(2)) return (((x * x) % mod) * num) % mod;
  else return (x * x) % mod;
};
const inverse = (num, exp) => {
  return pow(BigInt(num), BigInt(exp), BigInt(mod));
};
const sqrt = (n) => {
  let low = 1n,
    high = n,
    ans;
  while (low <= high) {
    let mid = (low + high) / 2n;
    if (mid * mid <= n) {
      ans = mid;
      low = mid + 1n;
    } else high = mid - 1n;
  }
  return ans;
};
let p2 = [];
p2[0] = 1;
p2[1] = 2;
for (let i = 2; i < 40; i++) p2[i] = p2[i - 1] * 2;
for (let i = 0; i < 40; i++) {
  let j = p2[i],
    arr = [];
  while (j > 0) {
    arr.push(j % 10);
    j = Math.floor(j / 10);
  }
  p2[i] = arr.reverse();
}
const solve = () => {
  let t = getValue();
  while (t--) {
    let n = getValue();
    let arr = [];
    while (n > 0) {
      arr.push(n % 10);
      n = Math.floor(n / 10);
    }
    arr.reverse();
    const helper = (arr1) => {
      let k = 0,
        op = 0,
        i = 0;
      while (k < arr.length && i < arr1.length) {
        if (arr[k] === arr1[i]) {
          op++;
          i++;
        }
        k++;
      }
      return arr.length - op + (arr1.length - op);
    };
    let ans = Infinity;
    for (let i = 0; i < p2.length; i++) {
      ans = Math.min(ans, helper(p2[i]));
    }
    console.log(ans);
  }
};
solve();