var inputnumber = readline();
var least = 0;
for(var i = 0; i < inputnumber ; i++){
    var arr = readline().split(" ");
    var num = arr[0]
    var n = arr [1];
    least = (2+(n-1)*2)*n/2;
    if(least > num || n%2 != num%2){
        print("NO");
    }else{
        print("YES");
    };
};

const processData = (lines) => {
  const num = +lines[0]
  for (let j =0; j<num; j++) {
    const [n, k] = lines[j+1].split(' ').map(x => +x)
    const r = n-k
    if (r >= 0 && r%2 == 0 && n >= k*k) {
      console.log('YES')
    } else {
      console.log('NO')
    }
  }
}

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

'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(/\W+/) // splits at spaces or new lines
    .map(string => {
      return string.trim();
    });

  main();
});

function readline() {
  return inputString[currentLine++];
}
// Make a Snippet for the code above this and then write your logic in main();

function main() {
  const n = readline(); // number of test cases

  for (let i = 0; i < n; i++) {
    const n = readline(); // test input 1
    const k = readline(); // test input 2
    const result = solution(n, k);
    console.log(result);
  }
}

function solution(n, k) {
  if (k * k <= n && n % 2 === k % 2) return 'YES';
  else return 'NO';
}

// console.log(solution(16, 4));

// Q: https://codeforces.com/contest/1327/problem/A

const readline = require('readline');

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

let input = [];

rl.on('line', lineInput => {
	input.push(lineInput);
});

rl.on('close', () => {
    const test = parseInt(input[0]);
    let n, k;
    for (let t = 0; t < test; t++) {
        [n, k] = input[t + 1].split(' ').map((a) => parseInt(a));
        console.log(solution(n, k));
    }
});

function solution(n, k) {
    if (Math.pow(k, 2) > n || n%2 !== k%2) {
        return 'NO';
    }
    return 'YES'
}

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

let input = ''
let ptr = 0
process.stdin.on('data', (i) => {
  input += i
})

process.stdin.on('end', () => {
  input = input.split('\n').map(a => a.replace(/(\r\n|\n|\r)/gm, ''))
  main()
})

function wr(...x) {
  console.log(...x)
}

function sort(a, inc = true) {
  if(inc) a.sort((a, b) => a - b)
  else a.sort((a, b) => b - a)
}

function readInt() {
  return parseInt(input[ptr++])
}

function readString() {
  return input[ptr++]
}

function readStrings() {
  return input[ptr++].split(' ')
}

function readInts() {
  return input[ptr++].split(' ').map(a => parseInt(a))
}

function main() {
  let t = readInt()
  while(t--) {
    const [n, k] = readInts()
    if(n % 2 === k % 2) {
      if(k * k <= n) {
        wr('YES')
      }
      else wr('NO')
    }
    else wr('NO')
  }
}

const processData = (lines) => {
  const num = +lines[0]
  for (let j =0; j<num; j++) {
    const [a, b] = lines[j+1].split(' ').map(x => +x)
    if (((a & 1) == (b & 1)) && (a >= (2 + (b - 1) * 2) * b / 2)) {
      console.log('YES')
    } else {
      console.log('NO')
    }
  }
}

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

var len = readline()
for (var i = 0;i < len;i ++) {
  var item = readline().split(' ');
  if (item[0]%2 == item[1]%2 && Number(item[0]) >= Number(item[1]) * Number(item[1])) {
    print('YES')
  } else {print('NO')}
}

var inputnumber = readline();
var least = 0;
for(var i = 0; i < inputnumber ; i++){
    var arr = readline().split(" ");
    var num = arr[0]
    var n = arr [1];
    least = (2+(n-1)*2)*n/2;
    if(least > num || n%2 != num%2){
        print("FALSE");
    }else{
        print("TRUE");
    };
};

var len = readline()
for (var i = 0;i < len;i ++) {
  var item = readline().split(' ');
  if (item[0]%2 == item[1]%2 && item[0] >= item[1]) {
    print('YES')
  } else {print('NO')}
}

var len = readline()
for (var i = 0;i < len;i ++) {
  var item = readline().split(' ');
  if (item[0]%2 == item[1]%2 && Number(item[0]) >= Number(item[1])) {
    print('YES')
  } else {print('NO')}
}

var arr = readline().split(' ')
for (var i = 1;i < arr.length;i ++) {
  var item = arr[i].split(' ');
  if (item[0]%2 == item[1]%2) {
    print('YES')
  } else {print('NO')}
}

var len = readline()
for (var i = 0;i < len;i ++) {
  var item = readline().split(' ');
  if (item[0]%2 == item[1]%2) {
    print('YES')
  } else {print('NO')}
}

const processData = (lines) => {
  const num = +lines[0]
  for (let j =0; j<num; j++) {
    const [n, k] = lines[j+1].split(' ').map(x => +x)
    const r = n-(k-1)
    if (r >= 0 && r%2 !== 0) {
      console.log('YES')
    } else {
      console.log('NO')
    }
  }
}

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

const processData = (lines) => {
  const num = +lines[0]
  for (let j =0; j<num; j++) {
    const [n, k] = lines[j+1].split(' ').map(x => +x)
    const r = n-k
    if (r >= 0 && r%2 === 0 && n > Math.pow(2, k)) {
      console.log('YES')
    } else {
      console.log('NO')
    }
  }
}

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

const processData = (lines) => {
  const num = +lines[0]
  for (let j =0; j<num; j++) {
    const [n, k] = lines[j+1].split(' ').map(x => +x)
    const r = n-k
    if (r >= 0 && r%2 == 0 && Math.pow(2, k) <= n) {
      console.log('YES')
    } else {
      console.log('NO')
    }
  }
}

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

const processData = (lines) => {
  const num = +lines[0]
  for (let j =1; j<num; j++) {
    const [n, k] = lines[j+1].split(' ').map(x => +x)
    const r = n-(k-1)
    if (r <= 0 || r%2 === 0) {
      console.log('NO')
    } else {
      console.log('YES')
    }
  }
}

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

const processData = (lines) => {
  const num = +lines[0]
  for (let j =0; j<num; j++) {
    const [n, k] = lines[j+1].split(' ').map(x => +x)
    const r = n-(k-1)
    if (r <= 0 || r%2 === 0) {
      console.log('NO')
    } else {
      console.log('YES')
    }
  }
}

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

