#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

mt19937 rnd(time(0));



int main() {
//    std::ios_base::sync_with_stdio(0);
//    std::cin.tie(0); std::cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n, k;
        cin >> n >> k;
        vector <int> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        sort(a.begin(), a.end());
        reverse(a.begin(), a.end());
        ll ans = 0;
        for (int i = 0; i < n / 2; i++) {
            ans += a[i];
        }
        int par = n % 2;
        for (int i = n / 2; i < n; i++) {
            if (a[i] == 1) {
                break;
            }
            if (par == 0) {
                if (k < 2) {
                    break;
                }
                if (a[i] == 2) {
                    par = 1;
                    k--;
                } else {
                    ans += a[i] - 2;
                    k -= 2;
                }
            } else {
                if (k < 1) {
                    break;
                }
                ans += a[i] - 1;
                k--;
                par = 0;
            }
        }
        cout << ans << '\n';
    }
    return 0;
}
