Petya has got an interesting flower. Petya is a busy person, so he sometimes forgets to water it. You are given $$$n$$$ days from Petya's live and you have to determine what happened with his flower in the end.The flower grows as follows:   If the flower isn't watered for two days in a row, it dies.  If the flower is watered in the $$$i$$$-th day, it grows by $$$1$$$ centimeter.  If the flower is watered in the $$$i$$$-th and in the $$$(i-1)$$$-th day ($$$i > 1$$$), then it grows by $$$5$$$ centimeters instead of $$$1$$$.  If the flower is not watered in the $$$i$$$-th day, it does not grow. At the beginning of the $$$1$$$-st day the flower is $$$1$$$ centimeter tall. What is its height after $$$n$$$ days?

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 of each test case contains the only integer $$$n$$$ ($$$1 \leq n \leq 100$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$a_i = 0$$$ or $$$a_i = 1$$$). If $$$a_i = 1$$$, the flower is watered in the $$$i$$$-th day, otherwise it is not watered.

For each test case print a single integer $$$k$$$ — the flower's height after $$$n$$$ days, or $$$-1$$$, if the flower dies.

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(var i = 1; i <= t * 2; i++){
        if(i % 2 == 1){
            n = lines[i];
        }else{
            var a = lines[i].split(' ').map(Number);
            var last = -1;
            var value = 1;
            for(j = 0; j < n; j++){
                if(a[j] == 1){
                    if(last == 1){
                        value += 5;
                    }else{
                        value++;
                    }
                }else if(last === 0){
                    value = -1;
                    break;
                }
                last = a[j];
            }
            console.log(value);
        }
    }
});

//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 N = nextInt();
		var list = nextIntArray(N);
		var now = 1;
		for(var i = 0; i < N; i++){
			if(i > 0){
				if(list[i - 1] == list[i]){
					if(list[i] == 0){
						now = -1;
						break;
					}else{
						now += 5;
					}
				}else{
					now += list[i];
				}
			}else{
				now += list[i];
			}
		}
		myout(now);
	}
}

"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() {
const x=+readline();
let g=[];
for(let i=0;i<x;i++){

    let y=+readline();
    let array=readline().split(" ");
let sum=1;

    for(let i=0;i<y;i++){

       if(parseInt(array[i])===0 && parseInt(array[i+1])===0) { g.push(-1);
      break;}
        else if(parseInt(array[i])===1 && parseInt(array[i+1])===1) sum=sum+5;

      else if(parseInt(array[i])==1) sum++;

    }
    if(g[i]==-1) continue;
g.push(sum);
  }

    for(let i=0;i<g.length;i++){
      console.log(g[i]);

    }

  }

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

let inputString = '';

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

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

    main(inputString);
});

function log(message){
    if(process.env.NODE_ENV === "debug"){
        console.log(message);
    }
}

function main(fileContents) {//input lines as an array
    // console.log(fileContents);
    fileContents.splice(0,1);

    for(let i = 1; i < fileContents.length; i+=2){
        console.log(scenario(fileContents[i].split(" ")))
    }

}

function scenario(listOfDays) {
    // console.log(listOfDays)
    let flowerHeight = 1
    for(let i = 0; i < listOfDays.length; i++){
        if(i > 0){
            if(listOfDays[i-1] == 0 && listOfDays[i] == 0){
                return -1;
            }
        }
        if(i > 0){
            if(listOfDays[i-1] == 1 && listOfDays[i] == 1){
                flowerHeight += 5
            }else if(listOfDays[i] == 1){
                flowerHeight += 1
            }
        }else{
            if(listOfDays[i] == 1){
                flowerHeight += 1
            }
        }
    }
    return flowerHeight;
}

const readline = require('readline');

const rl= readline.createInterface({
    input: process.stdin
});

