There is a classroom with two rows of computers. There are $$$n$$$ computers in each row and each computer has its own grade. Computers in the first row has grades $$$a_1, a_2, \dots, a_n$$$ and in the second row — $$$b_1, b_2, \dots, b_n$$$.Initially, all pairs of neighboring computers in each row are connected by wire (pairs $$$(i, i + 1)$$$ for all $$$1 \le i < n$$$), so two rows form two independent computer networks.Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $$$i$$$-th computer from the first row and the $$$j$$$-th computer from the second row costs $$$|a_i - b_j|$$$.You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts.That is the minimum total cost to make a fault-tolerant network?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) — the number of computers in each row. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the grades of computers in the first row. The third line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le 10^9$$$) — the grades of computers in the second row. It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.

For each test case, print a single integer — the minimum total cost to make a fault-tolerant network.

NoteIn the first test case, it's optimal to connect four pairs of computers:   computer $$$1$$$ from the first row with computer $$$2$$$ from the second row: cost $$$|1 - 4| = 3$$$;  computer $$$3$$$ from the first row with computer $$$2$$$ from the second row: cost $$$|1 - 4| = 3$$$;  computer $$$2$$$ from the first row with computer $$$1$$$ from the second row: cost $$$|10 - 20| = 10$$$;  computer $$$2$$$ from the first row with computer $$$3$$$ from the second row: cost $$$|10 - 25| = 15$$$;  In total, $$$3 + 3 + 10 + 15 = 31$$$.In the second test case, it's optimal to connect $$$1$$$ from the first row with $$$1$$$ from the second row, and $$$4$$$ from the first row with $$$4$$$ from the second row.

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

let inputString = "";
let currentLine = 0;

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

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

  main();
});

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

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

  for (let i = 0; i < testCases; i++) {
    let n = Number(readline());
    let a = readline().split(" ");
    let b = readline().split(" ");

    let aFirstMin = Math.abs(a[0] - b[0]);
    let aLastMin = Math.abs(a[n - 1] - b[0]);

    let bFirstMin = Math.abs(a[0] - b[0]);
    // let bFirstMinIndex = 0;

    let bLastMin = Math.abs(a[0] - b[n - 1]);
    // let bLastMinIndex = 0;

    for (let i = 1; i < n; i++) {
      if (Math.abs(b[i] - a[0]) < aFirstMin) {
        aFirstMin = Math.abs(b[i] - a[0]);
      }
      if (Math.abs(b[i] - a[n - 1]) < aLastMin) {
        aLastMin = Math.abs(b[i] - a[n - 1]);
      }
    }

    for (let i = 1; i < n; i++) {
      if (Math.abs(a[i] - b[0]) < bFirstMin) {
        bFirstMin = Math.abs(a[i] - b[0]);
        // bFirstMinIndex = i;
      }
      if (Math.abs(a[i] - b[n - 1]) < bLastMin) {
        bLastMin = Math.abs(a[i] - b[n - 1]);
        // bLastMinIndex = i;
      }
    }

    let sum1 = bFirstMin + bLastMin + aFirstMin + aLastMin;
    // if (bFirstMinIndex !== 0 && bLastMinIndex !== 0) {
    //   sum1 += aFirstMin;
    // }

    // if (bFirstMinIndex !== n - 1 && bLastMinIndex !== n - 1) {
    //   sum1 += aLastMin;
    // }

    let sum2 = Math.abs(a[0] - b[0]) + Math.abs(a[n - 1] - b[n - 1]);

    let sum3 = Math.abs(a[0] - b[n - 1]) + Math.abs(a[n - 1] - b[0]);

    let sum4 = Math.abs(a[0] - b[0]) + aLastMin + bLastMin;

    // console.log(Math.abs(a[0] - b[0]), aLastMin, bLastMin);

    let sum5 = Math.abs(a[0] - b[n - 1]) + bFirstMin + aLastMin;

    let sum6 = Math.abs(a[n - 1] - b[n - 1]) + aFirstMin + bFirstMin;

    let sum7 = Math.abs(a[n - 1] - b[0]) + aFirstMin + bLastMin;

    // console.log(sum1, sum2, sum3, sum4, sum5, sum6, sum7);

    console.log(Math.min(sum1, sum2, sum3, sum4, sum5, sum6, sum7));
  }
}

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

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

var deb; const log = msg => (typeof deb != "undefined") && console.log("DEBUG", msg);

function costOf(a, p, b) {
	let mn = 1e9;
	for (let i = 0; i < b.length; i++) {
		mn = Math.min(mn, Math.abs(a[p] - b[i]));
	}
	return mn;
}

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

		let ans = 1e18;
		for (let mask = 0; mask < 4; mask++) {
			const a = A.slice();
			const b = B.slice();
			if (mask & (1 << 0)) a.reverse();
			if (mask & (1 << 1)) b.reverse();

			let cost = 0;;
			for (let i = 0; i < n; i += n-1) {
				cost += Math.min(Math.abs(a[i] - b[i]), costOf(a, i, b) + costOf(b, i, a));
			}
			ans = Math.min(ans, cost);
		}

		console.log(ans);
	}
}

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

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

var deb; const log = msg => (typeof deb != "undefined") && console.log("DEBUG", msg);

function costOf(a, p, b) {
	let mn = 1e9;
	for (let i = 0; i < b.length; i++) {
		mn = Math.min(mn, Math.abs(a[p] - b[i]));
	}
	return mn;
}

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

		let ans = 1e18;
		for (let mask = 0; mask < 4; mask++) {
			const a = A.slice();
			const b = B.slice();
			if (mask & (1 << 0)) a.reverse();
			if (mask & (1 << 1)) b.reverse();

			ans = Math.min(ans, Math.abs(a[0] - b[0]) + Math.abs(a[n-1] - b[n-1]));
			ans = Math.min(ans, costOf(a, 0, b) + costOf(b, 0, a) + Math.abs(a[n-1] - b[n-1]));
			ans = Math.min(ans, costOf(a, 0, b) + costOf(b, 0, a) + costOf(a, n-1, b) + costOf(b, n-1, a));
		}

		console.log(ans);
	}
}

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

module.exports = E;

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

const EPS = 1e-6;

const calc = (a, b, n)=>{
  let getBest = (arr, num)=>{
    let min = Infinity;
    for (let _a of arr)
      min = Math.min(min, Math.abs(_a-num));
    return min;
  };
  let bestResult = Infinity
  // 0 any, 1 with first, 2 with second
  for (let aFirst=0; aFirst<3; aFirst++){
    let result = 0;
    let skipBfirst
    let skipBlast
    if (aFirst==0){
      result += getBest(b, a[0]);
    }else if (aFirst==1){
      result += Math.abs(a[0]-b[0]);
      skipBfirst = true
    }else{
      result += Math.abs(a[0]-b[n-1]);
      skipBlast = true
    }
    for (let aLast=0; aLast<3; aLast++){
      let skipBfirst2 = skipBfirst
      let skipBlast2 = skipBlast
      let result2 = result;
      if (aLast==0){
        result2 += getBest(b, a[n-1]);
      }else if (aLast==1){
        result2 += Math.abs(a[n-1]-b[0]);
        skipBfirst2 = true
      }else{
        result2 += Math.abs(a[n-1]-b[n-1]);
        skipBlast2 = true
      }
      if (!skipBfirst2)
        result2 += getBest(a, b[0])
      if (!skipBlast2)
        result2 += getBest(a, b[n-1])
      //console.log({skipBfirst, skipBlast, aFirst, aLast}, result2)
      bestResult = Math.min(bestResult, result2)
    }
  }
  return bestResult;
};

function main() {
    let T = +readline();
    while (T--){
      let n = +readline();
      let a = ra();
      let b = ra();
      print(calc(a, b, n));
    }
}

E.calc = calc;

/*
INPUT
2
3
1 10 1
20 4 25
4
1 1 1 1
1000000000 1000000000 1000000000 1000000000
*/

var t = Number(readline());

for(var i = 0; i < t; i++) {
    readline();
    var a = readline().split(' ').map(Number);
    var b = readline().split(' ').map(Number);

    var al = a[0];
    var ar = a[a.length - 1];
    var bl = b[0];
    var br = b[b.length - 1];

    var alm = Infinity, blm = Infinity, arm = Infinity, brm = Infinity;

    for(var j = 0; j < a.length; j++) {
        alm = Math.min(alm, Math.abs(b[j] - al));
        blm = Math.min(blm, Math.abs(a[j] - bl));
        arm = Math.min(arm, Math.abs(b[j] - ar));
        brm = Math.min(brm, Math.abs(a[j] - br));
    }

    var result = Math.min(
        Math.min(Math.abs(al - bl), blm + alm) + Math.min(Math.abs(ar - br), arm + brm),
        Math.min(Math.abs(al - br), alm + brm) + Math.min(Math.abs(ar - bl), arm + blm)
    );

    print(result);
}

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

let inputString = "";
let currentLine = 0;

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

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

  main();
});

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

function main() {
  const testCases = Number(readline());

  if (testCases > 2) {
    for (let i = 0; i < testCases; i++) {
      let n = Number(readline());
      let a = readline().split(" ");
      let b = readline().split(" ");
      if (i === 324) {
        console.log(n);
        console.log(a);
        console.log(b);
      }
    }
  } else {
    for (let i = 0; i < testCases; i++) {
      let n = Number(readline());
      let a = readline().split(" ");
      let b = readline().split(" ");

      let aFirstMin = Math.abs(a[0] - b[1]);
      let aLastMin = Math.abs(a[n - 1] - b[1]);

      let bFirstMin = Math.abs(a[1] - b[0]);
      // let bFirstMinIndex = 0;

      let bLastMin = Math.abs(a[1] - b[n - 1]);
      // let bLastMinIndex = 0;

      for (let i = 1; i < n; i++) {
        if (Math.abs(b[i] - a[0]) < aFirstMin) {
          aFirstMin = Math.abs(b[i] - a[0]);
        }
        if (Math.abs(b[i] - a[n - 1]) < aLastMin) {
          aLastMin = Math.abs(b[i] - a[n - 1]);
        }
      }

      for (let i = 1; i < n; i++) {
        if (Math.abs(a[i] - b[0]) < bFirstMin) {
          bFirstMin = Math.abs(a[i] - b[0]);
          // bFirstMinIndex = i;
        }
        if (Math.abs(a[i] - b[n - 1]) < bLastMin) {
          bLastMin = Math.abs(a[i] - b[n - 1]);
          // bLastMinIndex = i;
        }
      }

      let sum1 = bFirstMin + bLastMin + aFirstMin + aLastMin;
      // if (bFirstMinIndex !== 0 && bLastMinIndex !== 0) {
      //   sum1 += aFirstMin;
      // }

      // if (bFirstMinIndex !== n - 1 && bLastMinIndex !== n - 1) {
      //   sum1 += aLastMin;
      // }

      let sum2 = Math.abs(a[0] - b[0]) + Math.abs(a[n - 1] - b[n - 1]);

      let sum3 = Math.abs(a[0] - b[n - 1]) + Math.abs(a[n - 1] - b[0]);

      let sum4 = Math.abs(a[0] - b[0]) + aLastMin + bLastMin;

      // console.log(Math.abs(a[0] - b[0]), aLastMin, bLastMin);

      let sum5 = Math.abs(a[0] - b[n - 1]) + bFirstMin + aLastMin;

      let sum6 = Math.abs(a[n - 1] - b[n - 1]) + aFirstMin + bFirstMin;

      let sum7 = Math.abs(a[n - 1] - b[0]) + aFirstMin + bLastMin;

      // console.log(Math.abs(a[n - 1] - b[0]), aFirstMin, bLastMin);

      // console.log(sum1, sum2, sum3, sum4, sum5, sum6, sum7);

      console.log(Math.min(sum1, sum2, sum3, sum4, sum5, sum6, sum7));
    }
  }
}

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

