There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights.    Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).

The first line of the input contains integers n and k (1 ≤ n ≤ 1.5·105, 1 ≤ k ≤ n) — the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≤ hi ≤ 100), where hi is the height of the i-th plank of the fence.

Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.

NoteIn the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8.

print(function(n, k) {
	var sum = 0,
		ans = 0,
		min = 1e8,
		d = ('0 ' + readline()).split(' ').map(Number).map(function(v) {
			return sum += v;
		}).map(function(v, i, s) {
			return i + k <= n ? s[i + k] - s[i] : 1e8;
		});

	for (var i = 0; i <= n - k + 1; i++) {
		if (d[i] < min)
			min = d[i], ans = i;
	}
	return ans + 1;

}.apply(0, readline().split(' ').map(Number)));

print(function(n, k) {

	var sum = 0,
		ans = 0,
		min = 1e8,
		d = ('0 ' + readline()).split(' ').map(Number).map(function(v) {
			return sum += v;
		}).map(function(v, i, s) {
			return i + k <= n ? s[i + k] - s[i] : 1e8;
		});

	for (var i = 0; i <= n - k + 1; i++)
		if (d[i] < min)
			min = d[i],
			ans = i;

	return ans + 1;

}.apply(0, readline().split(' ').map(Number)));

function rda(){ return readline().split(" ").map(Number); };
function rdn(){ return +readline(); };

var inp = rda(), n = inp[0], k = inp[1];
delete inp;
var h = rda();

for(var i = 0, s = 0; i < n; i++){
    s += h[i];
    h[i] = s;
}

var val = h[k-1];
var ans = k-1;
for(var i = k; i < n; i++){
    if((h[i]-h[i-k]) < val){
        val = h[i]-h[i-k];
        ans = i;
    }
}

write(ans+2-k);

(function main() {
    var toInt = function(x) {
        return parseInt(x);
    }
    var firstline = readline().split(" ").map(toInt);
    var n = firstline[0],
        k = firstline[1];

    var ar = readline().split(" ").map(toInt);
    // NOT ar.sort(function(a, b){return a-b});

    // ar = [1, 2, 6, 1, 1, 7, 1];
    // k = 3;
    ar.reduce(function(sum, next, id, array) {
        var tmp = sum + next;
        array[id] = tmp;
        return tmp;
    });

    ar.splice(0,0,0);

    print((function() {
        var count = 999999999;
        var index = -1;
        for(var i = k; i < ar.length; ++i) {
            var tmp = ar[i]-ar[i-k]
            if (count > tmp) {
                index = i - k + 1;
                count = tmp;
            }
        }
        return index;
    })());
})();

var input = readline().split(' ').map(Number);
var n = input[0];
var k = input[1];
var a = readline().split(' ').map(Number);
var ans = 0;
var minsum = a.slice(0, k).reduce((a, b) => a + b);
var sum = minsum;
for (var i = k; i < a.length; i++) {
	var sum = sum + a[i] - a[i - k];
	if (sum < minsum) {
		minsum = sum;
		ans = i - k + 1;
	}
}

print(ans + 1);

var input = readline().split(' ').map(Number);
var n = input[0], k = input[1];
var h = readline().split(' ').map(Number);
var j = 0, min_sum = 0, sum = 0;
for (var i = k; i < n; i++) {
    sum = sum - h[i - k] + h[i];
    if (sum < min_sum) {
        min_sum = sum;
        j = i - k + 1
    }
}
print(j + 1);

var input = readline().split(' ').map(Number);
var n = input[0], k = input[1];
var h = readline().split(' ').map(Number);
var j = 0;
var min_sum = h.slice(0, k).reduce((a, b) => a + b);
var sum = min_sum;
for (var i = k; i < n; i++) {
    sum = sum - h[i - k] + h[i];
    if (sum < min_sum) {
        min_sum = sum;
        j = i - k + 1
    }
}
print(j + 1);

var x = new Array();
var n,k;
var buff = new Array();
function read() {
	return readline().split(' ').map(Number);
}
function main() {
	buff=read();
	n=buff[0],k=buff[1];
	buff=read();
	x[0]=0;
	for (var i=1; i<=n; i++) {
		x[i]=x[i-1]+buff[i-1];
	}
	var ans=1<<31-1,pos=0;
	for (var i=1; i<=n-k+1; i++) {
		if (ans>x[i+k-1]-x[i-1]) {
			ans=x[i+k-1]-x[i-1];
			pos=i;
		}
	}
	print(pos);
}
main();

;(function () {

    print(function (n, k) {
        var h = readline().split(' ').map(Number),
            r = 0, t = 0, l = 0;

        for (var i = 0; i < k; i++) {
            r += h[i];
        }

        l = r;

        for (var i = 1, _i = n - k + 1; i < _i; i++) {
            var d = r - h[i - 1] + h[i + k - 1];
            if (d < l) {
                t = i;
                l = d;
            }

            r = d;
        }

        return t + 1;
    }.apply(null, readline().split(' ').map(Number)));

}.call(this));

function main() {
    var line = readline().split(' ').map(Number);
    var n = line[0];
    var k = line[1];
    var data = readline().split(' ').map(Number);
    var sum = 0;
    for(var i=0;i<k-1;i++){
        sum += data[i];
    }
    var min = Number.MAX_VALUE
    var res = 1;
    for(var i=0;i<=n-k;i++){
        sum += data[i+k-1];
        if(sum < min){
            min = sum;
            res = i+1;
        }
        sum -= data[i];
    }
    print(res)
}

main()

var n = readline().split(/\s/gi).map(Number),
			k = n[1], n = n[0],
			heights = readline().split(/\s/gi).map(Number),
			sumH = heights.slice(0, k).reduce(function(a, b) { return a + b; }),
			tArr = [ sumH ], resultIndex = 0;

		for(var i = 1; i <= n - k; i++)	{
			var prevSumH = tArr[tArr.length - 1],
				minSumH;

			sumH = sumH - heights[i - 1] + heights[i + k - 1];
			minSumH = Math.min(prevSumH, sumH);
			// console.log(heights.slice(k - 1, k - 1 + k));
			// console.log(sumH);

			if(sumH == minSumH)	resultIndex = i;
			tArr.push(minSumH);
		}

		print(resultIndex + 1);

var optimalStartPoint = function ( planks, k ) {
  var optimal,
    minSum,
    sum,
    i;

   minSum = 0;
   optimal = 0;
   for ( i = 0; i < k; ++i ) {
     minSum += planks[ i ];
   }
   sum = minSum;

   for ( i = 1; i < planks.length - k + 1; ++i ) {
     sum = sum - planks[ i - 1 ] + planks[ i + k - 1 ];
     if ( sum < minSum ) {
       minSum = sum;
       optimal = i;
     }
   }
   return optimal;
};

var input = readline().split( " " ).map( i => parseInt( i, 10 ) );
var k = input[ 1 ];
var planks = readline().split( " " ).map( hi => parseInt( hi, 10 ) );
print( optimalStartPoint( planks, k ) + 1 );

(function main() {
    var nk = readline().split(' ').map(function(x) {return parseInt(x);});
    var n = nk[0];
    var k = nk[1];
    var a = readline().split(' ').map(function(x) {return parseInt(x);});
    var s = 0;
    var mins = s;
    var mini = 1;
    for (var i = k; i < n; ++i) {
        s += a[i] - a[i - k];
        if (s < mins) {
            mins = s;
            mini = i - k + 2;
        }
    }
    print(mini);
})();

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() {
  const [len, k] = readLine().split(" ").map(Number);
  const arr = readLine().split(" ").map(Number);
  let [min, minIndex, sum] = [Infinity, 0, 0];

  for (let i = 0; i < k; i++) sum += arr[i];

  min = sum;
  for (let i = k; i < len; i++) {
    sum = sum + arr[i] - arr[i - k];
    if (sum < min) {
      min = sum;
      minIndex = i - k + 1;
    }
  }

  console.log(minIndex + 1);
}

const input = [];

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

RL.on('line', line => input.push(line));

RL.on('close', () => {
  const [n, k] = input[0].split(' ').map(x => parseInt(x));
  const nums = input[1].split(' ').map(x => parseInt(x));

  let memory = 0; minSum = 0; let pos = 1;
  for (let i = 0; i < k; i += 1) {
    memory += nums[i];
  }
  minSum = memory;

  let j = 0;
  for (let i = k; i < n; i += 1) {
    memory = memory - nums[j] + nums[i];
    if (memory < minSum) {
      minSum = memory; pos = j + 2;
    }
    j += 1;
  }

  console.log(pos);
});

print(function(n, k) {
	var sum = 0,
		ans = 0,
		min = 1e6,
		d = ('0 ' + readline()).split(' ').map(Number).map(function(v) {
			return sum += v;
		}).map(function(v, i, s) {
			return i + k <= n ? s[i + k] - s[i] : 1e6;
		});

	for (var i = 0; i <= n - k; i++)
		if (d[i] < min)
			min = d[i], ans = i;

	return ans + 1;

}.apply(0, readline().split(' ').map(Number)));

print(function(n, k) {
	var sum = 0,
		ans = 0,
		min = 1e8,
		d = ('0 ' + readline()).split(' ').map(Number).map(function(v) {
			return sum += v;
		}).map(function(v, i, s) {
			return i + k <= n ? s[i + k] - s[i] : 1e6;
		});

	for (var i = 0; i <= n - k + 1; i++) {
		if (d[i] < min)
			min = d[i], ans = i;
	}
	return ans + 1;

}.apply(0, readline().split(' ').map(Number)));

function rda(){ return readline().split(" ").map(Number); };
function rdn(){ return +readline(); };

var inp = rda(), n = inp[0], k = inp[1];
delete inp;
var h = rda();

for(var i = 0, s = 0; i < n; i++){
    s += h[i];
    h[i] = s;
}

