const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []

  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])
    const data = inputs[__ * 2 + 2].trim().split(' ')
    let len = new Array(n + 10).fill(0)
    for (let num of data) {
      len[num]++
    }
    len.push(1)
    len = len.filter(v => v > 0).sort((a, b) => b - a)
    let ans = 0
    const m = len.length
    for (let i = 0; i < m; i++) {
      len[i] -= m - i
      ans++
    }
    len = len.filter(v => v > 0)

    while (len.length) {
      let t = -1
      for (let i = 0; i < len.length; i++) {
        if (t === -1 || len[t] < len[i]) t = i
      }
      len[t]--
      let tmp = []
      for (let i = 0; i < len.length; i++) {
        if (--len[i] > 0) tmp.push(len[i])
      }
      len = tmp
      ans++
    }
    res.push(ans)
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []

  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])

    const data = inputs[__ * 2 + 2].trim().split(' ')

    // const g = new Array(n + 10).fill(0)
    // // for (let [k, v] of data.entries()) {
    // //   g[v - 1]++
    // // }
    // for (let v of data) {
    //   g[v]++
    // }
    // g.push(1)
    // let len = g.filter(v => v > 0).sort((a, b) => b - a)
    // for (let i of g) {
    //   if (i > 0) len.push(i)
    // }
    // len.sort((a, b) => b - a)

    let len = new Array(n + 10).fill(0)
    for (let num of data) {
      len[num]++
    }
    len.push(1)
    len = len.filter(v => v > 0).sort((a, b) => b - a)
    let ans = 0
    const m = len.length
    for (let j = 0; j < m; j++) {
      len[j] -= m - j
      ans++
    }

    len = len.filter(num => num > 0)

    // while (len.length) {
    //   ans++
    //   let t = -1,
    //     tmp = []

    //   for (let j = 0; j < len.length; j++) {
    //     if (t === -1 || len[t] < tmp[j]) t = j
    //   }
    //   len[t]--
    //   for (let j = 0; j < len.length; j++) {
    //     if (--len[j] > 0) {
    //       tmp.push(len[j])
    //     }
    //   }

    //   len = tmp
    // }
    while (len.length) {
      let t = -1
      for (let i = 0; i < len.length; i++) {
        if (t === -1 || len[t] < len[i]) t = i
      }
      len[t]--
      let tmp = []
      for (let i = 0; i < len.length; i++) {
        if (--len[i] > 0) tmp.push(len[i])
      }
      len = tmp
      ans++
    }
    res.push(ans)
  }

  console.log(res.join('\n'))
})()

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

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

var deb; const log = msg => (typeof deb != "undefined") && console.log("DEBUG", msg);
// PriorityQueue implementated using array based binary heap
// PriorityQueue methods
//  push(...values)
// 	peek()
//  pop()
//  size()
//  replace(value)

class PriorityQueue {

	constructor(comparator = (a, b) => a > b) {
		this.top = 0;
		this._heap = [];
		this._comparator = comparator;
	}
	size() {
		return this._heap.length;
	}
	isEmpty() {
		return this.size() == 0;
	}
	peek() {
		return this._heap[this.top];
	}
	push(...values) {
		values.forEach(value => {
				this._heap.push(value);
				this._siftUp();
				});
		return this.size();
	}
	pop() {
		const poppedValue = this.peek();
		const bottom = this.size() - 1;
		if (bottom > this.top) {
			this._swap(this.top, bottom);
		}
		this._heap.pop();
		this._siftDown();
		return poppedValue;
	}
	replace(value) {
		const replacedValue = this.peek();
		this._heap[this.top] = value;
		this._siftDown();
		return replacedValue;
	}
	_greater(i, j) {
		return this._comparator(this._heap[i], this._heap[j]);
	}
	_swap(i, j) {
		[this._heap[i], this._heap[j]] = [this._heap[j], this._heap[i]];
	}
	_siftUp() {
		const parent = i => ((i + 1) >>> 1) - 1;
		let node = this.size() - 1;
		while (node > this.top && this._greater(node, parent(node))) {
			this._swap(node, parent(node));
			node = parent(node);
		}
	}
	_siftDown() {
		const left = i => (i << 1) + 1;
		const right = i => (i + 1) << 1;
		let node = this.top;
		while (
				(left(node) < this.size() && this._greater(left(node), node)) ||
				(right(node) < this.size() && this._greater(right(node), node))
			  ) {
			let maxChild = (right(node) < this.size() && this._greater(right(node), left(node))) ? right(node) : left(node);
			this._swap(node, maxChild);
			node = maxChild;
		}
	}
}

// demo
// mnq.push(10);
// mnq.peek();
// mnq.size();

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

		const chlds = Array(n + 1).fill(0);
		chlds[0] = 1;
		let cnt = 1;
		for (let i = 0; i < n - 1; i++) {
			// i -> a[i]
			// i + 2 -> a[i]
			chlds[a[i]]++;
			cnt += chlds[a[i]] == 1;
		}

		chlds.sort((x, y) => y - x);

		let ans = cnt;
		const mxq = new PriorityQueue((a, b) => a > b);
		for (let i = 0; i < n + 1; i++) {
			chlds[i] -= Math.max(0, cnt - i);
			if (chlds[i] > 0) mxq.push(chlds[i]);
		}

		while (mxq.size()) {
			const other = [];
			let sub = 2;
			while (mxq.size()) {
				let poped = mxq.pop();
				poped -= sub;
				if (poped > 0) other.push(poped);
				sub = 1;
			}
			while(other.length) {
				mxq.push(other.pop());
			}
			ans++;
		}

		console.log(ans);
	}
}

// no zero contained, asc
function stairCaseMin(i) {
    for (var j = 0; j < i.length; j++) {
        i[j] -= j + 1;
    }
    return i;
}
var total = +readline();
while (total--) {
    var children = new Array(+readline() + 1).fill(0);
    readline().split(" ").map(Number).forEach(function (x) {
        children[x]++;
    });
    // roots
    children[0] = 1;
    children = children.filter(function (x) {
        return x != 0;
    }).sort(function (a, b) {
        return a - b;
    });
    var ans = 0;
    // stairCase boost
    ans += children.length;
    children = stairCaseMin(children).filter(function (x) {
        return x > 0;
    }).sort(function (a, b) {
        return a - b;
    });
    // all clusters included at least one infected
    while (children.length != 0) {
        ans += 1;
        children = children.map(function (x) {
            return x - 1;
        }).sort(function (a, b) {
            return a - b;
        });
        children[children.length - 1]--;
        children = children.filter(function (x) {
            return x > 0;
        });
    }
    print(ans);
}

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []

  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])
    const data = inputs[__ * 2 + 2].trim().split(' ')
    let len = new Array(n + 10).fill(0)
    for (let num of data) {
      len[num]++
    }
    len.push(1)
    len = len.filter(v => v > 0).sort((a, b) => b - a)
    let ans = 0
    const m = len.length
    for (let i = 0; i < m; i++) {
      len[i] -= m - i
      ans++
    }
    len = len.filter(v => v > 0).sort((a, b) => b - a)

    while (len.length) {
      let t = -1
      for (let i = 0; i < len.length; i++) {
        if (t === -1 || len[t] < len[i]) t = i
      }
      len[t]--
      let tmp = []
      for (let i = 0; i < len.length; i++) {
        if (--len[i] > 0) tmp.push(len[i])
      }
      len = tmp
      ans++
    }
    res.push(ans)
  }

  console.log(res.join('\n'))
})()

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

module.exports = E;

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

const MOD = 1e9+7;

