There are $$$n$$$ workers and $$$m$$$ tasks. The workers are numbered from $$$1$$$ to $$$n$$$. Each task $$$i$$$ has a value $$$a_i$$$ — the index of worker who is proficient in this task.Every task should have a worker assigned to it. If a worker is proficient in the task, they complete it in $$$1$$$ hour. Otherwise, it takes them $$$2$$$ hours.The workers work in parallel, independently of each other. Each worker can only work on one task at once.Assign the workers to all tasks in such a way that the tasks are completed as early as possible. The work starts at time $$$0$$$. What's the minimum time all tasks can be completed by?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le m \le 2 \cdot 10^5$$$) — the number of workers and the number of tasks. The second line contains $$$m$$$ integers $$$a_1, a_2, \dots, a_m$$$ ($$$1 \le a_i \le n$$$) — the index of the worker proficient in the $$$i$$$-th task. The sum of $$$m$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$.

For each testcase, print a single integer — the minimum time all tasks can be completed by.

NoteIn the first testcase, the first worker works on tasks $$$1$$$ and $$$3$$$, and the second worker works on tasks $$$2$$$ and $$$4$$$. Since they both are proficient in the corresponding tasks, they take $$$1$$$ hour on each. Both of them complete $$$2$$$ tasks in $$$2$$$ hours. Thus, all tasks are completed by $$$2$$$ hours.In the second testcase, it's optimal to assign the first worker to tasks $$$1, 2$$$ and $$$3$$$ and the second worker to task $$$4$$$. The first worker spends $$$3$$$ hours, the second worker spends $$$2$$$ hours (since they are not proficient in the taken task).In the third example, each worker can be assigned to the task they are proficient at. Thus, each of them complete their task in $$$1$$$ hour.

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

'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

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