#include<iostream>
#include<vector>
#include<unordered_map>
#include<map>
#include<set>
#include<queue>
#include<list>
using namespace std;
void solve(vector<int>tree)
{
int n = tree.size() - 1;
long long ans = 0;
unordered_map<int, int>mp;
for(int i=2; i<=n ; i++)
{
mp[tree[i]]++;
}
priority_queue<int>hp;
for(auto it=mp.begin(); it!=mp.end(); it++)
{
hp.push(it->second);
}
hp.push(1);
multiset<int>right_list;
while(!hp.empty() || !right_list.empty())
{
ans ++;
//spread
if(!right_list.empty())
{
auto it = right_list.begin();
while(it != right_list.end())
{
if((*it) != 0)
{
int value = (*it);
it = right_list.erase(it);
value = value - 1;
if(value > 0) right_list.insert(value);
}
else
{
it = right_list.erase(it);
}
}
}
//infect and add to right_list
if(!hp.empty())
{
int topnode = hp.top();
hp.pop();
//infected
topnode--;
if(topnode > 0) right_list.insert(topnode);
//inserted in right list
}
//************ */
//if heap is empty then also I can infect one from above rightlist using my infect operation greedily
//to decrease the total no.of operation required
else if(!right_list.empty())
{
auto it = prev(right_list.end());
if((*it) != 0)
{
int value = (*it);
right_list.erase(it);
value = value - 1;
if(value > 0) right_list.insert(value);
}
else right_list.erase(it);
}
}
cout << ans << endl;
return ;
}
int main()
{
int number_of_test_cases;
int no_of_branches;
cin >> number_of_test_cases;
while(number_of_test_cases--)
{
cin >> no_of_branches;
vector<int>tree(no_of_branches+1, 0);
int index = 2;
while(no_of_branches > 1)
{
cin >> tree[index++];
no_of_branches--;
}
solve(tree);
}
return 0;
}