'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 main() {
    const testCases = Number(readline());
    for (let i = 0; i < testCases; i++) {
        let a = readline().split(" ").map(elem => Number(elem));
        let b = readline().split(" ").map(elem => Number(elem));
        if (findLostPermutation(b, a[1])) console.log("YES")
        else console.log("NO")
    }
}

function findLostPermutation(arr, sum) {
    arr.sort((a, b) => a - b)

    let last = arr[arr.length - 1]

    for (let i = 1; i <= last; i++) {
        if (arr.indexOf(i) === -1) {
            sum -= i
        }
        if (sum < 0) break;
    }
    if (sum === 0) return true
    let i = 1
    while (sum > 0) {
        sum -= last + i;
        i++
    }

    if (sum === 0) return true
    else return false
}

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();
var inf = 1000000000000;
for(var tc=1;tc<=T;tc++) {
	var [m, sum] = readline().split(" ").map(w => parseInt(w));
	var a = readline().split(" ").map(w => parseInt(w));
	var maxx = -inf;
	var num = [];
	for(var i=0;i<m;i++) {
		num[a[i]] = 1;
		maxx = max(a[i], maxx);
	}
	var ans = 0;
	for(var i=0;i<=maxx;i++) {
		if(!num[i]) ans += i;
	}
	while(sum > ans) sum -= (i++);
	console.log(sum===ans ? "Yes" : "No");
}

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

function solve(n, s, arr) {
    const max = Math.max(...arr)
    const map = arr.reduce((o, x) => {
        o[x] = 1
        // o[x] = (o[x] || 0) + 1
        return o
    }, {})
    let i
    for (i = 1; i <= max; i++) {
        if (!map[i]) {
            s -= i
        }
    }
    while (s > 0) {
        s -= i++
    }
    // console.log(i, s)
    return !s ? 'YES' : 'NO'
}

'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 [m, s] = rns(),
        a = rns();
  const max = Math.max(...a),
        set = new Set(a);
  let sum = 0;
  for (let i = 1; i <= max; i++) {
    if (set.has(i)) continue;
    sum += i;
  }
  let i = max + 1;
  while (1) {
    if (sum > s) return 'NO';
    if (sum === s) return 'YES';
    sum += i++;
  }
}
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++) {
      let s = solve(r);
      if (s === undefined) s = '';
      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);
}();

//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 S = nextInt();
		var list = nextIntArray(N);
		var set = new Set();
		var max = 0;
		for(var i = 0; i < N; i++){
			set.add(list[i]);
			max = Math.max(max, list[i]);
		}
		for(var i = 1; i <= max; i++){
			if(!set.has(i)){
				S -= i;
			}
		}
		if(S < 0){
			myout("No");
			continue;
		}
		while(S > 0){
			max++;
			S -= max;
		}
		if(S == 0){
			myout("Yes");
		}else{
			myout("No");
		}
	}
}

var numOfCases = Number(readline());

for (var testCase = 0; testCase < numOfCases; testCase++) {
    var firstLine = readline().split(' ').map(Number);
    var m = firstLine[0];
    var s = firstLine[1];

    var secondLine = readline().split(' ').map(Number);

    var result = process(secondLine, s);
    print(result);
}

function process(arr, sum) {
    var freq = {}
    for (var i = 0; i < arr.length; i++) {
        freq[arr[i]] = 1;
    }

    var currentSum = 0;
    var j = 1;
    while (Object.keys(freq).length) {
        if (freq[j]) {
            delete freq[j];
        } else {
            currentSum += j;
        }

        j++;
    }

    while (currentSum < sum) {
        currentSum += j;
        j++;
    }

    return currentSum === sum ? 'YES' : 'NO';
}

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();
var inf = 1000000000000;
for(var tc=1;tc<=T;tc++) {
	var [m, sum] = readline().split(" ").map(w => parseInt(w));
	var a = readline().split(" ").map(w => parseInt(w));
	var maxx = -inf;
	var num = [];
	for(var i=0;i<m;i++) {
		num[a[i]] = 1;
		maxx = max(a[i], maxx);
	}
	for(var i=1;sum>0;i++) {
		if(!num[i]) sum -= i;
	}
	console.log((i >= maxx && sum === 0) ? "Yes" : "No");
}

var numOfCases = Number(readline());

for (var testCase = 0; testCase < numOfCases; testCase++) {
    var firstLine = readline().split(' ').map(Number);
    var m = firstLine[0];
    var s = firstLine[1];

    var secondLine = readline().split(' ').map(Number);

    var result = process(secondLine, s);
    print(result);
}

function process(arr, sum) {
    var freq = {}
    for (var i = 0; i < arr.length; i++) {
        freq[arr[i]] = 1;
    }

    var currentSum = 0;

    var isNeedToCheckForPermutability = false
    for (var j = 1; currentSum <= sum; j++) {
        if (currentSum === sum) {
            if (Object.keys(freq).length === 0) {
                return 'YES';
            } else {
                isNeedToCheckForPermutability = true;
                break;
            }
        }

        if (!freq[j]) {
            currentSum += j;
        } else {
            delete freq[j];
        }
    }

    if (isNeedToCheckForPermutability) {
        while(Object.keys(freq).length) {
            if (freq[j]) {
                delete freq[j];
            } else {
                return 'NO';
            }
        }

        return 'YES';
    }

    return 'NO';
}


function handleString(str, lettersHash) {
    for (let letter of str) {
        if (!lettersHash[letter]) {
            lettersHash[letter] = 0;
        }

        ++lettersHash[letter];
    }
}

function findOriginalString(operationsData, resultString) {
    const lettersHash = [];
    for (let opData of operationsData) {
        handleString(opData, lettersHash);
    }

    handleString(resultString, lettersHash);

    for(let letterKey in lettersHash) {
        const letterCount = lettersHash[letterKey];
        if (letterCount % 2) {
            return letterKey;
        }
    }

    return null;
}

function main() {
    let t = +readline();
    while (t > 0) {
        const n = +readline();
        let i = 0;
        let operationsData = [];
        while (i < 2 * n) {
            operationsData.push(readline());
            ++i;
        }

        const resultString = readline();
        console.log(findOriginalString(operationsData, resultString));

        --t;
    }
}

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

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

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

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

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

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

const sumab = (a, b) => {
    if (a > b) return 0;
    let x = (b - a + 1);
    let y = a + b;
    if (x % 2 === 0) {
        x = Math.round(x / 2);
    } else {
        y = Math.round(y / 2);
    }
    return x * y;
}

const calc = (t)=>{
    /* read */
    let n = +rl();
    let freq = new Array(26).fill(0);
    let aCode = 'a'.charCodeAt(0);
    for (let i = 0; i <= 2*n; i++) {
        let s = rl();
        for (let i = 0; i < s.length; i++) {
            let code = s.charCodeAt(i);
            freq[code - aCode]++;
        }
    }
    for (let i = 0; i < 26; i++) {
        if (freq[i] % 2 === 1) {
            return String.fromCharCode(aCode + i);
        }
    }
};

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

'use strict';

function solve(n, a, s) {
  const count = new Array(26).fill(0);
  for (let word of a.concat(s)) {
    for (let char of word) {
      const num = char.charCodeAt(0) - 97;
      if (count[num]) {
        count[num]--;
      } else {
        count[num]++;
      }
    }
  }
  for (let [i, num] of count.entries()) {
    if (num) return String.fromCharCode(i + 97);
  }
}

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

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

function replaceAt(str, index, length, replacement) {
    if (index >= str.length) {
        return str.valueOf();
    }

    return str.substring(0, index) + replacement + str.substring(index + length);
}

function printMask(mask) {
    let res = "";
    while(mask > 0) {
        res += mask & 1;
        mask >>= 1;
    }

    return res.split('').reverse().join('');
}

function doesMaskContainOnlyOnes(mask) {
    for (let elem of mask) {
        if (!elem) {
            return false
        }
    }

    return true;
}

function handleTraversalEntry(str, operationsData, remainingOpsMask, treeTraversalArray, opHistory) {
    if (operationsData.length <= 0 || str.length == 0) {
        return null;
    }

/* console.log('<<<<');
    console.log(str);
    console.log(treeTraversalArray.length);
    console.log(printMask(remainingOpsMask))*/

    if (str.length == 1 && doesMaskContainOnlyOnes(remainingOpsMask)) {
        return str;
    }

    operationsData.forEach((opData, idx) => {
        if (remainingOpsMask[idx]) {
            return;
        }

        const matches = str.matchAll(opData);

        for (const match of matches) {

            const remainingOps = remainingOpsMask.slice();
            remainingOps[idx] = true;
            /*  console.log("<<")
              console.log(opHistory)
              console.log("<<")*/
            makeReplacement(str, operationsData, remainingOps, treeTraversalArray, match.index, opData.length, opHistory);
        };
    });

    return null;
}