let state = 't';
let t = 0;
let t1 = 0
let n = 0;
let output = []
rl.on('line', async input => {
    switch(state){
        case 't':
            t = parseInt(input)
            state = 'n'
            break
        case 'n':
            n = parseInt(input)
            state = 'days'
            break;
        case 'days':
            let days = input.split(" ")
            let life = 0;
            let death = false;
            for(let i = 0; i < days.length; i++){
                const day = days[i]
                if(day == '0'){
                    if(i > 0){
                        if(days[i - 1] == '0'){
                            death = true;
                        }
                    }
                }else{
                    if(i > 0){
                        if(days[i - 1] == '1'){
                            life += 5;
                        }else{
                            life+=1
                        }
                    }else{
                        life += 1
                    }
                }
            }
            t1++
            if(death){
                output.push(-1)
            }else{
                output.push(life + 1)
            }
            if(t == t1){
                console.log(output.join("\n"))
                return
            }
            state = 'n'
    }
});

async function solve() {
  let T = await int()
  while (T > 0) {
    await line()
    const a = await ints()
    let height = 1
    for (let i = 0; i < a.length; i++) {
      let previous = a[i - 1]
      let current = a[i]
      if (current === 0 && previous === 0) {
        height = -1
        break
      }
      if (current === 1 && previous === 1) height += 5
      else if (current === 1) height += 1
    }
    print(height)
    T--
  }
}

// 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 h = 1
    for (let i = 0; i < n; ++i) {
        if (arr[i] === 0) {
            if (arr[i - 1] === 0) {
                h = -1
                break
            }
        } else {
            if (arr[i - 1] === 1) {
                h += 5
            } else {
                h++
            }
        }
    }
    console.log(h)
}

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

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(var i = 1; i <= t * 2; i++){
        if(i % 2 == 1){
            n = lines[i];
        }else{
            var a = lines[i].split(' ').map(Number);
            var z = 0;
            var o = 0;
            var ans = 1;
            var d = false;
            for(j = 0; j < n; j++){
                if(a[j] == 1){
                    z = 0;
                    if(o == 2 || o + 1 == 2){
                        ans += 5;
                        o = 0;
                    }else{
                        ans++;
                        o++;
                    }
                }else{
                    o = 0;
                    z++;
                    if(z == 2){
                        d = true;
                        break;
                    }
                }
            }
            console.log(d ? '-1' : ans);
        }
    }
});


Niwel is a little golden bear. As everyone knows, bears live in forests, but Niwel got tired of seeing all the trees so he decided to move to the city.In the city, Niwel took on a job managing bears to deliver goods. The city that he lives in can be represented as a directed graph with n nodes and m edges. Each edge has a weight capacity. A delivery consists of a bear carrying weights with their bear hands on a simple path from node 1 to node n. The total weight that travels across a particular edge must not exceed the weight capacity of that edge.Niwel has exactly x bears. In the interest of fairness, no bear can rest, and the weight that each bear carries must be exactly the same. However, each bear may take different paths if they like.Niwel would like to determine, what is the maximum amount of weight he can deliver (it's the sum of weights carried by bears). Find the maximum weight.

The first line contains three integers n, m and x (2 ≤ n ≤ 50, 1 ≤ m ≤ 500, 1 ≤ x ≤ 100 000) — the number of nodes, the number of directed edges and the number of bears, respectively. Each of the following m lines contains three integers ai, bi and ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 1 000 000). This represents a directed edge from node ai to bi with weight capacity ci. There are no self loops and no multiple edges from one city to the other city. More formally, for each i and j that i ≠ j it's guaranteed that ai ≠ aj or bi ≠ bj. It is also guaranteed that there is at least one path from node 1 to node n.

Print one real value on a single line — the maximum amount of weight Niwel can deliver if he uses exactly x bears. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if .

NoteIn the first sample, Niwel has three bears. Two bears can choose the path , while one bear can choose the path . Even though the bear that goes on the path  can carry one unit of weight, in the interest of fairness, he is restricted to carry 0.5 units of weight. Thus, the total weight is 1.5 units overall. Note that even though Niwel can deliver more weight with just 2 bears, he must use exactly 3 bears on this day.

"use strict";