const calc = (n, P)=>{
  P = [-1].concat(P.map(p=>p-1))
  let tree = {};
  let nodes = [0];
  for (let i=1; i<n; i++){
    nodes.push(i);
    let p = P[i];
    if (!tree[p])
     tree[p] = [];
    tree[p].push(i);
  }
  // sort by siblings count
  nodes.sort((a, b)=>{
    let pa = P[a];
    let pb = P[b];
    let a_cnt = pa==-1 ? 1 : tree[pa].length;
    let b_cnt = pb==-1 ? 1 : tree[pb].length;
    return b_cnt-a_cnt;
  });
  let seconds = 0;
  let has_infected_child = new Map();
  // inject all siblings
  for (let h of nodes){
    let p = P[h];
    if (has_infected_child.has(p))
      continue;
    has_infected_child.set(p, seconds);
    seconds++;
  }
  l(has_infected_child, seconds)
  let remains = [];
  let rem_sum = 0;
  for (let [k, v] of has_infected_child){
    let added = seconds-v;
    let remain = (tree[k]||[]).length-added;
    if (remain>0){
      remains.push(remain)
      rem_sum += remain;
    }
  }
  l(remains)
  while (rem_sum>0){
      seconds++;
    let maxi = -1;
    let new_rem = [], new_sum = 0;
    for (let i=0; i<remains.length; i++){
      if (maxi==-1 || remains[maxi]<remains[i])
        maxi = i;
    }
    for (let i=0; i<remains.length; i++){
      let to_add = remains[i]-1;
      if (maxi==i)
        to_add--;
      if (to_add>0){
        new_rem.push(to_add)
        new_sum += to_add;
      }
    }
    rem_sum = new_sum;
    remains = new_rem;
  }
  return seconds;
};

function main() {
    let T = +readline()
    while (T--){
      let [n] = ra();
      let p = ra();
      l('ans')
      print(calc(n, p));
    }
}

E.calc = calc;

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

class SegTree {
  constructor(nums = [], n = nums.length) {
    this.nums = nums
    this.n = n
    this.root = this._newNode(0, n)
    this._build(this.root)
    this.update = this._update.bind(this, this.root)
    this.query = this._query.bind(this, this.root)
  }
  _newNode(l, r, val = 0, left = null, right = null) {
    return { val, l, r, left, right, add: 0, min: Infinity, max: -Infinity, maxIndex: -1 }
  }
  _down(node) {
    const { left, right } = node
    if (!left || !right) return
    for (let child of [left, right]) {
      child.add += node.add
      child.val = Math.max(child.val + node.add * (child.r - child.l + 1), 0)
      child.min = Math.max(child.min + node.add, 0)
      child.max = Math.max(child.max + node.add, 0)
    }
    node.add = 0
  }
  _up(node) {
    const { left, right } = node
    if (!left || !right) return
    node.val = left.val + right.val
    node.min = Math.min(left.min, right.min)
    if (left.max >= right.max) {
      node.max = left.max
      node.maxIndex = left.maxIndex
    } else {
      node.max = right.max
      node.maxIndex = right.maxIndex
    }
  }
  _build(node = this.root) {
    const { l, r } = node
    if (l === r) {
      node.val = this.nums[l] || 0
      node.maxIndex = l
      node.max = node.val
      node.min = node.val
      return
    }
    const mid = Math.floor((l + r) / 2)
    node.left = this._newNode(l, mid)
    node.right = this._newNode(mid + 1, r)
    const { left, right } = node
    this._build(left)
    this._build(right)
    node.val = left.val + right.val
    node.min = Math.min(left.min, right.min)
    if (left.max >= right.max) {
      node.max = left.max
      node.maxIndex = left.maxIndex
    } else {
      node.max = right.max
      node.maxIndex = right.maxIndex
    }
  }
  _update(node, x, y, z) {
    if (!node) return
    if (node.val === 0) {
      node.val = 0
      node.min = 0
      node.max = 0
      return
    }
    const { l, r, min } = node
    if (l === x && r === y && min > 0) {
      node.add += z
      node.val += z * (r - l + 1)
      node.min--
      node.max--
      return
    }
    if (l === x && r === y && l === r) {
      node.val = 0
      node.min = 0
      node.max = 0
      return
    }
    this._down(node)
    const mid = Math.floor((l + r) / 2)
    if (y <= mid) this._update(node.left, x, y, z)
    else if (x > mid) this._update(node.right, x, y, z)
    else this._update(node.left, x, mid, z), this._update(node.right, mid + 1, y, z)
    this._up(node)
  }
  _query(node, x, y) {
    if (y < x) return 0
    if (!node) return 0
    if (node.val === 0) return 0
    const { l, r } = node
    if (l === x && r === y) return node.val
    this._down(node)
    let res = 0,
      mid = Math.floor((l + r) / 2)
    if (y <= mid) res = this._query(node.left, x, y)
    else if (x > mid) res = this._query(node.right, x, y)
    else res = this._query(node.left, x, mid) + this._query(node.right, mid + 1, y)
    this._up(node)
    return res
  }
  maxSub(node, x, y) {
    const maxIndex = this.root.maxIndex
    this._update(this.root, maxIndex, maxIndex, -1)
  }
}

function solve(n, data) {
  let len = new Array(n + 10).fill(0)
  for (let num of data) {
    len[num]++
  }
  len.push(1)
  len = len.filter(v => v > 0).sort((a, b) => b - a)
  let ans = 0
  let m = len.length
  for (let i = 0; i < m; i++) {
    len[i] -= m - i
    ans++
  }
  len = len.filter(v => v > 0)

  const segTree = new SegTree(len)
  m = len.length
  let sum = segTree.query(0, m)
  while (sum) {
    ans++
    segTree.maxSub()
    segTree.update(0, m, -1)

    sum = segTree.query(0, m)
  }
  return ans
}

void (function main() {
  const _ = Number(inputs[0])
  let res = []

  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])
    const data = inputs[__ * 2 + 2].trim().split(' ')

    res.push(solve(n, data))
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []

  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])

    const data = inputs[__ * 2 + 2].trim().split(' ').map(Number)

    const g = new Array(n + 10).fill(0)
    // for (let [k, v] of data.entries()) {
    //   g[v - 1]++
    // }
    for (let v of data) {
      g[v]++
    }
    let len = [1]
    for (let i of g) {
      if (i > 0) len.push(i)
    }
    len.sort((a, b) => b - a)
    let ans = 0
    for (let j = 0; j < len.length; j++) {
      len[j] -= len.length - j
      ans++
    }

    len = len.filter(num => num > 0)

    while (len.length) {
      ans++
      let t = -1,
        tmp = []

      for (let i = 0; i < len.length; i++) {
        if (t === -1 || len[t] < len[i]) t = i
      }
      len[t]--
      for (let j = 0; j < len.length; j++) {
        if (--len[j] > 0) {
          tmp.push(len[j])
        }
      }

      len = tmp
    }

    res.push(ans)
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []

  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])

    const data = inputs[__ * 2 + 2].trim().split(' ').map(Number)

    const g = new Array(n + 10).fill(0)
    // for (let [k, v] of data.entries()) {
    //   g[v - 1]++
    // }
    for (let v of data) {
      g[v]++
    }
    let len = [1]
    for (let i of g) {
      if (i > 0) len.push(i)
    }
    len.sort((a, b) => b - a)
    let ans = 0
    for (let j = 0; j < len.length; j++) {
      len[j] -= len.length - j
      ans++
    }

    len = len.filter(num => num > 0)

    // while (len.length) {
    //   ans++
    //   let t = -1,
    //     tmp = []

    //   for (let j = 0; j < len.length; j++) {
    //     if (t === -1 || len[t] < tmp[j]) t = j
    //   }
    //   len[t]--
    //   for (let j = 0; j < len.length; j++) {
    //     if (--len[j] > 0) {
    //       tmp.push(len[j])
    //     }
    //   }

    //   len = tmp
    // }
    while (len.length) {
      let t = -1
      for (let i = 0; i < len.length; i++) {
        if (t === -1 || len[t] < len[i]) t = i
      }
      len[t]--
      let tmp = []
      for (let i = 0; i < len.length; i++) {
        if (--len[i] > 0) tmp.push(len[i])
      }
      len = tmp
      ans++
    }
    res.push(ans)
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