'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(/\W+/) // splits at spaces or new lines
    .map(string => {
      return string.trim();
    });

  main();
});

function readline() {
  return inputString[currentLine++];
}
// Make a Snippet for the code above this and then write your logic in main();

function main() {
  const n = readline(); // number of test cases

  for (let i = 0; i < n; i++) {
    const n = readline(); // test input 1
    const k = readline(); // test input 2
    const result = solution(n, k);
    console.log(result);
  }
}

function solution(a, b) {
  if (a % b === 0) return 'YES';
  else {
    return 'NO';
  }
}

const processData = (lines) => {
  const num = +lines[0]
  for (let j =0; j<num; j++) {
    const [n, k] = lines[j+1].split(' ').map(x => +x)
    const r = n-k
    if (r >= 0 && r%2 === 0 ) {
      console.log('YES')
    } else {
      console.log('NO')
    }
  }
}

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

const processData = (lines) => {
  const num = +lines[0]
  for (let j =0; j<num; j++) {
    const [n, k] = lines[j+1].split(' ').map(x => +x)
    if (n%2 === k%2 && n >= k) {
      console.log('YES')
    } else {
      console.log('NO')
    }
  }
}

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

const processData = (lines) => {
  const num = +lines[0]
  for (let j =0; j<num; j++) {
    const [a, b] = lines[j+1].split(' ').map(x => +x)
    if ((a & 1) == (b & 1) && (a >= (2 + b - 1) * b / 2)) {
      console.log('YES')
    } else {
      console.log('NO')
    }
  }
}

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


let lineCount = 0

require('readline').createInterface({
  input: process.stdin,
}).on('line', (line) => {
  if (lineCount++ !== 1) {
    return
  }
  const nums = line.split(' ').map(s => parseInt(s))
  nums.sort((a, b) => a - b)
  let result = 0
  nums.forEach((num) => {
    if (num > result) {
      result++
    }
  })
  process.stdout.write(result + '\n')
  process.exit()
})

class CodeForcesNodeJS
{
    constructor()
    {
        this.stdin_input = "";
        this.arrInput = [];
        this.nInputIndex = 0;
    }

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

        process.stdin.on("data", this.onData.bind(this));
        process.stdin.on("end", this.solveWrapper.bind(this));
    }

    onData(input)
    {
        this.stdin_input += input;
    }

    prepareInput(input)
    {
        this.arrInput = input.replace(/\n/g, " ").split(" ");
        this.nInputIndex = 0;
    }

    nextInput(strConvertType = null)
    {
        let _response = this.arrInput[this.nInputIndex];
        ++this.nInputIndex;

        if(typeof _response === "undefined")
        {
            throw new Error("End of input reached.");
        }

        if(typeof _response !== "string")
        {
            throw new Error("Internal Error: input at current step not an instance of string!");
        }

        switch(strConvertType)
        {
            case "Integer":
                _response = parseInt(_response, 10);
                break;

            case null:
                break;

            default:
                throw new Error("Unrecognized type for conversion!");
        }

        return _response;

    }

    async solveWrapper()
    {
        this.prepareInput(this.stdin_input);
        await this.solve().catch(console.error);
    }

    solve()
    {
        const nCount = this.nextInput("Integer");
        const arrValues = this.arrInput
            .slice(1, nCount + 1)
            .map(strValue => parseInt(strValue, 10))
            .sort((a, b) => a == b ? 0 : ( a < b ? -1 : 1));

        let nIndex = 0;
        arrValues.forEach(nValue => {
            if(nValue > nIndex)
            {
                ++nIndex;
            }
        });
        process.stdout.write(nIndex + '\n');
    }
}

// process.stdout.write("YES\n");
const _solver = new CodeForcesNodeJS();
_solver.start();

let fs =  require('fs');
let input = fs.readFileSync(0, 'ascii')
    .split('\n')
    .map(s => s.trim().split(" ").map(Number))
;

let [n, args] = input;
let days = 0;
args.sort((a,b) => a - b);

args.forEach(num => {
    if (days < num) days++;
});
console.log(days);

'use strict';

function main() {
  let n = parseInt(next()), i = 0;
  let ar = [];
  for(i = 0; i < n; i++) {
    ar.push(parseInt(next()));
  }
  //ar.sort(); this is wrong sorts alphabetically on literals
  ar.sort((a, b) => a-b); // sort using compare function to sort integers -ve means a comes first
  let cur = 0;
  for(i = 0; i < n; i++) {
    if(ar[i] > cur)
      cur++;
  }
  println(cur);
}

//process.stdin.resume();
process.stdin.setEncoding('ascii');

let input = '';
process.stdin.on('data', (chunk) => input += chunk);

// next: reads next value from input.
let inputCursor = 0;
let next = () => {
  return inputCursor < input.length ? input[inputCursor++] : '';
};

//print: print one or more space seperated values.
let print = (...data) => {
  for(let i = 0; i < data.length; i++) {
    process.stdout.write(data[i] + (i < data.length - 1 ? ' ' : ''));
  }
};

//println: print one or more space seperated values then a line feed.
let println = (...data) => {
  print(...data);
  print('\n');
};

//after the the input is read.
process.stdin.on('end', () => {
    input = input.trim().split(/[\s]+/);
    main();
});