let inputString = "";
let currentLine = 0;

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

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

  main();
});

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

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

  for (let i = 0; i < testCases; i++) {
    let n = Number(readline());
    let a = readline().split(" ");
    let b = readline().split(" ");

    let aFirstMin = Math.abs(a[0] - b[0]);
    let aLastMin = Math.abs(a[n - 1] - b[0]);

    let bFirstMin = Math.abs(a[1] - b[0]);
    // let bFirstMinIndex = 0;

    let bLastMin = Math.abs(a[1] - b[n - 1]);
    // let bLastMinIndex = 0;

    for (let i = 1; i < n; i++) {
      if (Math.abs(b[i] - a[0]) < aFirstMin) {
        aFirstMin = Math.abs(b[i] - a[0]);
      }
      if (Math.abs(b[i] - a[n - 1]) < aLastMin) {
        aLastMin = Math.abs(b[i] - a[n - 1]);
      }
    }

    for (let i = 1; i < n; i++) {
      if (Math.abs(a[i] - b[0]) < bFirstMin) {
        bFirstMin = Math.abs(a[i] - b[0]);
        // bFirstMinIndex = i;
      }
      if (Math.abs(a[i] - b[n - 1]) < bLastMin) {
        bLastMin = Math.abs(a[i] - b[n - 1]);
        // bLastMinIndex = i;
      }
    }

    let sum1 = bFirstMin + bLastMin + aFirstMin + aLastMin;
    // if (bFirstMinIndex !== 0 && bLastMinIndex !== 0) {
    //   sum1 += aFirstMin;
    // }

    // if (bFirstMinIndex !== n - 1 && bLastMinIndex !== n - 1) {
    //   sum1 += aLastMin;
    // }

    let sum2 = Math.abs(a[0] - b[0]) + Math.abs(a[n - 1] - b[n - 1]);

    let sum3 = Math.abs(a[0] - b[n - 1]) + Math.abs(a[n - 1] - b[0]);

    let sum4 = Math.abs(a[0] - b[0]) + aLastMin + bLastMin;

    // console.log(Math.abs(a[0] - b[0]), aLastMin, bLastMin);

    let sum5 = Math.abs(a[0] - b[n - 1]) + bFirstMin + aLastMin;

    let sum6 = Math.abs(a[n - 1] - b[n - 1]) + aFirstMin + bFirstMin;

    let sum7 = Math.abs(a[n - 1] - b[0]) + aFirstMin + bLastMin;

    // console.log(sum1, sum2, sum3, sum4, sum5, sum6, sum7);

    console.log(Math.min(sum1, sum2, sum3, sum4, sum5, sum6, sum7));
  }
}

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

let inputString = "";
let currentLine = 0;

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

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

  main();
});

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

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

  for (let i = 0; i < testCases; i++) {
    let n = Number(readline());
    let a = readline().split(" ");
    let b = readline().split(" ");

    let aFirstMin = Math.abs(a[0] - b[0]);
    let aLastMin = Math.abs(a[n - 1] - b[0]);

    let bFirstMin = Math.abs(a[1] - b[0]);
    // let bFirstMinIndex = 0;

    let bLastMin = Math.abs(a[1] - b[n - 1]);
    // let bLastMinIndex = 0;

    for (let i = 1; i < n; i++) {
      if (Math.abs(b[i] - a[0]) < aFirstMin) {
        aFirstMin = Math.abs(b[i] - a[0]);
      }
      if (Math.abs(b[i] - a[n - 1]) < aLastMin) {
        aLastMin = Math.abs(b[i] - a[n - 1]);
      }
    }

    for (let i = 1; i < n; i++) {
      if (Math.abs(a[i] - b[0]) < bFirstMin) {
        bFirstMin = Math.abs(a[i] - b[0]);
        // bFirstMinIndex = i;
      }
      if (Math.abs(a[i] - b[n - 1]) < bLastMin) {
        bLastMin = Math.abs(a[i] - b[n - 1]);
        // bLastMinIndex = i;
      }
    }

    let sum1 = bFirstMin + bLastMin + aFirstMin + aLastMin;
    // if (bFirstMinIndex !== 0 && bLastMinIndex !== 0) {
    //   sum1 += aFirstMin;
    // }

    // if (bFirstMinIndex !== n - 1 && bLastMinIndex !== n - 1) {
    //   sum1 += aLastMin;
    // }

    let sum2 = Math.abs(a[0] - b[0]) + Math.abs(a[n - 1] - b[n - 1]);

    let sum3 = Math.abs(a[0] - b[n - 1]) + Math.abs(a[n - 1] - b[0]);

    let sum4 = Math.abs(a[0] - b[0]) + aLastMin + bLastMin;

    console.log(Math.abs(a[0] - b[0]), aLastMin, bLastMin);

    let sum5 = Math.abs(a[0] - b[n - 1]) + bFirstMin + aLastMin;

    let sum6 = Math.abs(a[n - 1] - b[n - 1]) + aFirstMin + bFirstMin;

    let sum7 = Math.abs(a[n - 1] - b[0]) + aFirstMin + bLastMin;

    // console.log(sum1, sum2, sum3, sum4, sum5, sum6, sum7);

    console.log(Math.min(sum1, sum2, sum3, sum4, sum5, sum6, sum7));
  }
}

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

let inputString = "";
let currentLine = 0;

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

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

  main();
});

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

function main() {
  const testCases = Number(readline());

  if (testCases > 2) {
    for (let i = 0; i < testCases; i++) {
      let n = Number(readline());
      let a = readline().split(" ");
      let b = readline().split(" ");
      if (i === 261) {
        console.log(n);
        console.log(a);
        console.log(b);
      }
    }
  } else {
    for (let i = 0; i < testCases; i++) {
      let n = Number(readline());
      let a = readline().split(" ");
      let b = readline().split(" ");

      let aFirstMin = Math.abs(a[0] - b[1]);
      let aLastMin = Math.abs(a[n - 1] - b[1]);

      let bFirstMin = Math.abs(a[1] - b[0]);
      // let bFirstMinIndex = 0;

      let bLastMin = Math.abs(a[1] - b[n - 1]);
      // let bLastMinIndex = 0;

      for (let i = 1; i < n; i++) {
        if (Math.abs(b[i] - a[0]) < aFirstMin) {
          aFirstMin = Math.abs(b[i] - a[0]);
        }
        if (Math.abs(b[i] - a[n - 1]) < aLastMin) {
          aLastMin = Math.abs(b[i] - a[n - 1]);
        }
      }

      for (let i = 1; i < n; i++) {
        if (Math.abs(a[i] - b[0]) < bFirstMin) {
          bFirstMin = Math.abs(a[i] - b[0]);
          // bFirstMinIndex = i;
        }
        if (Math.abs(a[i] - b[n - 1]) < bLastMin) {
          bLastMin = Math.abs(a[i] - b[n - 1]);
          // bLastMinIndex = i;
        }
      }

      let sum1 = bFirstMin + bLastMin + aFirstMin + aLastMin;
      // if (bFirstMinIndex !== 0 && bLastMinIndex !== 0) {
      //   sum1 += aFirstMin;
      // }

      // if (bFirstMinIndex !== n - 1 && bLastMinIndex !== n - 1) {
      //   sum1 += aLastMin;
      // }

      let sum2 = Math.abs(a[0] - b[0]) + Math.abs(a[n - 1] - b[n - 1]);

      let sum3 = Math.abs(a[0] - b[n - 1]) + Math.abs(a[n - 1] - b[0]);

      let sum4 = Math.abs(a[0] - b[0]) + aLastMin + bLastMin;

      let sum5 = Math.abs(a[0] - b[n - 1]) + bFirstMin + aLastMin;

      let sum6 = Math.abs(a[n - 1] - b[n - 1]) + aFirstMin + bFirstMin;

      let sum7 = Math.abs(a[n - 1] - b[0]) + aFirstMin + bLastMin;

      // console.log(Math.abs(a[n - 1] - b[0]), aFirstMin, bLastMin);

      // console.log(sum1, sum2, sum3, sum4, sum5, sum6, sum7);

      console.log(Math.min(sum1, sum2, sum3, sum4, sum5, sum6, sum7));
    }
  }
}

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

let inputString = "";
let currentLine = 0;

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

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

  main();
});

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