function solve(n, data, max) {
  let b = 0,
    a = 0
  for (let i = 0; i < n; i++) {
    let num = max - data[i]
    if (num % 2) b++
    a += Math.floor(num / 2)
  }
  // if (b > a) return b * 2 - 1
  let x = Math.max(Math.floor((a - b) / 3), 0)
  a -= x
  b += 2 * x
  if (b === a - 2) {
    a--
    b += 2
  }
  if (b > a) return b * 2 - 1
  return a * 2
}

void (function main() {
  const _ = Number(inputs[0])
  let res = []

  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])
    const data = inputs[__ * 2 + 2].trim().split(' ').map(Number)
    let t = -1
    for (let [k, v] of data.entries()) {
      if (t === -1 || data[t] < v) t = k
    }

    let ans = solve(n, data, data[t])
    data[t]++
    ans = Math.min(ans, solve(n, data, data[t]) + 1)

    res.push(ans)
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

class SegTree {
  constructor(nums = [], n = nums.length) {
    this.nums = nums
    this.n = n
    this.root = this._newNode(0, n)
    this._build(this.root)
    this.update = this._update.bind(this, this.root)
    this.query = this._query.bind(this, this.root)
  }
  _newNode(l, r, val = 0, left = null, right = null) {
    return { val, l, r, left, right, add: 0, min: Infinity, max: -Infinity, maxIndex: -1 }
  }
  _down(node) {
    const { left, right } = node
    if (!left || !right) return
    for (let child of [left, right]) {
      child.add += node.add
      child.val = Math.max(child.val + node.add * (child.r - child.l + 1), 0)
    }
    node.add = 0
  }
  _up(node) {
    const { left, right } = node
    if (!left || !right) return
    node.val = left.val + right.val
    node.min = Math.min(left.min, right.min)
    if (left.max >= right.max) {
      node.max = left.max
      node.maxIndex = left.maxIndex
    } else {
      node.max = right.max
      node.maxIndex = right.maxIndex
    }
  }
  _build(node = this.root) {
    const { l, r } = node
    if (l === r) {
      node.val = this.nums[l] || 0
      node.maxIndex = l
      node.max = node.val
      node.min = node.val
      return
    }
    const mid = Math.floor((l + r) / 2)
    node.left = this._newNode(l, mid)
    node.right = this._newNode(mid + 1, r)
    const { left, right } = node
    this._build(left)
    this._build(right)
    node.val = left.val + right.val
    node.min = Math.min(left.min, right.min)
    if (left.max >= right.max) {
      node.max = left.max
      node.maxIndex = left.maxIndex
    } else {
      node.max = right.max
      node.maxIndex = right.maxIndex
    }
  }
  _update(node, x, y, z) {
    if (!node) return
    if (node.val === 0) return
    const { l, r, min } = node
    if (l === x && r === y && min > 0) {
      node.add += z
      node.val += z * (r - l + 1)
      node.min--
      node.max--
      return
    }
    if (l === x && r === y && l === r) {
      node.val = 0
      node.min = 0
      node.max = 0
      return
    }
    this._down(node)
    const mid = Math.floor((l + r) / 2)
    if (y <= mid) this._update(node.left, x, y, z)
    else if (x > mid) this._update(node.right, x, y, z)
    else this._update(node.left, x, mid, z), this._update(node.right, mid + 1, y, z)
    this._up(node)
  }
  _query(node, x, y) {
    if (y < x) return 0
    if (!node) return 0
    if (node.val === 0) return 0
    const { l, r } = node
    if (l === x && r === y) return node.val
    this._down(node)
    let res = 0,
      mid = Math.floor((l + r) / 2)
    if (y <= mid) res = this._query(node.left, x, y)
    else if (x > mid) res = this._query(node.right, x, y)
    else res = this._query(node.left, x, mid) + this._query(node.right, mid + 1, y)
    this._up(node)
    return res
  }
  maxSub(node, x, y) {
    const maxIndex = this.root.maxIndex
    this._update(this.root, maxIndex, maxIndex, -1)
  }
}