(function () {
    var read = {
        number: function () {
            return +readline();
        },
        arr: function (divider) {
            return readline().split(divider ? divider : '');
        },
        arrNumber: function (divider) {
            return readline()
                .split(divider ? divider : '')
                .map(item => +item);
        }
    }

    var n = read.number();
    var a = read.arrNumber(' ').sort((a,b) => a-b);

    var res = 0;
    var day = 1;

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

var n = readline();
var a = readline().split(' ').map(Number);

function comp(a, b){
	if(a > b) return 1;
	if(a < b) return -1;
	return 0;
}

var now = 1;
var k = 0;
a.sort(comp);
for (var i = 0; i < a.length; i++) {
	if(a[i] >= now){
		k++;
		now++;
	}
}
var b = [];
for (var i = 0; i < 1e6; i++) {
	b.shift(i);
}
print(k);

var n = readline();
var a = readline().split(' ').map(Number);

var b = new Array(3213213321);

function comp(a, b){
	if(a > b) return 1;
	if(a < b) return -1;
	return 0;
}

var now = 1;
var k = 0;
a.sort(comp);
for (var i = 0; i < a.length; i++) {
	if(a[i] >= now){
		k++;
		now++;
	}
}
var b = [];
for (var i = 0; i < 1e6; i++) {
	b.shift(i);
}
print(k);

var n = readline();
var a = readline().split(' ').map(Number);

function comp(a, b){
	if(a > b) return 1;
	if(a < b) return -1;
	return 0;
}

var now = 1;
var k = 0;
a.sort(comp);
for (var i = 0; i < a.length; i++) {
	if(a[i] >= now){
		k++;
		now++;
	}
}
var b = [];
for (var i = 0; i < 1e5; i++) {
	b.shift(i);
}
print(k);

var n = readline();
var a = readline().split(' ').map(Number);
function comp(a, b){
	if(a > b) return 1;
	if(a < b) return -1;
	return 0;
}

var now = 1;
var k = 0;
a.sort(comp);
for (var i = 0; i < a.length; i++) {
	if(a[i] >= now){
		k++;
		now++;
	}
}
print(k);

class CodeForcesNodeJS
{
    constructor()
    {
        this.stdin_input = "";
        this.arrInput = [];
        this.nInputIndex = 0;
    }

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

        process.stdin.on("data", this.onData.bind(this));
        process.stdin.on("end", this.solveWrapper.bind(this));
    }

    onData(input)
    {
        this.stdin_input += input;
    }

    prepareInput(input)
    {
        this.arrInput = input.replace(/\n/g, " ").split(" ");
        this.nInputIndex = 0;
    }

    nextInput(strConvertType = null)
    {
        let _response = this.arrInput[this.nInputIndex];
        ++this.nInputIndex;

        if(typeof _response === "undefined")
        {
            throw new Error("End of input reached.");
        }

        if(typeof _response !== "string")
        {
            throw new Error("Internal Error: input at current step not an instance of string!");
        }

        switch(strConvertType)
        {
            case "Integer":
                _response = parseInt(_response, 10);
                break;

            case null:
                break;

            default:
                throw new Error("Unrecognized type for conversion!");
        }

        return _response;

    }

    async solveWrapper()
    {
        this.prepareInput(this.stdin_input);
        await this.solve().catch(console.error);
    }

    solve()
    {
        // const nCount = this.nextInput("Integer");
        const arrValues = this.arrInput
            .slice(1)
            .map(strValue => parseInt(strValue, 10))
            .sort((a, b) => a > b);

        let nIndex = 0;
        arrValues.forEach(nValue => {
            if(nValue > nIndex)
            {
                ++nIndex;
            }
        });
        process.stdout.write(nIndex + '\n');
    }
}

// process.stdout.write("YES\n");
const _solver = new CodeForcesNodeJS();
_solver.start();

class CodeForcesNodeJS
{
    constructor()
    {
        this.stdin_input = "";
        this.arrInput = [];
        this.nInputIndex = 0;
    }

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

        process.stdin.on("data", this.onData.bind(this));
        process.stdin.on("end", this.solveWrapper.bind(this));
    }

    onData(input)
    {
        this.stdin_input += input;
    }

    prepareInput(input)
    {
        this.arrInput = input.replace(/\n/g, " ").split(" ");
        this.nInputIndex = 0;
    }

    nextInput(strConvertType = null)
    {
        let _response = this.arrInput[this.nInputIndex];
        ++this.nInputIndex;

        if(typeof _response === "undefined")
        {
            throw new Error("End of input reached.");
        }

        if(typeof _response !== "string")
        {
            throw new Error("Internal Error: input at current step not an instance of string!");
        }

        switch(strConvertType)
        {
            case "Integer":
                _response = parseInt(_response, 10);
                break;

            case null:
                break;

            default:
                throw new Error("Unrecognized type for conversion!");
        }

        return _response;

    }

    async solveWrapper()
    {
        this.prepareInput(this.stdin_input);
        await this.solve().catch(console.error);
    }

    solve()
    {
        const nCount = this.nextInput("Integer");
        const arrValues = this.arrInput
            .slice(1)
            .map(strValue => parseInt(strValue, 10))
            .sort((a, b) => a <= b);

        let nIndex = 0;
        arrValues.forEach(nValue => {
            if(nValue > nIndex)
            {
                ++nIndex;
            }
        });
        process.stdout.write(nIndex + '\n');
    }
}

// process.stdout.write("YES\n");
const _solver = new CodeForcesNodeJS();
_solver.start();

class CodeForcesNodeJS
{
    constructor()
    {
        this.stdin_input = "";
        this.arrInput = [];
        this.nInputIndex = 0;
    }

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

        process.stdin.on("data", this.onData.bind(this));
        process.stdin.on("end", this.solveWrapper.bind(this));
    }

    onData(input)
    {
        this.stdin_input += input;
    }

    prepareInput(input)
    {
        this.arrInput = input.replace(/\n/g, " ").split(" ");
        this.nInputIndex = 0;
    }

    nextInput(strConvertType = null)
    {
        let _response = this.arrInput[this.nInputIndex];
        ++this.nInputIndex;

        if(typeof _response === "undefined")
        {
            throw new Error("End of input reached.");
        }

        if(typeof _response !== "string")
        {
            throw new Error("Internal Error: input at current step not an instance of string!");
        }

        switch(strConvertType)
        {
            case "Integer":
                _response = parseInt(_response, 10);
                break;

            case null:
                break;

            default:
                throw new Error("Unrecognized type for conversion!");
        }

        return _response;

    }

    async solveWrapper()
    {
        this.prepareInput(this.stdin_input);
        await this.solve().catch(console.error);
    }

    solve()
    {
        const nCount = this.nextInput("Integer");
        const arrValues = this.arrInput
            .slice(1, nCount)
            .map(strValue => parseInt(strValue, 10))
            .sort((a, b) => a > b);

        if(nCount > 1000)
        {
            console.log(arrValues);
        }

        let nIndex = 0;
        arrValues.forEach(nValue => {
            if(nValue > nIndex)
            {
                ++nIndex;
            }
        });
        process.stdout.write(nIndex + '\n');
    }
}

// process.stdout.write("YES\n");
const _solver = new CodeForcesNodeJS();
_solver.start();