function main() {
  const testCases = Number(readline());

  if (testCases > 2) {
    for (let i = 0; i < testCases; i++) {
      let n = Number(readline());
      let a = readline().split(" ");
      let b = readline().split(" ");
      if (i === 262) {
        console.log(n);
        console.log(a);
        console.log(b);
      }
    }
  } else {
    for (let i = 0; i < testCases; i++) {
      let n = Number(readline());
      let a = readline().split(" ");
      let b = readline().split(" ");

      let aFirstMin = Math.abs(a[0] - b[1]);
      let aLastMin = Math.abs(a[n - 1] - b[1]);

      let bFirstMin = Math.abs(a[1] - b[0]);
      // let bFirstMinIndex = 0;

      let bLastMin = Math.abs(a[1] - b[n - 1]);
      // let bLastMinIndex = 0;

      for (let i = 1; i < n; i++) {
        if (Math.abs(b[i] - a[0]) < aFirstMin) {
          aFirstMin = Math.abs(b[i] - a[0]);
        }
        if (Math.abs(b[i] - a[n - 1]) < aLastMin) {
          aLastMin = Math.abs(b[i] - a[n - 1]);
        }
      }

      for (let i = 1; i < n; i++) {
        if (Math.abs(a[i] - b[0]) < bFirstMin) {
          bFirstMin = Math.abs(a[i] - b[0]);
          // bFirstMinIndex = i;
        }
        if (Math.abs(a[i] - b[n - 1]) < bLastMin) {
          bLastMin = Math.abs(a[i] - b[n - 1]);
          // bLastMinIndex = i;
        }
      }

      let sum1 = bFirstMin + bLastMin + aFirstMin + aLastMin;
      // if (bFirstMinIndex !== 0 && bLastMinIndex !== 0) {
      //   sum1 += aFirstMin;
      // }

      // if (bFirstMinIndex !== n - 1 && bLastMinIndex !== n - 1) {
      //   sum1 += aLastMin;
      // }

      let sum2 = Math.abs(a[0] - b[0]) + Math.abs(a[n - 1] - b[n - 1]);

      let sum3 = Math.abs(a[0] - b[n - 1]) + Math.abs(a[n - 1] - b[0]);

      let sum4 = Math.abs(a[0] - b[0]) + aLastMin + bLastMin;

      let sum5 = Math.abs(a[0] - b[n - 1]) + bFirstMin + aLastMin;

      let sum6 = Math.abs(a[n - 1] - b[n - 1]) + aFirstMin + bFirstMin;

      let sum7 = Math.abs(a[n - 1] - b[0]) + aFirstMin + bLastMin;

      // console.log(Math.abs(a[n - 1] - b[0]), aFirstMin, bLastMin);

      // console.log(sum1, sum2, sum3, sum4, sum5, sum6, sum7);

      console.log(Math.min(sum1, sum2, sum3, sum4, sum5, sum6, sum7));
    }
  }
}

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

let inputString = "";
let currentLine = 0;

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

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

  main();
});

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

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

  for (let i = 0; i < testCases; i++) {
    let n = Number(readline());
    let a = readline().split(" ");
    let b = readline().split(" ");

    let aFirstMin = Math.abs(a[0] - b[1]);
    let aLastMin = Math.abs(a[n - 1] - b[1]);

    let bFirstMin = Math.abs(a[1] - b[0]);
    // let bFirstMinIndex = 0;

    let bLastMin = Math.abs(a[1] - b[n - 1]);
    // let bLastMinIndex = 0;

    for (let i = 1; i < n; i++) {
      if (Math.abs(b[i] - a[0]) < aFirstMin) {
        aFirstMin = Math.abs(b[i] - a[0]);
      }
      if (Math.abs(b[i] - a[n - 1]) < aLastMin) {
        aLastMin = Math.abs(b[i] - a[n - 1]);
      }
    }

    for (let i = 1; i < n; i++) {
      if (Math.abs(a[i] - b[0]) < bFirstMin) {
        bFirstMin = Math.abs(a[i] - b[0]);
        // bFirstMinIndex = i;
      }
      if (Math.abs(a[i] - b[n - 1]) < bLastMin) {
        bLastMin = Math.abs(a[i] - b[n - 1]);
        // bLastMinIndex = i;
      }
    }

    let sum1 = bFirstMin + bLastMin + aFirstMin + aLastMin;
    // if (bFirstMinIndex !== 0 && bLastMinIndex !== 0) {
    //   sum1 += aFirstMin;
    // }

    // if (bFirstMinIndex !== n - 1 && bLastMinIndex !== n - 1) {
    //   sum1 += aLastMin;
    // }

    let sum2 = Math.abs(a[0] - b[0]) + Math.abs(a[n - 1] - b[n - 1]);

    let sum3 = Math.abs(a[0] - b[n - 1]) + Math.abs(a[n - 1] - b[0]);

    let sum4 = Math.abs(a[0] - b[0]) + aLastMin + bLastMin;

    let sum5 = Math.abs(a[0] - b[n - 1]) + bFirstMin + aLastMin;

    let sum6 = Math.abs(a[n - 1] - b[n - 1]) + aFirstMin + bFirstMin;

    let sum7 = Math.abs(a[n - 1] - b[0]) + aFirstMin + bLastMin;

    // console.log(sum1, sum2, sum3, sum4, sum5, sum6, sum7);

    console.log(Math.min(sum1, sum2, sum3, sum4, sum5, sum6, sum7));
  }
}

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

let inputString = "";
let currentLine = 0;

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

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

  main();
});

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

function main() {
  const testCases = Number(readline());

  if (testCases > 2) {
    for (let i = 0; i < testCases; i++) {
      let n = Number(readline());
      let a = readline().split(" ");
      let b = readline().split(" ");
      if (i === 82) {
        console.log(n);
        console.log(a);
        console.log(b);
      }
    }
  } else {
    for (let i = 0; i < testCases; i++) {
      let n = Number(readline());
      let a = readline().split(" ");
      let b = readline().split(" ");

      let aFirstMin = Math.abs(a[0] - b[1]);
      let aLastMin = Math.abs(a[n - 1] - b[1]);

      let bFirstMin = Math.abs(a[1] - b[0]);
      // let bFirstMinIndex = 0;

      let bLastMin = Math.abs(a[1] - b[n - 1]);
      // let bLastMinIndex = 0;

      for (let i = 1; i < n - 1; i++) {
        if (Math.abs(b[i] - a[0]) < aFirstMin) {
          aFirstMin = Math.abs(b[i] - a[0]);
        }
        if (Math.abs(b[i] - a[n - 1]) < aLastMin) {
          aLastMin = Math.abs(b[i] - a[n - 1]);
        }
      }

      for (let i = 1; i < n - 1; i++) {
        if (Math.abs(a[i] - b[0]) < bFirstMin) {
          bFirstMin = Math.abs(a[i] - b[0]);
          // bFirstMinIndex = i;
        }
        if (Math.abs(a[i] - b[n - 1]) < bLastMin) {
          bLastMin = Math.abs(a[i] - b[n - 1]);
          // bLastMinIndex = i;
        }
      }

      let sum1 = bFirstMin + bLastMin + aFirstMin + aLastMin;
      // if (bFirstMinIndex !== 0 && bLastMinIndex !== 0) {
      //   sum1 += aFirstMin;
      // }

      // if (bFirstMinIndex !== n - 1 && bLastMinIndex !== n - 1) {
      //   sum1 += aLastMin;
      // }

      let sum2 = Math.abs(a[0] - b[0]) + Math.abs(a[n - 1] - b[n - 1]);

      let sum3 = Math.abs(a[0] - b[n - 1]) + Math.abs(a[n - 1] - b[0]);

      let sum4 = Math.abs(a[0] - b[0]) + aLastMin + bLastMin;

      let sum5 = Math.abs(a[0] - b[n - 1]) + bFirstMin + aLastMin;

      let sum6 = Math.abs(a[n - 1] - b[n - 1]) + aFirstMin + bFirstMin;

      let sum7 = Math.abs(a[0] - b[n - 1]) + aLastMin + bFirstMin;

      // console.log(sum1, sum2, sum3, sum4, sum5, sum6, sum7);

      console.log(Math.min(sum1, sum2, sum3, sum4, sum5, sum6, sum7));
    }
  }
}

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

let inputString = "";
let currentLine = 0;

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

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

  main();
});

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

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

  if (testCases > 2) {
    for (let i = 0; i < testCases; i++) {
      let n = Number(readline());
      let a = readline().split(" ");
      let b = readline().split(" ");
      if (i === 82) {
        console.log(n);
        console.log(a);
        console.log(b);
      }
    }
  } else {
    for (let i = 0; i < testCases; i++) {
      let n = Number(readline());
      let a = readline().split(" ");
      let b = readline().split(" ");

      let aFirstMin = Math.abs(a[0] - b[1]);
      let aLastMin = Math.abs(a[n - 1] - b[1]);

      let bFirstMin = Math.abs(a[1] - b[0]);
      // let bFirstMinIndex = 0;

      let bLastMin = Math.abs(a[1] - b[n - 1]);
      // let bLastMinIndex = 0;

      for (let i = 1; i < n - 1; i++) {
        if (Math.abs(b[i] - a[0]) < aFirstMin) {
          aFirstMin = Math.abs(b[i] - a[0]);
        }
        if (Math.abs(b[i] - a[n - 1]) < aLastMin) {
          aLastMin = Math.abs(b[i] - a[n - 1]);
        }
      }

      for (let i = 1; i < n - 1; i++) {
        if (Math.abs(a[i] - b[0]) < bFirstMin) {
          bFirstMin = Math.abs(a[i] - b[0]);
          // bFirstMinIndex = i;
        }
        if (Math.abs(a[i] - b[n - 1]) < bLastMin) {
          bLastMin = Math.abs(a[i] - b[n - 1]);
          // bLastMinIndex = i;
        }
      }

      let sum1 = bFirstMin + bLastMin + aFirstMin + aLastMin;
      // if (bFirstMinIndex !== 0 && bLastMinIndex !== 0) {
      //   sum1 += aFirstMin;
      // }

      // if (bFirstMinIndex !== n - 1 && bLastMinIndex !== n - 1) {
      //   sum1 += aLastMin;
      // }

      let sum2 = Math.abs(a[0] - b[0]) + Math.abs(a[n - 1] - b[n - 1]);

      let sum3 = Math.abs(a[0] - b[n - 1]) + Math.abs(a[n - 1] - b[0]);

      let sum4 = Math.abs(a[0] - b[0]) + aLastMin + bLastMin;

      let sum5 = Math.abs(a[0] - b[n - 1]) + bFirstMin + aLastMin;

      let sum6 = Math.abs(a[n - 1] - b[n - 1]) + aFirstMin + bFirstMin;

      let sum7 = Math.abs(a[0] - b[n - 1]) + aLastMin + bFirstMin;

      console.log(sum1, sum2, sum3, sum4, sum5, sum6, sum7);

      console.log(Math.min(sum1, sum2, sum3, sum4, sum5, sum6, sum7));
    }
  }
}

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