function main()
{

  var n, m, x;
  var line = readline().split(" ").map(ints);
  n = line[0];
  m = line[1];
  x = line[2];

  var capacity = grid(n, n);
  var edges = grid(n, 0);
  var active = new Array(n);
  var stack = [], sum = 0;

  var streets = new Array(m);
  for (var i=0 ; i<m ; i++)
  {
    var street = readline().split(" ").map(ints);

    street[0]--;
    street[1]--;

    streets[i] = street;
    edges[street[0]].push(street[1]);
    edges[street[1]].push(street[0]);
  }

  print(binarySearch(1e-12, 1e12));

  function grid(x, y)
  {
    var data = new Array(y);

    for (var i=0 ; i<x ; i++)
    {
      data[i] = new Array(y);
      data[i].fill(0);
    }

    return data;
  }

  function flushPath()
  {
    var flow = Infinity;
    for (var i=1 ; i<stack.length ; i++)
    {
      flow = Math.min(flow, capacity[stack[i-1]][stack[i]]);
    }

    for (var i=1 ; i<stack.length ; i++)
    {
      var src = stack[i-1], dst = stack[i];

      capacity[src][dst] -= flow;
      capacity[dst][src] += flow;
    }

    sum += flow;
  }

  function findPath(src, dst)
  {
    stack.push(src);
    active[src] = true;

    for (var next of edges[src])
    {
      if (!active[next] && capacity[src][next] > 0)
      {
        if (next == dst)
        {
          stack.push(dst);
          flushPath();
          return true;
        }
        else if (findPath(next, dst))
        {
          return true;
        }
      }
    }

    stack.pop(src);

    return false;
  }

  function maxflow(src, dst)
  {
    sum = 0;

    do
    {
      active.fill(false);
      stack.length = 0;
    }
    while (findPath(src, dst));
  }

  function binarySearch(min, max)
  {
    do
    {
      var m = (max+min) / 2;
      var bearWeight = m / x;

      for (var row of capacity)
      {
        row.fill(0);
      }
      for (var street of streets)
      {
        capacity[street[0]][street[1]] = Math.floor(street[2] / bearWeight);
      }

      //console.log("try", bearWeight, "->", m);
      maxflow(0, n-1);
      //console.log("\t->", sum, sum*bearWeight);
      if (sum >= x)
      {
        min = m;
      }
      else
      {
        max = m;
      }
    }
    while (max-min > 1e-7);

    return m;
  }

  function ints(x)
  {
    return parseInt(x, 10);
  }

}

main();

"use strict";

function main()
{

  var n, m, x;
  var line = readline().split(" ").map(ints);
  n = line[0];
  m = line[1];
  x = line[2];

  var capacity = grid(n, n);
  var edges = grid(n, 0);
  var parent = new Array(n);
  var sum = 0;

  var streets = new Array(m);
  for (var i=0 ; i<m ; i++)
  {
    var street = readline().split(" ").map(ints);

    street[0]--;
    street[1]--;

    streets[i] = street;
    edges[street[0]].push(street[1]);
    edges[street[1]].push(street[0]);
  }

  print(binarySearch(1e-12, 1e12));

  function grid(x, y)
  {
    var data = new Array(y);

    for (var i=0 ; i<x ; i++)
    {
      data[i] = new Array(y);
      data[i].fill(0);
    }

    return data;
  }

  function augmentPath(src, dst)
  {
    parent.fill(-1);

    var q = [[src, src]];
    while (q.length)
    {
      var cur = q[0][0];
      var last = q[0][1];
      q.shift();

      if (parent[cur] < 0)
      {
        parent[cur] = last;

        for (var next of edges[cur])
        {
          if (capacity[cur][next] > 0)
          {
            q.push([next, cur]);
          }
        }
      }
    }

    var sum = 0;

    if (parent[dst] >= 0)
    {
      var maxflow = 1e12;

      //var path = [dst+1];
      var cur = dst;
      while (cur != src)
      {
        var last = parent[cur];

        maxflow = Math.min(maxflow, capacity[last][cur]);

        //path.push(last+1);
        cur = last;
      }

      //console.log("\tpush", maxflow, path.reverse());

      var cur = dst;
      while (cur != src)
      {
        var last = parent[cur];

        capacity[last][cur] -= maxflow;
        capacity[cur][last] += maxflow;

        cur = last;
      }

      sum += maxflow;
    }

    return sum;
  }

  function maxflow(src, dst)
  {
    sum = 0;

    do
    {
      var delta = augmentPath(src, dst);
      sum += delta;
    }
    while (delta > 0);
  }

  function binarySearch(min, max)
  {
    do
    {
      var m = (max+min) / 2;
      var bearWeight = m / x;

      for (var row of capacity)
      {
        row.fill(0);
      }
      for (var street of streets)
      {
        capacity[street[0]][street[1]] = Math.floor(street[2] / bearWeight);
      }

      //console.log("try", bearWeight, "->", m);
      maxflow(0, n-1);
      //console.log("\t->", sum, sum*bearWeight);
      if (sum >= x)
      {
        min = m;
      }
      else
      {
        max = m;
      }
    }
    while (max-min > 1e-7);

    return m;
  }

  function ints(x)
  {
    return parseInt(x, 10);
  }

}