class CodeForcesNodeJS
{
    constructor()
    {
        this.stdin_input = "";
        this.arrInput = [];
        this.nInputIndex = 0;
    }

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

        process.stdin.on("data", this.onData.bind(this));
        process.stdin.on("end", this.solveWrapper.bind(this));
    }

    onData(input)
    {
        this.stdin_input += input;
    }

    prepareInput(input)
    {
        this.arrInput = input.replace(/\n/g, " ").split(" ");
        this.nInputIndex = 0;
    }

    nextInput(strConvertType = null)
    {
        let _response = this.arrInput[this.nInputIndex];
        ++this.nInputIndex;

        if(typeof _response === "undefined")
        {
            throw new Error("End of input reached.");
        }

        if(typeof _response !== "string")
        {
            throw new Error("Internal Error: input at current step not an instance of string!");
        }

        switch(strConvertType)
        {
            case "Integer":
                _response = parseInt(_response, 10);
                break;

            case null:
                break;

            default:
                throw new Error("Unrecognized type for conversion!");
        }

        return _response;

    }

    async solveWrapper()
    {
        this.prepareInput(this.stdin_input);
        await this.solve().catch(console.error);
    }

    solve()
    {
        const nCount = this.nextInput("Integer");
        const arrValues = this.arrInput
            .slice(1)
            .map(strValue => parseInt(strValue, 10))
            .sort((a, b) => a <= b);

        let nIndex = 1;
        arrValues.forEach(nValue => {
            if(nValue >= nIndex)
            {
                ++nIndex;
            }
        });
        process.stdout.write(nIndex + '\n');
    }
}

// process.stdout.write("YES\n");
const _solver = new CodeForcesNodeJS();
_solver.start();

class CodeForcesNodeJS
{
    constructor()
    {
        this.stdin_input = "";
        this.arrInput = [];
        this.nInputIndex = 0;
    }

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

        process.stdin.on("data", this.onData.bind(this));
        process.stdin.on("end", this.solveWrapper.bind(this));
    }

    onData(input)
    {
        this.stdin_input += input;
    }

    prepareInput(input)
    {
        this.arrInput = input.replace(/\n/g, " ").split(" ");
        this.nInputIndex = 0;
    }

    nextInput(strConvertType = null)
    {
        let _response = this.arrInput[this.nInputIndex];
        ++this.nInputIndex;

        if(typeof _response === "undefined")
        {
            throw new Error("End of input reached.");
        }

        if(typeof _response !== "string")
        {
            throw new Error("Internal Error: input at current step not an instance of string!");
        }

        switch(strConvertType)
        {
            case "Integer":
                _response = parseInt(_response, 10);
                break;

            case null:
                break;

            default:
                throw new Error("Unrecognized type for conversion!");
        }

        return _response;

    }

    async solveWrapper()
    {
        this.prepareInput(this.stdin_input);
        await this.solve().catch(console.error);
    }

    solve()
    {
        const nCount = this.nextInput("Integer");
        const arrValues = this.arrInput
            .slice(1, nCount)
            .map(strValue => parseInt(strValue, 10))
            .sort((a, b) => a > b);

        let nIndex = 0;
        arrValues.forEach(nValue => {
            if(nValue > nIndex)
            {
                ++nIndex;
            }
        });
        process.stdout.write(nIndex + '\n');
    }
}

// process.stdout.write("YES\n");
const _solver = new CodeForcesNodeJS();
_solver.start();

class CodeForcesNodeJS
{
    constructor()
    {
        this.stdin_input = "";
        this.arrInput = [];
        this.nInputIndex = 0;
    }

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

        process.stdin.on("data", this.onData.bind(this));
        process.stdin.on("end", this.solveWrapper.bind(this));
    }

    onData(input)
    {
        this.stdin_input += input;
    }

    prepareInput(input)
    {
        this.arrInput = input.replace(/\n/g, " ").split(" ");
        this.nInputIndex = 0;
    }

    nextInput(strConvertType = null)
    {
        let _response = this.arrInput[this.nInputIndex];
        ++this.nInputIndex;

        if(typeof _response === "undefined")
        {
            throw new Error("End of input reached.");
        }

        if(typeof _response !== "string")
        {
            throw new Error("Internal Error: input at current step not an instance of string!");
        }

        switch(strConvertType)
        {
            case "Integer":
                _response = parseInt(_response, 10);
                break;

            case null:
                break;

            default:
                throw new Error("Unrecognized type for conversion!");
        }

        return _response;

    }

    async solveWrapper()
    {
        this.prepareInput(this.stdin_input);
        await this.solve().catch(console.error);
    }

    solve()
    {
        // const nCount = this.nextInput("Integer");
        const arrValues = this.arrInput
            .slice(1)
            .map(strValue => parseInt(strValue, 10))
            .sort((a, b) => a <= b);

        console.log(arrValues);
        let nIndex = 0;
        arrValues.forEach(nValue => {
            if(nValue > nIndex)
            {
                ++nIndex;
            }
        });
        process.stdout.write(nIndex + '\n');
    }
}

// process.stdout.write("YES\n");
const _solver = new CodeForcesNodeJS();
_solver.start();

class CodeForcesNodeJS
{
    constructor()
    {
        this.stdin_input = "";
        this.arrInput = [];
        this.nInputIndex = 0;
    }

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

        process.stdin.on("data", this.onData.bind(this));
        process.stdin.on("end", this.solveWrapper.bind(this));
    }

    onData(input)
    {
        this.stdin_input += input;
    }

    prepareInput(input)
    {
        this.arrInput = input.replace(/\n/g, " ").split(" ");
        this.nInputIndex = 0;
    }

    nextInput(strConvertType = null)
    {
        let _response = this.arrInput[this.nInputIndex];
        ++this.nInputIndex;

        if(typeof _response === "undefined")
        {
            throw new Error("End of input reached.");
        }

        if(typeof _response !== "string")
        {
            throw new Error("Internal Error: input at current step not an instance of string!");
        }

        switch(strConvertType)
        {
            case "Integer":
                _response = parseInt(_response, 10);
                break;

            case null:
                break;

            default:
                throw new Error("Unrecognized type for conversion!");
        }

        return _response;

    }

    async solveWrapper()
    {
        this.prepareInput(this.stdin_input);
        await this.solve().catch(console.error);
    }

    solve()
    {
        const nCount = this.nextInput("Integer");
        const arrValues = this.arrInput
            .slice(1, nCount)
            .map(strValue => parseInt(strValue, 10))
            .sort((a, b) => a == b ? 0 : ( a < b ? -1 : 1));

        let nIndex = 0;
        arrValues.forEach(nValue => {
            if(nValue > nIndex)
            {
                ++nIndex;
            }
        });
        process.stdout.write(nIndex + '\n');
    }
}

