'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 input() {
  return inputString[currentLine++];
}

function main() {
  let tc = parseInt(input());
  while(tc--){
    const [n, m] = input().split(' ').map(Number);
    const s = [];
    for(let i = 0; i < n; i++){
      s.push(input());
    }
    let ans = Number.POSITIVE_INFINITY;
    for(let i = 0; i < n; i++){
      for(let j = i+1; j < n; j++){
        ans = Math.min(ans, solve(s[i], s[j], m));
      }
    }
    console.log(ans);
  }
}

const solve = (s1, s2, m) => {
  let ret = 0;
  for(let i = 0; i < m; i++){
    let tmp = Number.POSITIVE_INFINITY;
    const [c1, c2] = [s1[i], s2[i]];
    tmp = Math.min(tmp, Math.abs(c1.charCodeAt(0)-c2.charCodeAt(0)), Math.abs(c2.charCodeAt(0)-c1.charCodeAt(0)));
    ret += tmp;
  }
  return ret;
}

/*
File Created: Tuesday, 17 May 2022
Author: Lukas Rimkus
*/
'use strict';

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

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(readLine());
    while(testCases--) {
        const [n,m] =  get_ints();
        const obj = {};
        for (let index = 0; index < n; index++) {
            obj[index] = readLine();
        }
        console.log(solve([n,m,obj]));
    }
});
function get_ints() {
    return data[currentLine++].split(' ').map(x=>Number(x));
}

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

function solve(input){
    let min = Number.MAX_SAFE_INTEGER;
    let w1;
    let w2;
    for(let i = 0; i < input[0] - 1; i++){
        w1 = Object.values(input[2])[i].split('').map(str => str.charCodeAt());
        for(let j = i + 1; j < input[0]; j++){
            w2 = Object.values(input[2])[j].split('').map(str => str.charCodeAt());
            let diff = 0;
            for(let m = 0; m < input[1]; m++){
                diff += Math.abs(w1[m] - w2[m])
            }
            if(min > diff){
                min = diff;
            }
        }
    }
    return min;
}

module.exports = solve;

'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 [n, m] = readline().split(' ').map(Number);
        let S = [];
        const alphabet = [...Array(26)].map((_, i) => String.fromCharCode(i + 97));
        const letterPos = {};
        alphabet.forEach((letter, i) => letterPos[letter] = i + 1);

        for (let i = 0; i < n; i++) {
            S[i] = readline().split('').map(l => letterPos[l]);
        }

        let min = Infinity;

        for (let i = 0; i < n; i++) {
            let localMin = Infinity;

            for (let j = 0; j < n; j++) {
                if (i === j) continue;
                let sum = 0;
                for (let k = 0; k < m; k++) {
                    sum += Math.abs(S[i][k] - S[j][k]);
                }
                localMin = Math.min(localMin, sum);
            }

            min = Math.min(min, localMin);
        }

        output(min);
    }
}

const { createInterface } = require('readline');
const max = Number.MAX_SAFE_INTEGER

function calcDiff(a, t) {
  let diff = max;
  for (let j = 0; j < t.length - 1; j++) {
    const b = t[j]
    let tempDiff = 0;
    for (let i = 0; i < a.length; i++) {
      tempDiff += Math.abs(a.charCodeAt(i) - b.charCodeAt(i))

    } if (tempDiff < diff) {
      diff = tempDiff
    }
  }
  return diff
}

function main() {
  const readline = createInterface({
    input: process.stdin,
    output: process.stdout
  });
  let lineNb = 0;
  let subLine = 0;
  let n;
  let minDiff = max;
  let subN;
  let t = [];

  readline.on('line', line => {
    if (!lineNb) {
      lineNb++
      n = parseInt(line);
    } else {
      const data = line.split(" ")
      if (data.length == 2) {
        diff = max;
        subLine = 0;
        lineNb++
        minDiff = max
        subN = data[0]
        t = []
      } else {
        subLine++;
        t.push(data[0])
        if (subLine > 1) {
          const res = calcDiff(t[subLine - 1], t)
          if (minDiff > res) {
            minDiff = res
          }
        }
        if (subLine == subN) {
          console.log(minDiff)
          if (lineNb == n + 1) {
            readline.close()
          }
        }
      }
    }

  });

}

main()

function diff(a, b) {
    var x = 0;
    for (let i = 0; i < a.length; i++) {
        x += Math.abs(a.charCodeAt(i) - b.charCodeAt(i));
    }
    return x;
}

function solve(inp) {
  var t = Number(inp.nextLine());
  while(t--) {
    const [n, m] = inp.nextLine().split(" ").map(x => Number(x));
    const arr = new Array(n);
    for (let i = 0; i < n; i++) {
        arr[i] = inp.nextLine();
    }

    var min = Number.MAX_SAFE_INTEGER;
    for (let i = 0; i < n; i++) {
        for (let j = i + 1; j < n; j++) {
            min = Math.min(min, diff(arr[i], arr[j]));
        }
    }
    console.log(min);
  }
}

class Input {
    constructor(lines) {
        this.lines = lines;
        this.next = 0;
    }

    addLine(line) {
        this.lines.push(line)
    }

    hasLine() {
       return this.lines.size() > 0;
    }

    nextLine() {
        const line = this.lines[this.next];
        if (line) {
            this.next++;
        }
        return line;
    }
}

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

const inp = new Input([]);

readline.on('line', line => {
    inp.addLine(line);
});

readline.on('close', () => {
    solve(inp);
});

const solve = (arr,n,l) => {
     let min = Infinity,cnt;
     for(let i=0;i<n;i++){
          for(let j=i+1;j<n;j++){
                cnt = 0;
               for(let k=0;k<l;k++) cnt+=Math.abs(arr[i][k].charCodeAt()-arr[j][k].charCodeAt());
               if(cnt<min) min = cnt;
          }
     }
     return min;
};
function main() {
  const fs = require("fs");
  const input = fs.readFileSync(0, "utf8").trim().split("\n");
  let line = 0;
  function readline() {
    return input[line++];
  }
  let n = parseInt(readline());
  for(let i=0;i<n;i++){
       let [l,k] = readline().split(" ").map(cur=>parseInt(cur));
       let arr = [];
       for(let j=0;j<l;j++){
            arr.push(readline());
       }
       console.log(solve(arr,l,k));
  }
}
main();