let inputString = "";
let currentLine = 0;

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

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

  main();
});

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

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

  for (let i = 0; i < testCases; i++) {
    let n = Number(readline());
    let a = readline().split(" ");
    let b = readline().split(" ");

    let aFirstMin = Math.abs(a[0] - b[0]);
    let aLastMin = Math.abs(a[n - 1] - b[0]);

    let bFirstMin = Math.abs(a[0] - b[0]);
    let bFirstMinIndex = 0;

    let bLastMin = Math.abs(a[0] - b[0]);
    let bLastMinIndex = 0;

    // loop over a
    for (let i = 1; i < n - 1; i++) {
      if (Math.abs(a[i] - b[0]) < bFirstMin) {
        bFirstMin = Math.abs(a[i] - b[0]);
        bFirstMinIndex = i;
      }
      if (Math.abs(a[i] - b[n - 1]) <= bLastMin) {
        bLastMin = Math.abs(a[i] - b[n - 1]);
        bLastMinIndex = i;
      }
    }

    if (
      (bFirstMinIndex === 0 && bLastMinIndex === n - 1) ||
      (bFirstMinIndex === n - 1 && bLastMinIndex === 0)
    ) {
      console.log(bFirstMin + bLastMin);
    } else {
      for (let i = 1; i < n - 1; i++) {
        if (Math.abs(b[i] - a[0]) < aFirstMin) {
          aFirstMin = Math.abs(b[i] - a[0]);
        }
        if (Math.abs(b[i] - a[n - 1]) <= aLastMin) {
          aLastMin = Math.abs(b[i] - a[n - 1]);
        }
      }

      let sum1 = bFirstMin + bLastMin + aFirstMin + aLastMin;
      // if (bFirstMinIndex !== 0 && bLastMinIndex !== 0) {
      //   sum1 += aFirstMin;
      // }

      // if (bFirstMinIndex !== n - 1 && bLastMinIndex !== n - 1) {
      //   sum1 += aLastMin;
      // }

      let sum2 = Math.abs(a[0] - b[0]) + Math.abs(a[n - 1] - b[n - 1]);

      let sum3 = Math.abs(a[0] - b[n - 1]) + Math.abs(a[n - 1] - b[0]);

      let sum4 = Math.abs(a[0] - b[0]) + aLastMin + bLastMin;

      let sum5 = Math.abs(a[0] - b[n - 1]) + bFirstMin + aLastMin;

      let sum6 = Math.abs(a[n - 1] - b[n - 1]) + aFirstMin + bFirstMin;

      let sum7 = Math.abs(a[0] - b[n - 1]) + aFirstMin + bLastMin;

      console.log(Math.min(sum1, sum2, sum3, sum4, sum5, sum6, sum7));
    }
  }
}

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

let inputString = "";
let currentLine = 0;

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

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

  main();
});

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

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

  for (let i = 0; i < testCases; i++) {
    let n = Number(readline());
    let a = readline().split(" ");
    let b = readline().split(" ");

    let aFirstMin = Math.abs(a[0] - b[1]);
    let aLastMin = Math.abs(a[n - 1] - b[1]);

    let bFirstMin = Math.abs(a[1] - b[0]);
    // let bFirstMinIndex = 0;

    let bLastMin = Math.abs(a[1] - b[n - 1]);
    // let bLastMinIndex = 0;

    for (let i = 1; i < n - 1; i++) {
      if (Math.abs(b[i] - a[0]) < aFirstMin) {
        aFirstMin = Math.abs(b[i] - a[0]);
      }
      if (Math.abs(b[i] - a[n - 1]) < aLastMin) {
        aLastMin = Math.abs(b[i] - a[n - 1]);
      }
    }

    for (let i = 1; i < n - 1; i++) {
      if (Math.abs(a[i] - b[0]) < bFirstMin) {
        bFirstMin = Math.abs(a[i] - b[0]);
        // bFirstMinIndex = i;
      }
      if (Math.abs(a[i] - b[n - 1]) < bLastMin) {
        bLastMin = Math.abs(a[i] - b[n - 1]);
        // bLastMinIndex = i;
      }
    }

    let sum1 = bFirstMin + bLastMin + aFirstMin + aLastMin;
    // if (bFirstMinIndex !== 0 && bLastMinIndex !== 0) {
    //   sum1 += aFirstMin;
    // }

    // if (bFirstMinIndex !== n - 1 && bLastMinIndex !== n - 1) {
    //   sum1 += aLastMin;
    // }

    let sum2 = Math.abs(a[0] - b[0]) + Math.abs(a[n - 1] - b[n - 1]);

    let sum3 = Math.abs(a[0] - b[n - 1]) + Math.abs(a[n - 1] - b[0]);

    let sum4 = Math.abs(a[0] - b[0]) + aLastMin + bLastMin;

    let sum5 = Math.abs(a[0] - b[n - 1]) + bFirstMin + aLastMin;

    let sum6 = Math.abs(a[n - 1] - b[n - 1]) + aFirstMin + bFirstMin;

    let sum7 = Math.abs(a[0] - b[n - 1]) + aLastMin + bFirstMin;

    console.log(Math.min(sum1, sum2, sum3, sum4, sum5, sum6, sum7));
  }
}

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

let inputString = "";
let currentLine = 0;

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

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

  main();
});

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

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

  for (let i = 0; i < testCases; i++) {
    let n = Number(readline());
    let a = readline().split(" ");
    let b = readline().split(" ");

    let aFirstMin = Math.abs(a[0] - b[0]);
    let aLastMin = Math.abs(a[0] - b[0]);

    let bFirstMin = Math.abs(a[0] - b[0]);
    let bFirstMinIndex = 0;

    let bLastMin = Math.abs(a[0] - b[0]);
    let bLastMinIndex = 0;

    // loop over a
    for (let i = 1; i < n; i++) {
      if (Math.abs(a[i] - b[0]) < bFirstMin) {
        bFirstMin = Math.abs(a[i] - b[0]);
        bFirstMinIndex = i;
      }
      if (Math.abs(a[i] - b[n - 1]) <= bLastMin) {
        bLastMin = Math.abs(a[i] - b[n - 1]);
        bLastMinIndex = i;
      }
    }

    if (
      (bFirstMinIndex === 0 && bLastMinIndex === n - 1) ||
      (bFirstMinIndex === n - 1 && bLastMinIndex === 0)
    ) {
      aFirstMin = 0;
      aLastMin = 0;
      console.log(bFirstMin + bLastMin);
    } else {
      for (let i = 1; i < n; i++) {
        if (Math.abs(b[i] - a[0]) < aFirstMin) {
          aFirstMin = Math.abs(b[i] - a[0]);
        }
        if (Math.abs(b[i] - a[n - 1]) <= aLastMin) {
          aLastMin = Math.abs(b[i] - a[n - 1]);
        }
      }

      let sum1 = bFirstMin + bLastMin;
      if (bFirstMinIndex !== 0 && bLastMinIndex !== 0) {
        sum1 += aFirstMin;
      }

      if (bFirstMinIndex !== n - 1 && bLastMinIndex !== n - 1) {
        sum1 += aLastMin;
      }

      let sum2 = Math.abs(a[0] - b[0]) + Math.abs(a[n - 1] - b[n - 1]);

      let sum3 = Math.abs(a[0] - b[n - 1]) + Math.abs(a[n - 1] - b[0]);

      let sum4 = Math.abs(a[0] - b[0]) + aLastMin + bLastMin;

      let sum5 = Math.abs(a[0] - b[n - 1]) + bFirstMin + aLastMin;

      let sum6 = Math.abs(a[n - 1] - b[n - 1]) + aFirstMin + bFirstMin;

      let sum7 = Math.abs(a[0] - b[n - 1]) + aFirstMin + bLastMin;

      console.log(Math.min(sum1, sum2, sum3, sum4, sum5, sum6, sum7));
    }
  }
}

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

let inputString = "";
let currentLine = 0;

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

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

  main();
});

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

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

  for (let i = 0; i < testCases; i++) {
    let n = Number(readline());
    let a = readline().split(" ");
    let b = readline().split(" ");

    let aFirstMin = Math.abs(a[0] - b[0]);
    let aLastMin = Math.abs(a[0] - b[0]);

    let bFirstMin = Math.abs(a[0] - b[0]);
    let bFirstMinIndex = 0;

    let bLastMin = Math.abs(a[0] - b[0]);
    let bLastMinIndex = 0;

    // loop over a
    for (let i = 1; i < n; i++) {
      if (Math.abs(a[i] - b[0]) < bFirstMin) {
        bFirstMin = Math.abs(a[i] - b[0]);
        bFirstMinIndex = i;
      }
      if (Math.abs(a[i] - b[n - 1]) <= bLastMin) {
        bLastMin = Math.abs(a[i] - b[n - 1]);
        bLastMinIndex = i;
      }
    }

    if (
      (bFirstMinIndex === 0 && bLastMinIndex === n - 1) ||
      (bFirstMinIndex === n - 1 && bLastMinIndex === 0)
    ) {
      aFirstMin = 0;
      aLastMin = 0;
      console.log(bFirstMin + bLastMin);
    } else {
      for (let i = 1; i < n; i++) {
        if (Math.abs(b[i] - a[0]) < aFirstMin) {
          aFirstMin = Math.abs(b[i] - a[0]);
        }
        if (Math.abs(b[i] - a[n - 1]) <= aLastMin) {
          aLastMin = Math.abs(b[i] - a[n - 1]);
        }
      }

      let sum = bFirstMin + bLastMin;
      if (bFirstMinIndex !== 0 && bLastMinIndex !== 0) {
        sum += aFirstMin;
      }

      if (bFirstMinIndex !== n - 1 && bLastMinIndex !== n - 1) {
        sum += aLastMin;
      }

      // square
      let sum2 = Math.abs(a[0] - b[0]) + Math.abs(a[n - 1] - b[n - 1]);

      // cross
      let sum3 = Math.abs(a[0] - b[n - 1]) + Math.abs(a[n - 1] - b[0]);

      console.log(Math.min(sum, sum2, sum3));
    }
  }
}

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

let inputString = "";
let currentLine = 0;

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

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

  main();
});

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

function main() {
  const testCases = readline();
  // let isPalindrome = true;

  for (let i = 0; i < testCases; i++) {
    let n = Number(readline());
    let a = readline().split(" ");
    let b = readline().split(" ");

    let aFirstMin = Math.abs(a[0] - b[0]);
    let aLastMin = Math.abs(a[0] - b[0]);

    let bFirstMin = Math.abs(a[0] - b[0]);
    let bFirstMinIndex = 0;

    let bLastMin = Math.abs(a[0] - b[0]);
    let bLastMinIndex = 0;
    // let aFirstMin = -1;
    // let aLastMin = -1;
    // let bFirstMin = -1;
    // let bLastMin = -1;

    // loop over a
    for (let i = 1; i < a.length; i++) {
      if (Math.abs(a[i] - b[0]) < bFirstMin) {
        bFirstMin = Math.abs(a[i] - b[0]);
        bFirstMinIndex = i;
      }
      if (Math.abs(a[i] - b[b.length - 1]) <= bLastMin) {
        bLastMin = Math.abs(a[i] - b[b.length - 1]);
        bLastMinIndex = i;
      }
    }

    if (
      (bFirstMinIndex === 0 && bLastMinIndex === n - 1) ||
      (bFirstMinIndex === n - 1 && bLastMinIndex === 0)
    ) {
      aFirstMin = 0;
      aLastMin = 0;
      console.log(bFirstMin + bLastMin);
    } else {
      for (let i = 1; i < b.length; i++) {
        if (Math.abs(b[i] - a[0]) < aFirstMin) {
          aFirstMin = Math.abs(b[i] - a[0]);
        }
        if (Math.abs(b[i] - a[a.length - 1]) <= aLastMin) {
          aLastMin = Math.abs(b[i] - a[a.length - 1]);
        }
      }

      let sum = bFirstMin + bLastMin;
      if (bFirstMinIndex !== 0 && bLastMinIndex !== 0) {
        sum += aFirstMin;
      }

      if (bFirstMinIndex !== n - 1 && bLastMinIndex !== n - 1) {
        sum += aLastMin;
      }
      console.log(sum);
    }
  }
}

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

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