// process.stdout.write("YES\n");
const _solver = new CodeForcesNodeJS();
_solver.start();

class CodeForcesNodeJS
{
    constructor()
    {
        this.stdin_input = "";
        this.arrInput = [];
        this.nInputIndex = 0;
    }

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

        process.stdin.on("data", this.onData.bind(this));
        process.stdin.on("end", this.solveWrapper.bind(this));
    }

    onData(input)
    {
        this.stdin_input += input;
    }

    prepareInput(input)
    {
        this.arrInput = input.replace(/\n/g, " ").split(" ");
        this.nInputIndex = 0;
    }

    nextInput(strConvertType = null)
    {
        let _response = this.arrInput[this.nInputIndex];
        ++this.nInputIndex;

        if(typeof _response === "undefined")
        {
            throw new Error("End of input reached.");
        }

        if(typeof _response !== "string")
        {
            throw new Error("Internal Error: input at current step not an instance of string!");
        }

        switch(strConvertType)
        {
            case "Integer":
                _response = parseInt(_response, 10);
                break;

            case null:
                break;

            default:
                throw new Error("Unrecognized type for conversion!");
        }

        return _response;

    }

    async solveWrapper()
    {
        this.prepareInput(this.stdin_input);
        await this.solve().catch(console.error);
    }

    solve()
    {
        const nCount = this.nextInput("Integer");
        const arrValues = this.arrInput
            .slice(1, nCount)
            .map(strValue => parseInt(strValue, 10))
            .sort((a, b) => a == b ? 0 : ( a < b ? -1 : 1));

        if(nCount > 1000)
        {
            console.log(arrValues);
        }

        let nIndex = 0;
        arrValues.forEach(nValue => {
            if(nValue > nIndex)
            {
                ++nIndex;
            }
        });
        process.stdout.write(nIndex + '\n');
    }
}

// process.stdout.write("YES\n");
const _solver = new CodeForcesNodeJS();
_solver.start();

class CodeForcesNodeJS
{
    constructor()
    {
        this.stdin_input = "";
        this.arrInput = [];
        this.nInputIndex = 0;
    }

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

        process.stdin.on("data", this.onData.bind(this));
        process.stdin.on("end", this.solveWrapper.bind(this));
    }

    onData(input)
    {
        this.stdin_input += input;
    }

    prepareInput(input)
    {
        this.arrInput = input.replace(/\n/g, " ").split(" ");
        this.nInputIndex = 0;
    }

    nextInput(strConvertType = null)
    {
        let _response = this.arrInput[this.nInputIndex];
        ++this.nInputIndex;

        if(typeof _response === "undefined")
        {
            throw new Error("End of input reached.");
        }

        if(typeof _response !== "string")
        {
            throw new Error("Internal Error: input at current step not an instance of string!");
        }

        switch(strConvertType)
        {
            case "Integer":
                _response = parseInt(_response, 10);
                break;

            case null:
                break;

            default:
                throw new Error("Unrecognized type for conversion!");
        }

        return _response;

    }

    async solveWrapper()
    {
        this.prepareInput(this.stdin_input);
        await this.solve().catch(console.error);
    }

    solve()
    {
        const nCount = this.nextInput("Integer");
        const arrValues = this.arrInput
            .slice(1, nCount)
            .map(strValue => parseInt(strValue, 10))
            .sort((a, b) => a > b);

        console.log(arrValues);
        let nIndex = 0;
        arrValues.forEach(nValue => {
            if(nValue > nIndex)
            {
                ++nIndex;
            }
        });
        process.stdout.write(nIndex + '\n');
    }
}

// process.stdout.write("YES\n");
const _solver = new CodeForcesNodeJS();
_solver.start();

let fs =  require('fs');
let input =  fs.readFileSync(0, 'ascii')
    .split('\n')
    .map(s => s.trim().split(" ").map(Number))
;

let [n, args] = input;
let days = 1;
let index = 1;
let length = args.length;
args.sort();
while (index < length) {
    if (args[index] >= days + 1) {
        days++;
    };
    index++;
}

console.log(days)

let fs =  require('fs');
let input = fs.readFileSync(0, 'ascii')
    .split('\n')
    .map(s => s.trim().split(" ").map(Number))
;

let [n, args] = input;
let days = 0;
args.sort();

args.forEach(num => {
    if (days < num) days++;
});
console.log(days);

'use strict';

function main() {
  let n = parseInt(next()), i = 0;
  let st = new Set();
  for(i = 0; i < n; i++) {
    st.add(parseInt(next()));
  }
  println(st.size);
}

//process.stdin.resume();
process.stdin.setEncoding('ascii');

let input = '';
process.stdin.on('data', (chunk) => input += chunk);

// next: reads next value from input.
let inputCursor = 0;
let next = () => {
  return inputCursor < input.length ? input[inputCursor++] : '';
};

//print: print one or more space seperated values.
let print = (...data) => {
  for(let i = 0; i < data.length; i++) {
    process.stdout.write(data[i] + (i < data.length - 1 ? ' ' : ''));
  }
};

//println: print one or more space seperated values then a line feed.
let println = (...data) => {
  print(...data);
  print('\n');
};

//after the the input is read.
process.stdin.on('end', () => {
    input = input.trim().split(/[\s]+/);
    main();
});

'use strict';

function main() {
  let n = parseInt(next()), i = 0;
  let ar = [];
  for(i = 0; i < n; i++) {
    ar.push(parseInt(next()));
  }
  console.log(ar);
  //ar.sort(); this is wrong sorts alphabetically on literals
  ar.sort((a, b) => a-b); // sort using compare function to sort integers -ve means a comes first
  console.log(ar);

  let cur = 0;
  for(i = 0; i < n; i++) {
    if(ar[i] > cur)
      cur++;
  }
  println(cur);
}