'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(n, m, s) {
  let res = Infinity;
  for (let i = 0; i < n; i++) {
    for (let j = i + 1; j < n; j++) {
      let cur = 0;
      for (let k = 0; k < m; k++) {
        if (s[i][k] === s[j][k]) continue;
        cur += Math.abs(s[i].charCodeAt(k) - s[j].charCodeAt(k));
      }
      res = Math.min(res, cur);
    }
  }
  return res.toString();
}

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

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

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

const wordDistance = (one, two) => {
  return one
    .map((ch, i) => Math.abs(ch.charCodeAt(0) - two[i].charCodeAt(0)))
    .reduce((a, b) => a + b, 0);
};

let lines = 0;

let words = [];
const findMinimumDistance = (words) => {
  let minimumDistance = 100000000;
  for (let i = 0; i < words.length - 1; i++) {
    const one = words[i].split("");
    for (let j = i + 1; j < words.length; j++) {
      const two = words[j].split("");
      const distance = wordDistance(one, two);
      // console.log(distance);
      if (minimumDistance > distance) {
        minimumDistance = distance;
      }
    }
  }
  console.log(minimumDistance);
};
rl.on("line", function (line) {
  if (lines !== 0) {
    if (line.split(" ").length === 2) {
      if (words.length > 0) {
        findMinimumDistance(words);
      }
      words = [];
    } else {
      words.push(line);
    }
  }
  lines++;
});

rl.on("close", function (line) {
  findMinimumDistance(words);
});

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\r\n');
let currentline = 0;
function readline(){
      return input[currentline++];
}

function alph() {
  var f = 'abcdefghijklmnopqrstuvwxyz';
  return f;
}
function Alph() {
  var f = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  return f;
}
function max(x,y) {
  return x >=y ? x : y;
}
function min(x,y) {
  return x <=y ? x : y;
}
var T = readline();
for(var q=1;q<=T;q++) {
  var [n, x] = readline().split(' ').map((x)=>parseInt(x));
  var a = []
  for(var i=0;i<n;i++) {
    var s = readline();
    a.push(s);
  }
  var ans = 1000000000;
  var f = alph();
  var m = new Map();
  for(var i=0;i<f.length;i++) {
    m.set(f[i], i);
  }
  var len = a.length;
  for(var i=0;i<len;i++) {
    for(var j=i+1;j<len;j++) {
      var tar = 0;
      for(var t=0;t<x;t++) {
        tar += max(m.get(a[i][t])-m.get(a[j][t]), m.get(a[j][t])-m.get(a[i][t]));
      }
      ans = min(ans, tar);
    }

  }
  console.log(ans);
  //fs.appendFileSync('output.txt', ans+'\r\n');

  //os.EOL
}
//fs.writeFileSync('output.txt', f.join(' '));

//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 numOfChars = nextInt();
    const stringArr = nextStrArray(n);

    let min = Infinity;
    for (let i = 0; i < stringArr.length; i++) {
        for (let j = i + 1; j < stringArr.length; j++){
            const s1 = stringArr[i];
            const s2 = stringArr[j];
            let sum = 0;
            for (let k = 0; k < numOfChars; k++) {
                sum += Math.abs(s1[k].charCodeAt(0) - s2[k].charCodeAt(0));
            }
            min = Math.min(min, sum)
        }
    }

    myout(min)
}

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, m] = lines[l++].trim().split(' ').map(Number)
        const arr = lines.slice(l, l + n).map(str => str.trim())
        l += n
        output[i] = solve(n, arr)
    }
    console.log(output.join('\n'))
// })()
})

function solve(n, arr) {
    let ans = Infinity
    for (let i = 0; i < n; i++) {
        for (let j = i + 1; j < n; j++) {
            ans = Math.min(ans, cal(arr, i, j))
        }
    }
    return ans
}
function cal(arr, p, q) {
    let ans = 0
    for (let i = 0; i < arr[0].length; i++) {
        const a = arr[p].charCodeAt(i)
        const b = arr[q].charCodeAt(i)
        const d = Math.abs(a - b)
        // if (arr[p] === 'a' || arr[q] === 'a') {
            ans += d
        // } else if (arr[p] === 'z' || arr[q] === 'z') {
        //     ans += d
        // } else {
        //     ans += Math.min(d, 26 - d)
        // }
    }
    // console.log(arr[p], arr[q], ans)
    return ans
}

'use strict';
// HEADER:
const print = console.log;
let IS_MULTITEST, testN;
const pcalc = testN=>{
  let res = calc(testN);
  if (Array.isArray(res)){
    pa(res);
  } else if (Number.isFinite(res) || typeof res=='string'){
    print(res);
  }
};

const main = ()=>{
  if (IS_MULTITEST){
    let T = +readline()
    for (testN=1; testN<=T; testN++)
      pcalc(T);
  } else
   pcalc();
};

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(`Test #${testN}`, obj);

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

// SOLUTION:
IS_MULTITEST = 1;

const calc = ()=>{
  // READING:
  let [n, m] = ra();
  let S = [];
  for (let i=0; i<n; i++)
    S.push(readline());
  LT({n, m, S});
  // PROCESSING:
  let ans = 0;
  let min = Infinity;
  let dist = (s1, s2)=>{
    let d = 0;
    for (let i=0; i<m; i++){
      let ch1 = s1.charCodeAt(i);
      let ch2 = s2.charCodeAt(i);
      d += Math.abs(ch1-ch2);
    }
    return d;
  };
  for (let i=0; i<n; i++){
    for (let j=0; j<n; j++){
      if (i!=j) min = Math.min(min, dist(S[i], S[j]))
    }
  }
  return min;
};

//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();
	var alpha = "abcdefghijklmnopqrstuvwxyz";
	while(hasNext()){
		var N = nextInt();
		var M = nextInt();
		var list = nextStrArray(N);
		var output = 100000000;
		for(var i = 0; i < N; i++){
			for(var j = i + 1; j < N; j++){
				var L = list[i];
				var R = list[j];
				var diff = 0;
				for(var k = 0; k < M; k++){
					diff += Math.abs(alpha.indexOf(L[k]) - alpha.indexOf(R[k]));
				}
				output = Math.min(output, diff);
			}
		}
		myout(output);
	}
}