function makeReplacement(str, operationsData, remainingOpsMask, treeTraversalArray, replacementIdx, replacementLength, opHistory) {
    operationsData.forEach((opData, idx) => {
        if (remainingOpsMask[idx]) {
            return;
        }

        const remainingOps = remainingOpsMask.slice();
        remainingOps[idx] = true;

        const updatedStr = replaceAt(str, replacementIdx, replacementLength, opData);
       /* let opHistoryNew = opHistory.slice();
        opHistoryNew.push(updatedStr)*/

        treeTraversalArray.push({
            resultString: updatedStr,
            remainingOpsMask: remainingOps,
       //     opHistory: opHistoryNew
        });
    });
}

function findOriginalString(operationsData, resultString) {
    let treeTraversalArray = [];

    treeTraversalArray.push({
        resultString,
        remainingOpsMask: [],
      //  opHistory: [resultString]
    });

    while (treeTraversalArray.length > 0) {
        const currentTraversalEntry = treeTraversalArray.pop();

        const res = handleTraversalEntry(
            currentTraversalEntry.resultString,
            operationsData,
            currentTraversalEntry.remainingOpsMask,
            treeTraversalArray,
          //  currentTraversalEntry.opHistory,
        );

        if(res) {
            console.log(res);
            ///console.log(currentTraversalEntry.opHistory);
            return;
        }
    }
}

function main() {
    let t = +readline();
    while (t > 0) {
        const n = +readline();
        let i = 0;
        let operationsData = [];
        while (i < 2 * n) {
            operationsData.push(readline());
            ++i;
        }

        const resultString = readline();
        findOriginalString(operationsData, resultString);

        --t;
    }
}

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

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 replaceAt(str, index, length, replacement) {
    if (index >= str.length) {
        return str.valueOf();
    }

    return str.substring(0, index) + replacement + str.substring(index + length);
}

function handleTraversalEntry(str, operationsData, remainingOpsMask, treeTraversalArray) {
    if (operationsData.length <= 0 || str.length == 0) {
        return null;
    }

    if (str.length == 1) {
        return str;
    }

    operationsData.forEach((opData, idx) => {
        if ((1 << idx) & remainingOpsMask) {
            return;
        }

        const matches = str.matchAll(opData);

        for (const match of matches) {
            const remainingOps = (1 << idx) | remainingOpsMask;
            makeReplacement(str, operationsData, remainingOps, treeTraversalArray, match.index, opData.length);
        };
    });

    return null;
}

function makeReplacement(str, operationsData, remainingOpsMask, treeTraversalArray, replacementIdx, replacementLength) {
    operationsData.forEach((opData, idx) => {
        if ((1 << idx) & remainingOpsMask) {
            return;
        }

        const remainingOps = (1 << idx) | remainingOpsMask;

        const updatedStr = replaceAt(str, replacementIdx, replacementLength, opData);

        treeTraversalArray.push({
            resultString: updatedStr,
            remainingOpsMask: remainingOps,
        });
    });
}

function findOriginalString(operationsData, resultString) {
    let treeTraversalArray = [];

    treeTraversalArray.push({
        resultString,
        remainingOps: 0,
    });

    while (treeTraversalArray.length > 0) {
        const currentTraversalEntry = treeTraversalArray.pop();

        const res = handleTraversalEntry(
            currentTraversalEntry.resultString,
            operationsData,
            currentTraversalEntry.remainingOpsMask,
            treeTraversalArray
        );

        if(res) {
            console.log(res);
            return;
        }
    }
}

function main() {
    let t = +readline();
    while (t > 0) {
        const n = +readline();
        let i = 0;
        let operationsData = [];
        while (i < 2 * n) {
            operationsData.push(readline());
            ++i;
        }

        const resultString = readline();
        findOriginalString(operationsData, resultString);

        --t;
    }
}

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

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


// Codeforces 1185/B
let lines = '';

process.stdin.resume();
process.stdin.setEncoding('utf-8');
process.stdin.on('data', input => lines += input);
process.stdin.on('end', () => {
  const { EOL } = require('os')
  lines = lines.split(EOL)

  main();
});

const main = () => {
  const npairs = parseInt(lines[0]);
  for (let i = 1; i < 2*npairs; i += 2) {
    console.log(solution(lines[i], lines[i+1]));
  }
};

const countChars = (s) => {
  let prevChar = s[0];
  let count = 1;
  let res = [];
  if (s.length === 1) return [`1${s[0]}`];
  for (let i = 1; i < s.length; i++) {
    if (i === s.length - 1) {
      if (s[i] !== prevChar) {
        res.push(`${count}${prevChar}`);
        res.push(`${1}${s[i]}`);
      } else {
        res.push(`${count+1}${s[i]}`);
      }
    } else if (s[i] !== prevChar) {
      res.push(`${count}${prevChar}`);
      count = 1;
    } else {
      count++;
    }
    prevChar = s[i];
  }
  return res;
};

const solution = (a, b) => {
  const a_arr = countChars(a);
  const b_arr = countChars(b);
  if (a_arr.length !== b_arr.length) return 'NO';
  for (let i = 0; i< a_arr.length; i++) {
    const aNum = parseInt(a_arr[i].substring(0, a_arr[i].length-1));
    const bNum = parseInt(b_arr[i].substring(0, b_arr[i].length-1));
    const aChar = a_arr[i][a_arr[i].length - 1];
    const bChar = b_arr[i][b_arr[i].length - 1];
    if (aNum > bNum) return 'NO';
    if (aChar !== bChar) return 'NO';
  }
  return 'YES';
};

(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 res = '';

    for(var i = 0; i < n; i++) {
        var s = readline();
        var t = readline();
        var indexT = 0;
        var check = true;
        for(var j = 0; j < s.length; j++) {
            while(s[j] !== t[indexT] && t[indexT - 1] === t[indexT]) {
                indexT++;
            }

            if(s[j] !== t[indexT]) {
                check = false;
                break;
            }
            indexT++;
        }
        while(indexT < t.length) {
            if(t[indexT] !== s[s.length - 1]) {
                check = false;
                break;
            }
            indexT++;
        }
        res += (check ? 'YES' : 'NO') + '\n';
    }
    print(res);
}());

const DEBUG = false
let readline = require('readline').createInterface({
  input: !DEBUG ? process.stdin : require('fs').createReadStream('./input'),
});

let main = () => {
  for (let i = 0; i < list.length; i+=2) {
    let itemA = list[i]
    let itemB = list[i+1]
    let next = 0;
    let j = 0;
    for (; ;) {
      if (itemA[j] == itemB[next] && next < itemB.length && j < itemA.length) {
        next++;
        j++;
      } else if (next > 0 && itemA[j - 1] == itemB[next]) {
        next++;
      } else {
        break
      }
    }
    console.log(next == itemB.length && j == itemA.length ? 'YES' : 'NO')
  }
}

let list = []
let n = 0, cnt = 0;
readline.on('line', (d) => {
  if (n == 0) {
    n = parseInt(d, 10)
  } else {
    list.push(d)
    if (++cnt >= n * 2) {
      readline.close();
    }
  }
});
readline.on('close', () => {
  main()
})

const DEBUG = false
let readline = require('readline').createInterface({
  input: !DEBUG ? process.stdin : require('fs').createReadStream('./input'),
});

let main = () => {
  for (let i = 0; i < list.length; i+=2) {
    let itemA = list[i]
    let itemB = list[i+1]

    if (itemA.length > itemB.length) {
      console.log('NO')
      continue
    }

    let next = 0;
    let j = 0;
    for (; ;) {
      if (itemA[j] == itemB[next] && next < itemB.length && j < itemA.length) {
        next++;
        j++;
      } else if (next > 0 && itemA[j - 1] == itemB[next]) {
        next++;
      } else {
        break
      }
    }
    console.log(next == itemB.length && j == itemA.length ? 'YES' : 'NO')
  }
}

let list = []
let n = 0, cnt = 0;
readline.on('line', (d) => {
  if (n == 0) {
    n = parseInt(d, 10)
  } else {
    list.push(d)
    if (++cnt >= n * 2) {
      readline.close();
    }
  }
});
readline.on('close', () => {
  main()
})

const DEBUG = false;
const { EOL } = require('os');
const fs = require('fs');