//process.stdin.resume();
process.stdin.setEncoding('ascii');

let input = '';
process.stdin.on('data', (chunk) => input += chunk);

// next: reads next value from input.
let inputCursor = 0;
let next = () => {
  return inputCursor < input.length ? input[inputCursor++] : '';
};

//print: print one or more space seperated values.
let print = (...data) => {
  for(let i = 0; i < data.length; i++) {
    process.stdout.write(data[i] + (i < data.length - 1 ? ' ' : ''));
  }
};

//println: print one or more space seperated values then a line feed.
let println = (...data) => {
  print(...data);
  print('\n');
};

//after the the input is read.
process.stdin.on('end', () => {
    input = input.trim().split(/[\s]+/);
    main();
});

'use strict';

function main() {
  let n = parseInt(next()), i = 0;
  let ar = [];
  for(i = 0; i < n; i++) {
    ar.push(parseInt(next()));
  }
  ar.sort();
  let cur = 0;
  for(i = 0; i < n; i++) {
    if(ar[i] > cur)
      cur++;
  }
  println(cur);
}

//process.stdin.resume();
process.stdin.setEncoding('ascii');

let input = '';
process.stdin.on('data', (chunk) => input += chunk);

// next: reads next value from input.
let inputCursor = 0;
let next = () => {
  return inputCursor < input.length ? input[inputCursor++] : '';
};

//print: print one or more space seperated values.
let print = (...data) => {
  for(let i = 0; i < data.length; i++) {
    process.stdout.write(data[i] + (i < data.length - 1 ? ' ' : ''));
  }
};

//println: print one or more space seperated values then a line feed.
let println = (...data) => {
  print(...data);
  print('\n');
};

//after the the input is read.
process.stdin.on('end', () => {
    input = input.trim().split(/[\s]+/);
    main();
});


// const bg = require('big-integer')
process.stdin.resume()
process.stdin.setEncoding('utf-8')
const MOD1 = 1000000007

function alpha(c = 'A') {
  return [...{
    _b: c === 'A' ? 65 : 97,
    _i: -1,
    [Symbol.iterator]() {
      return this
    },
    next() {
      this._i++
      if (this._i === 26) return {done: true}
      else return {done: false, value: String.fromCharCode(this._i + this._b)}
    }
  }]
}

class rc {
  static construct() {
    this.input = ''
    this.ptr = 0
  }
  static readString() {
    return this.input[this.ptr++]
  }

  static readStrings() {
    return this.input[this.ptr++].split(' ')
  }

  static readInt() {
    return +this.readString()
  }

  static readInts() {
    return this.readStrings().map(a => +a)
  }

  static readBigInteger() {
    return bg(this.readString())
  }

  static readBigIntegers() {
    return this.readStrings().map(a => bg(a))
  }
}

function wr(...x) {
  console.log(...x)
}

function sort(a, inc = true) {
  if (inc) a.sort((a, b) => a - b)
  else a.sort((a, b) => b - a)
}

rc.construct()
// bc.construct()
process.stdin.on('data', (i) => {
  rc.input += i
})

process.stdin.on('end', () => {
  rc.input = rc.input.split('\n').map(a => a.replace(/(\r\n|\n|\r)/gm, ''))
  main()
})

function main() {
  let t = rc.readInts()
  while(t--) {
    const n = rc.readInt()
    const a1 = rc.readInts()
    const a3 = new Array(n)
    sort(a1)
    let ans = -1
    for(let i = 1; i <= 1024; i++) {
      for(let j = 0; j < n; j++) {
        a3[j] = (a1[j] ^ i)
      }
      sort(a3)
      if(check(n, a1, a3)) {
        ans = i
        break
      }
    }
    wr(ans)
    function check(n, a1, a3) {
      for(let j = 0; j < n; j++) {
        if(a1[j] !== a3[j]) return  false
      }
      return true
    }
  }
}

//↓見なくていいよ！ここから--------------------------------------------------
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);
  myerr("-----start-----");
  var start = new Date();
  Main();
  var end = new Date() - start;
  myerr("----- end -----");
  myerr("time : " + (end) + "ms");
  myerr("memory : " + Math.round(process.memoryUsage().heapTotal / 1024) + "KB");
});
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){
  var returnObj = {
    list : input, index : 0, max : input.length,
    hasNext : function(){return (this.index < this.max);},
    next : function(){if(!this.hasNext()){throw "ArrayIndexOutOfBoundsException これ以上ないよ";}else{var returnInput = this.list[this.index];this.index++;return returnInput;}}
  };
  return returnObj;
}
function myout(s){console.log(s);}
function myerr(s){console.error("debug:" + require("util").inspect(s,false,null));}
//[no]要素の扱い。数値型
//不明値、異常時:引数そのまま返す 1:数値へ変換
//2:半角SPで分割 4:半角SPで分割し、数値配列へ
//6:1文字で分割  7:1文字で分割し、数値配列へ
//8:半角SPで結合 9:改行で結合 0:文字なしで結合
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(){
	var t = nextInt();
	var output = new Array(t);
	for(var i = 0; i < t; i++){
		var N = nextInt();
		var list = nextIntArray();
		var tmp = JSON.parse(JSON.stringify(list));
		var set = new Set(tmp);
		for(var j = 1; j <= 1024; j++){
			var isNO = false;
			for(var k = 0; k < N; k++){
				if(!set.has(list[k] ^ j)){
					isNO = true;
					break;
				}
			}
			if(isNO){
				continue;
			}else{
				output[i] = j;
				break;
			}
		}
		if(output[i] == null){
			output[i] = -1;
		}
	}
	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.shift() * 2;
    for (let i = 1; i < testCases; i += 2) {
        let obj = {};
        let arr = data[i].slice(0,-1).split(' ');
        for (let j = 0; j < arr.length; j += 1) obj[arr[j]] = 1;
        let j = 1;
        while (j < 1025) {
            let bit = true;
            for (let k = 0; k < arr.length; k += 1) {
                let xor = arr[k] ^ j;
                if (!obj[xor]) {
                    bit = false;
                    break;
                }
            }
            if (bit) {
                console.log(j);
                break;
            }
            j += 1;
        }
        if (j === 1025) console.log(-1);
    }
}

    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.shift() * 2;
    for (let i = 1; i < testCases; i += 2) {
        // let numEle = data[i].split(' ')[1] * 1;
        let obj = {};
        let arr = data[i].split(' ');
        for (let j = 0; j < arr.length; j += 1) obj[arr[j]] = 1;
        let j = 1;
        while (j < 1025) {
            let bit = true;
            for (let k = 0; k < arr.length; k += 1) {
                let xor = arr[k] ^ j;
                if (!obj[xor]) {
                    bit = false;
                    break;
                }
            }
            if (bit) {
                console.log(j);
                break;
            }
            j += 1;
        }
        if (j === 1025) console.log(-1);
    }
}