t = parseInt(readline());
for(i = 0; i < t; i++)
{
    var n = readline().split(" ");
    n[0] = parseInt(n[0]);
    n[1] = parseInt(n[1]);
    var strings = [];
    for(j = 0; j < n[0]; j++)
    {
        strings[j] = readline();
    }
    var min = 201;
    var diff = 0;
    for(j = 0; j < n[0] - 1; j++)
    {
        for(k = j + 1; k < n[0]; k++)
        {
            diff = 0;
            for(l = 0; l < n[1]; l++)
            {
                diff += Math.abs(strings[j].charCodeAt(l) - strings[k].charCodeAt(l));
            }
            if(diff < min) min = diff;
        }
    }
    print(min);
}

//import { readline, print } from '@ip-algorithmics/codeforces-io';
var t = +readline();
while (t--) {
    var _a = readline().split(' ').map(function (v) { return +v; }), n = _a[0], m = _a[1];
    var a = [];
    a.length = n;
    for (var i = 0; i < n; i++)
        a[i] = (readline().split('').map(function (v) { return v.charCodeAt(0); }));
    var min = 1000000;
    for (var i = 0; i < n; i++) {
        for (var j = 0; j < i; j++) {
            var sum = 0;
            for (var k = 0; k < m; k++) {
                sum += Math.abs(a[i][k] - a[j][k]);
            }
            min = Math.min(min, sum);
        }
    }
    print(min);
}

/*
File Created: Tuesday, 17 May 2022
Author: Lukas Rimkus
*/
'use strict';

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

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(readLine());
    while(testCases--) {
        const [n,m] =  get_ints();
        const obj = {};
        for (let index = 0; index < n; index++) {
            obj[index] = readLine();
        }
        console.log(solve([n,m,obj]));
    }
});
function get_ints() {
    return data[currentLine++].split(' ').map(x=>Number(x));
}

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

function solve(input){
    let min;
    let w1;
    let w2;
    for(let i = 0; i < input[0] - 1; i++){
        w1 = Object.values(input[2])[i].split('').map(str => str.charCodeAt());
        for(let j = i + 1; j < input[0]; j++){
            w2 = Object.values(input[2])[j].split('').map(str => str.charCodeAt());
            let diff = 0;
            for(let m = 0; m < input[1]; m++){
                diff += Math.abs(w1[m] - w2[m])
            }
            if(!min || min > diff){
                min = diff;
            }
        }
    }
    return min;
}

module.exports = solve;


var arr = readline().split('');

var dp = new Array(arr.length);
dp[dp.length-1]=0;

for(var i=arr.length-2; i>=0; i--){
  if(arr[i]===arr[i+1])
    dp[i]=dp[i+1]+1;
  else
    dp[i]=dp[i+1];
}

var x=parseInt(readline());

while(x--){
  var a = readline().split(' ').map(a => parseInt(a));
  print(dp[a[0]-1]-dp[a[1]-1]);
}

var s = readline();
var t = readline();
var a = [ 0 ];
var sum = 0;
for (var i = 1; i < s.length; i++) {
	if (s[i] === s[i - 1]) {
		sum++;
	}
	a.push(sum);
}
var ans = [];
for (var i = 0; i < t; i++) {
	var m = readline().split(' ').map((d) => parseInt(d));
	var tmp = a[m[1] - 1] - a[m[0] - 1];
	ans.push(tmp);
}
print(ans.join('\n'));

var s = readline();
var t = readline();
var a = [ 0 ];
var sum = 0;
for (var i = 1; i < s.length; i++) {
	if (s[i] === s[i - 1]) {
		sum++;
	}
	a.push(sum);
}

for (var i = 0; i < t; i++) {
	var m = readline().split(' ').map((d) => parseInt(d));
	var ans = a[m[1] - 1] - a[m[0] - 1];
	print(ans);
}

var m, current = 0, left, right;
var input = readline();
var charArray = input.split('');
var prefixSum = new Array(charArray.length);
prefixSum[0] = 0;
for (var i = 0; i < charArray.length - 1; i++) {
  if (charArray[i] == charArray[i + 1]) {
    current++;
  }
  prefixSum[i + 1] = current;
}

m = readline();

for (var i = 0; i < m; i++) {
  input = readline();
  left = Number(input.split(" ")[0]);
  right = Number(input.split(" ")[1]);
  print(prefixSum[right - 1] - prefixSum[left - 1]);
}

var myInput = readline();
var myLength = parseInt(readline());
var mySum = 0;
var sum_arr = [mySum];
for (var i = 1; i< myInput.length;i++){
    if (myInput[i-1] == myInput[i]){
        mySum += 1;
        sum_arr.push(mySum);
    }
    else{
        sum_arr.push(mySum);
    }
}
for (var j = 0;j < myLength; j++){
    var next = readline().split(' ').map((val)=>{
        return parseInt(val);
    });
    print(sum_arr[next[1]-1]-sum_arr[next[0]-1]);
}

;(function () {

	var s = readline();
	var n = +readline();

	s = '!' + s;
	var l = s.length;

	var b = [0];

	for( var i = 1; i < l; i++ )
		b.push( s[i] !== s[i-1] ? b[i - 1] : b[i - 1] + 1 );

	while(n--){
		print(readline().split(' ').reduce(function(first, second){
			return b[second] - b[first];
		}));
	}

}).call(this);

const input = [];

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

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

RL.on('close', () => {
  let s = input[0];
  const n =parseInt(input[1]);

  const map = []; let sum = 0;

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

  //console.log(map);
  const answer = [];
  for (i = 0; i < n; i += 1) {
    const [left, right] = input[i + 2].split(' ').map(x => parseInt(x));
    answer.push(map[right-1] - map[left-1]);
    answer.push('\n');
  }

  console.log(answer.join(''));
});

function main() {
    var line = readline();
    var n = line.length;
    var data = [];
    for(var i=0;i<n;i++){
        data[i] = line[i+1] == line[i] ? (data[i-1] || 0) + 1 : (data[i-1] || 0);
    }
    var m = Number(readline());
    for(var i=0;i<m;i++){
        var li = readline().split(' ').map(Number);
        var l = li[0];
        var r = li[1];
        print(data[r-2] - (data[l-2] || 0));
    }
}