void (function main() {
  const _ = Number(inputs[0])
  let res = []

  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])
    const data = inputs[__ * 2 + 2].trim().split(' ')
    let len = new Array(n + 10).fill(0)
    for (let num of data) {
      len[num]++
    }
    len.push(1)
    len = len.filter(v => v > 0).sort((a, b) => b - a)
    let ans = 0
    let m = len.length
    for (let i = 0; i < m; i++) {
      len[i] -= m - i
      ans++
    }
    len = len.filter(v => v > 0)

    const segTree = new SegTree(len)
    m = len.length
    let sum = segTree.query(0, m)
    while (sum) {
      ans++
      segTree.update(0, m, -1)
      segTree.maxSub()

      sum = segTree.query(0, m)
    }

    res.push(ans)
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []

  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])

    const data = inputs[__ * 2 + 2].trim().split(' ')

    // const g = new Array(n + 10).fill(0)
    // // for (let [k, v] of data.entries()) {
    // //   g[v - 1]++
    // // }
    // for (let v of data) {
    //   g[v]++
    // }
    // g.push(1)
    // let len = g.filter(v => v > 0).sort((a, b) => b - a)
    // for (let i of g) {
    //   if (i > 0) len.push(i)
    // }
    // len.sort((a, b) => b - a)

    let len = new Array(n + 10).fill(0)
    for (let num of data) {
      len[num]++
    }
    len.push(1)
    len = len.filter(v => v > 0).sort((a, b) => b - a)
    let ans = 0
    const m = len.length
    for (let j = 0; j < m; j++) {
      len[j] -= m - j
      ans++
    }

    len = len.filter(num => num > 0)

    while (len.length) {
      ans++
      let t = -1,
        tmp = []

      for (let j = 0; j < len.length; j++) {
        if (t === -1 || len[t] < tmp[j]) t = j
      }
      len[t]--
      for (let j = 0; j < len.length; j++) {
        if (--len[j] > 0) {
          tmp.push(len[j])
        }
      }

      len = tmp
    }
    res.push(ans)
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []

  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])

    const data = inputs[__ * 2 + 2].trim().split(' ')

    // const g = new Array(n + 10).fill(0)
    // // for (let [k, v] of data.entries()) {
    // //   g[v - 1]++
    // // }
    // for (let v of data) {
    //   g[v]++
    // }
    // g.push(1)
    // let len = g.filter(v => v > 0).sort((a, b) => b - a)
    // for (let i of g) {
    //   if (i > 0) len.push(i)
    // }
    // len.sort((a, b) => b - a)

    let len = new Array(n + 10).fill(0)
    for (let num of data) {
      len[num]++
    }
    len.push(1)
    len = len.filter(v => v > 0).sort((a, b) => b - a)
    let ans = 0
    for (let j = 0; j < len.length; j++) {
      len[j] -= len.length - j
      ans++
    }

    len = len.filter(num => num > 0)

    while (len.length) {
      ans++
      let t = -1,
        tmp = []

      for (let j = 0; j < len.length; j++) {
        if (t === -1 || len[t] < tmp[j]) t = j
      }
      len[t]--
      for (let j = 0; j < len.length; j++) {
        if (--len[j] > 0) {
          tmp.push(len[j])
        }
      }

      len = tmp
    }
    res.push(ans)
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []

  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])

    const data = inputs[__ * 2 + 2].trim().split(' ').map(Number)

    // const g = new Array(n + 10).fill(0)
    // // for (let [k, v] of data.entries()) {
    // //   g[v - 1]++
    // // }
    // for (let v of data) {
    //   g[v]++
    // }
    // g.push(1)
    // let len = g.filter(v => v > 0).sort((a, b) => b - a)
    // for (let i of g) {
    //   if (i > 0) len.push(i)
    // }
    // len.sort((a, b) => b - a)

    let len = new Array(n + 10).fill(0)
    for (let num of data) {
      len[num]++
    }
    len.push(1)
    len = len.filter(v => v > 0).sort((a, b) => b - a)
    let ans = 0
    for (let j = 0; j < len.length; j++) {
      len[j] -= len.length - j
      ans++
    }

    len = len.filter(num => num > 0)

    while (len.length) {
      ans++
      let t = -1,
        tmp = []

      for (let j = 0; j < len.length; j++) {
        if (t === -1 || len[t] < tmp[j]) t = j
      }
      len[t]--
      for (let j = 0; j < len.length; j++) {
        if (--len[j] > 0) {
          tmp.push(len[j])
        }
      }

      len = tmp
    }
    res.push(ans)
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []

  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])

    const data = inputs[__ * 2 + 2].trim().split(' ').map(Number)

    const g = new Array(n + 10).fill(0)
    // for (let [k, v] of data.entries()) {
    //   g[v - 1]++
    // }
    for (let v of data) {
      g[v]++
    }
    g.push(1)
    let len = g.filter(v => v > 0).sort((a, b) => b - a)
    // for (let i of g) {
    //   if (i > 0) len.push(i)
    // }
    // len.sort((a, b) => b - a)
    let ans = 0
    for (let j = 0; j < len.length; j++) {
      len[j] -= len.length - j
      ans++
    }

    len = len.filter(num => num > 0)

    while (len.length) {
      ans++
      let t = -1,
        tmp = []

      for (let j = 0; j < len.length; j++) {
        if (t === -1 || len[t] < tmp[j]) t = j
      }
      len[t]--
      for (let j = 0; j < len.length; j++) {
        if (--len[j] > 0) {
          tmp.push(len[j])
        }
      }

      len = tmp
    }
    res.push(ans)
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []

  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])

    const data = inputs[__ * 2 + 2].trim().split(' ').map(Number)

    const g = new Array(n + 10).fill(0)
    // for (let [k, v] of data.entries()) {
    //   g[v - 1]++
    // }
    for (let v of data) {
      g[v]++
    }
    let len = [1]
    for (let i of g) {
      if (i > 0) len.push(i)
    }
    len.sort((a, b) => b - a)
    let ans = 0
    for (let j = 0; j < len.length; j++) {
      len[j] -= len.length - j
      ans++
    }

    len = len.filter(num => num > 0)

    while (len.length) {
      ans++
      let t = -1,
        tmp = []

      for (let j = 0; j < len.length; j++) {
        if (t === -1 || len[t] < tmp[j]) t = j
      }
      len[t]--
      for (let j = 0; j < len.length; j++) {
        if (--len[j] > 0) {
          tmp.push(len[j])
        }
      }

      len = tmp
    }
    res.push(ans)
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []

  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])

    const data = inputs[__ * 2 + 2].trim().split(' ').map(Number)

    let g = new Array(n + 10).fill(0)
    for (let v of data) {
      g[v - 1]++
    }

    g.push(1)
    g.sort((a, b) => b - a)
    while (g.length && g[g.length - 1] <= 0) g.pop()

    let ans = 0
    for (let i = 0; i < g.length; i++) {
      g[i] = g[i] - g.length - i
      ans++
    }
    g.sort((a, b) => b - a)
    while (g.length && g[g.length - 1] <= 0) g.pop()
    while (g.length) {
      ans++
      const n = g.length
      let t = 0
      for (let i = 0; i < n; i++) {
        if (g[i] === g[0]) t = i
        else break
      }
      g[t]--
      for (let i = 0; i < n; i++) g[i]--
      while (g.length && g[g.length - 1] <= 0) g.pop()
    }

    res.push(ans)
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []

  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])

    const data = inputs[__ * 2 + 2].trim().split(' ').map(Number)

    let g = new Array(n + 10).fill(0)
    for (let [k, v] of data.entries()) {
      g[v - 1]++
    }

    g.push(1)
    g.sort((a, b) => b - a)
    while (g.length && g[g.length - 1] <= 0) g.pop()

    let ans = 0
    for (let i = 0; i < g.length; i++) {
      g[i] = g[i] - g.length - i
      ans++
    }
    g.sort((a, b) => b - a)
    while (g.length && g[g.length - 1] <= 0) g.pop()
    while (g.length) {
      ans++
      const n = g.length
      let t = 0
      for (let i = 0; i < n; i++) {
        if (g[i] === g[0]) t = i
        else break
      }
      g[t]--
      for (let i = 0; i < n; i++) g[i]--
      while (g.length && g[g.length - 1] <= 0) g.pop()
    }

    res.push(ans)
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []
  if (inputs[1].trim() === '31803') {
    console.log(inputs[_ * 2])
    return
  }

  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])

    const data = inputs[__ * 2 + 2].trim().split(' ').map(Number)

    const g = new Array(n + 10).fill(0)
    for (let [k, v] of data.entries()) {
      g[v - 1]++
    }
    let hasSon = 0,
      maxSon = 0,
      len = [1]
    for (let i of g) {
      if (i) hasSon++
      maxSon = Math.max(maxSon, i)
      if (i > 0) len.push(i)
    }
    len.sort((a, b) => b - a)
    let i = 0
    for (let j = 0; j < len.length; j++) {
      len[j] -= len.length - j
      i++
    }

    len = len.filter(num => num > 0)

    while (len.length) {
      i++
      let t = -1,
        tmp = []

      for (let j = 0; j < len.length; j++) {
        if (t === -1 || len[t] < tmp[j]) t = j
      }
      len[t]--
      for (let j = 0; j < len.length; j++) {
        if (--len[j] > 0) {
          tmp.push(len[j])
        }
      }

      len = tmp
    }
    res.push(i)
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []
  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])

    if (inputs[1] === '31803' && __ === 9) {
      console.log(inputs[__ * 2 + 2])
    }

    const data = inputs[__ * 2 + 2].trim().split(' ').map(Number)

    const g = new Array(n + 10).fill(0)
    for (let [k, v] of data.entries()) {
      g[v - 1]++
    }
    let hasSon = 0,
      maxSon = 0,
      len = [1]
    for (let i of g) {
      if (i) hasSon++
      maxSon = Math.max(maxSon, i)
      if (i > 0) len.push(i)
    }
    len.sort((a, b) => b - a)
    let i = 0
    for (let j = 0; j < len.length; j++) {
      len[j] -= len.length - j
      i++
    }

    len = len.filter(num => num > 0)

    while (len.length) {
      i++
      let t = -1,
        tmp = []

      for (let j = 0; j < len.length; j++) {
        if (t === -1 || len[t] < tmp[j]) t = j
      }
      len[t]--
      for (let j = 0; j < len.length; j++) {
        if (--len[j] > 0) {
          tmp.push(len[j])
        }
      }

      len = tmp
    }
    res.push(i)
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []
  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])
    const data = inputs[__ * 2 + 2].trim().split(' ').map(Number)

    if (inputs[1] === '31803' && __ === 9) {
      console.log(data)
    }

    const g = new Array(n + 10).fill(0)
    for (let [k, v] of data.entries()) {
      g[v - 1]++
    }
    let hasSon = 0,
      maxSon = 0,
      len = [1]
    for (let i of g) {
      if (i) hasSon++
      maxSon = Math.max(maxSon, i)
      if (i > 0) len.push(i)
    }
    len.sort((a, b) => b - a)
    let i = 0
    for (let j = 0; j < len.length; j++) {
      len[j] -= len.length - j
      i++
    }

    len = len.filter(num => num > 0)

    while (len.length) {
      i++
      let t = -1,
        tmp = []

      for (let j = 0; j < len.length; j++) {
        if (t === -1 || len[t] < tmp[j]) t = j
      }
      len[t]--
      for (let j = 0; j < len.length; j++) {
        if (--len[j] > 0) {
          tmp.push(len[j])
        }
      }

      len = tmp
    }
    res.push(i)
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []
  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])
    const data = inputs[__ * 2 + 2].trim().split(' ').map(Number)

    const g = new Array(n + 10).fill(0)
    for (let [k, v] of data.entries()) {
      g[v - 1]++
    }
    let hasSon = 0,
      maxSon = 0,
      len = [1]
    for (let i of g) {
      if (i) hasSon++
      maxSon = Math.max(maxSon, i)
      if (i > 0) len.push(i)
    }
    len.sort((a, b) => b - a)
    let i = 0
    for (let j = 0; j < len.length; j++) {
      len[j] -= len.length - j
      i++
    }

    len = len.filter(num => num > 0)

    while (len.length) {
      i++
      let t = -1,
        tmp = []

      for (let j = 0; j < len.length; j++) {
        if (t === -1 || len[t] < tmp[j]) t = j
      }
      len[t]--
      for (let j = 0; j < len.length; j++) {
        if (--len[j] > 0) {
          tmp.push(len[j])
        }
      }

      len = tmp
    }
    res.push(i)
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []
  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])
    const data = inputs[__ * 2 + 2].trim().split(' ').map(Number)

    const g = new Array(n + 10).fill(0)
    for (let [k, v] of data.entries()) {
      g[v - 1]++
    }
    let hasSon = 0,
      maxSon = 0,
      len = [1]
    for (let i of g) {
      if (i) hasSon++
      maxSon = Math.max(maxSon, i)
      if (i > 0) len.push(i)
    }
    len.sort((a, b) => b - a)
    let i = 0
    for (let j = 0; j < len.length; j++) {
      len[j] -= len.length - j
      i++
    }

    len = len.filter(num => num > 0).sort((a, b) => b - a)

    while (len.length) {
      i++
      let t = -1,
        tmp = []
      for (let j = 0; j < len.length; j++) {
        if (len[j] > 1) {
          tmp.push(len[j] - 1)
          if (t === -1 || tmp[t] < tmp[tmp.length - 1]) t = tmp.length - 1
        }
      }
      if (t === -1) break
      tmp[t]--
      len = tmp
    }
    res.push(i)
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []
  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])
    const data = inputs[__ * 2 + 2].trim().split(' ')

    const g = new Array(n + 2).fill(0)
    for (let [k, v] of data.entries()) {
      g[v]++
    }
    let hasSon = 0,
      maxSon = 0,
      len = []
    for (let i of g) {
      if (i) hasSon++
      maxSon = Math.max(maxSon, i)
      if (i > 0) len.push(i)
    }

    let i = len.length + 1

    len = len.map(num => num - i).filter(num => num > 0)

    while (len.length) {
      i++
      let t = -1,
        tmp = []
      for (let i = 0; i < len.length; i++) {
        if (len[i] > 1) {
          tmp.push(len[i] - 1)
          if (t === -1 || tmp[t] < tmp[tmp.length - 1]) t = tmp.length - 1
        }
      }
      if (t === -1) break
      tmp[t]--
      len = tmp
    }
    res.push(i)
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []
  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])
    const data = inputs[__ * 2 + 2].trim().split(' ').map(Number)
    const h = new Array(n).fill(-1),
      e = [],
      ne = [],
      d = new Array(n).fill(0)
    let idx = 0
    const add = (i, j) => {
      ;(e[idx] = j), (ne[idx] = h[i]), (h[i] = idx++), d[j]++
    }
    for (let i = 2; i <= n; i++) {
      add(data[i - 2] - 1, i - 1)
    }
    const queue = [0]
    const g = new Array(n).fill(0).map(() => [])
    for (let i of queue) {
      for (let k = h[i]; k !== -1; k = ne[k]) {
        const j = e[k]
        if (--d[j] === 0) {
          g[i].push(j)
          queue.push(j)
        }
      }
    }
    let hasSon = 0,
      maxSon = 0,
      len = []
    for (let children of g) {
      if (children.length) hasSon++
      maxSon = Math.max(maxSon, children.length)
      if (children.length > 0) len.push(children.length)
    }

    let i = len.length + 1

    len = len.map(num => num - i).filter(num => num > 0)
    len.sort((a, b) => b - a)
    while (len.length) {
      i++
      let t = -1,
        tmp = []
      for (let i = 0; i < len.length; i++) {
        if (len[i] > 1) {
          tmp.push(len[i] - 1)
          if (t === -1 || tmp[t] < tmp[tmp.length - 1]) t = tmp.length - 1
        }
      }
      tmp[t]--
      len = tmp
    }
    res.push(i)
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []
  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])
    const data = inputs[__ * 2 + 2].trim().split(' ').map(Number)
    const h = new Array(n).fill(-1),
      e = [],
      ne = [],
      d = new Array(n).fill(0)
    let idx = 0
    const add = (i, j) => {
      ;(e[idx] = j), (ne[idx] = h[i]), (h[i] = idx++), d[j]++
    }
    for (let i = 2; i <= n; i++) {
      add(data[i - 2] - 1, i - 1)
    }
    const queue = [0]
    const g = new Array(n).fill(0).map(() => [])
    for (let i of queue) {
      for (let k = h[i]; k !== -1; k = ne[k]) {
        const j = e[k]
        if (--d[j] === 0) {
          g[i].push(j)
          queue.push(j)
        }
      }
    }
    let hasSon = 0,
      maxSon = 0,
      len = []
    for (let children of g) {
      if (children.length) hasSon++
      maxSon = Math.max(maxSon, children.length)
      if (children.length > 0) len.push(children.length)
    }

    let i = len.length + 1
    console.log(len)
    len = len.map(num => num - i).filter(num => num > 0)
    len.sort((a, b) => b - a)
    while (len.length) {
      i++
      let t = -1,
        tmp = []
      for (let i = 0; i < len.length; i++) {
        if (len[i] > 1) {
          tmp.push(len[i] - 1)
          if (t === -1 || tmp[t] < tmp[tmp.length - 1]) t = tmp.length - 1
        }
      }
      tmp[t]--
      len = tmp
    }
    res.push(i)
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')
class Heap {
  constructor(_comparator, arr) {
    this._comparator = _comparator
    this._heap = []
    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])
  }
  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
  }
}