main();

"use strict";

function main()
{

  var n, m, x;
  var line = readline().split(" ").map(ints);
  n = line[0];
  m = line[1];
  x = line[2];

  var flow = grid(n, n);
  var capacity = grid(n, n);
  var parent = new Array(n);

  var out_edges = grid(n, 0);
  var in_edges = grid(n, 0);

  var streets = new Array(m);
  var minWeight = Infinity;
  var maxWeight = -Infinity;

  for (var i=0 ; i<m ; i++)
  {
    var street = readline().split(" ").map(ints);

    street[0]--;
    street[1]--;

    streets[i] = street;
    out_edges[street[0]].push(street[1]);
    in_edges[street[1]].push(street[0]);

    minWeight = Math.min(minWeight, street[2]);
    maxWeight = Math.max(maxWeight, street[2]);
  }

  print(binarySearch(minWeight, maxWeight * streets.length));

  function grid(x, y)
  {
    var data = new Array(y);

    for (var i=0 ; i<x ; i++)
    {
      data[i] = new Array(y);
      data[i].fill(0);
    }

    return data;
  }

  function augmentPath(src, dst)
  {
    parent.fill(-1);

    var q = [[src, src]];
    while (q.length)
    {
      var cur = q[0][0];
      var last = q[0][1];
      q.shift();

      if (parent[cur] < 0)
      {
        parent[cur] = last;

        for (var next of out_edges[cur])
        {
          if (flow[cur][next] < capacity[cur][next])
          {
            q.push([next, cur]);
          }
        }

        for (var next of in_edges[cur])
        {
          if (flow[cur][next] > 0)
          {
            q.push([next, cur]);
          }
        }
      }
    }

    var sum = 0;

    if (parent[dst] >= 0)
    {
      var maxflow = 1000001;

      //var path = [dst+1];
      var cur = dst;
      while (cur != src)
      {
        var last = parent[cur];

        var curflow = Math.round(flow[cur][last] + Math.round(capacity[last][cur] - flow[last][cur]));
        maxflow = Math.min(maxflow, curflow);

        //path.push(last+1);
        cur = last;
      }

      //console.log("\tpush", maxflow, path.reverse());

      var cur = dst;
      while (cur != src)
      {
        var last = parent[cur];

        var push = maxflow;
        if (flow[cur][last] > push)
        {
          flow[cur][last] = Math.round(flow[cur][last] - push);
        }
        else
        {
          push = Math.round(push - flow[cur][last]);
          flow[cur][last] = 0;

          flow[last][cur] = Math.round(flow[last][cur] + push);
        }

        cur = last;
      }

      sum += maxflow;
    }

    return sum;
  }

  function maxflow(src, dst)
  {
    var sum = 0;

    for (var row of flow)
    {
      row.fill(0);
    }

    do
    {
      var delta = augmentPath(src, dst);
      sum = Math.round(sum + delta);
    }
    while (delta > 0 && sum <= x);

    return sum;
  }

  function binarySearch(min, max)
  {
    var delta = (max - min) / 2;

    do
    {
      var m = min + delta;
      var bearWeight = m / x;

      for (var row of capacity)
      {
        row.fill(0);
      }
      for (var street of streets)
      {
        capacity[street[0]][street[1]] = Math.floor(street[2] / bearWeight);
      }

      //console.log("try", bearWeight, "->", m);
      var bears = maxflow(0, n-1);
      //console.log("\t->", bears, bears*bearWeight);
      if (bears >= x)
      {
        min = m;
      }

      delta /= 2;
    }
    while (delta > 1e-7);

    return m;
  }

  function ints(x)
  {
    return parseInt(x, 10);
  }
}

main();