var val = h[k-1];
var ans = k-1;
for(var i = k; i < n; i++){
    if((h[i]-h[i-k]) < val){
        val = h[i]-h[i-k];
        ans = i;
    }
    print(val, ans);
}

write(ans+2-k);

(function main() {
    var toInt = function(x) {
        return parseInt(x);
    }
    var firstline = readline().split(" ").map(toInt);
    var n = firstline[0],
        k = firstline[1];

    var ar = readline().split(" ").map(toInt);
    // NOT ar.sort(function(a, b){return a-b});

    ar.reduce(function(sum, next, id, array) {
        var tmp = sum + next;
        array[id] = tmp;
        return tmp;
    });

    // ar.splice(0,0,0);

    print((function() {
        var count = -1;
        var index = -1;
        for(var i = k-1; i < ar.length; ++i) {
            var tmp = ar[i]-ar[i-k+1]
            if (count < tmp) {
                index = i - k + 1;
                count = tmp;
            }
        }
        return index + 1;
    })());
})();

var input = readline().split(' ').map(Number);
var n = input[0];
var k = input[1];
var a = readline().split(' ').map(Number);
var ans = 2;
var minsum = a.slice(0, k).reduce((a, b) => a + b);
for (var i = k; i < a.length; i++) {
	var sum = minsum + a[i] - a[i - k];
	if (sum < minsum) {
		minsum = sum;
		ans = i;
	}
}

print(ans - 1);

var input = readline().split(' ').map(Number);
var n = input[0];
var k = input[1];
var a = readline().split(' ').map(Number);
var ans = 0;
var minsum = a.slice(0, k).reduce((a, b) => a + b);
for (var i = k; i < a.length; i++) {
	var sum = minsum + a[i] - a[i - k];
	if (sum < minsum) {
		minsum = sum;
		ans = i;
	}
}

print(ans - 1);

var input = readline().split(' ').map(Number);
var n = input[0];
var k = input[1];
var a = readline().split(' ').map(Number);
var ans = 2;
var minsum = a.slice(0, k).reduce((a, b) => a + b);
var sum = minsum;
for (var i = k; i < a.length; i++) {
	var sum = sum + a[i] - a[i - k];
	if (sum < minsum) {
		minsum = sum;
		ans = i;
	}
}

print(ans - 1);

var x = new Array();
var n,k;
var buff = new Array();
function read() {
	return readline().split(' ').map(Number);
}
function main() {
	buff=read();
	n=buff[0],k=buff[1];
	buff=read();
	for (var i=0; i<n; i++) {
		x[i]=buff[i];
	}
	for (var i=1; i<n; i++) {
		x[i]=x[i-1]+x[i];
	}
	var pos=0,ans=1<<31-1;
	for (var i=0; i<n-k+1; i++) {
		if (x[i+k-1]-x[i]<ans) {
			ans=x[i+k-1]-x[i];
			pos=i;
		}
	}
	print(pos+1);
}
main();

;(function () {

    print(function (n, k) {
        var h = readline().split(' ').map(Number),
            r = 0, t = 0;

        for (var i = 0; i < k; i++) {
            r += h[i];
        }

        for (var i = 1, _i = n - k + 1; i < _i; i++) {
            var d = r - h[i - 1] + h[i + k - 1];
            if (d < r) {
                t = i;
            }

            r = d;
        }

        return t + 1;
    }.apply(null, readline().split(' ').map(Number)));

}.call(this));

;(function () {

    print(function (n, k) {
        var h = readline().split(' ').map(Number),
            r = 0, t = 0;

        for (var i = 0; i < k; i++) {
            r += h[i];
        }

        for (var i = 1, _i = n - k + 1; i < _i; i++) {
            var d = r - h[i - 1] + h[i + k];
            if (d < r) {
                t = i;
            }

            r = d;
        }

        return t + 1;
    }.apply(null, readline().split(' ').map(Number)));

}.call(this));

;(function () {

    print(function (n, k) {
        var h = readline().split(' ').map(Number),
            r = 0, t = 0;

        for (var i = 0; i < k; i++) {
            r += h[i];
        }

        for (var i = 1, _i = n - k; i < _i; i++) {
            var d = r - h[i] + h[i + k - 1];
            if (d < r) {
                t = i;
            }

            r = d;
        }

        return t + 1;
    }.apply(null, readline().split(' ').map(Number)));

}.call(this));

;(function () {

    print(function (n, k) {
        var h = readline().split(' ').map(Number),
            r = 0, t = 0;

        for (var i = 0; i < k; i++) {
            r += h[i];
        }

        for (var i = 1, _i = n - k + 1; i < _i; i++) {
            var d = r - h[i - 1] + h[i + k - 1];
            if (d < r) {
                t = i;
            }

            if (d === 1) {
                return t + 1;
            }

            r = d;
        }

        return t + 1;
    }.apply(null, readline().split(' ').map(Number)));

}.call(this));

;(function () {

    print(function (n, k) {
        var h = readline().split(' ').map(Number),
            r = 0, t = 0;

        for (var i = 0; i < k; i++) {
            r += h[i];
        }

        for (var i = 1, _i = n - k + 1; i < _i; i++) {
            var d = r - h[i - 1] + h[i + k - 1];
            if (d < r) {
                t = i;
            }

            if (d === n) {
                return t + 1;
            }

            r = d;
        }

        return t + 1;
    }.apply(null, readline().split(' ').map(Number)));

}.call(this));

;(function () {

    print(function (n, k) {
        var h = readline().split(' ').map(Number),
            r = 0, t = 0;

        for (var i = 0; i < k; i++) {
            r += h[i];
        }

        for (var i = 1, _i = n - k + 1; i < _i; i++) {
            var d = r - h[i - 1] + h[i + k - 1];
            if (d < r) {
                t = i;
            }

            if (d === 1) {
                return t;
            }

            r = d;
        }

        return t + 1;
    }.apply(null, readline().split(' ').map(Number)));

}.call(this));

function main() {
    var line = readline().split(' ').map(Number);
    var n = line[0];
    var k = line[1];
    var data = readline().split(' ').map(Number);
    var sum = 0;
    for(var i=0;i<k-1;i++){
        sum += data[i];
    }
    var min = Number.MAX_VALUE
    var res = 1;
    for(var i=0;i<n-k;i++){
        sum += data[i+k-1];
        if(sum < min){
            min = sum;
            res = i+1;
        }
        sum -= data[i];
    }
    print(res)
}

main()

function main() {
    var line = readline().split(' ').map(Number);
    var n = line[0];
    var k = line[1];
    var data = readline().split(' ').map(Number);
    var sum = 0;
    for(var i=0;i<k-1;i++){
        sum += data[i];
    }
    var min = Number.MAX_VALUE
    var res = -1;
    for(var i=0;i<n-k;i++){
        sum += data[i+k-1];
        if(sum < min){
            min = sum;
            res = i+1;
        }
        sum -= data[i];
    }
    print(res)
}

main()

function main() {
    var line = readline().split(' ').map(Number);
    var n = line[0];
    var k = line[1];
    var data = readline().split(' ').map(Number);
    var sum = 0;
    for(var i=0;i<k-1;i++){
        sum += data[i];
    }
    var min = Number.MAX_VALUE
    var res = 1;
    for(var i=0;i<n-k;i++){
        sum += data[i+k-1];
        print(sum)
        if(sum < min){
            min = sum;
            res = i+1;
        }
        sum -= data[i];
    }
    print(res)
}

main()

var optimalStartPoint = function ( planks, k ) {
  var optimal,
    minSum,
    sum,
    i;

   minSum = 0;
   sum = 0;
   optimal = 0;
   for ( i = 0; i < k; ++i ) {
     minSum += planks[ i ];
   }

   for ( i = 1; i < planks.length - k + 1; ++i ) {
     sum = minSum - planks[ i - 1 ] + ( planks[ i + k - 1 ] || 0 );
     if ( sum < minSum ) {
       minSum = sum;
       optimal = i;
     }
   }
   return optimal;
};

var input = readline().split( " " ).map( i => parseInt( i, 10 ) );
var k = input[ 1 ];
var planks = readline().split( " " ).map( hi => parseInt( hi, 10 ) );
print( optimalStartPoint( planks, k ) + 1 );

var optimalStartPoint = function ( planks, k ) {
  var optimal,
    minSum,
    sum,
    i;

   minSum = 0;
   sum = 0;
   optimal = 0;
   for ( i = 0; i < k; ++i ) {
     minSum += planks[ i ];
   }

   for ( i = 1; i < planks.length - k + 1; ++i ) {
     sum = sum - planks[ i - 1 ] + planks[ i + k - 1 ];
     if ( sum < minSum ) {
       minSum = sum;
       optimal = i;
     }
   }
   return optimal;
};

var input = readline().split( " " ).map( i => parseInt( i, 10 ) );
var k = input[ 1 ];
var planks = readline().split( " " ).map( hi => parseInt( hi, 10 ) );
print( optimalStartPoint( planks, k ) + 1 );


Johnny has recently found an ancient, broken computer. The machine has only one register, which allows one to put in there one variable. Then in one operation, you can shift its bits left or right by at most three positions. The right shift is forbidden if it cuts off some ones. So, in fact, in one operation, you can multiply or divide your number by $$$2$$$, $$$4$$$ or $$$8$$$, and division is only allowed if the number is divisible by the chosen divisor. Formally, if the register contains a positive integer $$$x$$$, in one operation it can be replaced by one of the following:   $$$x \cdot 2$$$  $$$x \cdot 4$$$  $$$x \cdot 8$$$  $$$x / 2$$$, if $$$x$$$ is divisible by $$$2$$$  $$$x / 4$$$, if $$$x$$$ is divisible by $$$4$$$  $$$x / 8$$$, if $$$x$$$ is divisible by $$$8$$$ For example, if $$$x = 6$$$, in one operation it can be replaced by $$$12$$$, $$$24$$$, $$$48$$$ or $$$3$$$. Value $$$6$$$ isn't divisible by $$$4$$$ or $$$8$$$, so there're only four variants of replacement.Now Johnny wonders how many operations he needs to perform if he puts $$$a$$$ in the register and wants to get $$$b$$$ at the end.

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The following $$$t$$$ lines contain a description of test cases. The first and only line in each test case contains integers $$$a$$$ and $$$b$$$ ($$$1 \leq a, b \leq 10^{18}$$$) — the initial and target value of the variable, respectively.

