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

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

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;

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


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


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

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


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

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

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


let i = '';
process.stdin.on('data', c => i += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = i.split(EOL);
    var n = lines[0];
    var e = 0;
    for(j = 1; j <= n; j++){
        var k = lines[j].split('');
        var a = false;
        for(l = 0; l < k.length; l++){
            if(k[l - 1] != k[l] && k[l] != k[l + 1]){
                a = true;
            }
        }
        if(a === true){
           console.log('NO');
        }else{
            console.log('YES');
        }
    }
});

let buf='';

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

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

'use-strict'

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

let data = '';
let currentLine = 0;

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

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

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

    while(testCases--) {

        const a = readLine();

        const res = main(a);

        console.log(res)
    }
});

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

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

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

module.exports = main;

const fs = require("fs");

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

const testCases = [];
let T;

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

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

    testCases.push(testCase);
  }
}

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

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

  console.log(result);
}

parseInput();

testCases.forEach(solution);

'use strict';

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

let inputString = '';
let currentLine = 0;

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

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

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

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

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

    for (let test = 0; test < N; test++) {
        let s = readline();

        let a = 0;
        let b = 0;

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

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

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

const readline = require('readline')

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

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

'use strict';

var readline = require('readline');

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

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

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

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

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

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

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

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

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

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

    return 'YES';
};

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

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

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

const calc = (s)=>{
  for (let i=0; i<s.length;){
    let j = i;
    while (s[i]==s[j] && j<s.length) j++;
    if (j-i<2)
      return false;
    i = j;
  }
  return true;
};

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

E.calc = calc;

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

//Don't have to see. end------------------------------------------
function Main(){
	var t = nextInt();
	while(hasNext()){
		var S = next();
		var N = S.length;
		var c = 1;
		var ok = true;
		for(var i = 1; i < N; i++){
			if(S[i - 1] != S[i]){
				if(c == 1){
					ok = false;
				}else{
					c = 1;
				}
			}else{
				c++;
			}
		}
		if(c == 1){
			ok = false;
		}
		myout((ok) ? "YES" : "NO");
	}
}

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

let buf='';

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

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

'use-strict'

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

let data = '';
let currentLine = 0;

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

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

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

    while(testCases--) {

        const a = readLine();

        const res = main(a);

        console.log(res)
    }
});

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

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

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

module.exports = main;

const fs = require("fs");

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

const testCases = [];
let T;

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

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

    testCases.push(testCase);
  }
}

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

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

  console.log(result);
}

parseInput();

testCases.forEach(solution);