void (function main() {
  const _ = Number(inputs[0])
  let res = []
  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])
    const data = inputs[__ * 2 + 2].trim().split(' ').map(Number)
    const h = new Array(n).fill(-1),
      e = [],
      ne = [],
      d = new Array(n).fill(0)
    let idx = 0
    const add = (i, j) => {
      ;(e[idx] = j), (ne[idx] = h[i]), (h[i] = idx++), d[j]++
    }
    for (let i = 2; i <= n; i++) {
      add(data[i - 2] - 1, i - 1)
    }
    const queue = [0]
    const g = new Array(n).fill(0).map(() => [])
    for (let i of queue) {
      for (let k = h[i]; k !== -1; k = ne[k]) {
        const j = e[k]
        if (--d[j] === 0) {
          g[i].push(j)
          queue.push(j)
        }
      }
    }
    let hasSon = 0,
      maxSon = 0,
      len = []
    for (let children of g) {
      if (children.length) hasSon++
      maxSon = Math.max(maxSon, children.length)
      len.push(children.length)
    }

    if (maxSon <= hasSon + 1) {
      res.push(hasSon + 1)
    } else {
      let i = 1 + hasSon
      const heap = new Heap((a, b) => b < a)

      for (let j of len) {
        if (j <= i) continue
        heap.push(j - i)
      }

      while (heap.size()) {
        while (heap.size() && heap.top() <= i) {
          heap.pop()
        }
        if (heap.size() === 0) break

        let cur = heap.pop(),
          top = heap.top()
        if (!top) {
          i += Math.ceil(cur / 2)
          break
        }

        i += cur - top
        cur -= (cur - top) * 2
        if (cur > i) {
          heap.push(cur)
        }
      }
      res.push(i)
    }
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')
class Heap {
  constructor(_comparator, arr) {
    this._comparator = _comparator
    this._heap = []
    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])
  }
  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
  }
}