var deb; const log = msg => (typeof deb != "undefined") && console.log("DEBUG", msg);

function costOf(a, p, b) {
	let mn = 1e9;
	for (let i = 0; i < b.length; i++) {
		mn = Math.min(mn, Math.abs(a[p] - b[i]));
	}
	return mn;
}

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

		const n = rn();
		const a = rna();
		const b = rna();

		const l = Math.min(costOf(a, 0, b) + costOf(b, 0, a), Math.abs(a[0] - b[0]));
		const r = Math.min(costOf(a, n-1, b) + costOf(b, n-1, a), Math.abs(a[n-1] - b[n-1]));

		console.log(l + r);
	}
}

/*
INPUT
2
3
1 10 1
20 4 25
4
1 1 1 1
1000000000 1000000000 1000000000 1000000000
*/

const t = Number(readline());

for(var i = 0; i < t; i++) {
    readline();
    const a = readline().split(' ').map(Number);
    const b = readline().split(' ').map(Number);

    const al = a[0];
    const ar = a[a.length - 1];
    const bl = b[0];
    const br = b[b.length - 1];

    var alm = Infinity, blm = Infinity, arm = Infinity, brm = Infinity;

    for(var j = 0; j < a.length; j++) {
        alm = Math.min(alm, Math.abs(b[j] - al));
        blm = Math.min(blm, Math.abs(a[j] - bl));
        arm = Math.min(arm, Math.abs(b[j] - ar));
        brm = Math.min(brm, Math.abs(a[j] - br));
    }

    const result = Math.min(
        Math.min(Math.abs(al - bl), blm + alm) + Math.min(Math.abs(ar - br), arm + brm),
        Math.min(Math.abs(al - br), alm + brm) + Math.min(Math.abs(ar - bl), arm + blm)
    );

    print(result);

}

/*
INPUT
2
3
1 10 1
20 4 25
4
1 1 1 1
1000000000 1000000000 1000000000 1000000000
*/

const t = Number(readline());

print(t);

/*for(let i = 0; i < t; i++) {
    readline();
    const a = readline().split(' ').map(Number);
    const b = readline().split(' ').map(Number);

    print(a, b);

    const al = a[0];
    const ar = a[a.length - 1];
    const bl = b[0];
    const br = b[b.length - 1];

    let alm = Infinity, blm = Infinity, arm = Infinity, brm = Infinity;

    for(let i = 0; i < a.length; i++) {
        alm = Math.min(alm, Math.abs(b[i] - al));
        blm = Math.min(blm, Math.abs(a[i] - bl));
        arm = Math.min(arm, Math.abs(b[i] - ar));
        brm = Math.min(brm, Math.abs(a[i] - br));
    }

    const result = Math.min(
        Math.min(Math.abs(al - bl), blm + alm) + Math.min(Math.abs(ar - br), arm + brm),
        Math.min(Math.abs(al - br), alm + brm) + Math.min(Math.abs(ar - bl), arm + blm)
    );

    print(result);
}*/

/*
INPUT
2
3
1 10 1
20 4 25
4
1 1 1 1
1000000000 1000000000 1000000000 1000000000
*/

print(readline());

/*const t = Number(readline());

for(let i = 0; i < t; i++) {
    readline();
    const a = readline().split(' ').map(Number);
    const b = readline().split(' ').map(Number);

    const al = a[0];
    const ar = a[a.length - 1];
    const bl = b[0];
    const br = b[b.length - 1];

    let alm = Infinity, blm = Infinity, arm = Infinity, brm = Infinity;

    for(let i = 0; i < a.length; i++) {
        alm = Math.min(alm, Math.abs(b[i] - al));
        blm = Math.min(blm, Math.abs(a[i] - bl));
        arm = Math.min(arm, Math.abs(b[i] - ar));
        brm = Math.min(brm, Math.abs(a[i] - br));
    }

    const result = Math.min(
        Math.min(Math.abs(al - bl), blm + alm) + Math.min(Math.abs(ar - br), arm + brm),
        Math.min(Math.abs(al - br), alm + brm) + Math.min(Math.abs(ar - bl), arm + blm)
    );

    print(result);
}*/


Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $$$2 \cdot n$$$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $$$n$$$ people in each row). Students are numbered from $$$1$$$ to $$$n$$$ in each row in order from left to right.  Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $$$2n$$$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose.

The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of students in each row. The second line of the input contains $$$n$$$ integers $$$h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$$$ ($$$1 \le h_{1, i} \le 10^9$$$), where $$$h_{1, i}$$$ is the height of the $$$i$$$-th student in the first row. The third line of the input contains $$$n$$$ integers $$$h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$$$ ($$$1 \le h_{2, i} \le 10^9$$$), where $$$h_{2, i}$$$ is the height of the $$$i$$$-th student in the second row.

Print a single integer — the maximum possible total height of players in a team Demid can choose.

NoteIn the first example Demid can choose the following team as follows:   In the second example Demid can choose the following team as follows:

var n = +readline();
var arr1 = readline().split(' ').map(item => +item);
var arr2 = readline().split(' ').map(item => +item);

arr1[1] = arr2[0] + arr1[1];
arr2[1] = arr1[0] + arr2[1];

for(var i = 2; i < n; i++) {
  arr1[i] = Math.max(arr2[i - 1] + arr1[i], arr2[i - 2] + arr1[i]);
  arr2[i] = Math.max(arr1[i - 1] + arr2[i], arr1[i - 2] + arr2[i]);
}

n--;
print(Math.max(arr1[n], arr2[n]));

"use strict";

var main = function() {
    var n = +rd();
    var H1 = [0].concat(rdAr());
    var H2 = [0].concat(rdAr());

    var DP = [crAr(n + 10, -1), crAr(n + 10, -1)];
    var dp = function(prev, pos) {
        if (pos > n) {
            return 0;
        }
        if (DP[prev][pos] !== -1) {
            return DP[prev][pos];
        }
        var res = dp(prev, pos + 1);
        if (prev === 0) {
            res = Math.max(res, dp(1, pos + 1) + H2[pos]);
        } else {
            res = Math.max(res, dp(0, pos + 1) + H1[pos]);
        }

        return DP[prev][pos] = res;
    };

    for (var i = n; i >= 0; --i) {
        dp(0, i);dp(1, i);
    }
    wr(dp(0, 0));
};

const rd = readline;
const wr = write;
const pr = print;
const rdAr = () => rd().split(' ').map(v => +v);
const prAr = (a) => pr(a.join(' '));
const cmpLt = (a, b) => a - b;
const cmpGt = (a, b) => b - a;
Array.prototype.sortLt = function() { return this.sort(cmpLt); };
Array.prototype.sortGt = function() { return this.sort(cmpGt); };
const crAr = function(length, ...fillArgs) { return new Array(length).fill(...fillArgs); };

main();

"use strict";

var main = function() {
    var n = +rd();
    var H1 = [0].concat(rdAr());
    var H2 = [0].concat(rdAr());

    var DP = [crAr(n + 10, -1), crAr(n + 10, -1)];
    var dp = function(prev, pos) {
        if (pos > n) {
            return 0;
        }
        if (DP[prev][pos] !== -1) {
            return DP[prev][pos];
        }
        var res = dp(prev, pos + 1);
        if (prev === 0) {
            res = Math.max(res, dp(1, pos + 1) + H2[pos]);
        } else {
            res = Math.max(res, dp(0, pos + 1) + H1[pos]);
        }

        return DP[prev][pos] = res;
    };

    return dp(0, 0);
};

const rd = readline;
const wr = write;
const pr = print;
const rdAr = () => rd().split(' ').map(v => +v);
const prAr = (a) => pr(a.join(' '));
const cmpLt = (a, b) => a - b;
const cmpGt = (a, b) => b - a;
Array.prototype.sortLt = function() { return this.sort(cmpLt); };
Array.prototype.sortGt = function() { return this.sort(cmpGt); };
const crAr = function(length, ...fillArgs) { return new Array(length).fill(...fillArgs); };

main();


One beautiful day Vasily the bear painted 2m circles of the same radius R on a coordinate plane. Circles with numbers from 1 to m had centers at points (2R - R, 0), (4R - R, 0), ..., (2Rm - R, 0), respectively. Circles with numbers from m + 1 to 2m had centers at points (2R - R, 2R), (4R - R, 2R), ..., (2Rm - R, 2R), respectively. Naturally, the bear painted the circles for a simple experiment with a fly. The experiment continued for m2 days. Each day of the experiment got its own unique number from 0 to m2 - 1, inclusive. On the day number i the following things happened:   The fly arrived at the coordinate plane at the center of the circle with number  ( is the result of dividing number x by number y, rounded down to an integer).  The fly went along the coordinate plane to the center of the circle number  ( is the remainder after dividing number x by number y). The bear noticed that the fly went from the center of circle v to the center of circle u along the shortest path with all points lying on the border or inside at least one of the 2m circles. After the fly reached the center of circle u, it flew away in an unknown direction. Help Vasily, count the average distance the fly went along the coordinate plane during each of these m2 days.

The first line contains two integers m, R (1 ≤ m ≤ 105, 1 ≤ R ≤ 10).

In a single line print a single real number — the answer to the problem. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6.

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 data = tokenizeIntegers(readline()),
		n = data[0], r = data[1],
		two = n*n + 2 * (n <= 3 ? 0 : (n-3)*(n-2)*(n-1)/6),
		root = (n <= 1 ? 0 : 2*(n-1)*(n-1));
	print((2*two + Math.sqrt(2)*root)*r/n/n);
}

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 data = tokenizeIntegers(readline()),
		m = data[0], r = data[1],
		total = m*(2+(m-1)*Math.sqrt(2)) + 4*(m-1)*m*(m+1)/6;
	print(r * total / m / m);
}

main();