main()

s=readline()
var A = new Array(s.length)
for (var i=0; i<s.length-1; ++i) {
 A[i] = Number(s[i]==s[i+1])
}
A[s.length-1]=0

var T = new Array(s.length+1)
T[0]=0
for (var i=1; i<=s.length; ++i)
 T[i]=T[i-1]+A[i-1]

var n=Number(readline())
for (var i=0;i<n;++i) {
 var In = readline().split(' ').map(Number)
 print(T[In[1]-1]-T[In[0]-1])
}

var input = readline().split('');
var eq = [];
var counter = 0;
eq[0] = 0;
for (var i = -1; i < input.length; i++) {
    if (input[i] == input[i-1]) counter++;
    eq[i] = counter;
}

var n = readline().split(' ').map(Number);
var res = '';
for (var i = 0; i < n; i++) {
    var line = readline().split(' ').map(Number);
    var a = line[0];
    var b = line[1];
    res = res + (eq[b-1] - eq[a-1]).toString() + '\n';
}
write(res);

var str = readline(),
    m = +readline(),
    l = [],
    r = [],
    data = [0];

for (var i = 1; i < str.length; i ++)
{
  data[i] = data[i - 1] + ((str[i] === str[i - 1]) ? 1 : 0);
}

for (var i = 0; i < m; i ++)
{
  var line = readline().split(' ');
  l[i] = +line[0] - 1;
  r[i] = +line[1] - 1;

  print(data[r[i]] - data[l[i]]);
}

var s = readline().split(''),
			m = readline(),
			pairCount = [ 0 ];

		s.forEach(function(c, i)	{
			var prevPairCount = pairCount[pairCount.length - 1];

			pairCount.push(c == s[i - 1] ? prevPairCount + 1 : prevPairCount);
		});

		for(var a=0; a<m; a++)	{
			var queryPair = readline().replace(/\s{1,}/gi, ' ').split(/\s/),
				li = parseInt(queryPair[0]), ri = parseInt(queryPair[1]),
				queryResult = 0;

			print([ li, ri ].reduce(function(a, b)	{
				return pairCount[b] - pairCount[a];
			}));
		}

var s = 'a' + readline();

var n = +readline();

var a = [ 0 ];
var c = 0;

for( var i = 1; i < s.length; i++ )
	a[ i ] = s[ i ] === s[ i-1 ] ? ++c : c;

var ans = [];
while( n-- ){
	readline().split(' ').reduce(function(l, r){
		ans.push( a[ r ] - a[ l ] );
	});
}
print( ans.join( '\n' ) );

var s = 'a' + readline();

var n = +readline();

var a = [ 0 ];
var c = 0;

for( var i = 1; i < s.length; i++ )
	a[ i ] = s[ i ] === s[ i-1 ] ? ++c : c;

while( n-- ){
	readline().split(' ').reduce(function(l, r){
		print( a[ r ] - a[ l ] );
	});
}

var ps = new Array(1<<17);

for(var i=0;i<(1<<17);i++) {
  ps[i]=0;
}

var a = readline();
var n = a.length;

for(var i=1;i<n;i++) {
  if(a[i-1]==a[i]) {
    ps[i]=1;
  }

  ps[i]+=ps[i-1];
}

var q = readline();

while(q--) {
  var token = readline().split(' ');
  var l = token[0], r = token[1];

  print(ps[r-1]-ps[l-1]);
}

var s = readline().split(''),
    m = readline(),
    sQueryPair = {},
    query = [],
    result = [];

s.forEach(function(a, i)    {
    sQueryPair[i + 1] = a == s[i + 1] ? 1 : 0;
});

for(var a=0; a<m; a++)  {
    var queryPair = readline().replace(/\s{1,}/gi, ' ').split(/\s/),
        queryResult = 0;

    query.push(queryPair);

    for(var i=queryPair[0]; i<queryPair[1]; i++)    {
        queryResult += sQueryPair[i];
    }

    result.push(queryResult);
}

result.forEach(function(row, index) {
    print(row);
});

var s = readline().split(''),
			m = readline(),
			sQueryPair = [ 0 ];

		s.forEach(function(a, i)	{
			var preVal = sQueryPair[sQueryPair.length - 1];

			sQueryPair.push(a == s[i + 1] ? preVal + 1 : preVal);
		});

		for(var a=0; a<m; a++)	{
			var queryPair = readline().replace(/\s{1,}/gi, ' ').split(/\s/),
				li = parseInt(queryPair[0]), ri = parseInt(queryPair[1]),
				queryResult = 0;

			print(queryPair.reduce(function(a, b)	{
				return sQueryPair[b] - sQueryPair[a];
			}));
		}

var s = readline().split(''),
			m = readline(),
			sQueryPair = [ 0 ];

		s.some(function(a, i)	{
			var preVal = sQueryPair[sQueryPair.length - 1];

			if(i == s.length)	preVal++;

			sQueryPair.push(a == s[i + 1] ? preVal + 1 : preVal);
		});

		for(var a=0; a<m; a++)	{
			var queryPair = readline().replace(/\s{1,}/gi, ' ').split(/\s/),
				li = parseInt(queryPair[0]), ri = parseInt(queryPair[1]),
				queryResult = 0;

			print(queryPair.reduce(function(a, b)	{
				return sQueryPair[b] - sQueryPair[a];
			}));
		}


'use strict';

var fs = require('fs');

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

var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);

function solve(read) {
  const [rn, rb, rns, re, rbs] = convertInput(read);
  const n = rn();
  const res = [1];
  for (let i = 1; i < n; i++) {
    res[i] = n - i + 1;
  }
  return res.join(' ');
}
function convertInput(read) {
  const rn = () => Number(read());
  const rb = () => BigInt(read());
  const rns = () => read().split(/\s+/).map(Number);
  const re = () => read().split(/\s+/).map(s => Number(s) - 1);
  const rbs = () => read().split(/\s+/).map(BigInt);
  return [rn, rb, rns, re, rbs];
}
function main(r) {
  try {
    let t = Number(r());
    let res = new Array(t);
    let limit = 0;
    for (let i = 0; i < t; i++) {
      var _solve;
      const s = (_solve = solve(r)) !== null && _solve !== void 0 ? _solve : '';
      if (t < limit) {
        process.stdout.write(s.toString());
        process.stdout.write('\n');
      } else {
        res[i] = s;
      }
    }
    if (t >= limit) {
      return res.join('\n');
    } else {
      return '';
    }
  } catch (error) {
    console.log(error);
  }
}