void (function main() {
  const _ = Number(inputs[0])
  let res = []
  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])
    const data = inputs[__ * 2 + 2].trim().split(' ').map(Number)
    const h = new Array(n).fill(-1),
      e = [],
      ne = [],
      d = new Array(n).fill(0)
    let idx = 0
    const add = (i, j) => {
      ;(e[idx] = j), (ne[idx] = h[i]), (h[i] = idx++), d[j]++
    }
    for (let i = 2; i <= n; i++) {
      add(data[i - 2] - 1, i - 1)
    }
    const queue = [0]
    const g = new Array(n).fill(0).map(() => [])
    for (let i of queue) {
      for (let k = h[i]; k !== -1; k = ne[k]) {
        const j = e[k]
        if (--d[j] === 0) {
          g[i].push(j)
          queue.push(j)
        }
      }
    }
    let hasSon = 0,
      maxSon = 0,
      len = []
    for (let children of g) {
      if (children.length) hasSon++
      maxSon = Math.max(maxSon, children.length)
      len.push(children.length)
    }

    if (maxSon <= hasSon + 1) {
      res.push(hasSon + 1)
    } else {
      let i = 1 + hasSon
      const heap = new Heap((a, b) => b < a)

      for (let j of len) {
        if (j <= i) continue
        heap.push(j - i)
      }

      while (heap.size()) {
        while (heap.size() && heap.top() <= i) {
          heap.pop()
        }
        if (heap.size() === 0) break

        let cur = heap.pop(),
          top = heap.top()
        if (!top) {
          i += Math.ceil(cur / 2)
          break
        }

        i += cur - top + 1
        cur -= cur - top + 1
        if (cur > i) {
          heap.push(cur)
        }
      }
      res.push(i)
    }
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')
class Heap {
  constructor(_comparator, arr) {
    this._comparator = _comparator
    this._heap = []
    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])
  }
  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
  }
}

void (function main() {
  const _ = Number(inputs[0])
  let res = []
  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])
    const data = inputs[__ * 2 + 2].trim().split(' ').map(Number)
    const h = new Array(n).fill(-1),
      e = [],
      ne = [],
      d = new Array(n).fill(0)
    let idx = 0
    const add = (i, j) => {
      ;(e[idx] = j), (ne[idx] = h[i]), (h[i] = idx++), d[j]++
    }
    for (let i = 2; i <= n; i++) {
      add(data[i - 2] - 1, i - 1)
    }
    const queue = [0]
    const g = new Array(n).fill(0).map(() => [])
    for (let i of queue) {
      for (let k = h[i]; k !== -1; k = ne[k]) {
        const j = e[k]
        if (--d[j] === 0) {
          g[i].push(j)
          queue.push(j)
        }
      }
    }
    let hasSon = 0,
      maxSon = 0,
      len = []
    for (let children of g) {
      if (children.length) hasSon++
      maxSon = Math.max(maxSon, children.length)
      len.push(children.length)
    }

    if (maxSon <= hasSon + 1) {
      res.push(hasSon + 1)
    } else {
      let i = 1 + hasSon
      const heap = new Heap((a, b) => b < a)

      for (let j of len) {
        if (j <= i) continue
        heap.push(j - i)
      }

      console.log('-----------')
      while (heap.size()) {
        while (heap.size() && heap.top() <= i) {
          heap.pop()
        }
        if (heap.size() === 0) break

        let cur = heap.pop(),
          top = heap.top()
        if (!top) {
          i += Math.ceil(cur / 2)
          break
        }

        i += cur - top + 1
        cur -= cur - top + 1
        if (cur > i) {
          heap.push(cur)
        }
      }
      res.push(i)
    }
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []
  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])
    const data = inputs[__ * 2 + 2].trim().split(' ').map(Number)
    const h = new Array(n).fill(-1),
      e = [],
      ne = [],
      d = new Array(n).fill(0)
    let idx = 0
    const add = (i, j) => {
      ;(e[idx] = j), (ne[idx] = h[i]), (h[i] = idx++), d[j]++
    }
    for (let i = 2; i <= n; i++) {
      add(data[i - 2] - 1, i - 1)
    }
    const queue = [0]
    const g = new Array(n).fill(0).map(() => [])
    for (let i of queue) {
      for (let k = h[i]; k !== -1; k = ne[k]) {
        const j = e[k]
        if (--d[j] === 0) {
          g[i].push(j)
          queue.push(j)
        }
      }
    }
    let hasSon = 0,
      maxSon = 0,
      len = []
    for (let children of g) {
      if (children.length) hasSon++
      maxSon = Math.max(maxSon, children.length)
      len.push(children.length)
    }

    if (maxSon <= hasSon + 1) {
      res.push(hasSon + 1)
    } else {
      let i = 1 + hasSon
      len.sort((a, b) => a - b)
      while (len[len.length - 1] > i) {
        let last = len.pop()
        i += Math.ceil((last - i) / 2)
      }
      res.push(i)
    }
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []
  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])
    const data = inputs[__ * 2 + 2].trim().split(' ').map(Number)
    const h = new Array(n).fill(-1),
      e = [],
      ne = [],
      d = new Array(n).fill(0)
    let idx = 0
    const add = (i, j) => {
      ;(e[idx] = j), (ne[idx] = h[i]), (h[i] = idx++), d[j]++
    }
    for (let i = 2; i <= n; i++) {
      add(data[i - 2] - 1, i - 1)
    }
    const queue = [0]
    const g = new Array(n).fill(0).map(() => [])
    for (let i of queue) {
      for (let k = h[i]; k !== -1; k = ne[k]) {
        const j = e[k]
        if (--d[j] === 0) {
          g[i].push(j)
          queue.push(j)
        }
      }
    }
    let hasSon = 0,
      maxSon = 0
    for (let children of g) {
      if (children.length) hasSon++
      maxSon = Math.max(maxSon, children.length)
    }

    if (maxSon > hasSon + 1) {
      maxSon -= hasSon + 1
      let ans = 1 + hasSon + Math.ceil(maxSon / 2)
      res.push(ans)
    } else res.push(hasSon + 1)
  }

  console.log(res.join('\n'))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const _ = Number(inputs[0])
  let res = []
  for (let __ = 0; __ < _; __++) {
    let n = Number(inputs[__ * 2 + 1])
    const data = inputs[__ * 2 + 2].trim().split(' ').map(Number)
    const h = new Array(n).fill(-1),
      e = [],
      ne = [],
      d = new Array(n).fill(0)
    let idx = 0
    const add = (i, j) => {
      ;(e[idx] = j), (ne[idx] = h[i]), (h[i] = idx++), d[j]++
    }
    for (let i = 2; i <= n; i++) {
      add(data[i - 2] - 1, i - 1)
    }
    const queue = [0]
    const g = new Array(n).fill(0).map(() => [])
    for (let i of queue) {
      for (let k = h[i]; k !== -1; k = ne[k]) {
        const j = e[k]
        if (--d[j] === 0) {
          g[i].push(j)
          queue.push(j)
        }
      }
    }
    let hasSon = 0,
      maxSon = 0
    for (let children of g) {
      if (children.length) hasSon++
      maxSon = Math.max(maxSon, children.length)
    }

    maxSon -= hasSon + 1
    let ans = 1 + hasSon + Math.ceil(Math.max(maxSon, 0) / 2)
    res.push(ans)
  }

  console.log(res.join('\n'))
})()

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

module.exports = E;

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

const MOD = 1e9+7;