Output $$$t$$$ lines, each line should contain one integer denoting the minimum number of operations Johnny needs to perform. If Johnny cannot get $$$b$$$ at the end, then write $$$-1$$$.

NoteIn the first test case, Johnny can reach $$$5$$$ from $$$10$$$ by using the shift to the right by one (i.e. divide by $$$2$$$).In the second test case, Johnny can reach $$$44$$$ from $$$11$$$ by using the shift to the left by two (i.e. multiply by $$$4$$$).In the third test case, it is impossible for Johnny to reach $$$21$$$ from $$$17$$$.In the fourth test case, initial and target values are equal, so Johnny has to do $$$0$$$ operations.In the fifth test case, Johnny can reach $$$3$$$ from $$$96$$$ by using two shifts to the right: one by $$$2$$$, and another by $$$3$$$ (i.e. divide by $$$4$$$ and by $$$8$$$).


Artem is building a new robot. He has a matrix $$$a$$$ consisting of $$$n$$$ rows and $$$m$$$ columns. The cell located on the $$$i$$$-th row from the top and the $$$j$$$-th column from the left has a value $$$a_{i,j}$$$ written in it. If two adjacent cells contain the same value, the robot will break. A matrix is called good if no two adjacent cells contain the same value, where two cells are called adjacent if they share a side. Artem wants to increment the values in some cells by one to make $$$a$$$ good.More formally, find a good matrix $$$b$$$ that satisfies the following condition —   For all valid ($$$i,j$$$), either $$$b_{i,j} = a_{i,j}$$$ or $$$b_{i,j} = a_{i,j}+1$$$. For the constraints of this problem, it can be shown that such a matrix $$$b$$$ always exists. If there are several such tables, you can output any of them. Please note that you do not have to minimize the number of increments.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n, m$$$ ($$$1 \le n \le 100$$$, $$$1 \le m \le 100$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines each contain $$$m$$$ integers. The $$$j$$$-th integer in the $$$i$$$-th line is $$$a_{i,j}$$$ ($$$1 \leq a_{i,j} \leq 10^9$$$).

For each case, output $$$n$$$ lines each containing $$$m$$$ integers. The $$$j$$$-th integer in the $$$i$$$-th line is $$$b_{i,j}$$$.

NoteIn all the cases, you can verify that no two adjacent cells have the same value and that $$$b$$$ is the same as $$$a$$$ with some values incremented by one.

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 [rowLen, colLen] = readLine().split(" ").map(Number);
    const arr = [];
    let i = 0;
    while (i++ < rowLen) {
      const row = readLine().split(" ").map(Number);
      arr.push(row);
    }
    for (let row = 0; row < rowLen; row++) {
      for (let col = 0; col < colLen; col++) {
        if ((row + col) % 2 === 0) {
          if (arr[row][col] % 2 === 1) arr[row][col]++;
        } else {
          if (arr[row][col] % 2 === 0) arr[row][col]++;
        }
      }
    }
    for (let row = 0; row < rowLen; row++) console.log(arr[row].join(" "));
  }
}

let input = ''

process.stdin.on('data', (x) => input += x)

process.stdin.on('end', () => {
  const lines = input.trim().split('\n')
  const T = parseInt(lines[0])
  let curLine = 1
  for (let t = 0; t < T; t++) {
    const [n] = lines[curLine++].split(' ').map((x) => parseInt(x))
    for (let r = 0; r < n; r++) {
      console.log(lines[curLine++].split(' ').map((x, c) => parseInt(x) + (r + c + parseInt(x)) % 2).join(' '))
    }
  }
})

let input = ''

process.stdin.on('data', (x) => input += x)

process.stdin.on('end', () => {
  const lines = input.trim().split('\n')
  const T = parseInt(lines[0])
  let curLine = 1
  for (let t = 0; t < T; t++) {
    const [n, m] = lines[curLine++].split(' ').map((x) => parseInt(x))
    const mat = new Array(n)
    const cells = []
    for (let r = 0; r < n; r++) {
      mat[r] = lines[curLine++].split(' ').map((x) => parseInt(x))
      for (let c = 0; c < m; c++) {
        cells.push([r, c])
      }
    }
    cells.sort(([ra, ca], [rb, cb]) => mat[ra][ca] - mat[rb][cb])
    cells.forEach(([r, c]) => {
      const delta = [[1, 0], [-1, 0], [0, 1], [0, -1]]
      let change = false
      delta.forEach(([dr, dc]) => {
        const nr = r + dr
        const nc = c + dc

        if (nr < 0 || nr >= n || nc < 0 || nc >= m) {
          return
        }

        if (mat[nr][nc] === mat[r][c]) {
          change = true
        }
      })

      if (change) {
        mat[r][c]++
      }
    })

    for (let r = 0; r < n; r++) {
      console.log(mat[r].join(' '))
    }
  }
})

let input = ''

process.stdin.on('data', (x) => input += x)

process.stdin.on('end', () => {
  const lines = input.trim().split('\n')
  const T = parseInt(lines[0])
  let curLine = 1
  for (let t = 0; t < T; t++) {
    const [n, m] = lines[curLine++].split(' ').map((x) => parseInt(x))
    const mat = new Array(n)
    const cells = []
    for (let r = 0; r < n; r++) {
      mat[r] = lines[curLine++].split(' ').map((x) => parseInt(x))
      for (let c = 0; c < m; c++) {
        cells.push([r, c])
      }
    }
    cells.sort(([ra, ca], [rb, cb]) => mat[ra][ca] - mat[rb][cb])
    for (let rem = 0; rem < 2; rem++) {
      cells.forEach(([r, c]) => {
        if ((r + c) % 2 !== rem) {
          return
        }

        const delta = [[1, 0], [-1, 0], [0, 1], [0, -1]]
        let change = false
        delta.forEach(([dr, dc]) => {
          const nr = r + dr
          const nc = c + dc

          if (nr < 0 || nr >= n || nc < 0 || nc >= m) {
            return
          }

          if (mat[nr][nc] === mat[r][c]) {
            change = true
          }
        })

        if (change) {
          mat[r][c]++
        }
      })
    }

    for (let r = 0; r < n; r++) {
      console.log(mat[r].join(' '))
    }
  }
})


Autocomplete is a program function that enables inputting the text (in editors, command line shells, browsers etc.) completing the text by its inputted part. Vasya is busy working on a new browser called 'BERowser'. He happens to be working on the autocomplete function in the address line at this very moment. A list consisting of n last visited by the user pages and the inputted part s are known. Your task is to complete s to make it an address of one of the pages from the list. You have to find the lexicographically smallest address having a prefix s.

The first line contains the s line which is the inputted part. The second line contains an integer n (1 ≤ n ≤ 100) which is the number of visited pages. Then follow n lines which are the visited pages, one on each line. All the lines have lengths of from 1 to 100 symbols inclusively and consist of lowercase Latin letters only.

If s is not the beginning of any of n addresses of the visited pages, print s. Otherwise, print the lexicographically minimal address of one of the visited pages starting from s. The lexicographical order is the order of words in a dictionary. The lexicographical comparison of lines is realized by the '<' operator in the modern programming languages.

function trim(s) {
	return s.replace(/^\s+|\s+$/gm, '');
}

function tokenize(s) {
	return trim(s).split(/\s+/);
}

function tokenizeIntegers(s) {
	var tokens = tokenize(s);
	for (var i = 0; i < tokens.length; i += 1) {
		tokens[i] = parseInt(tokens[i], 10);
	};
	return tokens;
}

function main() {
	var prefix = trim(readline());

	var numCandidates = parseInt(readline(), 10),
		candidates = new Array(numCandidates);
	for (var candidateIndex = 0; candidateIndex < numCandidates; ++candidateIndex) {
		candidates[candidateIndex] = trim(readline());
	}

	candidates.sort();

	var regex = new RegExp("^"+prefix);

	for (var candidateIndex = 0; candidateIndex < numCandidates; ++candidateIndex) {
		if (regex.test(candidates[candidateIndex])) {
			print(candidates[candidateIndex]);
			return;
		}
	}

	print(prefix);
}

main();

function trim(s) {
	return s.replace(/^\s+|\s+$/gm, '');
}

function tokenize(s) {
	return trim(s).split(/\s+/);
}

function tokenizeIntegers(s) {
	var tokens = tokenize(s);
	for (var i = 0; i < tokens.length; i += 1) {
		tokens[i] = parseInt(tokens[i], 10);
	};
	return tokens;
}

function main() {
	var prefix = trim(readline());

	var numCandidates = parseInt(readline(), 10),
		candidates = new Array(numCandidates);
	for (var candidateIndex = 0; candidateIndex < numCandidates; ++candidateIndex) {
		candidates[candidateIndex] = trim(readline());
	}

	candidates.sort();

	var regex = new RegExp("^"+prefix);

	for (var candidateIndex = 0; candidateIndex < numCandidates; ++candidateIndex) {
		if (candidates[candidateIndex].match(regex)) {
			print(candidates[candidateIndex]);
			return;
		}
	}

	print(prefix);
}

main();

function trim(s) {
	return s.replace(/^\s+|\s+$/gm, '');
}

function tokenize(s) {
	return trim(s).split(/\s+/);
}

function tokenizeIntegers(s) {
	var tokens = tokenize(s);
	for (var i = 0; i < tokens.length; i += 1) {
		tokens[i] = parseInt(tokens[i], 10);
	};
	return tokens;
}