let r = process.stdin;
if (DEBUG) {
  r = fs.createReadStream('input');
}
let data = '';
function calc(s, t) {
  let idx1 = 0;
  let idx2 = 0;
  let cnt1 = 0;
  let cnt2 = 0;
  while (idx1 < s.length && idx2 < t.length) {
    const c = s[idx1];
    cnt1 = 0;
    cnt2 = 0;
    if (s[idx1] !== t[idx2]) {
      return 'NO';
    }
    while (idx1 + cnt1 < s.length && s[idx1 + cnt1] === c) {
      cnt1 += 1;
    }
    while (idx2 + cnt2 < t.length && t[idx2 + cnt2] === c) {
      cnt2 += 1;
    }
    if (cnt2 < cnt1) {
      return 'NO';
    }
    idx1 += cnt1;
    idx2 += cnt2;
  }
  if (idx2 < t.length || idx1 < s.length) {
    return 'NO';
  }
  return 'YES';
}
r.on('data', c => (data += c));
r.on('end', () => {
  const lines = data.split(EOL);
  const tests = Number(lines[0]);
  for (let i = 1; i <= tests; i += 1) {
    console.log(calc(lines[2 * i - 1], lines[2 * i]));
  }
});

var number = readline();
var descriptions = [];

function matchesOrNot(word1, word2){
    var w1Arr = [word1[0], 1];
    var w2Arr = [word2[0], 1];

    if(word1.length === 0 && word2.length !== 0 || word2.length === 0 && word1.length !== 0){
        return "NO";
    }
    if(word1.length === 0 &&  word2.length === 0){
        return "YES";
    }

    var w1i = 0;
    var w2i = 0;

    for(var m = 1; m < word1.length; m++){
        if(word1[m] == w1Arr[w1i]){
            w1Arr[w1i+1] +=1;
        }
        else{
            w1Arr.push(word1[m], 1);
            w1i+=2;
        }
    }

    for(var n = 1; n < word2.length; n++){
        if(word2[n] == w2Arr[w2i]){
            w2Arr[w2i+1] +=1;
        }
        else{
            w2Arr.push(word2[n], 1);
            w2i+=2;
        }
    }

    if(w1Arr.length != w2Arr.length){
        return "NO";
    }

    for(var i = 1; i < w1Arr.length; i+=2){
        if(w1Arr[i-1] == w2Arr[i-1]){
            if(w1Arr[i] > w2Arr[i]) return "NO";
        }else{
            return "NO";
        }
    }

    return "YES";
}

for(var k = 0; k < number*2; k++){
    descriptions.push(readline());
}

for(var i = 1; i < number*2; i+=2){
    print(matchesOrNot(descriptions[i-1], descriptions[i]));
}

// Codeforces 1185/B
let lines = '';

process.stdin.resume();
process.stdin.setEncoding('utf-8');
process.stdin.on('data', input => lines += input);
process.stdin.on('end', () => {
  const { EOL } = require('os')
  lines = lines.split(EOL)

  main();
});

const main = () => {
  const npairs = parseInt(lines[0]);
  for (let i = 1; i < 2*npairs; i += 2) {
    console.log(solution(lines[i], lines[i+1]));
  }
};

const countChars = (s) => {
  let prevChar = s[0];
  let count = 1;
  let res = [];
  for (let i = 1; i < s.length; i++) {
    if (i === s.length - 1) {
      if (s[i] !== prevChar) {
        res.push(`${count}${prevChar}`);
        res.push(`${1}${s[i]}`);
      } else {
        res.push(`${count+1}${s[i]}`);
      }
    } else if (s[i] !== prevChar) {
      res.push(`${count}${prevChar}`);
      count = 1;
    } else {
      count++;
    }
    prevChar = s[i];
  }
  return res;
};

const solution = (a, b) => {
  const a_arr = countChars(a);
  const b_arr = countChars(b);
  if (a_arr.length !== b_arr.length) return 'NO';
  for (let i = 0; i< a_arr.length; i++) {
    if (a_arr[i][0] > b_arr[i][0]) return 'NO';
    if (a_arr[i][1] !== b_arr[i][1]) return 'NO';
  }
  return 'YES';
};


'use strict';

function _defineProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }

  return obj;
}