const calc = (n, P)=>{
  P = [-1].concat(P.map(p=>p-1))
  let tree = {};
  let nodes = [0];
  for (let i=1; i<n; i++){
    nodes.push(i);
    let p = P[i];
    if (!tree[p])
     tree[p] = [];
    tree[p].push(i);
  }
  // sort by siblings count
  nodes.sort((a, b)=>{
    let pa = P[a];
    let pb = P[b];
    let a_cnt = pa==-1 ? 1 : tree[pa].length;
    let b_cnt = pb==-1 ? 1 : tree[pb].length;
    return b_cnt-a_cnt;
  });
  let seconds = 0;
  let has_infected_child = new Map();
  // inject all siblings
  for (let h of nodes){
    let p = P[h];
    if (has_infected_child.has(p))
      continue;
    has_infected_child.set(p, seconds);
    seconds++;
  }
  l(has_infected_child, seconds)
  let remains = [];
  let rem_sum = 0;
  for (let [k, v] of has_infected_child){
    let added = seconds-v;
    let remain = (tree[k]||[]).length-added;
    if (remain>0){
      remains.push(remain)
      rem_sum += remain;
    }
  }
  l(remains)
  while (rem_sum>0){
      seconds++;
    let maxi = -1;
    let new_rem = [], new_sum = 0;
    for (let i=0; i<remains; i++){
      if (maxi==-1 || remains[maxi]<remains[i])
        maxi = i;
    }
    for (let i=0; i<remains; i++){
      let to_add = remains[i]-1;
      if (maxi==i)
        to_add--;
      if (to_add>0){
        new_rem.push(to_add)
        new_sum += to_add;
      }
    }
    rem_sum = new_sum;
    remains = new_rem;
  }
  return seconds;
};

function main() {
    let T = +readline()
    while (T--){
      let [n] = ra();
      let p = ra();
      l('ans')
      print(calc(n, p));
    }
}

E.calc = calc;

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

module.exports = E;

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

const MOD = 1e9+7;

const calc = (n, P)=>{
  P = [-1].concat(P.map(p=>p-1))
  let tree = {};
  let nodes = [0];
  for (let i=1; i<n; i++){
    nodes.push(i);
    let p = P[i];
    if (!tree[p])
     tree[p] = [];
    tree[p].push(i);
  }
  // sort by siblings count
  nodes.sort((a, b)=>{
    let pa = P[a];
    let pb = P[b];
    let a_cnt = pa==-1 ? 1 : tree[pa].length;
    let b_cnt = pb==-1 ? 1 : tree[pb].length;
    return b_cnt-a_cnt;
  });
  let seconds = 0;
  let has_infected_child = new Map();
  // inject all siblings
  for (let h of nodes){
    let p = P[h];
    if (has_infected_child.has(p))
      continue;
    has_infected_child.set(p, seconds);
    seconds++;
  }
  l(has_infected_child, seconds)
  let remains = [];
  for (let [k, v] of has_infected_child){
    let added = seconds-v;
    let remain = (tree[k]||[]).length-added;
    remains.push([k, remain])
  }
  remains.sort((a, b)=>b[1]-a[1]);
  for (let remain of remains){
    let [k] = remain;
    let v = has_infected_child.get(k)
    let added = seconds-v;
    let rem = (tree[k]||[]).length-added;
    if (rem>0){
      seconds += Math.ceil(rem/2);
    }
  }
  return seconds;
};

function main() {
    let T = +readline()
    while (T--){
      let [n] = ra();
      let p = ra();
      l('ans')
      print(calc(n, p));
    }
}

E.calc = calc;

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

module.exports = E;

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

const MOD = 1e9+7;

const calc = (n, P)=>{
  P = [-1].concat(P.map(p=>p-1))
  let tree = {};
  let nodes = [0];
  for (let i=1; i<n; i++){
    nodes.push(i);
    let p = P[i];
    if (!tree[p])
     tree[p] = [];
    tree[p].push(i);
  }
  // sort by siblings count
  nodes.sort((a, b)=>{
    let pa = P[a];
    let pb = P[b];
    let a_cnt = pa==-1 ? 1 : tree[pa].length;
    let b_cnt = pb==-1 ? 1 : tree[pb].length;
    return b_cnt-a_cnt;
  });
  let seconds = 0;
  let has_infected_child = new Map();
  // inject all siblings
  for (let h of nodes){
    let p = P[h];
    if (has_infected_child.has(p))
      continue;
    has_infected_child.set(p, seconds);
    seconds++;
  }
  l(has_infected_child, seconds)
  let remain = [];
  for (let [k, v] of has_infected_child){
    let added = seconds-v;
    let remain = (tree[k]||[]).length-added;
    if (remain>0){
      seconds += Math.ceil(remain/2);
    }
  }
  l('remain', remain)
  return seconds;
};

function main() {
    let T = +readline()
    while (T--){
      let [n] = ra();
      let p = ra();
      l('ans')
      print(calc(n, p));
    }
}

E.calc = calc;

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

module.exports = E;

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

const MOD = 1e9+7;

class SegmTree {

}

const calc = (n, P)=>{
  P = [-1].concat(P.map(p=>p-1))
  let tree = {};
  let nodes = [0];
  for (let i=1; i<n; i++){
    nodes.push(i);
    let p = P[i];
    if (!tree[p])
     tree[p] = [];
    tree[p].push(i);
  }
  let ni = 0;
  // sort by siblings count
  nodes.sort((a, b)=>{
    let pa = P[a];
    let pb = P[b];
    let a_cnt = pa==-1 ? 1 : tree[pa].length;
    let b_cnt = pb==-1 ? 1 : tree[pb].length;
    return b_cnt-a_cnt;
  });
  let healthy = new Set();
  for (let node of nodes)
    healthy.add(node);
  let seconds = 0;
  let has_infected_child = new Map();
  // inject all siblings
  for (let h of healthy){
    let p = P[h];
    if (has_infected_child.has(p))
      continue;
    has_infected_child.set(p, seconds);
    seconds++;
  }
  let remain = [];
  for (let [k, v] of has_infected_child){
    let added = seconds-v;
    let remain = (tree[k]||[]).length-added;
    if (remain>0){
      seconds += Math.ceil(remain/2);
    }
  }
  l('remain', remain)
  return seconds;
};

function main() {
    let T = +readline()
    while (T--){
      let [n] = ra();
      let p = ra();
      l('ans')
      print(calc(n, p));
    }
}

E.calc = calc;

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

module.exports = E;

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

const MOD = 1e9+7;

class SegmTree {

}

const calc = (n, P)=>{
  let tree = {};
  let child_skips = {0: 0};
  let nodes = [0];
  for (let i=1; i<n; i++){
    child_skips[i] = 0;
    nodes.push(i);
    let p = P[i-1]-1;
    if (!tree[p])
     tree[p] = [];
    tree[p].push(i);
  }
  let ni = 0;
  nodes.sort((a, b)=>{
    return (tree[b]||[]).length-(tree[a]||[]).length
  });
  for (let t in tree){
    tree[t].sort((a, b)=>{
      return (tree[b]||[]).length-(tree[a]||[]).length
    });
  }
  console.log('sorted', nodes)
  let seconds = 0;
  let infected = new Set();
  let has_infected_child = new Set();

  while (infected.size<n){
    seconds++
    l(`second ${seconds}`)
    // spreading
    for (let infp of has_infected_child){
      while (child_skips[infp]<(tree[infp]||[]).length && infected.has(tree[infp][child_skips[infp]]))
        child_skips[infp]++;
      if (child_skips[infp]<(tree[infp]||[]).length)
        infected.add(tree[infp][child_skips[infp]]);
    }
    // injection
    while (ni<n && has_infected_child.has(nodes[ni]))ni++;
    if (ni<n && tree[nodes[ni]]){
      let node = tree[nodes[ni]][0];
      child_skips[nodes[ni]]++;
      infected.add(node);
      has_infected_child.add(nodes[ni]);
      l('inject', node);
    } else {
      let inj = 0;
      for (let infp of has_infected_child){
        while (child_skips[infp]<(tree[infp]||[]).length && infected.has(tree[infp][child_skips[infp]]))
          child_skips[infp]++;
        if (child_skips[infp]<(tree[infp]||[]).length){
          infected.add(tree[infp][child_skips[infp]]);
          l('inject leaf', tree[infp][child_skips[infp]]);
          inj = 1;
          break;
        }
      }
      if (!inj) // add root
        infected.add(0);
    }
  }

    return seconds;
};