const inputs = fs__default["default"].readFileSync(0, 'utf-8').split(/\r?\n/);
void function () {
  const read = ((i = 0) => () => inputs[i++])();
  const output = main(read);
  process.stdout.write(output);
}();

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];
    for(i = 1; i <= t; i++){
        var a = lines[i];
        var ans = 1 + ' ';
        for(j = 3; j <= a; j++){
            ans += j + ' ';
        }
        ans += 2;
        console.log(ans);
    }
});

//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 P = [];
		for(var i = 1; i <= N / 2; i++){
			P.push(i);
			P.push(N - i + 1);
		}
		if(N % 2 == 1){
			P.push(Math.ceil(N / 2));
		}
		myout(myconv(P, 8));
	}
}

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

function stringArr1() {
  return readline().trim().split(" ");
}
function getValue() {
  return parseInt(readline());
}
function iInpArr1() {
  return readline()
    .trim()
    .split("")
    .map((cur) => parseInt(cur));
}
let mod = 1e9 + 7;
mod = BigInt(mod);
const pow = (num, exp, mod) => {
  if (exp === BigInt(0)) return BigInt(1);
  let x = pow(num, exp / BigInt(2), mod);
  if (exp % BigInt(2)) return (((x * x) % mod) * num) % mod;
  else return (x * x) % mod;
};
const inverse = (num, exp) => {
  return pow(BigInt(num), BigInt(exp), BigInt(mod));
};
const sqrt = (n) => {
  let low = 1n,
    high = n,
    ans;
  while (low <= high) {
    let mid = (low + high) / 2n;
    if (mid * mid <= n) {
      ans = mid;
      low = mid + 1n;
    } else high = mid - 1n;
  }
  return ans;
};

const solve = () => {
  let t = getValue();
  while (t--) {
    let n = getValue();
    let i = 1,
      j = n,
      k = 1;
    let res = new Array(n).fill(0);
    while (i <= j) {
      if (i === j) res[i - 1] = k++;
      else {
        res[j - 1] = k++;
        res[i - 1] = k++;
      }
      i++;
      j--;
    }
    console.log(res.join(" "));
  }
};
solve();

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();
  });
  solve();
});
function readline() {
  return inputString[currentLine++];
}

function solve() {
  var t = readline();
  for (var k = 0; k < t; k++) {
    var n = readline();
    var res = [1];
    for (var i = n; i > 1; i--) {
      res.push(i);
      if (i == 2) {
        process.stdout.write(res.join(" ") + '\n');
      }
    }
  }
}

/*
** Template
*/

// Common Template Starts //
//*
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++];
}
// Common Template Ends //

// Driver Function Starts //
const main = () => {
    console.clear()
    let t = parseInt(readline())
    // let primes = sivOfAtkin(1000)
    while (t--) {
        let n = parseInt(readline())
        // let arr = readline().split(' ').map(Number)
        console.log(solve(n))
    }
}
// Driver Function Ends //

// MAIN LOGIC
const solve = (n) => {
    let res = ''
    let i = 1
	while(++i <= n)
	{
		res +=  ( i + ' ' )
	}

    res +=  1

    return res
}

function factorial(num) {
    let rval=1
    for (let i = 2; i <= num; i++)
        rval = rval * i
    return rval
}

function excludes(a, b) {
    let setB = new Set(b)
    return [...new Set(a)].filter(x => !setB.has(x))
}

function intersect(a, b) {
    let setB = new Set(b)
    return [...new Set(a)].filter(x => setB.has(x))
}

function gcd(a, b) {
    if (b == 0) return a
    return gcd(b, a % b)
}

const factors = number => Array.from(Array(number + 1), (_, i) => i).filter(i => number % i === 0)

function sivOfAtkin(limit) {
    let limitSqrt = Math.sqrt(limit)
    let sieve = new Array(limit + 1).fill(false)
    sieve[2] = sieve[3] = true

    let n = 0
    for (let x = 1; x <= limitSqrt; x++) {
        let xx = x * x
        for (let y = 1; y <= limitSqrt; y++) {
            let yy = y * y
            if (xx + yy >= limit) break

            // first quadratic using m = 12 and r in R1 = {r : 1, 5}
            n = (4 * xx) + (yy)
            if (n <= limit && (n % 12 == 1 || n % 12 == 5)) sieve[n] = !sieve[n]

            // second quadratic using m = 12 and r in R2 = {r : 7}
            n = (3 * xx) + (yy)
            if (n <= limit && (n % 12 == 7)) sieve[n] = !sieve[n]

            // third quadratic using m = 12 and r in R3 = {r : 11}
            n = (3 * xx) - (yy)
            if (x > y && n <= limit && (n % 12 == 11)) sieve[n] = !sieve[n]
        }
    }

    return sieve
}

const isOdd = (num) => num & 1
const isEven = (num) => !(num & 1)
const minOfArray = (arr) => Math.min.apply(null, arr)
const maxOfArray = (arr) => Math.max.apply(null, arr)
const uniqueArray = (arr) => [...new Set(arr)]
const sortArray = (arr) => arr.sort((a, b) => a - b)
const sumOfArray = (arr) => arr.reduce((partialSum, a) => partialSum + a, 0)

// cmd: cat input.txt | node main.js

/*
* auther yeling
*
*
*/
'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 print(str) {
    console.log(str);
}

// const { readline, print, testOutput } = require('@ip-algorithmics/codeforces-io');
// main();
// testOutput();

function begin(n) {
    if(n == 1) {
        print('1');
        return;
    }
    let res = new Array(n).fill(0);
    for(let i = 1; i < n; i++) {
        res[i] = i + 2;
    }
    res[0] = 1;
    res[n - 1] = 2;
    print(res.join(' '));
}

function main() {
    var caseNum = parseInt(readline(), 10);
    for (var i = 0; i < caseNum; i++) {
        var n = parseInt(readline(), 10);
        begin(n);
    }
}