function main() {
	var prefix = trim(readline()),
		prefixLength = prefix.length;

	var numCandidates = parseInt(readline(), 10),
		candidates = new Array(numCandidates);
	for (var candidateIndex = 0; candidateIndex < numCandidates; ++candidateIndex) {
		candidates[candidateIndex] = trim(readline());
	}

	candidates.sort();

	for (var candidateIndex = 0; candidateIndex < numCandidates; ++candidateIndex) {
		if (candidates[candidateIndex].substring(0, prefixLength) == prefix) {
			print(candidates[candidateIndex]);
			return;
		}
	}

	print(prefix);
}

main();

'use strict'

let inp = readline()
let N = +readline()
let lll

let dic = []
while (lll = readline()) {
  dic.push(lll)
}

print(dic.filter(w => w.startsWith(inp)).sort()[0] || inp)

var input = readline();
var check = new RegExp('^' + input);
var iteration = readline();

var result;

for (var i = 0; i < iteration; i++) {
	var currentInput = readline();
	//currentInput.match(check)&& result?(( currentInput < result)&&result = currentInput):	result = currentInput
	if (currentInput.match(check)) {

result? (currentInput < result?	result = currentInput:result):(result = currentInput)
	}

}

!!result? print(result):print(input)

var word = readline();
var re = new RegExp('^' + word);
var n = parseInt(readline());
var res;
for (var i = 0; i < n; i++) {
	var input = readline();
	if (input.match(re)) {
		if (res) {
			if (input < res) {
				res = input;
			}
		} else {
			res = input;
		}
	}
}

if (res) {
	print(res);
} else {
	print(word);
}

var input = readline();
var iteration = readline();
var check = new RegExp('^' + input);

var result;

for (var i = 0; i < iteration; i++) {
	var currentInput = readline();
	currentInput.match(check)&& result? (currentInput < result? result = currentInput : result) : (result = currentInput)
}
!!result? print(result):print(input)

var input = readline();
var iteration = readline();
var check = new RegExp('^' + input);

var result;

for (var i = 0; i < iteration; i++) {
	var currentInput = readline();
	currentInput.match(check)&& result? (currentInput < result? (result = currentInput) : result) : (result = currentInput)
}
!!result? print(result):print(input)


Given an array a1, a2, ..., an of n integers, find the largest number in the array that is not a perfect square.A number x is said to be a perfect square if there exists an integer y such that x = y2.

The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of elements in the array. The second line contains n integers a1, a2, ..., an ( - 106 ≤ ai ≤ 106) — the elements of the array. It is guaranteed that at least one element of the array is not a perfect square.

Print the largest number in the array which is not a perfect square. It is guaranteed that an answer always exists.

NoteIn the first sample case, 4 is a perfect square, so the largest number in the array that is not a perfect square is 2.

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

  const arr = d.split(' ').map(Number);
  arr.sort((a, b) => b - a);

  for (let i = 0; i < arr.length; i++) {
    if (!Number.isInteger(Math.sqrt(arr[i]))) {
      console.log(arr[i]);
      break;
    }
  }

  c++;
});

var x=parseInt(readline());
var arr=readline().split(' ').map(a => parseInt(a));

arr.sort((a,b) => a-b);

for(var i=arr.length-1; i>=0; i--){
  var sqrt= Math.floor(Math.sqrt(arr[i]));
  if(sqrt*sqrt !== arr[i]){
    print(arr[i]);
    break;
  }else
    continue;
}

n=parseInt(readline());
mas=readline().split(' ');
function compareNumeric(a, b) {
  if (a > b) return 1;
  if (a < b) return -1;
}
for(i=0;i<n; i++) {
    mas[i] = parseInt(mas[i]);
}
mas.sort(compareNumeric).reverse();
for(i=0;i<n; i++) {
    if(Math.sqrt(mas[i])%1 !== 0) {
        print(mas[i]);
        break;
    }
}

var x = readline();
var nums = readline().split(' ').map(x => Number.parseInt(x));

function isPerfect(x){
    var comp = Math.sqrt(x).toFixed(0)

    return x == comp * comp;
}
var max = -Infinity;
for(var i = 0; i < nums.length; i++){
    if(!isPerfect(nums[i])){
        max = Math.max(max, nums[i]);
    }
}

print(max);

var x=parseInt(readline());
var arr=readline().split(' ').map(a => parseInt(a));

arr.sort((a,b) => a-b);

for(var i=arr.length-1; i>=0; i--){
  var sqrt= Math.sqrt(arr[i]);
  if(sqrt*sqrt !== arr[i]){
    print(arr[i]);
    break;
  }else
    continue;
}

n=parseInt(readline());
mas=readline().split('');
function compareNumeric(a, b) {
  if (a > b) return 1;
  if (a < b) return -1;
}
for(i=0;i<mas.length; i++) {
    mas[i] = parseInt(mas[i]);
}
mas.sort(compareNumeric).reverse();
for(i=0;i<mas.length; i++) {
    if(Math.sqrt(mas[i])%1 !== 0) {
        print(mas[i]);
        break;
    }
}


In this problem we consider a special type of an auction, which is called the second-price auction. As in regular auction n bidders place a bid which is price a bidder ready to pay. The auction is closed, that is, each bidder secretly informs the organizer of the auction price he is willing to pay. After that, the auction winner is the participant who offered the highest price. However, he pay not the price he offers, but the highest price among the offers of other participants (hence the name: the second-price auction).Write a program that reads prices offered by bidders and finds the winner and the price he will pay. Consider that all of the offered prices are different.

The first line of the input contains n (2 ≤ n ≤ 1000) — number of bidders. The second line contains n distinct integer numbers p1, p2, ... pn, separated by single spaces (1 ≤ pi ≤ 10000), where pi stands for the price offered by the i-th bidder.

The single output line should contain two integers: index of the winner and the price he will pay. Indices are 1-based.

readline();
var index;
var max = 0;
var max2 = 0;
readline().split(' ').map(function(v){
	return +v;
}).forEach(function(v, i){
	if(v>max){
		max2 = max;
		max = v;
		index = i;
	}else if(v>max2){
		max2 = v;
	}
});
print(index+1, max2);

var
    num      = parseInt(readline()),
    arr      = readline().split(" ")
;

for(var i in arr) {
    arr[i] = parseInt(arr[i]);
}

var sort = arr.slice(0);
sort.sort(function(a, b){if(a==b) return 0; return a > b ? 1 : -1})
sort.reverse()

for(var i in arr){
    if (sort[0] == arr[i]) {
        print((i - 1 + 2) + " " + sort[1])
        break;
    }
}

'use strict';

/*let input = `2
5 7`.split('\n');
function print(output) {
	console.log(output);
}
function readline() {
	return input.shift();
}*/

//code

var n = Number(readline());
var prices = readline().split(' ').map(function (el, i) {
	return { index: i, val: Number(el) };
});
prices.sort(function (a, b) {
	return a.val - b.val;
});
print(prices.pop().index + 1 + " " + prices.pop().val);

readline();
var a=readline().split(" "),b=a.slice().sort(function(a,b){return +a-b}),l=b.length;print(a.indexOf(b[l-1])+1+" "+b[l-2]);

var n = Number(readline());
var inp = readline().split(' ').map(cur => Number(cur));
var max = Math.max(...inp);
var index = inp.indexOf(max)
inp.splice(index, 1);
var res = [index + 1, Math.max(...inp)];
print(res.join(' '));

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

  const arr = d.split(' ').map(Number);
  const sortedArr = arr.slice();
  sortedArr.sort((a, b) => a - b);
  const max = sortedArr[sortedArr.length - 1];
  const secondMax = sortedArr[sortedArr.length - 2];
  let maxIdx;

  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === max) {
      maxIdx = i + 1;
      break;
    }
  }

  console.log(maxIdx, secondMax);

  c++;
});

var
    num = readline(),
    arr = readline().split(" "),
    maxx = 0,
    index = 0
;

var max = Math.max.apply(Math, arr);

for(var i in arr){
    if (arr[i] < max && arr[i] > maxx) {
        maxx = arr[i];

    }
    if (max == arr[i]) index = parseInt(i) + 1;
}

print(index + " " + maxx)


Igor the analyst has adopted n little bunnies. As we all know, bunnies love carrots. Thus, Igor has bought a carrot to be shared between his bunnies. Igor wants to treat all the bunnies equally, and thus he wants to cut the carrot into n pieces of equal area. Formally, the carrot can be viewed as an isosceles triangle with base length equal to 1 and height equal to h. Igor wants to make n - 1 cuts parallel to the base to cut the carrot into n pieces. He wants to make sure that all n pieces have the same area. Can you help Igor determine where to cut the carrot so that each piece have equal area?  Illustration to the first example.

The first and only line of input contains two space-separated integers, n and h (2 ≤ n ≤ 1000, 1 ≤ h ≤ 105).

The output should contain n - 1 real numbers x1, x2, ..., xn - 1. The number xi denotes that the i-th cut must be made xi units away from the apex of the carrot. In addition, 0 < x1 < x2 < ... < xn - 1 < h must hold.  Your output will be considered correct if absolute or relative error of every number in your output doesn't exceed 10 - 6. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .

NoteDefinition of isosceles triangle: https://en.wikipedia.org/wiki/Isosceles_triangle.

  var tmp = readline().split(' ').map(x => parseInt(x));
  var n= tmp[0];
  var h = tmp[1];
  var res = [];
  for(var i = 0; i < n - 1; i += 1) {
    res.push(Math.sqrt((i + 1) / n) * h);
  }
  print(res.join(' '));


Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he decided to solve some crosswords. It's well known that it is a very interesting occupation though it can be very difficult from time to time. In the course of solving one of the crosswords, Leha had to solve a simple task. You are able to do it too, aren't you?Leha has two strings s and t. The hacker wants to change the string s at such way, that it can be found in t as a substring. All the changes should be the following: Leha chooses one position in the string s and replaces the symbol in this position with the question mark "?". The hacker is sure that the question mark in comparison can play the role of an arbitrary symbol. For example, if he gets string s="ab?b" as a result, it will appear in t="aabrbb" as a substring.Guaranteed that the length of the string s doesn't exceed the length of the string t. Help the hacker to replace in s as few symbols as possible so that the result of the replacements can be found in t as a substring. The symbol "?" should be considered equal to any other symbol.