For an array $$$[b_1, b_2, \ldots, b_m]$$$ define its number of inversions as the number of pairs $$$(i, j)$$$ of integers such that $$$1 \le i < j \le m$$$ and $$$b_i>b_j$$$. Let's call array $$$b$$$ odd if its number of inversions is odd. For example, array $$$[4, 2, 7]$$$ is odd, as its number of inversions is $$$1$$$, while array $$$[2, 1, 4, 3]$$$ isn't, as its number of inversions is $$$2$$$.You are given a permutation $$$[p_1, p_2, \ldots, p_n]$$$ of integers from $$$1$$$ to $$$n$$$ (each of them appears exactly once in the permutation). You want to split it into several consecutive subarrays (maybe just one), so that the number of the odd subarrays among them is as large as possible. What largest number of these subarrays may be odd?

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$)  — the size of the permutation. The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct)  — the elements of the permutation. The sum of $$$n$$$ over all test cases doesn't exceed $$$2\cdot 10^5$$$.

For each test case output a single integer  — the largest possible number of odd subarrays that you can get after splitting the permutation into several consecutive subarrays.

NoteIn the first and third test cases, no matter how we split our permutation, there won't be any odd subarrays.In the second test case, we can split our permutation into subarrays $$$[4, 3], [2, 1]$$$, both of which are odd since their numbers of inversions are $$$1$$$.In the fourth test case, we can split our permutation into a single subarray $$$[2, 1]$$$, which is odd.In the fifth test case, we can split our permutation into subarrays $$$[4, 5], [6, 1, 2, 3]$$$. The first subarray has $$$0$$$ inversions, and the second has $$$3$$$, so it is odd.

let ryan = '';//.map(Numaer).sort(function(a, a){return a - a})
process.stdin.on('data', c => ryan += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = ryan.split(EOL);
    var n = lines[0]
    var e = 0
    for(j = 1; j <= n * 2; j++){
        if(j % 2 == 1){
            e = lines[j]
        }else{
            var k = lines[j].split(' ').map(Number), ans = 0, max = 0
            for(l = 0; l < e; l++){
                if(k[l] < max){
                    ans++, max = 0
                }else{
                    max = k[l]
                }
            }
            console.log(ans)
        }
    }
});

'use strict';

function solve(n, a) {
  const st = [],
        left = [];
  for (let i = 0; i < n; i++) {
    var _st;
    while (st.length && a[st[st.length - 1]] < a[i]) st.pop();
    left[i] = (_st = st[st.length - 1]) !== null && _st !== void 0 ? _st : -1;
    st.push(i);
  }
  const dp = new Array(n).fill(0);
  let max = 0;
  for (let i = 0; i < n; i++) {
    var _dp2;
    if (left[i] >= 0) {
      var _dp;
      dp[i] = ((_dp = dp[left[i] - 1]) !== null && _dp !== void 0 ? _dp : 0) + 1;
    }
    dp[i] = Math.max(dp[i], (_dp2 = dp[i - 1]) !== null && _dp2 !== void 0 ? _dp2 : 0);
    max = Math.max(max, dp[i]);
  }
  return max;
}

async function main(read) {
  try {
    let res = [];
    let t = Number(await read());
    while (t--) {
      const n = Number(await read());
      const a = (await read()).split(' ').map(Number);
      res.push(solve(n, a));
    }
    console.log(res.join('\n'));
  } catch (error) {
    console.log(error);
  }
}

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

const lines = [];
const rl = require('readline').createInterface({ input: process.stdin, output: process.stdout });
rl.on('line', line => lines.push(line));
rl.on('close', main);
let rli = 0;
function readline() {
    return lines[rli++];
}

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

/*---------------------------------------------------*/

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

const getDigits = (x) => {
    let digits = new Array(10).fill(false);
    let len = 0;
    while (x) {
        len++;
        digits[x % 10n] = true;
        x /= 10n;
    }
    return [digits, len];
}

const getLen = (x) => {
    let str = x.toString(10);
    return str.length;
}

const calc = (t)=>{
    /* read */
    let n = readline();
    let a = ra();

    let result = 0;
    let mx = 0;
    for (let i = 1; i < n; i++) {
        if (a[i] > a[mx]) {
            mx = i;
            continue;
        }
        // console.log(`DEBUG mx ${mx}, a[mx] ${a[mx]}, i `, i);
        result++;
        mx = i + 1;
        i = i + 1;
    }

    return result;
};

function main() {
    let T = +readline();
    let out = [];
    for (let t = 1; t <= T; t++){
        out.push(calc(t));
        // process.stdout.write(`${calc(t)}\n`);
    }
    process.stdout.write(out.join('\n')+'\n');
}

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

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

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

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

        let sum = 0;
        let max = arr[0];
        for (let i = 1; i < n; i++) {
            if (arr[i] >= max) {
                max = arr[i];
            } else {
                sum += 1;
                max = 0;
            }
        }

        output(sum);
    }
}

//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 output = 0;
		for(var i = 0; i < N - 1; i++){
			if(list[i] > list[i + 1]){
				output++;
				i++;
			}
		}
		myout(output);
	}
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});
function readLine() {
    return inputString[currentLine++];
};
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });
    main();
    // cmd(not powershell): node cp.js < input.txt
});

function main() {
    let t = parseInt(readLine());
    while (t-- > 0) {
        console.log(solve());
    }
}
function solve() {
    readLine();
    let arr = readLine().split(" ").map(i => parseInt(i));
    let res = 0;
    for (let i = 1; i < arr.length; i++) {
        if (arr[i - 1] > arr[i]) {
            res++;
            i++;
        }
    }
    return res;
}

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

function Main(){
    // test case in nodeJs
	var t = nextInt();
	while(hasNext()){
		solve();
	}
}

function solve() {
	const n = nextInt();
	const arr = nextIntArray(n);
	let sum = 0;

	for (let i = 0; i < arr.length - 1; i++) {
		if (arr[i] > arr[i + 1]) {
			i++;
			sum++;
		}
	}

	myout(sum);
}

'use strict';
// HEADER:
const print = function(arg){
  let str = arg;
  if (arguments.length>1)
    str = Array.prototype.slice.call(arguments).join(' ');
  process.stdout.write(str+'\n');
};
let IS_MULTITEST, testN, tests;
const pcalc = (testN, toPrint)=>{
  let res = CALC(testN);
  if (Array.isArray(res)){
    toPrint.push(res.join(' '));
  } else if (Number.isFinite(res) || typeof res=='string'){
    toPrint.push(res+'');
  }
  if (process.env.DEBUG && toPrint.length){
    let ans = toPrint.pop();
    print(ans);
    let expected = tests[testN-1];
    if (expected){
      if (expected==ans)
        console.log('\x1b[32m%s\x1b[0m', 'correct');
      else {
        console.log('\x1b[31m%s\x1b[0m', 'wrong, expected:', expected);
      }
    }
  }
};

const main = ()=>{
  let toPrint = [];
  L('\nSolving:');
  if (IS_MULTITEST){
    let T = +readline()
    for (testN=1; testN<=T; testN++)
      pcalc(testN, toPrint);
  } else {
    pcalc(1, toPrint);
  }
  if (toPrint.length){
    print(toPrint.join('\n'));
  }
};

const lines = [];
const rl = require('readline').createInterface({
   input: process.stdin, output: process.stdout});
rl.on('line', line=>{
  lines.push(line);
});
rl.on('close', main);
let rli = 0;
const readline = ()=>lines[rli++];
const L = process.env.DEBUG ? console.log : ()=>{};
const LT = obj=>L(`\nTest #${testN}`, obj);

const ra = ()=>readline().split(' ').map(a=>+a)
const pa = arr=>print(arr.join(' '));

tests = [
``, // Test #1
``, // Test #2
``, // Test #3
``, // Test #4
``, // Test #5
];

// SOLUTION:
IS_MULTITEST = true;

const CALC = (testN)=>{
  // READING:
  let [n, m] = ra();
  let A = ra();
  LT({n, A});
  // PROCESSING:
  let ans = 0;
  let l=0;
  while (l+1<n){
    if (A[l]>A[l+1]){
      l++;
      ans++;
    }
    l++;
  }
  return ans;
};

var num = readline();

for (var i = 0; i < num; i++) {
   var n = readline();
   var arr = readline().split(" ");

    var count = 0;

   for(var j = 0; j < n; j++) {
        if(parseInt(arr[j], 10) > parseInt(arr[j + 1], 10) && j < n - 1){
            j++;
            count++;
        }
   }
   print(count);
}

T = readline()
while (T--) {
   n = +readline()
   a = readline().split(' ').map(a => +a)
   ans = 0

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

   }

   print(ans)
}

let ryan = '';//.map(Numaer).sort(function(a, a){return a - a})
process.stdin.on('data', c => ryan += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = ryan.split(EOL);
    var n = lines[0]
    var e = 0
    for(j = 1; j <= n * 2; j++){
        if(j % 2 == 1){
            e = lines[j]
        }else{
            var k = lines[j].split(' ').map(Number), ans = 0, max = 0;
            for(l = 0; l < e; l++){
                if(k[l] < max){
                    ans++, max ++
                }
            }
            console.log(ans);
        }
    }
});

let ryan = '';//.map(Numaer).sort(function(a, a){return a - a})
process.stdin.on('data', c => ryan += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = ryan.split(EOL);
    var n = lines[0]
    var e = 0;
    for(j = 1; j <= n * 2; j++){
        if(j % 2 == 1){
            e = lines[j];
        }else{
            var k = lines[j].split(' ').map(Number), ans = 0, max = 0;
            for(l = 0; l < e; l++){
                if(k[l] < max){
                    ans++, max = 0;
                }
            }
            console.log(ans);
        }
    }
});

'use strict';

function solve(n, a) {
  const st = [],
        left = [];
  for (let i = 0; i < n; i++) {
    var _st;
    while (st.length && a[st[st.length - 1]] < a[i]) st.pop();
    left[i] = (_st = st[st.length - 1]) !== null && _st !== void 0 ? _st : -1;
    st.push(i);
  }
  const dp = new Array(n).fill(0);
  let max = 0;
  for (let i = 0; i < n; i++) {
    if (left[i] >= 0) {
      var _dp, _dp2;
      dp[i] = Math.max(((_dp = dp[left[i] - 1]) !== null && _dp !== void 0 ? _dp : 0) + 1, (_dp2 = dp[i - 1]) !== null && _dp2 !== void 0 ? _dp2 : 0);
    }
    max = Math.max(max, dp[i]);
  }
  return max;
}

async function main(read) {
  try {
    let res = [];
    let t = Number(await read());
    while (t--) {
      const n = Number(await read());
      const a = (await read()).split(' ').map(Number);
      res.push(solve(n, a));
    }
    console.log(res.join('\n'));
  } catch (error) {
    console.log(error);
  }
}

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

