#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include<math.h>
#include<functional>
#include<stack>
#include <algorithm>
#include<unordered_map>
#include <map>
#include<numeric>
#define inf 0x7fffffff
using namespace std;
const int N = 1e6 + 6;
const int mod = 1e9 + 7;
inline int f(int x, int final) {
if(x <= final) {
return final - x;
} else {
int res = x - final,cnt=0;
while(x) {
x /= 2;
cnt++;
res = min(res, abs(x - final) + cnt);
}
return res;
}
}
void solve() {
int n, m; cin >> n >> m;
vector<int>a(n + 1);
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
int ans = 1e9;
for(int i = 1; i <= n; i++) {
int mx = a[i];
while(mx > 1) {
priority_queue<int>q1;
int res1 = 0;
for(int j = 1; j <= n; j++) {
q1.push(f(a[j], mx));
}
for(int j = 1; j <= m; j++) {
q1.pop();
}
while(q1.size()) {
res1 += q1.top();q1.pop();
}
ans = min(ans, res1);
mx /= 2;
}
}
cout << ans << '\n';
}
signed main()
{
cin.tie(0)->sync_with_stdio(false);
int q = 1;
cin >> q;
for (int i = 1; i <= q; i++)
{
solve();
}
}
// function<void(int, int)> dfs = [&] (int u, int parent) {
// // cerr << "u = " << u << " parent = " << parent << "\n";
// stf[u][0] = parent;
// vis[u] = 1;
// stmax[u][0] = max(h[u], h[parent]);
// for(int v : adj[u]) {
// if(v == parent) continue;
// dep[v] = dep[u] + 1;
// dfs(v, u);
// }
// };
// int find(int x) {
// if(fa[x] == x) return x;
// return fa[x] = find(fa[x]);
// }