The first line contains two integers n and m (1 ≤ n ≤ m ≤ 1000) — the length of the string s and the length of the string t correspondingly. The second line contains n lowercase English letters — string s. The third line contains m lowercase English letters — string t.

In the first line print single integer k — the minimal number of symbols that need to be replaced. In the second line print k distinct integers denoting the positions of symbols in the string s which need to be replaced. Print the positions in any order. If there are several solutions print any of them. The numbering of the positions begins from one.

var input = readline().split(' ').map(function(x){return Number(x)});
var a = input[0];
var b = input[1];
var s1 = readline();
var s2 = readline();
var min = 100000;
var res = [];
for(var i = 0 ; i <= (s2.length - s1.length) ; i++){
    var ss = s2.substring(i, i + s1.length);
    var tmp = [];
    var act = 0;
    for(var j = 0 ; j < s1.length ; j++){
        if( s1[j] !== ss[j] ){
            act++; tmp.push(j + 1);
        }
    }
    if(act < min){
        min = act;
        res = tmp;
    }
}

print(min);
for(var i = 0 ; i < res.length ; i ++)write(res[i] + " ");

var st = readline().split(' ').map(Number);
var s = readline();
var t = readline();
var sl = st[0];
var tl = st[1];
var ans = 1100;
var pos = [];
for (var i=0; i<=tl-sl; i++){
	var count = 0;
	var poscount = [];
	for (var j=0; j<sl; j++){
		if (s[j] != t[i+j]){
			count++;
			poscount.push(j+1);
		}
	}
	if (count < ans){
		ans = count;
		pos = poscount;
	}
}
print(ans);
print(pos.join(' '));


Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order.According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai.Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date.

The first line contains a single positive integer n (1 ≤ n ≤ 5000) — the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≤ bi < ai ≤ 109) — the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly.

Print a single integer — the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date.

NoteIn the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5.In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.

var n = parseInt(readline());
var a = new Array();
for(var i=0; i<n; i++)
{
   var nums = readline().split(' ');
   var obj = new Object();
   obj.x = parseInt(nums[0]);
   obj.y = parseInt(nums[1]);
   a.push(obj);
}
a.sort(sortObj);

var ans = 0;

for(var i=0; i<n; i++)
{
    if(a[i].y>=ans)
	{
	    ans=a[i].y;
	}
	else
	{
	    ans=a[i].x;
	}
}
print(ans);

function sortObj(ojA, ojB)
{
    if(ojA.x==ojB.x)
	{
	    return ojA.y-ojB.y;
	}
	else
	{
	    return ojA.x - ojB.x;
	}
}

var node = function(x) {
    this.a=x[0];
    this.b=x[1];
}

var by_node = function(x,y){
    if(x.a!=y.a) return x.a<y.a;
    return x.b<y.b;
}

var merge = function(l,r,cmp){
    if(!l) l=[];
    if(!r) r=[];
    var ans=[];
    var sl=0;
    var sr=0;
    while(sl<l.length && sr<r.length){
        if(cmp(l[sl],r[sr])) ans.push(l[sl++]);
        else ans.push(r[sr++]);
    }
    if(sl==l.length) while(sr<r.length) ans.push(r[sr++]);
    if(sr==r.length) while(sl<l.length) ans.push(l[sl++]);
    //print("-M:"+ans.length+"->"+l.length+" + "+r.length);
    return ans;
}

var merge_sort = function(arr,cmp) {
    if(!arr) return arr;
    if(arr.length<2) return arr;
    var mid = Math.floor(arr.length/2);
    var right=[];
    for(var i=mid;i<arr.length;i++) right.push(arr[i]);
    var left = arr.splice(0,mid);

    //print("Sort-----------------------");
    //print("arr   length : "+arr.length);
    //print("left  length : "+left.length);
    //print("right length : "+right.length);
    //print("ans   length : "+ans.length);

    var l=merge_sort(left,cmp);
    var r=merge_sort(right,cmp);
    return merge(l,r,cmp);
}

var main = function(){
    var line=readline().split(' ');
    var n=parseInt(line);
    var arr = [];
    for(var i=0;i<n;i++) {
        line=readline();
        arr[i] = new node(line.split(' ').map(function(x){return parseInt(x);}));
    }
    //print("arr length = "+arr.length);
    var sarr=merge_sort(arr,by_node);
    //print("sarr length = "+sarr.length);
    //sarr.forEach(function(x){print(x.a+" "+x.b);});
    var ans=sarr[0].b;
    sarr.forEach(function(x){
        if(x.b>=ans) ans=x.b;
        else ans=x.a;
    })
    print(ans);
}

main();

var intp = function(x){
	return parseInt(x);
}

var node = function(x){
	this.a=parseInt(x[0]);
	this.b=parseInt(x[1]);
}

var by_node = function(x,y){
	return x.a!=y.a?x.a>y.a:x.b>y.b;
}

var out_type = function(x){
	print(typeof(x.a)+" "+typeof(x.b));
}

var out_value = function(x){
	print(x.a+" "+x.b);
}

var qsort = function(arr,cmp)
{
	if(!arr) return [];
	if(arr.length<=1) return arr;
	var p=Math.floor(Math.random()*arr.length);
	var pv=arr[p];
	arr[p]=arr[arr.length-1];
	arr.length--;
	var l=arr.filter(function(value){return cmp(pv,value);});
	var r=arr.filter(function(value){return !cmp(pv,value);});
	return qsort(l,cmp).concat([pv],qsort(r,cmp));
}

var line;
while(line=readline()) {
	var n = line.split(' ').map(intp)[0];
	var arr = [];
	for(var i=0;i<n;i++) {
		line=readline();
		arr[i] = new node(line.split(' '));
	}
	sarr=qsort(arr,by_node);
	var ans=sarr[0].b;
	sarr.forEach(function(x){
		if(x.b>=ans) ans=x.b;
		else ans=x.a;
	});
	print(ans);
}

var node = function(x){
    this.a=x[0];
    this.b=x[1];
}

var by_node = function(x,y){
    if(x.a!=y.a) return x.a-y.a;
    return x.b-y.b;
}

var out_value = function(x){
    print(x.a+" "+x.b);
}

while(true) {
    line = readline();
    if(!line) break;
    n = parseInt(line);
    var arr = new Array(n+5);
    for(var i=0;i<n;i++) {
        line = readline();
        var numbers = line.split(' ').map(function(x){return parseInt(x);});
        arr[i] = new node(numbers);
    }
    arr.sort(by_node);
    var ans=arr[0].b;
    arr.forEach(function(x){
        if(x.b>=ans) ans=x.b;
        else ans=x.a;
    });
    print(ans);
}

var arr = [], elements;
var queries = parseInt(readline())
for (var l = 0; l < queries;l++){
	elements = readline().split(' ').map((val)=>{
		return parseInt(val);
	})
	arr.push(elements);
}
arr.sort((a,b)=>{
	if (a[0] == b[0]){
		return a[1] - b[1];
	}
	else{
	return a[0] - b[0];
	}
})
var enDay = Math.min(arr[0][0], arr[0][1])
var interim;
for (var k = 1; k<arr.length; k++){
	interim = Math.min(arr[k][0],arr[k][1])
	if (interim >= enDay){
		enDay = interim;
	}
	else{
		enDay = (interim == arr[k][0]) ? arr[k][1] : arr[k][0];
	}
}
print(enDay)

var n = parseInt(readline());
var a= [];
for (var i=0;i<n;i++) {a.push(readline().split(' ').map(Number))}
var p =a.sort((a, b)=>{ if ((a[0]-b[0])!==0) {return a[0]-b[0]} else {return a[1]-b[1]}}).map(function (xs) {var mi= Math.min.apply(null,xs); var ma = Math.max.apply(null,xs);if (mi>=this.t) {this.t=mi; return mi} else {this.t=ma; return ma}},{t:0}).pop();
print (p);

var n = +readline();
var arr=[];
for(var i=0;i<n;i++)
    arr.push(readline().split(" ").map(v=>+v));
arr.sort((a,b)=>{
    if(a[0]===b[0])
        return a[1]-b[1];
    return a[0]-b[0];
});

var prev=0;
for(var i=0;i<n;i++){
    var t=arr[i][1]<prev?0:1;
    prev=arr[i][t];
}
print(prev);

n = readline().split(' ').map(function(x){return parseInt(x);})[0];

var arr=[];

for(var i=0;i<n;i++) {
    var numbers = readline().split(' ').map(function(x){return parseInt(x)});
    arr[i]={a:numbers[0], b:numbers[1]};
}

arr.sort(function(x,y){
    if(x.a!=y.a) return x.a>y.a;
    else return x.b>y.b;
})

var ans=arr[0].b;

for(var i=0;i<n;i++) {
    if(arr[i].b>=ans) ans=arr[i].b;
    else ans=arr[i].a;
}

print(ans);

n = parseInt(readline());

var arr=[];

for(var i=0;i<n;i++) {
    var numbers = readline().split(' ').map(function(x){return parseInt(x)});
    arr[i]={a:numbers[0], b:numbers[1]};
}

arr.sort(function(x,y){
    if(x.a!=y.a) return x.a>y.a;
    else return x.b>y.b;
})

var ans=arr[0].b;

for(var i=0;i<n;i++) {
    if(arr[i].b>=ans) ans=arr[i].b;
    else ans=arr[i].a;
}

print(ans);

n = parseInt(readline());

var arr=[];

