#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 505;
int a[N];
int n, m;
set<int> kn;
vector<int> v;
void solve()
{
kn.clear();
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
kn.insert(a[i]);
int qf = a[i];
while (qf > 1)
{
kn.insert(qf / 2);
qf /= 2;
}
}
for (auto c : kn)
{
v.push_back(c);
}
int len = v.size();
int minn = 1e9;
for (int i = 0; i < len; i++)
{
vector<int> da;
int pos = v[i], scnt = 0;
for (int j = 1; j <= n; j++)
{
int x = a[j], pre = a[j], cnt = 0;
while (x >= pos)
pre = x, x /= 2, cnt++;
if (pre == x)
{
cnt += min(abs(pos - x), abs(pos - pre));
}
else
{
cnt += min(abs(pos - x), abs(pos - pre) - 1);
}
da.push_back(cnt);
}
sort(da.begin(), da.end());
int dan = 0;
for (int j = 0; j < n - m; j++)
{
dan += da[j];
}
minn = min(minn, dan);
}
cout << minn << endl;
v.clear();
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int _ = 1;
cin >> _;
while (_--)
{
solve();
}
}