constint MAXN = 5e2 + 10; const ll INF = 0x3f3f3f3f3f3f3f3f; int n, m; int a[MAXN]; intfstep(int x, int cur){ if (x >= cur) { return x - cur; } int s = 0, lstc = cur; while (cur > x) { lstc = cur; cur >>= 1; s++; } assert(cur != 0); int res = min(s + (x - cur), (s - 1) + (lstc - x)); return res; } ll calc(int x){ vector<int> vec; for (int i = 1; i <= n; i++) { int step = fstep(x, a[i]); vec.push_back(step); } ll res = 0; sort(vec.begin(), vec.end()); for (int i = 0; i < n - m; i++) { res += vec[i]; } return res; } voidsolve(){ cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i]; } ll ans = INF; for (int i = 1; i <= n; i++) { int x = a[i]; while (x) { ans = min(ans, calc(x)); x >>= 1; } } printf("%lld\n", ans); } intmain(){ int T; cin >> T; while (T--) solve(); return0; }