function solve(n, m, a) {
  const w = new Array(n).fill(0);
  for (let i of a) w[i - 1]++;
  const bst = new BinarySearchTree();
  for (let i = 0; i < n; i++) {
    bst.insert(w[i]);
  }
  let max = bst.getMax(),
      min = bst.getMin();
  let res = max;
  while (max > min + 1) {
    bst.delete(max);
    bst.delete(min);
    bst.insert(max - 1);
    bst.insert(min + 2);
    max = bst.getMax();
    min = bst.getMin();
    res = Math.min(res, max);
  }
  return res;
}
class BinarySearchTree {
  constructor(_compare = (a, b) => a - b) {
    _defineProperty(this, "root", null);
    _defineProperty(this, "length", 0);
    _defineProperty(this, "min", null);
    _defineProperty(this, "max", null);
    _defineProperty(this, "minCache", true);
    _defineProperty(this, "maxCache", true);
    this._compare = _compare;
    this.compare = this.compare.bind(this);
  }
  isT(t) {
    return t !== undefined && t !== null;
  }
  compare(a, b) {
    const {
      isT
    } = this;
    if (isT(a) && isT(b)) return this._compare(a, b);
    if (isT(a)) return 1;
    if (isT(b)) return -1;
    return 0;
  }
  isEmpty() {
    return !this.root;
  }
  size() {
    return this.root ? this.root.size : 0;
  }
  getRoot() {
    return this.root;
  }
  getMin() {
    if (this.minCache) {
      return this.min;
    }
    const min = this.searchKth(this.size());
    this.min = min;
    this.minCache = true;
    return min;
  }
  getMax() {
    if (this.maxCache) {
      return this.max;
    }
    const max = this.searchKth(1);
    this.max = max;
    this.maxCache = true;
    return max;
  }
  balance(node) {
    node.height = this.getHeight(node);
    const blance = this.getBalance(node);
    let res;
    if (Math.abs(blance) === 2) {
      if (blance > 0) {
        var _node$left$left$heigh, _node$left, _node$left$left, _node$left$right$heig, _node$left2, _node$left2$right;
        const heightDif = ((_node$left$left$heigh = (_node$left = node.left) === null || _node$left === void 0 ? void 0 : (_node$left$left = _node$left.left) === null || _node$left$left === void 0 ? void 0 : _node$left$left.height) !== null && _node$left$left$heigh !== void 0 ? _node$left$left$heigh : 0) - ((_node$left$right$heig = (_node$left2 = node.left) === null || _node$left2 === void 0 ? void 0 : (_node$left2$right = _node$left2.right) === null || _node$left2$right === void 0 ? void 0 : _node$left2$right.height) !== null && _node$left$right$heig !== void 0 ? _node$left$right$heig : 0);
        if (heightDif > 0) {
          res = this.rotateRight(node);
        } else if (heightDif < 0) {
          res = this.rotateLeftRight(node);
        }
      } else {
        var _node$right$left$heig, _node$right, _node$right$left, _node$right$right$hei, _node$right2, _node$right2$right;
        const heightDif = ((_node$right$left$heig = (_node$right = node.right) === null || _node$right === void 0 ? void 0 : (_node$right$left = _node$right.left) === null || _node$right$left === void 0 ? void 0 : _node$right$left.height) !== null && _node$right$left$heig !== void 0 ? _node$right$left$heig : 0) - ((_node$right$right$hei = (_node$right2 = node.right) === null || _node$right2 === void 0 ? void 0 : (_node$right2$right = _node$right2.right) === null || _node$right2$right === void 0 ? void 0 : _node$right2$right.height) !== null && _node$right$right$hei !== void 0 ? _node$right$right$hei : 0);
        if (heightDif > 0) {
          res = this.rotateRightLeft(node);
        } else if (heightDif < 0) {
          res = this.rotateLeft(node);
        }
      }
    }
    return res ? res : node;
  }
  rotateRight(node) {
    const left = node.left;
    const leftRight = left.right;
    left.right = node;
    node.left = leftRight;
    node.height = this.getHeight(node);
    left.height = this.getHeight(left);
    node.size = this.getSize(node);
    left.size = this.getSize(left);
    return left;
  }
  rotateLeft(node) {
    const right = node.right;
    const rightLeft = right.left;
    right.left = node;
    node.right = rightLeft;
    node.height = this.getHeight(node);
    right.height = this.getHeight(right);
    node.size = this.getSize(node);
    right.size = this.getSize(right);
    return right;
  }
  rotateLeftRight(node) {
    node.left = this.rotateLeft(node.left);
    return this.rotateRight(node);
  }
  rotateRightLeft(node) {
    node.right = this.rotateRight(node.right);
    return this.rotateLeft(node);
  }
  getBalance(node) {
    return this.getHeight(node.left) - this.getHeight(node.right);
  }
  getHeight(node) {
    var _node$left$height, _node$left3, _node$right$height, _node$right3;
    if (!node) return 0;
    return Math.max((_node$left$height = (_node$left3 = node.left) === null || _node$left3 === void 0 ? void 0 : _node$left3.height) !== null && _node$left$height !== void 0 ? _node$left$height : 0, (_node$right$height = (_node$right3 = node.right) === null || _node$right3 === void 0 ? void 0 : _node$right3.height) !== null && _node$right$height !== void 0 ? _node$right$height : 0) + 1;
  }
  getSize(node) {
    var _node$left$size, _node$left4, _node$right$size, _node$right4;
    if (!node) return 0;
    return ((_node$left$size = (_node$left4 = node.left) === null || _node$left4 === void 0 ? void 0 : _node$left4.size) !== null && _node$left$size !== void 0 ? _node$left$size : 0) + ((_node$right$size = (_node$right4 = node.right) === null || _node$right4 === void 0 ? void 0 : _node$right4.size) !== null && _node$right$size !== void 0 ? _node$right$size : 0) + node.count;
  }
  createNode(val) {
    return {
      id: Math.random() * new Date().valueOf(),
      val,
      left: null,
      right: null,
      size: 1,
      height: 1,
      count: 1
    };
  }
  insert(val) {
    let cur = this.createNode(val);
    if (this.isEmpty()) {
      this.root = cur;
      this.length++;
    } else {
      [, cur] = this.insertNode(this.root, cur);
    }
    if (this.min === null || this.compare(this.min, val) > 0) {
      this.min = val;
    }
    if (this.max === null || this.compare(this.max, val) < 0) {
      this.max = val;
    }
  }
  insertNode(node, cur, parent = null) {
    node.size++;
    const compareResult = this.compare(cur.val, node.val);
    let res;
    if (compareResult === 0) {
      node.count++;
      return [false, node];
    } else if (compareResult > 0) {
      if (node.right) {
        res = this.insertNode(node.right, cur, node);
        if (!res[0]) return res;
      } else {
        node.right = cur;
        this.length++;
        res = [true, cur];
      }
    } else {
      if (node.left) {
        res = this.insertNode(node.left, cur, node);
        if (!res[0]) return res;
      } else {
        node.left = cur;
        this.length++;
        res = [true, cur];
      }
    }
    let preHeight = node.height;
    const newNode = this.balance(node);
    if (newNode === node && node.height === preHeight) {
      res = [false, res[1]];
    } else if (newNode !== node) {
      if (parent) {
        parent.left === node ? parent.left = newNode : parent.right = newNode;
      } else {
        this.root = newNode;
      }
      res = [false, res[1]];
    }
    return res;
  }
  delete(val) {
    if (!this.root) return;
    this.deleteNode(val, this.root, null);
  }
  deleteNode(val, node, parent) {
    if (!node) return null;
    let res = this.compare(val, node.val);
    if (res === 0) {
      node.count--;
      node.size--;
      if (node.count > 0) return node;
      if (!node.left || !node.right) {
        if (this.min === val) {
          this.minCache = false;
        }
        if (this.max === val) {
          this.maxCache = false;
        }
        this.length--;
        if (!parent) {
          var _node$left5;
          this.root = (_node$left5 = node.left) !== null && _node$left5 !== void 0 ? _node$left5 : node.right;
          return this.root;
        } else {
          var _node$left6;
          return (_node$left6 = node.left) !== null && _node$left6 !== void 0 ? _node$left6 : node.right;
        }
      } else {
        const selectLeft = node.left.height > node.right.height;
        let replaceNode = selectLeft ? this.pre(node) : this.next(node),
            name = selectLeft ? 'left' : 'right';
        node.val = replaceNode.val;
        node.count = replaceNode.count;
        replaceNode.count = 0;
        node[name] = this.deleteNode(replaceNode.val, node[name], node);
      }
    } else if (res > 0) {
      node.right = this.deleteNode(val, node.right, node);
    } else {
      node.left = this.deleteNode(val, node.left, node);
    }
    node.size = this.getSize(node);
    node.height;
    const newNode = this.balance(node);
    if (parent) {
      parent.left === node ? parent.left = newNode : parent.right = newNode;
    } else {
      this.root = newNode;
    }
    return newNode;
  }
  next(node) {
    let next = node.right;
    while ((_next = next) !== null && _next !== void 0 && _next.left) {
      var _next;
      next = next.left;
    }
    return next;
  }
  pre(node) {
    let pre = node.left;
    while ((_pre = pre) !== null && _pre !== void 0 && _pre.right) {
      var _pre;
      pre = pre.right;
    }
    return pre;
  }
  search(val, compare) {
    if (this.isEmpty()) {
      return null;
    }
    const [, node] = this.searchCeilingNode(this.root, val, compare !== null && compare !== void 0 ? compare : this.compare);
    return node.val;
  }
  searchCeilingNode(node, val, compare, parent = null) {
    const res = compare(val, node.val);
    if (res === 0) {
      return [parent, node];
    } else if (res > 0) {
      if (node.right) {
        return this.searchCeilingNode(node.right, val, compare, node);
      } else {
        return [parent, node];
      }
    } else {
      if (node.left) {
        const [p, value] = this.searchCeilingNode(node.left, val, compare, node);
        if (compare(value.val, val) < 0) {
          return [parent, node];
        } else {
          return [p, value];
        }
      } else {
        return [parent, node];
      }
    }
  }
  ceiling(val) {
    if (this.isEmpty()) {
      return null;
    }
    const [, node] = this.searchCeilingNode(this.root, val, this.compare);
    return this.compare(node.val, val) >= 0 ? node.val : null;
  }
  searchFloorNode(node, val, compare, parent = null) {
    const res = compare(val, node.val);
    if (res === 0) {
      return [parent, node];
    } else if (res > 0) {
      if (node.right) {
        const [p, value] = this.searchFloorNode(node.right, val, compare, node);
        if (compare(value.val, val) > 0) {
          return [parent, node];
        } else {
          return [p, value];
        }
      } else {
        return [parent, node];
      }
    } else {
      if (node.left) {
        return this.searchFloorNode(node.left, val, compare, node);
      } else {
        return [parent, node];
      }
    }
  }
  floor(val) {
    if (this.isEmpty()) {
      return null;
    }
    const [, node] = this.searchFloorNode(this.root, val, this.compare);
    return this.compare(node.val, val) <= 0 ? node.val : null;
  }
  searchKth(k) {
    if (this.isEmpty()) {
      return null;
    }
    if (k <= 0 || k > this.size()) {
      return null;
    }
    const node = this.searchNodeKth(this.root, k);
    return node.val;
  }
  searchNodeKth(node, k) {
    var _node$right$size2, _node$right5, _node$right$size3, _node$right6;
    const rSize = (_node$right$size2 = (_node$right5 = node.right) === null || _node$right5 === void 0 ? void 0 : _node$right5.size) !== null && _node$right$size2 !== void 0 ? _node$right$size2 : 0;
    if (rSize === k - 1 || rSize < k && rSize + node.count >= k) return node;
    if (node.right && rSize > k - 1) return this.searchNodeKth(node.right, k);else return this.searchNodeKth(node.left, k - ((_node$right$size3 = (_node$right6 = node.right) === null || _node$right6 === void 0 ? void 0 : _node$right6.size) !== null && _node$right$size3 !== void 0 ? _node$right$size3 : 0) - node.count);
  }
  countGreaterThanEq(val) {
    if (!this.root) return 0;
    return this.countCompare(val, (a, b) => this._compare(a, b), this.root);
  }
  countCompare(val, compare, node, pre = 0) {
    const res = compare(val, node.val);
    if (res === 0) {
      var _node$right$size4, _node$right7;
      return pre + ((_node$right$size4 = (_node$right7 = node.right) === null || _node$right7 === void 0 ? void 0 : _node$right7.size) !== null && _node$right$size4 !== void 0 ? _node$right$size4 : 0) + node.count;
    } else if (res > 0) {
      if (node.right) {
        return this.countCompare(val, compare, node.right, pre);
      } else {
        return pre;
      }
    } else {
      var _node$right$size5, _node$right8;
      let count = pre + ((_node$right$size5 = (_node$right8 = node.right) === null || _node$right8 === void 0 ? void 0 : _node$right8.size) !== null && _node$right$size5 !== void 0 ? _node$right$size5 : 0) + node.count;
      if (node.left) {
        return this.countCompare(val, compare, node.left, count);
      } else {
        return count;
      }
    }
  }
  toArray() {
    if (!this.root) return [];
    const res = [];
    const dfs = node => {
      if (node.left) dfs(node.left);
      res.push(node.val);
      if (node.right) dfs(node.right);
    };
    dfs(this.root);
    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 { ok } = require("assert");
const fs = require("fs");
const input = fs.readFileSync(0, 'utf8').trim().split('\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();
var inf = 1000000000000;
for(var qe=1;qe<=T;qe++) {
  var [n,m] = readline().split(' ').map((x) => parseInt(x));

  var a = readline().split(' ').map((x) => parseInt(x));
  a.sort();
  var cnt = []
  for(var i=1;i<=n;i++) cnt[i] = 0;
  for(var i=0;i<m;i++) {
    var j = i;
    while(j<m && a[i]==a[j]) j++;
    cnt[a[i]] = j-i;
    i = j-1;
  }
   var ans = inf;
  var L = 1, R = m;
  while(L <= R) {
    var mid = L+Math.floor((R-L)/2);
    if(ok(mid)) {
      R = mid-1;
      ans = min(ans, mid);
    }
    else L = mid+1;
  }

  function ok(x) {
    var rem = 0;
    for(var i=1;i<=n;i++) {
      var remove;
      if(cnt[i]==0) remove = -x;
      else remove = cnt[i]-x;

      if(remove > 0) rem += remove;
      else {
        rem -= Math.floor((abs(remove))/2)
      }
      // i = j-1;
    }
    // if(qe==2) console.log([x, rem])
    if(rem <= 0) return true;
    else return false;
  }
  console.log(ans)

}

var t = +readline();

while (t--) {
	var k = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	var arr = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	if (k[0] === 1) {
		print(k[1]);
		continue;
	}

	var arrMapper = new Array(k[0]); // bad
	var arrDec = new Array(k[0]);

	for (var i = 1; i <= k[0]; i++) {
		arrMapper[i - 1] = 0;
	}

	for (var i = 0; i < arr.length; i++) {
		arrMapper[arr[i] - 1]++;
	}
	arrMapper.sort(function (a, b) {
		return b - a;
	});

	var l = 1;
	var r = arrMapper[0];
	var m = parseInt((l + r) / 2);

	var res = arrMapper[0];

	while (l !== m && r != m) {
		var cacl = caclTime(m, arrMapper);
		if (cacl > m) l = m;
		else {
			r = m;
			res = Math.min(res, m);
		}
		m = parseInt((l + r) / 2);
	}

	// print(l, r);

	for (var i = l; i <= r; i++) {
		var cacl = caclTime(i, arrMapper);
		if (cacl <= i) res = Math.min(res, i);
	}

	print(res);
}

function caclTime(m, arrMapper) {
	var sumNeed = 0;
	var indexNeed = arrMapper.length - 1;
	for (var i = arrMapper.length - 1; i >= 0; i--) {
		var check = parseInt((m - arrMapper[i]) / 2);
		if (check <= 0) {
			indexNeed = Math.min(arrMapper.length - 1, i + 1);
			break;
		}
		sumNeed += check;
	}

	var resurt = arrMapper[0];
	var sum = 0;
	for (i = 0; i <= indexNeed - 1; i++) {
		var current = i + 1;
		var sumFromi = (arrMapper[i] - arrMapper[i + 1]) * current;
		var add = sum + sumFromi;
		if (add <= sumNeed) {
			sum += sumFromi;
			resurt = arrMapper[i + 1];
		} else {
			var remain = sumNeed - sum;
			var div = parseInt(remain / current);
			resurt = arrMapper[i] - div;
			break;
		}
	}
	// print(m, resurt, sumNeed, indexNeed);

	return resurt;
}

var t = +readline();

while (t--) {
	var k = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	var arr = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	if (k[0] === 1) {
		print(k[1]);
		continue;
	}

	var arrMapper = new Array(k[0]); // bad

	for (var i = 1; i <= k[0]; i++) {
		arrMapper[i - 1] = 0;
	}

	for (var i = 0; i < arr.length; i++) {
		arrMapper[arr[i] - 1]++;
	}
	arrMapper.sort(function (a, b) {
		return b - a;
	});

	var r = arrMapper[0];
	var maxTime = parseInt(r / 3);
	var l = 1;
	var m = parseInt((l + r) / 2);
	var res = arrMapper[0];

	while (l !== m && r != m) {
		var cacl = caclTime(m, arrMapper);
		if (cacl > m) l = m;
		else {
			r = m;
			res = Math.min(res, m);
		}
		m = parseInt((l + r) / 2);
	}

	// print(l, r);

	for (var i = l; i <= r; i++) {
		var cacl = caclTime(i, arrMapper);
		if (cacl <= i) res = Math.min(res, i);
	}

	print(res);
}

function caclTime(m, arrMapper) {
	var head = 0;
	var last = arrMapper.length - 1;
	var give = -1;
	var checked = -1;
	var resurt = arrMapper[0];
	var sum = 0;

	while (head < last) {
		if (give <= 0) {
			give = arrMapper[head] - m;
			resurt = Math.max(resurt, arrMapper[head]);
			sum = 0;
		}
		if (checked <= 0) {
			checked = parseInt((m - arrMapper[last]) / 2);
			resurt = Math.max(resurt, arrMapper[last]);
		}
		if (checked <= 0 || give <= 0) break;

		if (checked >= give) {
			resurt = Math.min(resurt, arrMapper[head] - m);
			checked -= give;
			give = 0;
			head++;
			if (checked === 0) last--;
		} else {
			sum += checked;
			resurt = Math.min(resurt, arrMapper[head] - sum);
			give -= checked;
			checked = 0;
			last--;
		}
	}

	if (head === last) {
		resurt = Math.max(resurt, arrMapper[head]);
	} else if (last - head > 1) {
		resurt = Math.max(resurt, arrMapper[head + 1]);
		resurt = Math.max(resurt, arrMapper[last - 1]);
	}

	// print("m = ", m, " res = ", resurt);

	return resurt;
}

var t = +readline();

while (t--) {
	var k = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	var arr = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	if (k[0] === 1) {
		print(k[1]);
		continue;
	}

	var arrMapper = new Array(k[0]); // bad

	for (var i = 1; i <= k[0]; i++) {
		arrMapper[i - 1] = 0;
	}

	for (var i = 0; i < arr.length; i++) {
		arrMapper[arr[i] - 1]++;
	}
	arrMapper.sort(function (a, b) {
		return b - a;
	});

	var r = arrMapper[0];
	var maxTime = parseInt(r / 3);
	var l = 1;
	var m = parseInt((l + r) / 2);
	var res = arrMapper[0];

	while (l !== m && r != m) {
		var cacl = caclTime(m, arrMapper);
		if (cacl > m) l = m;
		else {
			r = m;
			res = Math.min(res, m);
		}
		m = parseInt((l + r) / 2);
	}

	// print(l, r);

	for (var i = l; i <= r; i++) {
		var cacl = caclTime(i, arrMapper);
		if (cacl <= i) res = Math.min(res, i);
	}

	print(res);
}

function caclTime(m, arrMapper) {
	var head = 0;
	var last = arrMapper.length - 1;
	var give = -1;
	var checked = -1;
	var resurt = arrMapper[0];
	var sum = 0;

	while (head < last) {
		if (give <= 0) {
			give = arrMapper[head] - m;
			resurt = Math.max(resurt, arrMapper[head]);
			sum = 0;
		}
		if (checked <= 0) {
			checked = parseInt((m - arrMapper[last]) / 2);
			resurt = Math.max(resurt, arrMapper[last]);
		}
		if (checked <= 0 || give <= 0) break;

		if (checked >= give) {
			resurt = Math.min(resurt, arrMapper[head] - m);
			checked -= give;
			give = 0;
			head++;
			if (checked === 0) last--;
		} else {
			sum += checked;
			resurt = Math.min(resurt, arrMapper[head] - sum);
			give -= checked;
			checked = 0;
			last--;
		}
	}

	if (head === last) {
		resurt = Math.max(resurt, arrMapper[head]);
	}

	// print("m = ", m, " res = ", resurt);

	return resurt;
}

var t = +readline();

while (t--) {
	var k = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	var arr = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	if (k[0] === 1) {
		print(k[1]);
		continue;
	}

	var arrMapper = new Array(k[0]); // bad

	for (var i = 1; i <= k[0]; i++) {
		arrMapper[i - 1] = 0;
	}

	for (var i = 0; i < arr.length; i++) {
		arrMapper[arr[i] - 1]++;
	}
	arrMapper.sort(function (a, b) {
		return b - a;
	});

	var r = arrMapper[0];
	var maxTime = parseInt(r / 3);
	var l = 1;
	var m = parseInt((l + r) / 2);
	var res = arrMapper[0];

	while (l !== m && r != m) {
		var cacl = caclTime(m, arrMapper);
		if (cacl > m) l = m;
		else {
			r = m;
			res = Math.min(res, m);
		}
		m = parseInt((l + r) / 2);
	}

	for (var i = l; i <= r; i++) {
		var cacl = caclTime(i, arrMapper);
		if (cacl <= i) res = Math.min(res, i);
	}

	print(res);
}

function caclTime(m, arrMapper) {
	var head = 0;
	var last = arrMapper.length - 1;
	var give = -1;
	var checked = -1;
	var resurt = arrMapper[0];
	var sum = 0;

	while (head < last) {
		if (give <= 0) {
			give = arrMapper[head] - m;
			resurt = Math.max(resurt, arrMapper[head]);
			sum = 0;
		}
		if (checked <= 0) {
			checked = parseInt((m - arrMapper[last]) / 2);
			resurt = Math.max(resurt, arrMapper[last]);
		}
		if (checked <= 0 || give <= 0) break;

		if (checked >= give) {
			resurt = Math.min(resurt, arrMapper[head] - m);
			checked -= give;
			give = 0;
			head++;
			if (checked === 0) last--;
		} else {
			sum += checked;
			resurt = Math.min(resurt, arrMapper[head] - sum);
			give -= checked;
			checked = 0;
			last--;
		}
	}

	return resurt;
}

var t = +readline();

while (t--) {
	var k = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	var arr = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	if (k[0] === 1) {
		print(k[1]);
		continue;
	}

	var arrMapper = new Array(k[0]); // bad

	for (var i = 1; i <= k[0]; i++) {
		arrMapper[i - 1] = 0;
	}

	for (var i = 0; i < arr.length; i++) {
		arrMapper[arr[i] - 1]++;
	}

	arrMapper.sort(function (a, b) {
		return a < b;
	});

	var r = arrMapper[0];
	var maxTime = parseInt(r / 3);
	var l = r - maxTime;
	if (l >= 2) l--;
	var m = parseInt((l + r) / 2);
	var res = arrMapper[0];

	while (l !== m && r != m) {
		var cacl = caclTime(m, arrMapper);
		if (cacl > m) l = m;
		else {
			r = m;
			res = Math.min(res, m);
		}
		m = parseInt((l + r) / 2);
	}

	for (var i = l; i <= r; i++) {
		var cacl = caclTime(i, arrMapper);
		if (cacl <= i) res = Math.min(res, i);
	}

	print(res);
}

function caclTime(m, arrMapper) {
	var head = 0;
	var last = arrMapper.length - 1;
	var give = -1;
	var checked = -1;
	var resurt = arrMapper[0];
	var sum = 0;

	while (head < last) {
		if (give <= 0) give = arrMapper[head] - m;
		if (checked <= 0) checked = parseInt((m - arrMapper[last]) / 2);
		if (checked <= 0 || give <= 0) break;

		if (checked >= give) {
			resurt = Math.min(resurt, arrMapper[head] - m);
			checked -= give;
			give = 0;
			sum = 0;
			head++;
			if (checked === 0) last--;
		} else {
			sum += checked;
			resurt = Math.min(resurt, arrMapper[head] - sum);
			give -= checked;
			checked = 0;
			last--;
		}
	}

	return resurt;
}

var t = +readline();

while (t--) {
	var k = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	var arr = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	if (k[0] === 1) {
		print(k[1]);
		continue;
	}

	var arrMapper = new Array(k[0]); // bad

	for (var i = 1; i <= k[0]; i++) {
		arrMapper[i - 1] = 0;
	}

	for (var i = 0; i < arr.length; i++) {
		arrMapper[arr[i] - 1]++;
	}

	arrMapper.sort(function (a, b) {
		return a < b;
	});

	var r = arrMapper[0];
	var maxTime = parseInt(r / 3);
	var l = r - maxTime;
	var m = parseInt((l + r) / 2);
	var res = arrMapper[0];

	while (l !== m && r != m) {
		var cacl = caclTime(m, arrMapper);
		if (cacl > m) l = m;
		else {
			r = m;
			res = Math.min(res, m);
		}
		m = parseInt((l + r) / 2);
	}

	for (var i = l; i <= r; i++) {
		var cacl = caclTime(i, arrMapper);
		if (cacl <= i) res = Math.min(res, i);
	}

	print(res);
}

function caclTime(m, arrMapper) {
	var head = 0;
	var last = arrMapper.length - 1;
	var give = -1;
	var checked = -1;
	var resurt = -1;

	while (head < last) {
		if (give <= 0) give = arrMapper[head] - m;
		if (checked <= 0) checked = parseInt((m - arrMapper[last]) / 2);
		if (checked <= 0 || give <= 0) break;

		if (checked >= give) {
			resurt = Math.max(resurt, arrMapper[head] - give);
			checked -= give;
			give = 0;
			head++;
		} else {
			resurt = Math.max(resurt, arrMapper[head] - checked);
			give -= checked;
			checked = 0;
			last--;
		}
	}

	if (resurt === -1) resurt = arrMapper[0]; // no case

	return resurt;
}

var t = +readline();

while (t--) {
	var k = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	var arr = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	if (k[0] === 1) {
		print(k[1]);
		continue;
	}

	var arrMapper = new Array(k[0]); // bad

	for (var i = 1; i <= k[0]; i++) {
		arrMapper[i - 1] = 0;
	}

	for (var i = 0; i < arr.length; i++) {
		arrMapper[arr[i] - 1]++;
	}

	arrMapper.sort(function (a, b) {
		return a < b;
	});

	var r = arrMapper[0];
	var maxTime = parseInt(r / 3);
	var l = 1;
	var m = parseInt((l + r) / 2);
	var res = parseInt(2e5) + 1;

	while (l !== m && r != m) {
		var cacl = caclTime(m, arrMapper);
		if (cacl > m) l = m;
		else r = m;
		res = Math.min(res, cacl);
		m = parseInt((l + r) / 2);
	}

	for (var i = l; i <= r; i++) {
		var cacl = caclTime(i, arrMapper);
		res = Math.min(res, cacl);
	}

	print(res);
}

function caclTime(m, arrMapper) {
	var head = 0;
	var last = arrMapper.length - 1;
	var give = -1;
	var checked = -1;
	var resurt = -1;

	while (head < last) {
		if (give <= 0) give = parseInt(arrMapper[head] / 3);
		if (checked <= 0) checked = parseInt((m - arrMapper[last]) / 2);
		if (checked <= 0 || give <= 0) break;
		if (checked >= give) {
			checked -= give;
			resurt = Math.max(resurt, arrMapper[head] - give);
			resurt = Math.max(resurt, arrMapper[last] + give * 2);
			give = 0;
			head++;
		} else {
			give -= checked;
			resurt = Math.max(resurt, arrMapper[head] - checked);
			resurt = Math.max(resurt, arrMapper[last] + checked * 2);
			checked = 0;
			last--;
		}
	}

	if (resurt === -1) resurt = arrMapper[0];
	return resurt;
}

var t = +readline();

while (t--) {
	var k = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	var arr = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	if (k[0] === 1) {
		print(k[1]);
		continue;
	}

	var arrMapper = new Array(k[0]); // bad

	for (var i = 1; i <= k[0]; i++) {
		arrMapper[i - 1] = 0;
	}

	for (var i = 0; i < arr.length; i++) {
		arrMapper[arr[i] - 1]++;
	}

	arrMapper.sort(function (a, b) {
		return a < b;
	});

	var r = arrMapper[0];
	var maxTime = parseInt(r / 3);
	var l = r - maxTime;
	var m = parseInt((l + r) / 2);

	while (l !== m && r != m) {
		var cacl = caclTime(m, arrMapper);
		if (cacl > m) l = m;
		else r = m;
		m = parseInt((l + r) / 2);
	}

	var res = -1;

	for (var i = l; i <= r; i++) {
		if (res !== -1) break;
		var cacl = caclTime(i, arrMapper);
		if (cacl >= i) res = i;
	}

	print(res);
}

function caclTime(m, arrMapper) {
	var head = 0;
	var last = arrMapper.length - 1;
	var give = -1;
	var checked = -1;
	var resurt = m;

	while (head < last) {
		if (give <= 0) give = parseInt(arrMapper[head] / 3);
		if (checked <= 0) checked = parseInt((m - arrMapper[last]) / 2);
		if (checked <= 0 || give <= 0) break;
		if (checked >= give) {
			checked -= give;
			resurt = Math.max(resurt, arrMapper[head] - give);
			resurt = Math.max(resurt, arrMapper[last] + give * 2);
			give = 0;
			head++;
		} else {
			give -= checked;
			resurt = Math.max(resurt, arrMapper[head] - checked);
			resurt = Math.max(resurt, arrMapper[last] + checked * 2);
			checked = 0;
			last--;
		}
	}
	return resurt;
}

var t = +readline();

while (t--) {
	var k = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	var arr = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	if (k[0] === 1) {
		print(k[1]);
		continue;
	}

	var arrMapper = new Array(k[0]); // bad

	for (var i = 1; i <= k[0]; i++) {
		arrMapper[i - 1] = 0;
	}

	for (var i = 0; i < arr.length; i++) {
		arrMapper[arr[i] - 1]++;
	}

	arrMapper.sort(function (a, b) {
		return a < b;
	});

	var r = arrMapper[0];
	var maxTime = parseInt(r / 3);
	var l = r - maxTime;
	var m = parseInt((l + r) / 2);

	while (l !== m && r != m) {
		var cacl = caclTime(m, arrMapper);
		if (cacl >= m) l = m;
		else r = m;
		m = parseInt((l + r) / 2);
	}

	var res = -1;

	for (var i = l; i <= r; i++) {
		if (res !== -1) break;
		var cacl = caclTime(i, arrMapper);
		if (cacl >= i) res = i;
	}

	print(res);
}

function caclTime(m, arrMapper) {
	var head = 0;
	var last = arrMapper.length - 1;
	var give = -1;
	var checked = -1;
	var resurt = m;

	while (head < last) {
		if (give <= 0) give = parseInt(arrMapper[head] / 3);
		if (checked <= 0) checked = parseInt((m - arrMapper[last]) / 2);
		if (checked <= 0 || give <= 0) break;
		if (checked >= give) {
			checked -= give;
			resurt = Math.max(resurt, arrMapper[head] - give);
			resurt = Math.max(resurt, arrMapper[last] + give * 2);
			give = 0;
			head++;
		} else {
			give -= checked;
			resurt = Math.max(resurt, arrMapper[head] - checked);
			resurt = Math.max(resurt, arrMapper[last] + checked * 2);
			checked = 0;
			last--;
		}
	}
	return resurt;
}

var t = +readline();

while (t--) {
	var k = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	var arr = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	if (k[0] === 1) {
		print(k[1]);
		continue;
	}
	var arrCheck = new Array(k[0] + 1);
	var maxP = -1;
	var minP = parseInt(2e5 + 1);
	for (var i = 0; i < k[1]; i++) {
		if (arrCheck[arr[i]] === undefined) {
			arrCheck[arr[i]] = 0;
		}
		++arrCheck[arr[i]];
		maxP = Math.max(maxP, arrCheck[arr[i]]);
		minP = Math.min(minP, arrCheck[arr[i]]);
	}
	var div = maxP - minP;
	var s = parseInt(div / 3);
	div -= s;
	print(minP + div);
}

var t = +readline();

while (t--) {
	var k = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	var arr = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	var arrCheck = new Array(k[0] + 1);
	var maxP = -1;
	var minP = parseInt(2e5 + 1);
	for (var i = 0; i < k[1]; i++) {
		if (arrCheck[arr[i]] === undefined) {
			arrCheck[arr[i]] = 0;
		}
		++arrCheck[arr[i]];
		maxP = Math.max(maxP, arrCheck[arr[i]]);
		minP = Math.min(minP, arrCheck[arr[i]]);
	}
	var div = maxP - minP;
	var s = parseInt(div / 3);
	div -= s;
	print(minP + div);
}

var t = +readline();

while (t--) {
	var k = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	var arr = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	var arrCheck = new Array(k[0] + 1);
	var maxP = -1;
	var minP = parseInt(2e5 + 1);
	for (var i = 0; i < k[1]; i++) {
		if (arrCheck[arr[i]] === undefined) {
			arrCheck[arr[i]] = 0;
		}
		++arrCheck[arr[i]];
		maxP = Math.max(maxP, arrCheck[arr[i]]);
		minP = Math.min(minP, arrCheck[arr[i]]);
	}
	var div = maxP - minP;
	var s = parseInt(maxP / 3);
	maxP -= s * 2;
	print(minP + maxP);
}

var t = +readline();

while (t--) {
	var k = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	var arr = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	var arrCheck = new Array(k[0] + 1);
	var maxP = -1;
	var minP = parseInt(2e5 + 1);
	for (var i = 0; i < k[1]; i++) {
		if (arrCheck[arr[i]] === undefined) {
			arrCheck[arr[i]] = 0;
		}
		++arrCheck[arr[i]];
		maxP = Math.max(maxP, arrCheck[arr[i]]);
		minP = Math.min(minP, arrCheck[arr[i]]);
	}
	var div = maxP - minP;
	var s = parseInt(maxP / 3);
	maxP -= s * 2;
	print(s + maxP);
}

var t = +readline();

while (t--) {
	var k = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	var arr = readline()
		.split(" ")
		.map(function (a) {
			return +a;
		});
	var arrCheck = new Array(k[0] + 1);
	var maxP = -1;
	var minP = parseInt(2e5 + 1);
	for (var i = 0; i < k[1]; i++) {
		if (arrCheck[arr[i]] === undefined) {
			arrCheck[arr[i]] = 0;
		}
		++arrCheck[arr[i]];
		maxP = Math.max(maxP, arrCheck[arr[i]]);
		minP = Math.max(minP, arrCheck[arr[i]]);
	}
	var s = parseInt(maxP / 3);
	maxP -= s * 2;
	print(s + maxP);
}

'use strict';

function _defineProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }

  return obj;
}

function solve(n, m, a) {
  const w = new Array(n).fill(0);
  for (let i of a) w[i - 1]++;
  const max = new Heap((a, b) => b - a),
        min = new Heap((a, b) => a - b);
  for (let i = 0; i < n; i++) {
    min.push(w[i]);
    max.push(w[i]);
  }
  let res = Math.max(max.top(), min.top());
  while (max.top() > min.top()) {
    let a = max.pop(),
        b = min.pop();
    a--;
    b += 2;
    max.push(a);
    min.push(b);
    res = Math.min(res, Math.max(max.top(), min.top()));
  }
  return res;
}
class Heap {
  constructor(_comparator, arr) {
    _defineProperty(this, "_heap", []);
    this._comparator = _comparator;
    if (arr) {
      this._heap = [...arr];
      for (let i = arr.length >> 1; i; i--) this.down(i);
    }
  }
  swap(i, j) {
    [this._heap[i], this._heap[j]] = [this._heap[j], this._heap[i]];
  }
  _has(i) {
    return Object.prototype.hasOwnProperty.call(this._heap, i);
  }
  comparator(i, j) {
    return this._comparator(this._heap[i], this._heap[j]) < 0;
  }
  parent(i) {
    return i - 1 >> 1;
  }
  children(i) {
    return [2 * i + 1, 2 * i + 2];
  }
  up(i) {
    let parent = this.parent(i);
    while (i > 0 && this.comparator(i, parent)) {
      this.swap(i, parent);
      [i, parent] = [parent, parent - 1 >> 1];
    }
  }
  down(i) {
    let [left, right] = this.children(i);
    while (left < this._heap.length) {
      if (!this._has(right)) right = left;
      if (this.comparator(right, left)) [left, right] = [right, left];
      if (this.comparator(i, left)) break;
      this.swap(i, left);
      i = left;
      [left, right] = [2 * i + 1, 2 * i + 2];
    }
  }
  push(node) {
    this._heap.push(node);
    this.up(this._heap.length - 1);
  }
  pop() {
    if (this.size() === 0) return null;
    let res = this._heap[0];
    this.swap(0, this._heap.length - 1);
    this._heap.pop();
    this.down(0);
    return res;
  }
  remove(i) {
    if (i >= this.size()) return;
    let res = this._heap[i];
    this.swap(i, this._heap.length - 1);
    this._heap.pop();
    this.down(i);
    this.up(i);
    return res;
  }
  modify(i, val) {
    if (i >= this.size()) return;
    let res = this._heap[i];
    this._heap[i] = val;
    this.down(i);
    this.up(i);
    return res;
  }
  top() {
    if (this.size() === 0) return null;
    return this._heap[0];
  }
  size() {
    return this._heap.length;
  }
}

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

'use strict';

function _defineProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }

  return obj;
}

