#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
void solve(int cs) {
int n, k;
cin >> n >> k;
map<int,int> cnt;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
cnt[x] += 1;
}
if (k & 1) {
int mxcc = 0;
for (auto it : cnt) {
mxcc = max(mxcc, it.second);
}
cout << n - mxcc << "\n";
} else {
vector<int> st;
for (auto it : cnt) {
st.push_back(it.second);
}
sort(st.begin(), st.end());
int res = 0;
res += st.back();
st.pop_back();
if (st.size()) {
res += st.back();
}
cout << n - res << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc = 1;
cin >> tc;
for (int cs = 1; cs <= tc; cs++) {
solve(cs);
}
return 0;
}