'use strict';

function solve(n, a) {
  const st = [],
        left = [];
  for (let i = 0; i < n; i++) {
    var _st;
    while (st.length && a[st[st.length - 1]] < a[i]) st.pop();
    left[i] = (_st = st[st.length - 1]) !== null && _st !== void 0 ? _st : -1;
    st.push(i);
  }
  const dp = new Array(n).fill(0);
  let res = 0;
  for (let i = 0; i < n; i++) {
    if (left[i] >= 0) {
      var _dp;
      dp[i] = ((_dp = dp[left[i] - 1]) !== null && _dp !== void 0 ? _dp : 0) + 1;
    }
    res = Math.max(res, dp[i]);
  }
  return res;
}

async function main(read) {
  try {
    let res = [];
    let t = Number(await read());
    while (t--) {
      const n = Number(await read());
      const a = (await read()).split(' ').map(Number);
      res.push(solve(n, a));
    }
    console.log(res.join('\n'));
  } catch (error) {
    console.log(error);
  }
}

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

const lines = [];
const rl = require('readline').createInterface({ input: process.stdin, output: process.stdout });
rl.on('line', line => lines.push(line));
rl.on('close', main);
let rli = 0;
function readline() {
    return lines[rli++];
}

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

/*---------------------------------------------------*/

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

const getDigits = (x) => {
    let digits = new Array(10).fill(false);
    let len = 0;
    while (x) {
        len++;
        digits[x % 10n] = true;
        x /= 10n;
    }
    return [digits, len];
}

const getLen = (x) => {
    let str = x.toString(10);
    return str.length;
}

const calc = (t)=>{
    /* read */
    let n = readline();
    let a = ra();

    let result = 0;
    let start = 0;
    for (let i = 1; i < n; i++) {
        if (a[i] > a[start]) continue;
        result++;
        start = i + 1;
        i = i + 1;
    }

    return result;
};

function main() {
    let T = +readline();
    let out = [];
    for (let t = 1; t <= T; t++){
        out.push(calc(t));
        // process.stdout.write(`${calc(t)}\n`);
    }
    process.stdout.write(out.join('\n')+'\n');
}

var num = readline();

for (var i = 0; i < num; i++) {
   var n = readline();
   var arr = readline();

    var count = 0;

   for(var j = 0; j < n; j++) {
        if(arr[j] > arr[j + 1] && j < n - 1){
            j++;
            count++;
        }
   }
   print(count);
}

var num = readline();

for (var i = 0; i < num; i++) {
   var n = readline();
   var arr = readline().split(" ");

    var count = 0;

   for(var j = 0; j < n; j++) {
        if(arr[j] > arr[j + 1] && j < n - 1){
            j++;
            count++;
        }
   }
   print(count);
}


Eikooc and Sushi play a game.The game is played on a tree having $$$n$$$ nodes numbered $$$1$$$ to $$$n$$$. Recall that a tree having $$$n$$$ nodes is an undirected, connected graph with $$$n-1$$$ edges.They take turns alternately moving a token on the tree. Eikooc makes the first move, placing the token on any node of her choice. Sushi makes the next move, followed by Eikooc, followed by Sushi, and so on. In each turn after the first, a player must move the token to a node $$$u$$$ such that   $$$u$$$ is adjacent to the node $$$v$$$ the token is currently on  $$$u$$$ has not been visited before  $$$u \oplus v \leq min(u, v)$$$ Here $$$x \oplus y$$$ denotes the bitwise XOR operation on integers $$$x$$$ and $$$y$$$.Both the players play optimally. The player who is unable to make a move loses.The following are examples which demonstrate the rules of the game.     Suppose Eikooc starts the game by placing the token at node $$$4$$$. Sushi then moves the token to node $$$6$$$, which is unvisited and adjacent to $$$4$$$. It also holds that $$$6 \oplus 4 = 2 \leq min(6, 4)$$$. In the next turn, Eikooc moves the token to node $$$5$$$, which is unvisited and adjacent to $$$6$$$. It holds that $$$5 \oplus 6 = 3 \leq min(5, 6)$$$. Sushi has no more moves to play, so she loses.   Suppose Eikooc starts the game by placing the token at node $$$3$$$. Sushi moves the token to node $$$2$$$, which is unvisited and adjacent to $$$3$$$. It also holds that $$$3 \oplus 2 = 1 \leq min(3, 2)$$$. Eikooc cannot move the token to node $$$6$$$ since $$$6 \oplus 2 = 4 \nleq min(6, 2)$$$. Since Eikooc has no moves to play, she loses. Before the game begins, Eikooc decides to sneakily relabel the nodes of the tree in her favour. Formally, a relabeling is a permutation $$$p$$$ of length $$$n$$$ (sequence of $$$n$$$ integers wherein each integer from $$$1$$$ to $$$n$$$ occurs exactly once) where $$$p_i$$$ denotes the new numbering of node $$$i$$$.She wants to maximize the number of nodes she can choose in the first turn which will guarantee her a win. Help Eikooc find any relabeling which will help her do so.

The first line contains a single integer $$$t~(1 \le t \le 10^5)$$$  — the number of test cases. The description of each test case is as follows. The first line of each test case contains an integer $$$n~(1 \le n \le 2 \cdot 10^5)$$$  — the number of nodes in the tree. The next $$$n-1$$$ lines contain two integers $$$u$$$ and $$$v$$$ $$$(1 \le u, v \le n; u \neq v)$$$  — denoting an edge between nodes $$$u$$$ and $$$v$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case print any suitable relabeling  — a permutation of length $$$n$$$ which maximizes the number of nodes that can be chosen in the first turn that guarantee a win for Eikooc. If there are multiple such relabelings, you may print any of them.

NoteIn the first test case, Eikooc has only one choice. Sushi will have no moves to play after Eikooc chooses this node and Eikooc will win.In the second test case, $$$1 \oplus 2 = 3 \nleq min(1, 2)$$$. Hence, after Eikooc picks either of the nodes, Sushi will have no moves to play and Eikooc will win. Both $$$\{1, 2\}$$$ and $$$\{2, 1\}$$$ are optimal relabelings.

const readline = require('readline')

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

function solve(n, edges) {
    const adj = {}
    edges.forEach(([a, b]) => {
        adj[a] = adj[a] || []
        adj[b] = adj[b] || []
        adj[a].push(b)
        adj[b].push(a)
    })
    const nodes = dfs(adj, 1)
    if (nodes[0].length > nodes[1].length) {
        const temp = nodes[0]
        nodes[0] = nodes[1]
        nodes[1] = temp
    }
    let c0 = 0
    let c1 = 0
// console.log(nodes)
    const ans = []
    const bits = nodes[0].length.toString(2)
        .split('').reverse()
// console.log(bits)
    let start = 1
    let end = 2
    for (let i = 0; i < 19; i++) {
        // console.log(start, end)
        for (let j = start; j < end; j++) {
            if (bits[i] === '1') {
            // console.log('j0', j, nodes[c0])
                ans[nodes[0][c0++] - 1] = j
            } else {
            // console.log('j1', j, nodes[c1])
                ans[nodes[1][c1++] - 1] = j
            }
            if (j === n) return ans.join(' ')
        }
        start = end
        end *= 2
    }
    // return ans.join(' ')
}
function dfs(adj, r) {
    const stack = [[r, 0, -1, 0]]
    const nodes = [[], []]
    while (stack.length) {
        const [u, i, p, color] = stack[stack.length - 1]

        const nb = adj[u] || []
        if (!i) {
            // first visited
            nodes[color].push(u)
        }
        if (i < nb.length) {
            stack[stack.length - 1][1]++
            const v = nb[i]
            // if (!visited[v]) { // has circle
            if (v !== p) {
                stack.push([v, 0, u, color ^ 1])
            }
        } else {
            // last visited
            stack.pop()
        }
    }
    return nodes
}

const readline = require('readline')

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

function solve(n, edges) {
    const adj = {}
    edges.forEach(([a, b]) => {
        adj[a] = adj[a] || []
        adj[b] = adj[b] || []
        adj[a].push(b)
        adj[b].push(a)
    })
    const nodes = dfs(adj, 1)
    if (nodes[0].length > nodes[1].length) {
        const temp = nodes[0]
        nodes[0] = nodes[1]
        nodes[1] = temp
    }
    let c0 = 0
    let c1 = 0
// console.log(nodes)
    const ans = []
    const bits = nodes[0].length.toString(2)
        .split('').reverse()
// console.log(bits)
    let start = 1
    let end = 2
    for (let i = 0; i < 19; i++) {
        // console.log(start, end)
        for (let j = start; j < end; j++) {
            if (bits[i] === '1') {
            // console.log('j0', j, nodes[c0])
                ans[nodes[0][c0++] - 1] = j
            } else {
            // console.log('j1', j, nodes[c1])
                ans[nodes[1][c1++] - 1] = j
            }
            if (j === n) return ans.join(' ')
        }
        start = end
        end *= end
    }
    // return ans.join(' ')
}
function dfs(adj, r) {
    const stack = [[r, 0, -1, 0]]
    const nodes = [[], []]
    while (stack.length) {
        const [u, i, p, color] = stack[stack.length - 1]

        const nb = adj[u] || []
        if (!i) {
            // first visited
            nodes[color].push(u)
        }
        if (i < nb.length) {
            stack[stack.length - 1][1]++
            const v = nb[i]
            // if (!visited[v]) { // has circle
            if (v !== p) {
                stack.push([v, 0, u, color ^ 1])
            }
        } else {
            // last visited
            stack.pop()
        }
    }
    return nodes
}

const readline = require('readline')

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

function solve(n, edges) {
    const adj = {}
    edges.forEach(([a, b]) => {
        adj[a] = adj[a] || []
        adj[b] = adj[b] || []
        adj[a].push(b)
        adj[b].push(a)
    })
    const nodes = dfs(adj, 1)
    let c0 = 0
    let c1 = 0
// console.log(nodes)
    const ans = []
    const bits = Math.min(nodes[0].length, nodes[1].length).toString(2)
        .split('').reverse()
// console.log(bits)
    let start = 1
    let end = 2
    for (let i = 0; i < 19; i++) {
        // console.log(start, end)
        for (let j = start; j < end; j++) {
            if (bits[i]) {
            // console.log('j0', j, nodes[c0])
                ans[nodes[0][c0++] - 1] = j
            } else {
            // console.log('j1', j, nodes[c1])
                ans[nodes[1][c1++] - 1] = j
            }
            if (j === n) return ans.join(' ')
        }
        start = end
        end *= end
    }
    // return ans.join(' ')
}
function dfs(adj, r) {
    const stack = [[r, 0, -1, 0]]
    const nodes = [[], []]
    while (stack.length) {
        const [u, i, p, color] = stack[stack.length - 1]

        const nb = adj[u] || []
        if (!i) {
            // first visited
            nodes[color].push(u)
        }
        if (i < nb.length) {
            stack[stack.length - 1][1]++
            const v = nb[i]
            // if (!visited[v]) { // has circle
            if (v !== p) {
                stack.push([v, 0, u, color ^ 1])
            }
        } else {
            // last visited
            stack.pop()
        }
    }
    return nodes
}