function solve(n, m, a) {
  const w = new Array(n).fill(0);
  for (let i of a) w[i - 1]++;
  const max = new Heap((a, b) => b - a),
        min = new Heap((a, b) => a - b);
  w.sort((a, b) => a - b);
  for (let i = 0; i < n; i++) {
    if (i < n / 2) min.push(w[i]);else max.push(w[i]);
  }
  let res = Math.max(max.top(), min.top());
  while (max.top() > min.top()) {
    let a = max.pop(),
        b = min.pop();
    a--;
    b += 2;
    res = Math.min(res, Math.max(a, b));
    max.push(a);
    min.push(b);
  }
  return res;
}
class Heap {
  constructor(_comparator, arr) {
    _defineProperty(this, "_heap", []);
    this._comparator = _comparator;
    if (arr) {
      this._heap = [...arr];
      for (let i = arr.length >> 1; i; i--) this.down(i);
    }
  }
  swap(i, j) {
    [this._heap[i], this._heap[j]] = [this._heap[j], this._heap[i]];
  }
  _has(i) {
    return Object.prototype.hasOwnProperty.call(this._heap, i);
  }
  comparator(i, j) {
    return this._comparator(this._heap[i], this._heap[j]) < 0;
  }
  parent(i) {
    return i - 1 >> 1;
  }
  children(i) {
    return [2 * i + 1, 2 * i + 2];
  }
  up(i) {
    let parent = this.parent(i);
    while (i > 0 && this.comparator(i, parent)) {
      this.swap(i, parent);
      [i, parent] = [parent, parent - 1 >> 1];
    }
  }
  down(i) {
    let [left, right] = this.children(i);
    while (left < this._heap.length) {
      if (!this._has(right)) right = left;
      if (this.comparator(right, left)) [left, right] = [right, left];
      if (this.comparator(i, left)) break;
      this.swap(i, left);
      i = left;
      [left, right] = [2 * i + 1, 2 * i + 2];
    }
  }
  push(node) {
    this._heap.push(node);
    this.up(this._heap.length - 1);
  }
  pop() {
    if (this.size() === 0) return null;
    let res = this._heap[0];
    this.swap(0, this._heap.length - 1);
    this._heap.pop();
    this.down(0);
    return res;
  }
  remove(i) {
    if (i >= this.size()) return;
    let res = this._heap[i];
    this.swap(i, this._heap.length - 1);
    this._heap.pop();
    this.down(i);
    this.up(i);
    return res;
  }
  modify(i, val) {
    if (i >= this.size()) return;
    let res = this._heap[i];
    this._heap[i] = val;
    this.down(i);
    this.up(i);
    return res;
  }
  top() {
    if (this.size() === 0) return null;
    return this._heap[0];
  }
  size() {
    return this._heap.length;
  }
}

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