for(var i=0;i<n;i++) {
    var numbers = readline().split(' ').map(function(x){return parseInt(x)});
    arr.push({a:numbers[0], b:numbers[1]});
}

arr.sort(function(x,y){
    if(x.a!=y.a) return x.a>y.a;
    else return x.b>y.b;
})

var ans=arr[0].b;

for(var i=0;i<n;i++) {
    if(arr[i].b>=ans) ans=arr[i].b;
    else ans=arr[i].a;
}

print(ans);

n = parseInt(readline());

var arr=[];

for(var i=0;i<n;i++) {
    var numbers = readline().split(' ').map(function(x){return parseInt(x)});
    arr.push({a:numbers[0], b:numbers[1]});
}

arr.sort(function(x,y){
    if(x.a!=y.a) return x.a>y.a;
    else return x.b>y.b;
})

var ans=arr[0].b;

arr.forEach(function(x){
    if(x.b>=ans) ans=x.b;
    else ans=x.a;
})

print(ans);

var arr = [], elements = []
var queries = parseInt(readline())
for (var l = 0; l < queries;l++){
	readline().split(' ').map((val)=>{
		elements.push(parseInt(val));
	})
	arr.push(elements);
}
arr.sort((a,b)=>{
	return a[0] - b[0]
})
var enDay = Math.min(arr[0][0], arr[0][1])
var interim;
for (var k = 1; k<arr.length; k++){
	interim = Math.min(arr[k][0],arr[k][1])
	if (interim >= enDay){
		enDay = interim;
	}
	else{
		enDay = (interim == arr[k][0]) ? arr[k][1] : arr[k][0];
	}
}
print(enDay)

var arr = [], elements = []
var queries = parseInt(readline())
for (var l = 0; l < queries;l++){
	readline().split(' ').map((val)=>{
		elements.push(parseInt(val));
	})
	arr.push(elements);
	elements.pop();
	elements.pop()
}
arr.sort((a,b)=>{
	return a[0] - b[0]
})
var enDay = Math.min(arr[0][0], arr[0][1])
var interim;
for (var k = 1; k<arr.length; k++){
	interim = Math.min(arr[k][0],arr[k][1])
	if (interim >= enDay){
		enDay = interim;
	}
	else{
		enDay = (interim == arr[k][0]) ? arr[k][1] : arr[k][0];
	}
}
print(enDay)

var arr = [], elements;
var queries = parseInt(readline())
for (var l = 0; l < queries;l++){
	elements = readline().split(' ').map((val)=>{
		return parseInt(val);
	})
	arr.push(elements);
}
arr.sort((a,b)=>{
	return a[0] - b[0]
})
var enDay = Math.min(arr[0][0], arr[0][1])
var interim;
for (var k = 1; k<arr.length; k++){
	interim = Math.min(arr[k][0],arr[k][1])
	if (interim >= enDay){
		enDay = interim;
	}
	else{
		enDay = (interim == arr[k][0]) ? arr[k][1] : arr[k][0];
	}
}
print(enDay)

var n = +readline();
var arr=[];
for(var i=0;i<n;i++)
    arr.push(readline().split(" ").map(v=>+v));
for(var i=0;i<n;i++)
    print(arr[i]);
arr.sort((a,b)=>{
    if(a[0]===b[0])
        return a[1]-b[1];
    return a[0]-b[0];
});

var ans=0;
var prev=0;
for(var i=0;i<n;i++){
    var t=arr[i][1]<prev;
    ans=arr[i][t];
    prev=arr[i][t];
}
print(ans);

var n = +readline();
var arr=[];
for(var i=0;i<n;i++)
    arr.push(readline().split(" ").map(v=>+v));
print(arr);
arr.sort((a,b)=>{
    if(a[0]===b[0])
        return a[1]-b[1];
    return a[0]-b[0];
});

var ans=0;
var prev=0;
for(var i=0;i<n;i++){
    var t=arr[i][1]<prev;
    ans=arr[i][t];
    prev=arr[i][t];
}
print(ans);

var n = +readline();
var arr=[];
for(var i=0;i<n;i++)
    arr.push(readline().split(" ").map(v=>+v));
arr.sort((a,b)=>{
    if(a[0]===b[0])
        return a[1]-b[1];
    return a[0]-b[0];
});

for(var i=0;i<n;i++)
    print(arr[i]);
var ans=0;
var prev=0;
for(var i=0;i<n;i++){
    var t=arr[i][1]<prev?0:1;
    ans=arr[i][t];
    prev=arr[i][t];
}
print(ans);

var n = +readline();
var arr=[];
for(var i=0;i<n;i++)
    arr.push(readline().split(" ").map(v=>+v));
arr.sort((a,b)=>{
    if(a[0]===b[0])
        return a[1]-b[1];
    return a[0]-b[0];
});

var ans=0;
var prev=0;
for(var i=0;i<n;i++){
    var t=arr[i][1]<prev;
    ans=arr[i][t];
    prev=arr[i][t];
}
print(ans);


You have $$$n$$$ chains, the $$$i$$$-th chain consists of $$$c_i$$$ vertices. Vertices in each chain are numbered independently from $$$1$$$ to $$$c_i$$$ along the chain. In other words, the $$$i$$$-th chain is the undirected graph with $$$c_i$$$ vertices and $$$(c_i - 1)$$$ edges connecting the $$$j$$$-th and the $$$(j + 1)$$$-th vertices for each $$$1 \le j < c_i$$$.Now you decided to unite chains in one graph in the following way:   the first chain is skipped;  the $$$1$$$-st vertex of the $$$i$$$-th chain is connected by an edge with the $$$a_i$$$-th vertex of the $$$(i - 1)$$$-th chain;  the last ($$$c_i$$$-th) vertex of the $$$i$$$-th chain is connected by an edge with the $$$b_i$$$-th vertex of the $$$(i - 1)$$$-th chain.   Picture of the first test case. Dotted lines are the edges added during uniting process Calculate the length of the longest simple cycle in the resulting graph.A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of chains you have. The second line of each test case contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$2 \le c_i \le 10^9$$$) — the number of vertices in the corresponding chains. The third line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$a_1 = -1$$$; $$$1 \le a_i \le c_{i - 1}$$$). The fourth line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$b_1 = -1$$$; $$$1 \le b_i \le c_{i - 1}$$$). Both $$$a_1$$$ and $$$b_1$$$ are equal to $$$-1$$$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.

For each test case, print the length of the longest simple cycle.

NoteIn the first test case, the longest simple cycle is shown below:   We can't increase it with the first chain, since in such case it won't be simple — the vertex $$$2$$$ on the second chain will break simplicity.

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

const calculate = (chainNumberList, cList, aList, bList) => {
  if (chainNumberList === 1) return 0

  let currentCycleLength = 0
  let maxCycleLength = 0

  for (let index = 1; index < chainNumberList; index++) {
    const a = aList[index]
    const b = bList[index]

    if (index === 1) {
      currentCycleLength = Math.abs(a - b)
    } else {
      const baseLength = Math.abs(a - b)
      const partLength = cList[index - 1] - 1 - baseLength
      if (baseLength > partLength + currentCycleLength) {
        currentCycleLength = baseLength
      } else {
        currentCycleLength += partLength
      }
    }

    if (a === b) {
      currentCycleLength = 0
    }

    currentCycleLength += 2

    if (maxCycleLength < cList[index] + currentCycleLength) {
      maxCycleLength = cList[index] - 1 + currentCycleLength
    }
  }

  currentCycleLength += cList[cList.length - 1] - 1

  if (maxCycleLength < currentCycleLength) {
    maxCycleLength = currentCycleLength
  }

  return maxCycleLength
}

let index = -1
let maxRound = 0
let round = 0

let cList, aList, bList, chainNumberList
let answer = []

readline.on('line', line => {
  if (index === -1) {
    maxRound = Number(line)
    index = 0
  } else {
    const mode = index % 4
    if (mode === 0) {
      chainNumberList = Number(line)
    } else if (mode === 1) {
      cList = line.split(' ').map(val => Number(val))
    } else if (mode === 2) {
      aList = line.split(' ').map(val => Number(val))
    } else if (mode === 3) {
      bList = line.split(' ').map(val => Number(val))
      answer.push(calculate(chainNumberList, cList, aList, bList))
      round++
      if (round === maxRound) {
        readline.close()
        console.log(answer.join('\n'))
        return
      }
    }
    index++
  }
})

// chainNumberList, cList, aList, bList
// console.log(calculate(4, [3, 4, 3, 3], [-1, 1, 2, 2], [-1, 2, 2, 3]))

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 INF = 1e9+5;

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

		let ans = 0;
		for (let i = 1, cur = -INF; i < n; i++) {
			if (a[i] == b[i]) {
				cur = 0;
			} else {
				cur += c[i-1] - 1 - Math.abs(a[i] - b[i]);
				cur = Math.max(cur, Math.abs(a[i] - b[i]));
			}
			cur += 2;
			ans = Math.max(ans, cur + c[i] - 1);
		}

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

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

		let ans = 0;
		let cur = Math.abs(a[1] - b[1]);
		for (let i = 1; i < n; i++) {
			cur += 2;
			ans = Math.max(ans, cur + c[i] - 1);
			if (a[i+1] == b[i+1]) {
				cur = 0;
			} else {
				cur += Math.min(a[i+1], b[i+1]) - 1
				cur += c[i] - Math.max(a[i+1], b[i+1]);
				cur = Math.max(cur, Math.abs(a[i+1] - b[i+1]));
			}
		}

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

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