let stop = '';
process.stdin.on('data', c => stop += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = stop.split(EOL);
    console.log(1, 4, 3, 5, 2);
    console.log(4, 1, 6, 2, 5, 3)
});

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];
    for(i = 1; i <= t; i++){
        var a = lines[i].split('').map(Number);
        var x = 0;
        var y = 0;
        for(j = 0; j < 6; j++){
            if(j >= 3){
                x += a[j];
            }else{
                y += a[j];
            }
        }
        console.log(x == y ? 'YES' : 'NO');
    }
});

//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 P = [];
		for(var i = 1; i <= N; i++){
			P.push(i);
		}
		for(var i = 1; i < N - 1; i += 2){
			var tmp = P[i];
			P[i] = P[i + 1];
			P[i + 1] = tmp;
		}
		myout(myconv(P, 8));
	}
}

//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 P = [];
		for(var i = 1; i <= N; i += 2){
			P.push(i);
		}
		for(var i = 2; i <= N; i += 2){
			P.push(i);
		}
		myout(myconv(P, 8));
	}
}

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

function stringArr1() {
  return readline().trim().split(" ");
}
function getValue() {
  return parseInt(readline());
}
function iInpArr1() {
  return readline()
    .trim()
    .split("")
    .map((cur) => parseInt(cur));
}
let mod = 1e9 + 7;
mod = BigInt(mod);
const pow = (num, exp, mod) => {
  if (exp === BigInt(0)) return BigInt(1);
  let x = pow(num, exp / BigInt(2), mod);
  if (exp % BigInt(2)) return (((x * x) % mod) * num) % mod;
  else return (x * x) % mod;
};
const inverse = (num, exp) => {
  return pow(BigInt(num), BigInt(exp), BigInt(mod));
};
const sqrt = (n) => {
  let low = 1n,
    high = n,
    ans;
  while (low <= high) {
    let mid = (low + high) / 2n;
    if (mid * mid <= n) {
      ans = mid;
      low = mid + 1n;
    } else high = mid - 1n;
  }
  return ans;
};

const solve = () => {
  let t = getValue();
  while (t--) {
    let n = getValue();
    let arr = [];
    for (let i = n; i >= 1; i--) arr.push(i);
    console.log(arr.join(" "));
  }
};
solve();


'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 => string.trim());

    main();
});

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

const addBigNumbers = (x, y) => {
    const a = [...x];
    a.reverse();
    const b = [...y];
    b.reverse();
    const res = [];
    const size = Math.max(a.length, b.length);

    for (let i = 0; i < size; i++) {
        const oof = (res[i] || 0) + (a[i] || 0) + (b[i] || 0);
        const cat = oof % 10;
        const rest = parseInt(oof / 10);

        res[i] = cat;
        res[i + 1] = rest;
    }

    res.reverse();

    return res[0] === 0 ? res.slice(1) : res;
};

function main() {
    const n = parseInt(readline());
    const arrays = [];
    for (let i = 0; i < n; i++) {
        arrays.push(parseInt(readline()));
    }

    arrays.forEach(a => {
        const size = parseInt(a / 2);
        let sumSmol = 0;
        let sum = [];

        for (let i = 0; i < size; i++) {
            const mic = 2 * i + 1;
            const mare = 2 * (i + 1) + 1;
            const gucciMic = (i + 1) * (mare * mare - mic * mic);
            if (i < 225000) {
                sumSmol += gucciMic;
            } else {
                const gucci = `${gucciMic}`.split('').map(o => parseInt(o));
                sum = addBigNumbers(sum, gucci);
            }
        }

        sum = addBigNumbers(sum, `${sumSmol}`.split('').map(o => parseInt(o)));

        console.log(sum.join(''));
    });
}

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

const store = {};

function main() {
	let t = +readLine();
	while (t--) {
		let n = +readLine();
		let s = 0n;

		while (n > 1) {
			s += BigInt(4 * (n - 1) * Math.floor(n / 2));
			n -= 2;
		}

		console.log(s.toString());
	}
}

'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 => string.trim());

    main();
});

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

const addBigNumbers = (x, y) => {
    const a = [...x];
    a.reverse();
    const b = [...y];
    b.reverse();
    const res = [];
    const size = Math.max(a.length, b.length);

    for (let i = 0; i < size; i++) {
        const oof = (res[i] || 0) + (a[i] || 0) + (b[i] || 0);
        const cat = oof % 10;
        const rest = parseInt(oof / 10);

        res[i] = cat;
        res[i + 1] = rest;
    }

    res.reverse();

    return res[0] === 0 ? res.slice(1) : res;
};

function main() {
    const n = parseInt(readline());
    const arrays = [];
    for (let i = 0; i < n; i++) {
        arrays.push(parseInt(readline()));
    }

    arrays.forEach(a => {
        if (a === 1) {
            console.log(0);
        }

        const size = parseInt(a / 2);
        let sumSmol = 0;
        let sum = [];

        for (let i = 0; i < size; i++) {
            const mic = 2 * i + 1;
            const mare = 2 * (i + 1) + 1;
            const gucciMic = (i + 1) * (mare * mare - mic * mic);
            if (i < 225000) {
                sumSmol += gucciMic;
            } else {
                const gucci = `${gucciMic}`.split('').map(o => parseInt(o));
                sum = addBigNumbers(sum, gucci);
            }
        }

        sum = addBigNumbers(sum, `${sumSmol}`.split('').map(o => parseInt(o)));

        console.log(sum.join(''));
    });
}

'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 => string.trim());

    main();
});

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

const addBigNumbers = (x, y) => {
    const a = [...x];
    a.reverse();
    const b = [...y];
    b.reverse();
    const res = [];
    const size = Math.max(a.length, b.length);

    for (let i = 0; i < size; i++) {
        const oof = (res[i] || 0) + (a[i] || 0) + (b[i] || 0);
        const cat = oof % 10;
        const rest = parseInt(oof / 10);

        res[i] = cat;
        res[i + 1] = rest;
    }

    res.reverse();

    return res[0] === 0 ? res.slice(1) : res;
};