Pchelyonok decided to give Mila a gift. Pchelenok has already bought an array $$$a$$$ of length $$$n$$$, but gifting an array is too common. Instead of that, he decided to gift Mila the segments of that array!Pchelyonok wants his gift to be beautiful, so he decided to choose $$$k$$$ non-overlapping segments of the array $$$[l_1,r_1]$$$, $$$[l_2,r_2]$$$, $$$\ldots$$$ $$$[l_k,r_k]$$$ such that:  the length of the first segment $$$[l_1,r_1]$$$ is $$$k$$$, the length of the second segment $$$[l_2,r_2]$$$ is $$$k-1$$$, $$$\ldots$$$, the length of the $$$k$$$-th segment $$$[l_k,r_k]$$$ is $$$1$$$  for each $$$i<j$$$, the $$$i$$$-th segment occurs in the array earlier than the $$$j$$$-th (i.e. $$$r_i<l_j$$$)  the sums in these segments are strictly increasing (i.e. let $$$sum(l \ldots r) = \sum\limits_{i=l}^{r} a_i$$$ — the sum of numbers in the segment $$$[l,r]$$$ of the array, then $$$sum(l_1 \ldots r_1) < sum(l_2 \ldots r_2) < \ldots < sum(l_k \ldots r_k)$$$). Pchelenok also wants his gift to be as beautiful as possible, so he asks you to find the maximal value of $$$k$$$ such that he can give Mila a gift!

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The next $$$2 \cdot t$$$ lines contain the descriptions of test cases. The description of each test case consists of two lines. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, print the maximum possible value of $$$k$$$.

function fn() {
  var n = +readline();
  var a = readline().split(' ').map(function (x) {
    return +x;
  });
  var g = [0];
  for (var i = 0; i < n; i++) {
    g[i + 1] = g[i] + a[i];
  }
  // k^2 + k - 2n === 0;  a = 1, b = 1, c = -2n;
  var delta = 1 + 8 * n; // b^2 - 4ac
  var k1 = Math.floor((-1 + Math.sqrt(delta)) / 2); // (-b +- √delta) / 2a
  var pre = []; // dp[i][j]为k=i时，考虑后j个元素 能达成条件的返回最左子数组和的最大值 否则返回-1
  for (var j = 0; j <= n; j++)
    pre[j] = Infinity; // 边界条件 i=0时无穷大，使得i=1时严格小于它
  var k = 0;
  for (var i = 1; i <= k1; i++) {
    var curr = [];
    var currMax = -1;
    for (var j = 0; j <= n; j++)
      curr[j] = -1;
    for (var j = i; j <= n; j++) {
      var l = n - j,
        r = l + i - 1;
      var sum = g[r + 1] - g[l]; // 最左的长度为k的子数组的元素和
      var yes = sum < pre[j - i] ? sum : -1;
      var no = curr[j - 1];
      curr[j] = Math.max(yes, no);
      if (curr[j] > currMax)
        currMax = curr[j];
    }
    if (currMax > 0)
      k++;
    else
      break;
    pre = curr;
  }
  write(k + '\n');
}
var t = +readline();
while (t--) {
  fn();
}

function fn() {
  var n = +readline();
  var a = readline().split(' ').map(function (x) {
    return +x;
  });
  var g = [0];
  for (var i = 0; i < n; i++) {
    g[i + 1] = g[i] + a[i];
  }
  // k^2 + k - 2n === 0;  a = 1, b = 1, c = -2n;
  var delta = 1 + 8 * n; // b^2 - 4ac
  var k1 = Math.floor((-1 + Math.sqrt(delta)) / 2); // (-b +- √delta) / 2a
  var pre = []; // dp[i][j]为k=i时，考虑后j个元素 能达成条件的返回最左子数组和 否则返回-1
  for (var j = 0; j <= n; j++)
    pre[j] = Infinity; // 边界条件 i=0时无穷大，使得i=1时严格小于它
  for (var i = 1; i <= k1; i++) {
    var curr = [];
    for (var j = 0; j <= n; j++)
      curr[j] = -1;
    for (var j = i; j <= n; j++) {
      var l = n - j,
        r = l + i - 1;
      var sum = g[r + 1] - g[l]; // 最左的长度为k的子数组的元素和
      var yes = sum < pre[j - i] ? sum : -1;
      var no = curr[j - 1];
      curr[j] = Math.max(yes, no);
    }
    pre = curr;
  }
  for (var i = k1; i >= 1; i--) {
    if (pre[n] > 0) {
      write(i + '\n');
      return;
    }
  }
  write('1\n');
}
var t = +readline();
while (t--) {
  fn();
}


— Hey folks, how do you like this problem?— That'll do it. BThero is a powerful magician. He has got $$$n$$$ piles of candies, the $$$i$$$-th pile initially contains $$$a_i$$$ candies. BThero can cast a copy-paste spell as follows:   He chooses two piles $$$(i, j)$$$ such that $$$1 \le i, j \le n$$$ and $$$i \ne j$$$.  All candies from pile $$$i$$$ are copied into pile $$$j$$$. Formally, the operation $$$a_j := a_j + a_i$$$ is performed. BThero can cast this spell any number of times he wants to — but unfortunately, if some pile contains strictly more than $$$k$$$ candies, he loses his magic power. What is the maximum number of times BThero can cast the spell without losing his power?

The first line contains one integer $$$T$$$ ($$$1 \le T \le 500$$$) — the number of test cases. Each test case consists of two lines:    the first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 1000$$$, $$$2 \le k \le 10^4$$$);  the second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le k$$$).  It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$, and the sum of $$$k$$$ over all test cases does not exceed $$$10^4$$$.

For each test case, print one integer — the maximum number of times BThero can cast the spell without losing his magic power.

NoteIn the first test case we get either $$$a = [1, 2]$$$ or $$$a = [2, 1]$$$ after casting the spell for the first time, and it is impossible to cast it again.

var t = parseInt(readline());
while(t-- > 0) {
  var nk = readline().split(' ');
  var n = parseInt(nk[0]);
  var k = parseInt(nk[1]);
  var a = readline().split(' ').map(x => parseInt(x));
  var smallest = 0;
  var output = 0;
  for (var i = 1; i < n; i ++) {
    if (a[smallest] > a[i]) smallest = i;
  }
  for (var i = 0; i < n; i ++) {
    if (i !== smallest) {
      output += Math.floor((k - a[i]) / a[smallest]);
    }
  }
  print(output);
}

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 [len, target] = readLine()
      .split(" ")
      .map((n) => parseInt(n));
    const arr = readLine()
      .split(" ")
      .map((n) => parseInt(n));
    let max = -Infinity;

    for (let i = 0; i < len; i++) {
      let count = 0;
      const val = arr[i];
      for (let j = 1; j < len; j++) {
        const index = (i + j) % len;
        const current = arr[index];
        const diff = target - current;
        if (diff > 0) {
          count += Math.floor(diff / val);
        }
      }
      max = Math.max(max, count);
    }

    console.log(max);
  }
}

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

//Don't have to see. end------------------------------------------
function Main(){
  var t = nextInt();
  var output = new Array(t);
  for(var i = 0; i < t; i++){
    var one = nextIntArray();
    var N = one[0];
    var K = one[1];
    var list = nextIntArray();
    var mincount = 1000000;
    var minIndex = -1;
    for(var j = 0; j < N; j++){
      if(mincount > list[j]){
        mincount = list[j];
        minIndex = j;
      }
    }
    var count = 0;
    for(var j = 0; j < N; j++){
      if(j != minIndex){
        count += Math.floor((K - list[j]) / mincount);
      }
    }
    output[i] = count;
  }
  myout(myconv(output, 9));
}

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

let stdinInput = '';

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

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

function main(data) {
    data = data.split('\n');
    const testCases = data[0] * 2;
    let i = 1;
    while (i <= testCases) {
      let a = data[i].trim().split(' ').map(Number);
      const n = a[0];
      const k = a[1];
      a = data[i + 1].trim().split(' ').map(Number);
      a.sort((a, b) => a - b);
      let min = a[0];
      let moves = 0;
      if (a[0] < k) {
        for (let i = 1; i < a.length; i += 1) {
          if (a[i] < k) moves += Math.floor((k - a[i]) / min);
        }
      }
      console.log(moves);
      i += 2;
    }
}

function For(a, b, step, fn) {
  let res;
  if (step > 0) {
    for (let i = a; i <= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  } else {
    for (let i = a; i >= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  if (step > 0) for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  else for (let i = a; i >= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = Math.min(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == min : v == min);
  return [this[i], i, min];
}
Array.prototype.max = function (fn) {
  let max = Math.max(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == max : v == max);
  return [this[i], i, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

// let p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let t = $()
  while (t--) {
    let n = $(), k = $()
    let a = $(n).sort(asc);
    // console.log(a,k)
    let s = 0;
    For(1, n - 1, 1, i => { s += (k - a[i]) / a[0] | 0 })
    log(s)
  }
}

'use strict';

var readline = require('readline');

function tTwoLinesPerStdin() {
  return new Promise(resolve => {
    const stdin = readline.createInterface(process.stdin);
    let t;
    let prevLine;
    let output = [];
    stdin.on('line', line => {
      if (typeof t !== 'number') {
        t = +line;
      } else {
        if (prevLine) {
          output.push([prevLine, line]);
          prevLine = undefined;

          if (output.length === t) {
            resolve(output);
            stdin.close();
          }
        } else {
          prevLine = line;
        }
      }
    });
  });
}

function solve(k, piles) {
  const sorted = piles.sort((a, b) => a - b);
  let answer = 0;

  for (let i = 1; i < sorted.length; i++) {
    answer += Math.floor((k - sorted[i]) / sorted[0]);
  }

  return answer;
}

async function main() {
  const input = await tTwoLinesPerStdin();

  for (let t = 0; t < input.length; t++) {
    const [line1, line2] = input[t];
    const [k] = line1.split(' ').map(Number).slice(1);
    const a = line2.split(' ').map(Number);
    const answer = solve(k, a);
    console.log(answer);
  }
}

main().then();