'use strict';

function _defineProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }

  return obj;
}

function solve(n, m, a) {
  const w = new Array(n).fill(0);
  for (let i of a) w[i - 1]++;
  const max = new Heap((a, b) => b - a),
        min = new Heap((a, b) => a - b);
  for (let i = 0; i < w.length; i++) {
    max.push(w[i]);
    min.push(w[i]);
  }
  let res = Math.max(max.top(), min.top());
  while (max.top() > min.top()) {
    let a = max.pop(),
        b = min.pop();
    a--;
    b += 2;
    res = Math.min(res, Math.max(a, b));
    max.push(a);
    min.push(b);
  }
  return res;
}
class Heap {
  constructor(_comparator, arr) {
    _defineProperty(this, "_heap", []);
    this._comparator = _comparator;
    if (arr) {
      this._heap = [...arr];
      for (let i = arr.length >> 1; i; i--) this.down(i);
    }
  }
  swap(i, j) {
    [this._heap[i], this._heap[j]] = [this._heap[j], this._heap[i]];
  }
  _has(i) {
    return Object.prototype.hasOwnProperty.call(this._heap, i);
  }
  comparator(i, j) {
    return this._comparator(this._heap[i], this._heap[j]) < 0;
  }
  parent(i) {
    return i - 1 >> 1;
  }
  children(i) {
    return [2 * i + 1, 2 * i + 2];
  }
  up(i) {
    let parent = this.parent(i);
    while (i > 0 && this.comparator(i, parent)) {
      this.swap(i, parent);
      [i, parent] = [parent, parent - 1 >> 1];
    }
  }
  down(i) {
    let [left, right] = this.children(i);
    while (left < this._heap.length) {
      if (!this._has(right)) right = left;
      if (this.comparator(right, left)) [left, right] = [right, left];
      if (this.comparator(i, left)) break;
      this.swap(i, left);
      i = left;
      [left, right] = [2 * i + 1, 2 * i + 2];
    }
  }
  push(node) {
    this._heap.push(node);
    this.up(this._heap.length - 1);
  }
  pop() {
    if (this.size() === 0) return null;
    let res = this._heap[0];
    this.swap(0, this._heap.length - 1);
    this._heap.pop();
    this.down(0);
    return res;
  }
  remove(i) {
    if (i >= this.size()) return;
    let res = this._heap[i];
    this.swap(i, this._heap.length - 1);
    this._heap.pop();
    this.down(i);
    this.up(i);
    return res;
  }
  modify(i, val) {
    if (i >= this.size()) return;
    let res = this._heap[i];
    this._heap[i] = val;
    this.down(i);
    this.up(i);
    return res;
  }
  top() {
    if (this.size() === 0) return null;
    return this._heap[0];
  }
  size() {
    return this._heap.length;
  }
}

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