function main() {
    const n = parseInt(readline());
    const arrays = [];
    const start = new Date();
    for (let i = 0; i < n; i++) {
        arrays.push(parseInt(readline()));
    }

    arrays.forEach(a => {
        if (a === 1) {
            console.log(0);
        }

        const size = parseInt(a / 2);
        let sumSmol = 0;
        let sum = [];

        for (let i = 0; i < size; i++) {
            const mic = 2 * i + 1;
            const mare = 2 * (i + 1) + 1;
            const gucciMic = (i + 1) * (mare * mare - mic * mic);
            if (i < 225000) {
                sumSmol += gucciMic;
            } else {
                const gucci = `${gucciMic}`.split('').map(o => parseInt(o));
                sum = addBigNumbers(sum, gucci);
            }
        }

        sum = addBigNumbers(sum, `${sumSmol}`.split('').map(o => parseInt(o)));

        console.log(sum.join(''));
    });
    const end = new Date();

    console.log(end - start);
}


const readline = require('readline');
const input = [];
function A(q) {
    for (let i = 1; i <= Number(q[0]); i++) {
        console.log(solve(q[i]))
    }
}
function solve(s) {
    const stack = {
        '1': 0,
        '2': 0,
        '3': 0
    };
    let min = 0, iter = 0;
    for(let i = 0; i < s.length; i++) {
        while(iter < s.length && (stack['1'] === 0 || stack['2'] === 0 || stack['3'] === 0)) {
            stack[s[iter++]]++;
        }
        let full = true;
        for(let i in stack) {
            full = full && stack[i]
        }
        if (full) {
            if (iter - i < min || min === 0) {
                min = iter - i;
            }
        }
        stack[s[i]]--;
    }
    return min;
}
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
rl.on('line', q => {
    input.push(q.trim())
});
rl.on('close', () => {
    A(input);
})

const processData = (lines) => {
  const n = +lines[0]
  for (let i=0; i<n; i++) {
    const s = lines[i+1].split('').map(x => x - 1)
    let pos = [null, null, null]
    let min = null
    let count = 0
    for (let i =0; i<s.length; i++) {
      if (pos[s[i]] === null) {
        count++
      }
      pos[s[i]] = i
      if (count === 3) {
        let start = Math.min.apply(null, pos)
        let end = Math.max.apply(null, pos)
        const diff = end - start
        if (min === null) {
          min = diff
        } else {
          if (min > diff) {
            min = diff
          }
        }

      }
    }
    if (min === null) {
      console.log(0)
    } else {
      console.log(min+1)
    }
  }
}

let i = ''
process.stdin.on('data', c => i += c)
process.stdin.on('end', () => {
  const {EOL} = require('os')
  const lines = i.split(EOL) /*your input text, split by lines*/
  processData(lines)
})

function comp(a,b) { return a-b; }
function solve(str) {
    var one = 0, two = 0, three = 0, ans = 200005, d = 0, arr = [];
    for (var i in str) {
        if (str[i] == '1')
            one = i;
        else if (str[i] == '2')
            two = i;
        else
            three = i;

        if (one && two && three) {
            arr = [one,two,three];
            arr.sort(comp);
            d = arr[2]-arr[0]+1;
            ans = Math.min(ans,d);
        }
    }
    print(ans == 200005 ? 0 : ans);
}
var t = Number(readline());
while (t--) {
    var str = readline();
    solve(str);
}

const readline = require('readline');
const input = [];
function A(q) {
    for (let i = 1; i <= Number(q[0]); i++) {
        console.log(solve(q[i]))
    }
}
function solve(s) {
    const stack = {
        '1': false,
        '2': false,
        '3': false
    };
    let count = 0, min = 0;
    function check(q) {
        count++;
        stack[q] = true;
        let full = true;
        for(let i in stack) {
            full = full && stack[i]
        }
        if (full) {
            for(let i in stack) {
                stack[i] = false;
            }
            if (count < min || min === 0) {
                min = count;
            }
            count = 0;
        }
    }
    for(let i of s) {
        check(i);
    }
    return min;
}
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
rl.on('line', q => {
    input.push(q.trim())
});
rl.on('close', () => {
    A(input);
})


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

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

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

		let x = 0;
		while (n-1 >= 1 << x) x++;
		x--;

		const ans = [];
		for (let i = n-1; i > 0; i--) {
			ans.push(i);
			if (i == 1 << x) ans.push(0);
		}

		console.log(ans.join(' '));
	}
}

const solve = n=>{
     if(n===2){
          console.log("0 1");
          return;
     }
     let t = 1<<(Math.floor(Math.log2(n-1)));
     let arr = [];
     for(let i=1;i<t;i++) arr.push(i);
     arr.push(0);
     arr.push(t);
     for(let i=t+1;i<n;i++) arr.push(i);
     console.log(arr.join(" "));
}
function main() {
  const fs = require("fs");
  const input = fs.readFileSync(0, "utf8").trim().split("\n");
  let line = 0;
  function readline() {
    return input[line++];
  }
  let t = parseInt(readline());
  for(let i=0;i<t;i++){
       let n = parseInt(readline());
       solve(n);
  }
}
main();

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];
    for(i = 1; i <= t; i++){
        var n = lines[i];
        var b = 1;
        while(b * 2 < n){
            b *= 2;
        }
        var ans = '';
        for(j = b - 1; j >= 0; j--){
            ans += j + ' ';
        }
        for(j = b; j < n; j++){
            ans += j + ' ';
        }
        console.log(ans);
    }
});

const main = () => {

	var t = readInt();
	while (t--) {
		var n = readInt();
		var largestPowOf2 = 1;
		var temp = n - 1;
		while ((temp >> 1) > 0) {
			temp >>= 1;
			largestPowOf2 <<= 1;
		}
		// Put 1,2,3,... 0, largestPower of 2, larger numbers
		var ans = [];
		for (var i = 1; i < largestPowOf2; i++)
			ans.push(i);
		ans.push(0);
		for (var i = largestPowOf2; i < n; i++)
			ans.push(i);
		oneLineArrayPrint(ans);
	}

	return;
}

