Problem: 2057B - Gorilla and the Exam
Language: c++17 

Solution 1 (runtime: 202 ms):
/*
#include<iostream>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
long long a[20000];
char b[1005][1005];
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		map<int, int> mp;
		int n;
		cin >> n;
		for (int i = 1; i <= n; i++)
		{
			a[i] = 1;
		}
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= n; j++)
			{
				cin >> b[i][j];
				if (j > i)
				{
					if (b[i][j] == '1')
					{
						a[j]++;
					}
					else a[i]++;
				}
			}
		}
		for (int i = 1; i <= n; i++)
		{
			mp[a[i]] = i;
		}
		for (int i = 1; i <= n; i++)
		{
			cout << mp[i] << " ";
		}
		cout << endl;
	}
}
*/
/*
#include<iostream>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	int n;
	cin >> n;
	vector<int> f(n + 1, 1);
	vector<int> a(n + 1);
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		for (int j = 1; j < i; j++)
		{
			if (a[j] < a[i])
			{
				f[i] = max(f[j] + 1, f[i]);
			}
		}
	}
	cout << *max_element(f.begin(), f.end()) << endl;
}
*/
/*
#include<iostream>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
void mmin(int &x, int y)
{
	if (x > y) x = y;
}
int main()
{
	string s, t;
	cin >> s >> t;
	int n = s.length();
	int m = t.length();
	s = ' ' + s;
	t = ' ' + t;
	vector<vector<int>>f(n + 2, vector<int>(m + 2, 1000000));
	f[0][0] = 0;
	for (int i = 0; i <= n; i++)
	{

		for (int j = 0; j <= m; j++)
		{
			if (s[i + 1] == t[j + 1])
			{
				mmin(f[i + 1][j + 1], f[i][j]);
			}
			mmin(f[i + 1][j], f[i][j] + 1);
			mmin(f[i][j+1], f[i][j] + 1);
			mmin(f[i + 1][j+1], f[i][j] + 1);
		}
	}
	cout << f[n][m];
}
*/
/*
#include<iostream>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int l[105], r[105], s[105], a[105];
int main()
{
	int n;
	cin >> n;
	for (int i = 1;  i <= n; i++)
	{
		cin >> a[i];
		l[i] = 1;
		r[i] = 1;
	}
	for (int i = n - 1; i >= 1; i--)
	{
		for (int j = i + 1; j <= n; j++)
		{
			if (a[i] > a[j] && r[i] <= r[j] + 1)
			{
				r[i] = r[j] + 1;
			}
		}
	}
	for (int i = 2; i <= n; i++)
	{
		for (int j = i - 1; j >= 1; j--)
		{
			if (a[i] > a[j] && l[i] <= l[j] + 1)
			{
				l[i] = l[j] + 1;
			}
		}
	}
	for (int i = 1; i <= n; i++)
	{
		s[i] = l[i] + r[i] - 1;
	}
	sort(s + 1, s + 1 + n);
	cout << n - s[n];
}
*/
/*
#include<iostream>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int a[105], b[105], c[105];
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		int lb = (n + 1) / 2;
		int lc = n / 2;
		int bm = 0, cm = 0,ans;
		for (int i = 1; i <= n; i++)
		{
			cin >> a[i];
			if (i % 2 != 0)
			{
				bm = max(a[i], bm);
			}
			else
			{
				cm = max(a[i], cm);
			}
		}
		ans = max((bm + lb), (cm + lc));
		cout << ans << endl;
	}
}
*/
/*
#include<iostream>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	int T, m;
	cin >> T >> m;
	vector<vector<int>>f(m + 1, vector<int>(T + 1, 0));
	for (int i = 1; i <= m; i++)
	{
		int t, v;
		cin >> t >> v;
		for (int j = 0; j <= T; j++)
		{
			f[i][j] = f[i - 1][j];
			if (j - t >= 0)
			{
				f[i][j] = max(f[i][j], f[i - 1][j - t] + v);
			}
		}
	}
	cout << f[m][T] << endl;
}
*/
/*
#include<iostream>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	long long T;
	int m;
	cin >> T >> m;
	vector<vector<int>>f(m + 1, vector<int>(T + 1, 0));
	for (int i = 1; i <= m; i++)
	{
		int t, v;
		cin >> t >> v;
		for (long long j = 0; j <= T; j++)
		{
			f[i][j] = f[i - 1][j];
			if (j >= t)
			{
				f[i][j] = max(f[i][j], f[i][j - t] + v);
			}
		}
	}
	cout << f[m][T] << endl;
}
*/
/*
#include<iostream>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	int T;
	int m;
	cin >> T >> m;
	vector<long long>f(T + 1, 0);
	for (int i = 1; i <= m; i++)
	{
		int v, p;
		cin >> v >> p;
		for (long long j = T; j >=1; j--)
		{
			if (j >= v)
			{
				f[j] = max(f[j], f[j - v] + v * p);
			}
		}
	}
	cout << f[T] << endl;
}
*/
/*
#include<iostream>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int f[4005];
int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		int n, k;
		f[0] = 0;
		cin >> n >> k;
		for (int i = 1; i <= k;i++)
		{
			f[i] = 400000;
		}
		for (int i = 1; i <= n; i++)
		{
			int a, b, c, d;
			cin >> a >> b >> c >> d;
			for (int j = k; j >=0; j--)
			{
				f[j] = min(f[j], f[j - 1] + a);
				if (j >= 2) f[j] = min(f[j], f[j - 2] + b);
				if (j >= 3) f[j] = min(f[j], f[j - 3] + c);
				if (j >= 4) f[j] = min(f[j], f[j - 4] + d);
			}
		}
		cout << f[k] << endl;
	}
}
*/
/*
#include<iostream>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int w[1005], v[1005],b[105]={0}, g[205][1005], dp[1005];
int main()
{
	int m, n,x,t=0;
	cin >> m >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> w[i] >> v[i] >> x;
		t = max(t, x);
		b[x]++;
		g[x][b[x]] = i;
	}
	for (int i = 1; i <= t; i++)
	{
		for (int j = m; j >= 0; j--)
		{
			for (int k = 1; k <= b[i]; k++)
			{
				if (j >= w[g[i][k]])
				{
					dp[j] = max(dp[j], dp[j - w[g[i][k]]] + v[g[i][k]]);
				}
			}
		}
	}
	cout << dp[m];
}
*/
/*
#include<iostream>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int a[2005][2005],b[2005];
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, m;
		bool flag = 1;
		cin >> n >> m;
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				cin >> a[i][j];
			}
		}
		for (int i = 1; i <= n; i++)
		{
			sort(a[i] + 1, a[i] + 1 + m);
			b[a[i][1]] = i;
			if (a[i][1] >= n )
			{
				flag = 0;
				break;
			}
		}
		if (flag)
		{
			for (int i = 1; i <= n; i++)
			{
				for (int j = 2; j <= m; j++)
				{
					if (a[i][j] - a[i][j - 1] != n)
					{
						flag = 0;
						break;
					}
				}
			}
		}
		if (flag)
		{
			for (int i = 0; i < n; i++)
			{
				cout << b[i] << " ";
			}
			cout << endl;
		}
		else
		{
			cout << -1 << endl;
		}
	}
}
*/
/*
#include<iostream>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int a[200005];
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, k, ans = 0;
		cin >> n >> k;
		for (int i = 1; i <= n; i++)
		{
			cin >> a[i];
		}
		sort(a + 1, a + 1 + n);
		int l = 1, r = n;
		while (l < r)
		{
			if (a[l] + a[r] == k)
			{
				ans++;
				l++;
				r--;
			}
			if (a[l] + a[r] < k)
			{
				l++;
			}
			if (a[l] + a[r] > k)
			{
				r--;
			}
		}
		cout << ans << endl;
	}
}
*/
/*
#include<iostream>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
long long a[200005], b[200005];
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		bool flag = 1;
		cin >> n;
		for (int i = 1; i <= n; i++)
		{
			cin >> a[i];
			b[i] = a[i] - b[i - 1];
			if (b[i] < 0) flag = 0;
		}
		if (flag) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}
*/
/*
#include<iostream>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		string s;
		cin >> s;
		int len = s.length();
		int l = 0, r = 1, ans = 0;
		vector<bool>kill (len*2, 0);
		while (r > l && (0 <= l && l <= len - 1) && (0 <= r && r <= len - 1))
		{

			if ((s[l] == '1' && (s[r] == '1' || s[r] == '2')) || (s[l] == '0' && (s[r] == '0' || s[r] == '2')))
			{
				ans+=2;
				kill[r] = 1;
				kill[l] = 1;
				if (l == 0)
				{
					l += 2;
					r += 2;
				}
				else
				{
					l -= 1;
					r += 1;
					if (!(r > l && (0 <= l && l <= len - 1) && (0 <= r && r <= len - 1))) break;
				}
			}
			else
			{
				l++;
				while (kill[l] && l < len) l++;
				r++;
				while (kill[r] && r < len) r++;
			}
		}
		cout << len - ans << endl;
	}
}
*/
#include<iostream>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
long long a[100005], b[100005];
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, k, cnt = 1, ans = 0;;
		cin >> n >> k;
		for (int i = 1; i <= n; i++)
		{
			cin >> a[i];
			b[i] = 0;
		}
		sort(a + 1, a + 1 + n);
		for (int i = 1; i <= n; i++)
		{
			if (i > 1 && a[i] != a[i - 1])
			{
				cnt++;
			}
			b[cnt]++;
		}
		sort(b + 1, b + 1 + cnt);
		for (int i = 1; i <= cnt; i++)
		{
			if (b[i] <= k)
			{
				k -= b[i];
				ans++;
			}
			else break;
		}
		if (ans == 0)
		{
			cout << cnt << endl;
		}
		else cout << max(cnt - ans , 1) << endl;;
		//cout << ans << " " << cnt << endl;
	}
}

Solution 2 (runtime: 187 ms):
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod=1e9+7;
void solve(){
    int n,k;cin>>n>>k;
    vector<int>a(n);
    vector<int>cnt={1};
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    sort(a.begin(),a.end());
    for(int i=1;i<n;i++){
        if(a[i]==a[i-1]){
            cnt.back()++;
        }
        else{
            cnt.emplace_back(1);
        }
    }
    sort(cnt.begin(),cnt.end());
    for(int i=0;i<cnt.size()-1;i++){
        if(cnt[i]>k){
            cout<<cnt.size()-i<<'\n';
            return;
        }
        k-=cnt[i];
    }
    cout<<1<<'\n';
}
int main(){
    int t;cin>>t;
    while(t--){
        solve();
    }
    return 0;
}