function main() {
    const x = readline();

    // 1 1 7
    // 6 9, 15, 18
    Array(Number(x)).fill(1).map((t, iii) => {
        var n = parseInt(readline())
        var map = {}
        var c = readline().split(' ').map((x, iii) => {
            return parseInt(x)
        })
        var a = readline().split(' ').map((x, iii) => {
            return parseInt(x)
        })
        var b = readline().split(' ').map((x, iii) => {
            return parseInt(x)
        })

        var currentLength = 0
        var len = 0
        var max = 0
        for (let i = 1; i < n; i++) {
            currentLength = 1+c[i]+Math.abs(a[i]-b[i])
            if (a[i] !== b[i]) {
                currentLength = Math.max(currentLength, len + c[i]-Math.abs(a[i]-b[i])+1)
            }
            max = Math.max(max, currentLength)
            len = currentLength
            // console.log(currentLength, a[i], b[i], c[i - 1])
        }
        console.log(max)
    })
}

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

const calculate = (chainNumberList, cList, aList, bList) => {
  if (chainNumberList === 1) return 0

  let maxCycleLength = 0
  let currentCycleLength = 0
  for (let index = 1; index < chainNumberList; index++) {
    const a = aList[index]
    const b = bList[index]
    const cOld = cList[index - 1]

    if (index === 1) {
      currentCycleLength += Math.abs(b - a)
    } else {
      currentCycleLength += cOld - 1 - Math.abs(b - a)
    }

    if (a === b) {
      if (maxCycleLength < currentCycleLength) {
        maxCycleLength = currentCycleLength
        currentCycleLength = 0
      }
    } else {
      const newCurrentCycleLength = currentCycleLength + cOld - 1
      if (maxCycleLength < newCurrentCycleLength) {
        maxCycleLength = newCurrentCycleLength
      }
    }
    currentCycleLength += 2
  }

  currentCycleLength += cList[cList.length - 1] - 1

  if (maxCycleLength < currentCycleLength) {
    maxCycleLength = currentCycleLength
  }

  return maxCycleLength
}

let index = -1
let maxRound = 0
let round = 0

let cList, aList, bList, chainNumberList
let answer = []

readline.on('line', line => {
  if (index === -1) {
    maxRound = Number(line)
    index = 0
  } else {
    const mode = index % 4
    if (mode === 0) {
      chainNumberList = Number(line)
    } else if (mode === 1) {
      cList = line.split(' ').map(val => Number(val))
    } else if (mode === 2) {
      aList = line.split(' ').map(val => Number(val))
    } else if (mode === 3) {
      bList = line.split(' ').map(val => Number(val))
      answer.push(calculate(chainNumberList, cList, aList, bList))
      round++
      if (round === maxRound) {
        readline.close()
        console.log(answer.join('\n'))
        return
      }
    }
    index++
  }
})

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

const calculate = (chainNumberList, cList, aList, bList) => {
  let maxCycleLength = 0
  let currentCycleLength = 0
  for (let index = 1; index < chainNumberList; index++) {
    const a = aList[index]
    const b = bList[index]
    const cOld = cList[index - 1]

    if (index === 1) {
      currentCycleLength += Math.abs(b - a)
    } else {
      currentCycleLength += cOld - 1 - Math.abs(b - a)
    }

    if (a === b) {
      if (maxCycleLength < currentCycleLength) {
        maxCycleLength = currentCycleLength
        currentCycleLength = 0
      }
    } else {
      const newCurrentCycleLength = currentCycleLength + cOld - 1
      if (maxCycleLength < newCurrentCycleLength) {
        maxCycleLength = newCurrentCycleLength
      }
    }
    currentCycleLength += 2
  }

  currentCycleLength += cList[cList.length - 1] - 1

  if (maxCycleLength < currentCycleLength) {
    maxCycleLength = currentCycleLength
  }

  return maxCycleLength
}

let index = -1
let maxRound = 0
let round = 0

let cList, aList, bList, chainNumberList
let answer = []

readline.on('line', line => {
  if (index === -1) {
    maxRound = Number(line)
    index = 0
  } else {
    const mode = index % 4
    if (mode === 0) {
      chainNumberList = Number(line)
    } else if (mode === 1) {
      cList = line.split(' ').map(val => Number(val))
    } else if (mode === 2) {
      aList = line.split(' ').map(val => Number(val))
    } else if (mode === 3) {
      bList = line.split(' ').map(val => Number(val))
      answer.push(calculate(chainNumberList, cList, aList, bList))
      round++
      if (round === maxRound) {
        readline.close()
        console.log(answer.join('\n'))
        return
      }
    }
    index++
  }
})

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

const calculate = (chainNumberList, cList, aList, bList) => {
  let maxCycleLength = 0
  let currentCycleLength = 0
  for (let index = 1; index < chainNumberList; index++) {
    const a = aList[index]
    const b = bList[index]
    const cOld = cList[index - 1]

    if (index === 1) {
      currentCycleLength += Math.abs(b - a)
    } else {
      currentCycleLength += cOld - 1 - Math.abs(b - a)
    }

    if (a === b) {
      if (maxCycleLength < currentCycleLength) {
        maxCycleLength = currentCycleLength
        currentCycleLength = 0
      }
    }
    currentCycleLength += 2
  }

  currentCycleLength += cList[cList.length - 1] - 1

  if (maxCycleLength < currentCycleLength) {
    maxCycleLength = currentCycleLength
  }

  return maxCycleLength
}

let index = -1
let maxRound = 0
let round = 0

let cList, aList, bList, chainNumberList
let answer = []

readline.on('line', line => {
  if (index === -1) {
    maxRound = Number(line)
    index = 0
  } else {
    const mode = index % 4
    if (mode === 0) {
      chainNumberList = Number(line)
    } else if (mode === 1) {
      cList = line.split(' ').map(val => Number(val))
    } else if (mode === 2) {
      aList = line.split(' ').map(val => Number(val))
    } else if (mode === 3) {
      bList = line.split(' ').map(val => Number(val))
      answer.push(calculate(chainNumberList, cList, aList, bList))
      round++
      if (round === maxRound) {
        readline.close()
        console.log(answer.join('\n'))
        return
      }
    }
    index++
  }
})

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

const calculate = (chainNumberList, cList, aList, bList) => {
  let maxCycleLength = 0
  let currentCycleLength = 0
  for (let index = 1; index < chainNumberList; index++) {
    const a = aList[index]
    const b = bList[index]
    const cOld = cList[index - 1]

    if (index === 1) {
      currentCycleLength += Math.abs(b - a)
    } else {
      currentCycleLength += cOld - 1 - Math.abs(b - a)
    }

    if (a === b) {
      if (maxCycleLength < currentCycleLength) {
        maxCycleLength = currentCycleLength
        currentCycleLength = 0
      }
    }
    currentCycleLength += 2
  }

  currentCycleLength += cList[cList.length - 1] - 1

  if (maxCycleLength < currentCycleLength) {
    maxCycleLength = currentCycleLength
  }

  return maxCycleLength
}

let index = -1
let maxRound = 0
let round = 0

let cList, aList, bList, chainNumberList

readline.on('line', line => {
  if (index === -1) {
    maxRound = Number(line)
    index = 0
  } else {
    const mode = index % 4
    if (mode === 0) {
      chainNumberList = Number(line)
    } else if (mode === 1) {
      cList = line.split(' ').map(val => Number(val))
    } else if (mode === 2) {
      aList = line.split(' ').map(val => Number(val))
    } else if (mode === 3) {
      bList = line.split(' ').map(val => Number(val))
      console.log(calculate(chainNumberList, cList, aList, bList))
      round++
      if (round === maxRound) {
        readline.close()
        return
      }
    }
    index++
  }
})

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

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

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

		let ans = 0;
		for (let i = 1, cur = 0; i < n; i++) {
			if (a[i] == b[i]) {
				cur = 0;
			} else {
				cur += c[i-1] - 1 - Math.abs(a[i] - b[i]);
				cur = Math.max(cur, Math.abs(a[i] - b[i]));
			}
			cur += 2;
			ans = Math.max(ans, cur + c[i] - 1);
		}

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

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

		let ans = 0;
		let cur = Math.abs(a[1] - b[1]);
		for (let i = 1; i < n; i++) {
			cur += 2;
			ans = Math.max(ans, cur + c[i] - 1);
			if (a[i+1] == b[i+1]) {
				cur = 0;
			} else {
				cur += Math.min(a[i+1], b[i+1]) - 1
				cur += c[i] - Math.max(a[i+1], b[i+1]);
			}
		}

		console.log(ans);
	}
}


You are given a multiset $$$S$$$ initially consisting of $$$n$$$ distinct non-negative integers. A multiset is a set, that can contain some elements multiple times.You will perform the following operation $$$k$$$ times:   Add the element $$$\lceil\frac{a+b}{2}\rceil$$$ (rounded up) into $$$S$$$, where $$$a = \operatorname{mex}(S)$$$ and $$$b = \max(S)$$$. If this number is already in the set, it is added again. Here $$$\operatorname{max}$$$ of a multiset denotes the maximum integer in the multiset, and $$$\operatorname{mex}$$$ of a multiset denotes the smallest non-negative integer that is not present in the multiset. For example:    $$$\operatorname{mex}(\{1,4,0,2\})=3$$$;   $$$\operatorname{mex}(\{2,5,1\})=0$$$. Your task is to calculate the number of distinct elements in $$$S$$$ after $$$k$$$ operations will be done.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\le t\le 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1\le n\le 10^5$$$, $$$0\le k\le 10^9$$$) — the initial size of the multiset $$$S$$$ and how many operations you need to perform. The second line of each test case contains $$$n$$$ distinct integers $$$a_1,a_2,\dots,a_n$$$ ($$$0\le a_i\le 10^9$$$) — the numbers in the initial multiset. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, print the number of distinct elements in $$$S$$$ after $$$k$$$ operations will be done.