const readInt = () => parseInt(readline());
const readString = () => readline();
const readIntArr = () => readline().split(" ").map(zzzz => parseInt(zzzz));
const parseLong = (string) => { // javascript's largest long is < (1<<62)
	var n = string.length;
	var res = 0;
	for (var i = 0; i < n; i++) {
		res *= 10;
		res += parseInt(string[i]);
	}
	return res;
}
const readLong = () => parseLong(readLong());
const readLongArr = () => readline().split(" ").map(zzzz => parseLong(zzzz));
const oneLineArrayPrint = (arr) => print(arr.map(zzzz => zzzz.toString()).join(' '));
const multiLineArrayPrint = (arr) => print(arr.map(zzzz => zzzz.toString()).join('\n'));
const multiLineArrayOfArraysPrint = (arr) => print(arr.map(yyyy => yyyy.map(zzzz => zzzz.toString()).join(' ')).join('\n'));
const makeArr = (defaultVal, dimensionsArr) => {
	var n = dimensionsArr.length;
	if (n === 1) return Array(dimensionsArr[0]).fill(defaultVal);
	else {
		var temp = [];
		for (var i = 0; i < dimensionsArr[0]; i++)
			temp.push(makeArr(defaultVal, dimensionsArr.slice(1)));
		return temp;
	}
}

for (_ = 0; _ < 1; _++) {
	main();
}

// newline for testing
 //newline for testing
// codeforces js cannot use "let" keyword. use "var" instead
// codeforces js cannot destructure arrays

const solve = n=>{
     let t = 1<<(Math.floor(Math.log2(n)));
     let arr = [];
     for(let i=1;i<t;i++) arr.push(i);
     arr.push(0);
     arr.push(t);
     for(let i=t+1;i<n;i++) arr.push(i);
     console.log(arr.join(" "));
}
function main() {
  const fs = require("fs");
  const input = fs.readFileSync(0, "utf8").trim().split("\n");
  let line = 0;
  function readline() {
    return input[line++];
  }
  let t = parseInt(readline());
  for(let i=0;i<t;i++){
       let n = parseInt(readline());
       solve(n);
  }
}
main();


'use strict';

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

class INPUT {
	constructor() {
		this.input = '';
		this.curLine = 0;
	}

	append(str) {
		this.input += str;
	}

	prepare() {
		this.input = this.input.split('\n').map((str) => str.trim());
	}

	readline() {
		return this.input[this.curLine++];
	}

	readInts() {
		return this.readline()
			.split(' ')
			.map((x) => parseInt(x, 10));
	}
}

const input = new INPUT();

process.stdin.on('data', (str) => {
	input.append(str);
});

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

function solve() {
	let [n, k] = input.readInts();
	let a = input.readInts().map((x, idx) => Math.log2(x) + idx);
	let b = [...new Array(n - 1)].map((_, idx) =>
		a[idx + 1] <= a[idx] + 1e-9 ? 1 : 0
	);

	for (let i = 1; i < b.length; i++) {
		b[i] += b[i - 1];
	}

	let ans = 0;

	for (let i = 0; i < n - k; i++) {
		if (b[i + k - 1] - (i > 0 ? b[i - 1] : 0) === 0) {
			ans++;
		}
	}

	console.log(ans);
}

function main() {
	let [T] = input.readInts();

	while (T--) {
		solve();
	}
}

'use strict';

function solve(n, m, a) {
  let res = 0;
  for (let i = 0, j = 0; i < n; i = ++j) {
    while (j + 1 < n && a[j] < a[j + 1] * 2) {
      j++;
    }
    res += Math.max(0, j - i + 1 - m);
  }
  return res;
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const [n, m] = (await read()).split(' ').map(Number);
      const a = (await read()).split(' ').map(Number);
      res[i] = solve(n, m, a);
    }
    return 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', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\r\n');
let currentline = 0;
function readline(){
      return input[currentline++];
}

function alph() {
  var f = 'abcdefghijklmnopqrstuvwxyz';
  return f;
}
function Alph() {
  var f = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  return f;
}
function max(x,y) {
  return x >=y ? x : y;
}
function min(x,y) {
  return x <=y ? x : y;
}
function abs(x) {
  return x < 0 ? x*(-1) : x;
}
var T = readline();
for(var qe=1;qe<=T;qe++) {
  var [n,k] = readline().split(' ').map((x) => parseInt(x));
  var a = readline().split(' ').map((x) => parseInt(x));
  var ans = 0;
  var ok = []
  for(var i=1;i<n;i++) {
    ok[i] = ((a[i]/a[i-1]) > 0.5);
  }
  var j = 0;
  for(var i=1;i<n;i++) {
    if(!ok[i]) continue;
    j = max(i, j);
    while(j<n && ok[j]) j++;

    if((j-i)+1 > k) ans++;
  }

  console.log(ans)
  //fs.appendFileSync('output.txt', ans+'\r\n');

}
//

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, k] = lines[l++].trim().split(' ').map(Number)
        const arr = lines[l++].trim().split(' ').map(Number)
        output[i] = solve(n, k, arr)
    }
    console.log(output.join('\n'))
// })()
})

function solve(n, k, arr) {
    const dp = []
    let ans = 0
    for (let i = 0; i < arr.length; i++) {
        if (i && arr[i - 1] < 2 * arr[i]) {
            dp[i] = dp[i - 1] + 1
        } else {
            dp[i] = 1
        }
        if (dp[i] >= k + 1) ans++
    }
            // console.log(dp)
    return ans
}

'use strict';

function solve(n, a) {
  const map = new Map();
  for (let [i, num] of a.entries()) {
    if (!map.has(num)) map.set(num, []);
    map.get(num).push(i);
  }
  let res = [1, a[0], 0, 0];
  for (let [num, list] of map) {
    const sum = [];
    let l = 0,
        r = 0;
    sum[0] = 1;
    for (let i = 1; i < list.length; i++) {
      var _list;
      const cur = sum[i - 1] - (list[i] - ((_list = list[i - 1]) !== null && _list !== void 0 ? _list : -1) - 1) + 1;
      if (cur > 0) {
        r = i;
      } else {
        l = i;
        r = i;
      }
      sum[i] = Math.max(0, cur);
      if (res[0] < sum[i]) {
        res = [sum[i], num, list[l], list[r]];
      }
    }
  }
  const [, x, y, z] = res;
  return `${x} ${y + 1} ${z + 1}`;
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const n = Number(await read());
      const a = (await read()).split(' ').map(Number);
      res[i] = solve(n, a);
    }
    return 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', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});