'use strict';

function _defineProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }

  return obj;
}

function solve(n, m, a) {
  const w = new Array(n).fill(0);
  for (let i of a) w[i - 1]++;
  const max = new Heap((a, b) => b - a),
        min = new Heap((a, b) => a - b);
  for (let i = 0; i < n; i++) {
    max.push(w[i]);
    min.push(w[i]);
  }
  let res = Math.max(max.top(), min.top());
  while (max.top() > min.top()) {
    let a = max.pop(),
        b = min.pop();
    a--;
    b += 2;
    res = Math.min(res, Math.max(a, b));
    max.push(a);
    min.push(b);
  }
  return res;
}
class Heap {
  constructor(_comparator, arr) {
    _defineProperty(this, "_heap", []);
    this._comparator = _comparator;
    if (arr) {
      this._heap = [...arr];
      for (let i = arr.length >> 1; i; i--) this.down(i);
    }
  }
  swap(i, j) {
    [this._heap[i], this._heap[j]] = [this._heap[j], this._heap[i]];
  }
  _has(i) {
    return Object.prototype.hasOwnProperty.call(this._heap, i);
  }
  comparator(i, j) {
    return this._comparator(this._heap[i], this._heap[j]) < 0;
  }
  parent(i) {
    return i - 1 >> 1;
  }
  children(i) {
    return [2 * i + 1, 2 * i + 2];
  }
  up(i) {
    let parent = this.parent(i);
    while (i > 0 && this.comparator(i, parent)) {
      this.swap(i, parent);
      [i, parent] = [parent, parent - 1 >> 1];
    }
  }
  down(i) {
    let [left, right] = this.children(i);
    while (left < this._heap.length) {
      if (!this._has(right)) right = left;
      if (this.comparator(right, left)) [left, right] = [right, left];
      if (this.comparator(i, left)) break;
      this.swap(i, left);
      i = left;
      [left, right] = [2 * i + 1, 2 * i + 2];
    }
  }
  push(node) {
    this._heap.push(node);
    this.up(this._heap.length - 1);
  }
  pop() {
    if (this.size() === 0) return null;
    let res = this._heap[0];
    this.swap(0, this._heap.length - 1);
    this._heap.pop();
    this.down(0);
    return res;
  }
  remove(i) {
    if (i >= this.size()) return;
    let res = this._heap[i];
    this.swap(i, this._heap.length - 1);
    this._heap.pop();
    this.down(i);
    this.up(i);
    return res;
  }
  modify(i, val) {
    if (i >= this.size()) return;
    let res = this._heap[i];
    this._heap[i] = val;
    this.down(i);
    this.up(i);
    return res;
  }
  top() {
    if (this.size() === 0) return null;
    return this._heap[0];
  }
  size() {
    return this._heap.length;
  }
}

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