"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, k] = readline()
      .trim()
      .split(" ")
      .map((cur) => parseInt(cur));
    let arr = readline()
      .trim()
      .split(" ")
      .map((cur) => parseInt(cur));
    arr.sort((a, b) => a - b);
    let mex = Infinity;
    if (arr[0] !== 0) mex = 0;
    for (let i = 1; i < n; i++) {
      if (arr[i - 1] + 1 !== arr[i]) {
        mex = Math.min(mex, arr[i - 1] + 1);
        break;
      }
    }
    if (mex === Infinity) {
      console.log(k + n);
      continue;
    }
    let obj = {};
    for (let i = 0; i < n; i++) obj[arr[i]] = 1;
    let i = 0,
      max = arr[n - 1];
    while (i < k) {
      let j = Math.ceil((mex + max) / 2);
      if (obj[j]) break;
      else {
        arr.push(j);
        obj[j] = 1;
        if (mex === j) {
          while (obj[j]) j++;
          mex = j;
        }
        max = Math.max(j, max);
      }
      i++;
    }
    console.log(Object.keys(obj).length);
  }
};
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, k], set) {
    set.sort((a, b) => a - b)
    let min = Number.MAX_SAFE_INTEGER, prv = -1
    for (let i of set) {
        if (i - 1 != prv) {
            min = prv + 1
            break
        }
        prv = i
    }
    let max = set[n - 1];
    let a = Math.ceil((min + max) / 2)
    let flag = true
    for (let i of set) {
        if (i == a) flag = false
    }
    if (k == 0) flag = false
    if (min + 1 <= max) return n + Number(flag)
    else return n + k
}

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

'use strict';

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

let inputString = '';
let currentLine = 0;

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

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

  main();
});

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

// Make a Snippet for the code above this and then write your logic in main();
var mod = 1000000000n + 7n
var maxN = 1002

function main() {
  const x = readline();

  Array(parseInt(x)).fill(1).map((t, iii) => {
    var [n, k] = readline().split(' ').map((x, iii) => {
      return parseInt(x)
    })
    var a = readline().split(' ').map((x, iii) => {
      return parseInt(x)
    })
    var exist = false
    for (let i = 0; i < n; i++) {
      if (a[i] >= n) exist = true
    }
    if (!exist) return console.log(n + k)
    if (k === 0) return console.log(n)

    var mex = 0
    var max = 0
    for (let i = 0; i <= n; i++) {
      if (a[i] > max) max = a[i]
    }
    for (let i = 0; i <= n; i++) {
      if (!a.includes(i)) {
        mex = i
        break
      }
    }
    if (!a.includes(Math.ceil((mex + max) / 2))) return console.log(n + 1)
    console.log(n)
    // console.log(exist ? n : n + k)
  })
}

function gcd(a, b) {
  if (!b) {
    return a;
  }

  return gcd(b, a % b);
}

function getInt(num){
        return parseInt(num, 10)
    }

var t = parseInt(readline(), 10);
var lines= [];
for(var i = 0; i<t; i++){
    var line1 = readline().split(' ').map(getInt);
    var n = line1[0];
    var k = line1[1];
    var sorted = readline().split(' ').map(getInt).sort(function(a,b){
        return a-b;
    }).filter(function(item, i, arr){
        return !i || arr[i-1] !== item
    });
    var max = sorted[sorted.length - 1];
    var mex = getMex(sorted, max);

    var addition = 0;
    if(mex-max ===  1){
        addition = k;
    }else if(k >= 1){
        var newItem = Math.ceil((max+mex)/2);
        addition = sorted.some( function(item){
            return item === newItem;
        }) ? 0 : 1;

    }
    print(n + addition);
}

function getMex(arr,max){
    var res = 0;

    arr.forEach(function(item){
        if(res === item){
            res = item +1;
        }
    } )
    return res
}

///////////////////////////////// pre-define /////////////////////////////////////
const pr = console.log;
///////////////////////////////////////////////////////////////////////////////////

const solve = (n, k, a) => {
    if (k == 0) return pr(n);
    let max = Math.max.apply(Math, a);
    let mex = n;
    for (let i = 0; i < n; i++) {
        if (i != a[i]) {
            mex = i;
            break;
        }
    }
    if (mex > max) return pr(n + k);
    let add = mex + max + 1 >> 1;
    for (let i = 0; i < n; i++) {
        if (add == a[i]) {
            return pr(n);
        }
    }
    pr(n + 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(line.split(" ").map(x => Number(x)));
    });
    rl.on('close', () => {
        let t = input[0][0];
        let i = 1;
        while (t--) {
            solve(input[i][0], input[i][1], input[i + 1]);
            i += 2;
        }
    });
};

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, k] = readline()
      .trim()
      .split(" ")
      .map((cur) => parseInt(cur));
    let arr = readline()
      .trim()
      .split(" ")
      .map((cur) => parseInt(cur));
    arr.sort((a, b) => a - b);
    let mex = Infinity;
    if (arr[0] === 0) mex = 0;
    for (let i = 1; i < n; i++) {
      if (arr[i - 1] + 1 !== arr[i]) {
        mex = Math.min(mex, arr[i - 1] + 1);
        break;
      }
    }
    if (mex === Infinity) mex = arr[n - 1] + 1;
    let obj = {};
    for (let i = 0; i < n; i++) obj[arr[i]] = 1;
    let i = 0,
      max = arr[n - 1];
    while (i < k) {
      let j = Math.ceil((mex + max) / 2);
      if (obj[j]) break;
      else {
        arr.push(j);
        obj[j] = 1;
        if (mex === j) {
          while (obj[j]) j++;
          mex = j;
        }
        max = Math.max(j, max);
      }
      i++;
    }
    let l = {},
      cnt = 0;
    for (let i = 0; i < arr.length; i++) {
      if (!l[arr[i]]) cnt++;
      else l[arr[i]] = 1;
    }
    console.log(cnt);
  }
};
solve();


Once little Vasya read an article in a magazine on how to make beautiful handmade garland from colored paper. Vasya immediately went to the store and bought n colored sheets of paper, the area of each sheet is 1 square meter.The garland must consist of exactly m pieces of colored paper of arbitrary area, each piece should be of a certain color. To make the garland, Vasya can arbitrarily cut his existing colored sheets into pieces. Vasya is not obliged to use all the sheets to make the garland.Vasya wants the garland to be as attractive as possible, so he wants to maximize the total area of m pieces of paper in the garland. Calculate what the maximum total area of the pieces of paper in the garland Vasya can get.

The first line contains a non-empty sequence of n (1 ≤ n ≤ 1000) small English letters ("a"..."z"). Each letter means that Vasya has a sheet of paper of the corresponding color. The second line contains a non-empty sequence of m (1 ≤ m ≤ 1000) small English letters that correspond to the colors of the pieces of paper in the garland that Vasya wants to make.

Print an integer that is the maximum possible total area of the pieces of paper in the garland Vasya wants to get or -1, if it is impossible to make the garland from the sheets he's got. It is guaranteed that the answer is always an integer.

NoteIn the first test sample Vasya can make an garland of area 6: he can use both sheets of color b, three (but not four) sheets of color a and cut a single sheet of color c in three, for example, equal pieces. Vasya can use the resulting pieces to make a garland of area 6.In the second test sample Vasya cannot make a garland at all — he doesn't have a sheet of color z.

print(function(a, b) {
	var c = new Int32Array(256);
	var has = new Int8Array(256);

	for (var i = 0; i < a.length; i++)
		c[a.charCodeAt(i)]++;

	for (var i = 0; i < 256; i++)
		if (c[i] > 0) has[i] = 1;

	var ans = 0;
	for (var i = 0; i < b.length; i++) {
		var clr = b.charCodeAt(i);
		if (has[clr] === 0) return -1;
		if (c[clr] > 0) {
			c[clr]--;
			ans++;
		}
	}
	return ans;
}(readline(), readline()));

function trim(s) {
	return s.replace(/^\s+|\s+$/gm, '');
}

function tokenize(s) {
	return trim(s).split(/\s+/);
}

function tokenizeIntegers(s) {
	var tokens = tokenize(s);
	for (var i = 0; i < tokens.length; i += 1) {
		tokens[i] = parseInt(tokens[i], 10);
	};
	return tokens;
}

function main() {
	function string2hash(s) {
		var h = { nonzero: [] },
			alpha = 'abcdefghijklmnopqrstuvwxyz';
		for (var i = 0; i < 26; ++i) {
			h[alpha.charAt(i)] = 0;
		}
		for (var i = 0; i < s.length; ++i) {
			var ch = s.charAt(i);
			if (h[ch] == 0) {
				h.nonzero.push(ch);
			}
			h[ch] += 1;
		}
		return h;
	}
	var source = string2hash(readline()),
		target = string2hash(readline()),
		count = 0;
	for (var i = 0; i < target.nonzero.length; ++i) {
		var ch = target.nonzero[i];
		if (source[ch] == 0) {
			print(-1);
			return;
		}
		count += Math.min(source[ch], target[ch]);
	}
	print(count);
}

main();

print(function(a, b) {
	var c = new Int32Array(1000);
	for (var i = 0; i < a.length; i++)
		c[a.charCodeAt(i)]++;

	var ans = 0;
	for (var i = 0; i < b.length; i++)
		if (c[b.charCodeAt(i)] > 0) {
			c[b.charCodeAt(i)]--;
			ans++;
		}

	return ans == 0 ? -1 : ans;
}(readline(), readline()));

print(function(a, b) {
	var c = new Int8Array(255);
	for (var i = 0; i < a.length; i++)
		c[a.charCodeAt(i)]++;

	var ans = 0;
	for (var i = 0; i < b.length; i++)
		if (c[b.charCodeAt(i)] > 0) {
			c[b.charCodeAt(i)]--;
			ans++;
		}

	return ans == 0 ? -1 : ans;
}(readline(), readline()));

print(function(a, b) {
	var c = new Int32Array(255);
	for (var i = 0; i < a.length; i++)
		c[a.charCodeAt(i)]++;

	var ans = 0;
	for (var i = 0; i < b.length; i++)
		if (c[b.charCodeAt(i)] > 0) {
			c[b.charCodeAt(i)]--;
			ans++;
		}

	return ans == 0 ? -1 : ans;
}(readline(), readline()));