var z;
var n , k;
var a = [];
var p = [];

function readArray()
{
    return readline().split( ' ' ).map( Number );
}

function check( sz )
{
    if ( sz == 0 ) return { ok : true , left : 0 , right : 0 , whichOnes : undefined };

    var f = new Array( n );
    for ( var i=0 ; i<n ; i++ ) f[i] = 0;

    var j=0 , i=0;
    var cnt = 0 , ret = 0;
    var found = false;
    var end = -8e9;
    var whichOnes = [];

    while ( i<2*n )
    {
        // find a closing bracket
        while ( p[i].type == +1 ) i++;

        while ( (j<2*n) && (p[i].pos - p[j].pos + 1) >= sz )
        {
            var id = p[j].id;
            if ( p[j].type == +1 )
            {
                if ( f[id] == 0 ) f[id] = 1 , cnt++;
            }
            j++;
        }

        if ( cnt > ret )
        {
            ret = cnt;
            if ( ret >= k && found == false )
            {
                var c = 0;
                for ( var l=0 ; l<n && c<k ; l++ )
                    if ( f[l] == 1 )
                    {
                        c++;
                        whichOnes.push( l+1 );
                    }

                found = true;
            }
        }

        var id = p[i].id;
        if ( f[id] == 0 ) f[id] = 2;
        else if ( f[id] == 1 ) f[id] = 2 , cnt--;
        i++;
    }

    return { ok : found , right : end , left : end - sz + 1 , whichOnes : whichOnes };
}

function main()
{
    z = readArray();
    n = z[0] , k = z[1];
    for ( var i=0 ; i<n ; i++ ) a[i] = readArray();

    for ( var i=0 ; i<n ; i++ )
    {
        p.push( { pos:a[i][0] , type:+1 , id:i } );
        p.push( { pos:a[i][1] , type:-1 , id:i } );
    }
    p.sort( function( fi , se ) {
        if ( fi.pos != se.pos ) return fi.pos - se.pos;
        if ( fi.type != se.type ) return fi.type==+1 ? -1 : +1;
        return fi.id - se.id;
    } );

    /*
    print();
    p.forEach( function( z ) {
        print( z.pos + " , " + z.type + " , " + z.id );
    } );
    print();
    */

    var l = 0 , r = 2e9 + 5;
    while ( l < r )
    {
        var mid = Math.floor( ( l + r ) / 2 );
        //print( l + " " + r + " " + mid );
        if ( check( mid ).ok == true ) l = mid+1;
        else r = mid;
    }

    var ans = check( r-1 );
    if ( ans.whichOnes == undefined )
    {
        ans.whichOnes = []
        for ( var i=0 ; i<k ; i++ )
            ans.whichOnes.push( i+1 );
    }

    print( r-1 );
    // print( ans.left + " " + ans.right );
    ans.whichOnes.forEach( function(x) { write( x + " " ); } );
    print();

} main();

/*
    About the solution, Note that if there is any answer exists with size X,
                        There is also such an answer that it's right bound is
                        on the one of the end boundaries of one of the coupons.

                ( Suppose input boundaries are [Li..Ri] )
        Proof : Suppose you have found an answer with size X that lies on [P..Q] and Q is not
                on the one of the end boundaries(Ri's) of some of the coupons.
                You can always move [P..Q] forward untill Q become equal to one of the Ri's.
*/

var z;
var n , k;
var a = [];
var p = [];

function readArray()
{
    return readline().split( ' ' ).map( Number );
}

function check( sz , which )
{
    // special case
    if ( sz == 0 ) return { ok : true , left : 0 , right : 0 , whichOnes : undefined };

    // status of each boundary : 0=not seen , 1=compeletely in the current boundary , 2=out of ...
    var f = new Array( n );
    for ( var i=0 ; i<n ; i++ ) f[i] = 0;

    var j=0 , i=0;          // i is moving forward and j is keeping distance sz from it
    var cnt = 0 , ret = 0;
    var found = false;
    var end = -8e9;
    var whichOnes = [];

    while ( i<2*n )
    {
        // find a closing bracket ... just closing brackets matter for i
        while ( p[i].type == +1 ) i++;

        // j keeps distance SZ from i
        while ( (j<2*n) && (p[i].pos - p[j].pos + 1) >= sz )
        {
            var id = p[j].id;
            if ( p[j].type == +1 )
            {
                if ( f[id] == 0 ) f[id] = 1 , cnt++;
            }
            j++;
        }

        // update the answer
        if ( cnt > ret )
        {
            ret = cnt;
            if ( ret >= k && found == false )
            {
                if ( which )
                {
                    var c = 0;
                    for ( var l=0 ; l<n && c<k ; l++ )
                        if ( f[l] == 1 )
                        {
                            c++;
                            whichOnes.push( l+1 );
                        }
                }

                found = true;
            }
        }

        var id = p[i].id;
        if ( f[id] == 0 ) f[id] = 2;
        else if ( f[id] == 1 ) f[id] = 2 , cnt--;
        i++;
    }

    return { ok : found , right : end , left : end - sz + 1 , whichOnes : whichOnes };
}