function main() {
    let T = +readline()
    while (T--){
      let [n] = ra();
      let p = ra();
      l('ans')
      print(calc(n, p));
    }
}

E.calc = calc;

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

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

var deb; const log = msg => (typeof deb != "undefined") && console.log("DEBUG", msg);
// PriorityQueue implementated using array based binary heap
// PriorityQueue methods
//  push(...values)
// 	peek()
//  pop()
//  size()
//  replace(value)

class PriorityQueue {

	constructor(comparator = (a, b) => a > b) {
		this.top = 0;
		this._heap = [];
		this._comparator = comparator;
	}
	size() {
		return this._heap.length;
	}
	isEmpty() {
		return this.size() == 0;
	}
	peek() {
		return this._heap[this.top];
	}
	push(...values) {
		values.forEach(value => {
				this._heap.push(value);
				this._siftUp();
				});
		return this.size();
	}
	pop() {
		const poppedValue = this.peek();
		const bottom = this.size() - 1;
		if (bottom > this.top) {
			this._swap(this.top, bottom);
		}
		this._heap.pop();
		this._siftDown();
		return poppedValue;
	}
	replace(value) {
		const replacedValue = this.peek();
		this._heap[this.top] = value;
		this._siftDown();
		return replacedValue;
	}
	_greater(i, j) {
		return this._comparator(this._heap[i], this._heap[j]);
	}
	_swap(i, j) {
		[this._heap[i], this._heap[j]] = [this._heap[j], this._heap[i]];
	}
	_siftUp() {
		const parent = i => ((i + 1) >>> 1) - 1;
		let node = this.size() - 1;
		while (node > this.top && this._greater(node, parent(node))) {
			this._swap(node, parent(node));
			node = parent(node);
		}
	}
	_siftDown() {
		const left = i => (i << 1) + 1;
		const right = i => (i + 1) << 1;
		let node = this.top;
		while (
				(left(node) < this.size() && this._greater(left(node), node)) ||
				(right(node) < this.size() && this._greater(right(node), node))
			  ) {
			let maxChild = (right(node) < this.size() && this._greater(right(node), left(node))) ? right(node) : left(node);
			this._swap(node, maxChild);
			node = maxChild;
		}
	}
}

// demo
// mnq.push(10);
// mnq.peek();
// mnq.size();

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

		const chlds = Array(n + 1).fill(0);
		chlds[0] = 1;
		let cnt = 1;
		for (let i = 0; i < n - 1; i++) {
			// i -> a[i]
			// i + 2 -> a[i]
			chlds[a[i]]++;
			cnt += chlds[a[i]] == 1;
		}

		let ans = cnt;
		const mxq = new PriorityQueue((a, b) => a > b);
		for (let i = 0; i < n + 1; i++) {
			chlds[i] -= cnt;
			if (chlds[i] > 0) mxq.push(chlds[i]);
		}

		while (mxq.size()) {
			const other = [];
			let sub = 2;
			while (mxq.size()) {
				let poped = mxq.pop();
				poped -= sub;
				if (poped > 0) other.push(poped);
				sub = 1;
			}
			for (let i = 0; i < other.length; i++) {
				mxq.push(other[i]);
			}
			ans++;
		}

		console.log(ans);
	}
}

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

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

var deb; const log = msg => (typeof deb != "undefined") && console.log("DEBUG", msg);
// PriorityQueue implementated using array based binary heap
// PriorityQueue methods
//  push(...values)
// 	peek()
//  pop()
//  size()
//  replace(value)

class PriorityQueue {

	constructor(comparator = (a, b) => a > b) {
		this.top = 0;
		this._heap = [];
		this._comparator = comparator;
	}
	size() {
		return this._heap.length;
	}
	isEmpty() {
		return this.size() == 0;
	}
	peek() {
		return this._heap[this.top];
	}
	push(...values) {
		values.forEach(value => {
				this._heap.push(value);
				this._siftUp();
				});
		return this.size();
	}
	pop() {
		const poppedValue = this.peek();
		const bottom = this.size() - 1;
		if (bottom > this.top) {
			this._swap(this.top, bottom);
		}
		this._heap.pop();
		this._siftDown();
		return poppedValue;
	}
	replace(value) {
		const replacedValue = this.peek();
		this._heap[this.top] = value;
		this._siftDown();
		return replacedValue;
	}
	_greater(i, j) {
		return this._comparator(this._heap[i], this._heap[j]);
	}
	_swap(i, j) {
		[this._heap[i], this._heap[j]] = [this._heap[j], this._heap[i]];
	}
	_siftUp() {
		const parent = i => ((i + 1) >>> 1) - 1;
		let node = this.size() - 1;
		while (node > this.top && this._greater(node, parent(node))) {
			this._swap(node, parent(node));
			node = parent(node);
		}
	}
	_siftDown() {
		const left = i => (i << 1) + 1;
		const right = i => (i + 1) << 1;
		let node = this.top;
		while (
				(left(node) < this.size() && this._greater(left(node), node)) ||
				(right(node) < this.size() && this._greater(right(node), node))
			  ) {
			let maxChild = (right(node) < this.size() && this._greater(right(node), left(node))) ? right(node) : left(node);
			this._swap(node, maxChild);
			node = maxChild;
		}
	}
}

// demo
// mnq.push(10);
// mnq.peek();
// mnq.size();

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

		const chlds = Array(n + 1).fill(0);
		chlds[0] = 1;
		let cnt = 1;
		for (let i = 0; i < n - 1; i++) {
			// i -> a[i]
			// i + 2 -> a[i]
			chlds[a[i]]++;
			cnt += chlds[a[i]] == 1;
		}

		let ans = cnt;
		const mxq = new PriorityQueue((a, b) => a > b);
		for (let i = 0; i < n + 1; i++) {
			chlds[i] -= cnt;
			if (chlds[i] > 0) mxq.push(chlds[i]);
		}

		while (mxq.size()) {
			const other = [];
			while (mxq.size()) {
				let poped = mxq.pop();
				poped = Math.max(0, poped - 2);
				if (poped) other.push(poped);
			}
			for (let i = 0; i < other.length; i++) {
				mxq.push(other[i]);
			}
			ans++;
		}

		console.log(ans);
	}
}

function stairCaseMin(i) {
    for (var j = 0; j < i.length; j++) {
        i[j] -= j + 1;
    }
    return i;
}
var total = +readline();
while (total--) {
    var children = new Array(+readline() + 1).fill(0);
    readline().split(" ").map(Number).forEach(function (x) {
        children[x]++;
    });
    // roots
    children[0] = 1;
    children = children.filter(function (x) {
        return x != 0;
    }).sort(function (a, b) {
        return a - b;
    });
    var ans = 0;
    // stairCase boost
    ans += children.length;
    children = stairCaseMin(children).filter(function (x) {
        return x > 0;
    }).sort(function (a, b) {
        return a - b;
    });
    // all clusters included at least one infected
    while (children.length != 0) {
        ans += 1;
        children = children.map(function (x) {
            return x - 1;
        }).sort(function (a, b) {
            return a - b;
        });
        children[0]--;
        children = children.filter(function (x) {
            return x > 0;
        });
    }
    print(ans);
}