function main()
{
    // get input and prepare it for solving the problem
    z = readArray();
    n = z[0] , k = z[1];
    for ( var i=0 ; i<n ; i++ ) a[i] = readArray();

    for ( var i=0 ; i<n ; i++ )
    {
        p.push( { pos:a[i][0] , type:+1 , id:i } );
        p.push( { pos:a[i][1] , type:-1 , id:i } );
    }
    p.sort( function( fi , se ) {
        if ( fi.pos != se.pos ) return fi.pos - se.pos;
        if ( fi.type != se.type ) return fi.type==+1 ? -1 : +1;
        return fi.id - se.id;
    } );

    // Binary Search on the size
    var l = 0 , r = 2e9 + 5;
    while ( l < r )
    {
        var mid = Math.floor( ( l + r ) / 2 );
        if ( check( mid , false ).ok == true ) l = mid+1;
        else r = mid;
    }

    // Prepare answer for output
    var ans = check( r-1 , true );
    if ( ans.whichOnes == undefined )
    {
        ans.whichOnes = []
        for ( var i=0 ; i<k ; i++ )
            ans.whichOnes.push( i+1 );
    }

    print( r-1 );
    // print( ans.left + " " + ans.right );
    ans.whichOnes.forEach( function(x) { write( x + " " ); } );
    print();

} main();

var z;
var n , k;
var a = [];
var p = [];

function readArray()
{
    return readline().split( ' ' ).map( Number );
}

function check( sz )
{
    if ( sz == 0 ) return { ok : true , left : 0 , right : 0 , whichOnes : undefined };

    var f = new Array( n );
    for ( var i=0 ; i<n ; i++ ) f[i] = 0;

    var j=0 , i=0;
    var cnt = 0 , ret = 0;
    var found = false;
    var end = -8e9;
    var whichOnes = [];

    while ( i<2*n )
    {
        // find a closing bracket
        while ( p[i].type == +1 ) i++;

        while ( (j<2*n) && (p[i].pos - p[j].pos + 1) >= sz )
        {
            var id = p[j].id;
            if ( p[j].type == +1 )
            {
                if ( f[id] == 0 ) f[id] = 1 , cnt++;
            }
            j++;
        }

        if ( cnt > ret )
        {
            ret = cnt;
            if ( ret >= k && found == false )
            {
                for ( var l=0 ; l<n ; l++ )
                    if ( f[l] == 1 )
                        whichOnes.push( l+1 );

                found = true;
            }
        }

        var id = p[i].id;
        if ( f[id] == 0 ) f[id] = 2;
        else if ( f[id] == 1 ) f[id] = 2 , cnt--;
        i++;
    }

    return { ok : found , right : end , left : end - sz + 1 , whichOnes : whichOnes };
}

function main()
{
    z = readArray();
    n = z[0] , k = z[1];
    for ( var i=0 ; i<n ; i++ ) a[i] = readArray();

    for ( var i=0 ; i<n ; i++ )
    {
        p.push( { pos:a[i][0] , type:+1 , id:i } );
        p.push( { pos:a[i][1] , type:-1 , id:i } );
    }
    p.sort( function( fi , se ) {
        if ( fi.pos != se.pos ) return fi.pos - se.pos;
        if ( fi.type != se.type ) return fi.type==+1 ? -1 : +1;
        return fi.id - se.id;
    } );

    /*
    print();
    p.forEach( function( z ) {
        print( z.pos + " , " + z.type + " , " + z.id );
    } );
    print();
    */

    var l = 0 , r = 500;
    while ( l < r )
    {
        var mid = Math.floor( ( l + r ) / 2 );
        //print( l + " " + r + " " + mid );
        if ( check( mid ).ok == true ) l = mid+1;
        else r = mid;
    }

    var ans = check( r-1 );
    if ( ans.whichOnes == undefined )
    {
        ans.whichOnes = []
        for ( var i=0 ; i<k ; i++ )
            ans.whichOnes.push( i+1 );
    }

    print( r-1 );
    // print( ans.left + " " + ans.right );
    ans.whichOnes.forEach( function(x) { write( x + " " ); } );
    print();

} main();

var z;
var n , k;
var a = [];
var p = [];

function readArray()
{
    return readline().split( ' ' ).map( Number );
}

function check( sz )
{
    if ( sz == 0 ) return { ok : true , left : 0 , right : 0 , whichOnes : undefined };

    var f = new Array( n );
    for ( var i=0 ; i<n ; i++ ) f[i] = 0;

    var j=0 , i=0;
    var cnt = 0 , ret = 0;
    var found = false;
    var end = -8e9;
    var whichOnes = [];

    while ( i<2*n )
    {
        // find a closing bracket
        while ( p[i].type == +1 ) i++;

        while ( (j<2*n) && (p[i].pos - p[j].pos + 1) >= sz )
        {
            var id = p[j].id;
            if ( p[j].type == +1 )
            {
                if ( f[id] == 0 ) f[id] = 1 , cnt++;
            }
            j++;
        }

        if ( cnt > ret )
        {
            ret = cnt;
            if ( ret >= k && found == false )
            {
                for ( var l=0 ; l<n ; l++ )
                    if ( f[l] == 1 )
                        whichOnes.push( l+1 );

                found = true;
            }
        }

        var id = p[i].id;
        if ( f[id] == 0 ) f[id] = 2;
        else if ( f[id] == 1 ) f[id] = 2 , cnt--;
        i++;
    }

    return { ok : found , right : end , left : end - sz + 1 , whichOnes : whichOnes };
}

function main()
{
    z = readArray();
    n = z[0] , k = z[1];
    for ( var i=0 ; i<n ; i++ ) a[i] = readArray();

    for ( var i=0 ; i<n ; i++ )
    {
        p.push( { pos:a[i][0] , type:+1 , id:i } );
        p.push( { pos:a[i][1] , type:-1 , id:i } );
    }
    p.sort( function( fi , se ) {
        if ( fi.pos != se.pos ) return fi.pos - se.pos;
        if ( fi.type != se.type ) return fi.type==+1 ? -1 : +1;
        return fi.id - se.id;
    } );

    /*
    print();
    p.forEach( function( z ) {
        print( z.pos + " , " + z.type + " , " + z.id );
    } );
    print();
    */

    var l = 0 , r = 2e9 + 5;
    while ( l < r )
    {
        var mid = Math.floor( ( l + r ) / 2 );
        //print( l + " " + r + " " + mid );
        if ( check( mid ).ok == true ) l = mid+1;
        else r = mid;
    }

    var ans = check( r-1 );
    if ( ans.whichOnes == undefined )
    {
        ans.whichOnes = []
        for ( var i=0 ; i<k ; i++ )
            ans.whichOnes.push( i+1 );
    }

    print( r-1 );
    // print( ans.left + " " + ans.right );
    ans.whichOnes.forEach( function(x) { write( x + " " ); } );
    print();

} main();