#include <bits/stdc++.h>
using namespace std;
vector<long long> a;
map<pair<long long, int>, long long> dp;
int k;
long long f(long long mask, int n) { // 2^K * N
if (dp.count({mask, n})) return dp[ {mask, n}];
else if (n == 0) return 0;
long long &ans = dp[ {mask, n}];
long long msk = mask, bit, pw = 1, ck = 1;
while (msk) { // O(K)
bit = msk % 10;
msk /= 10;
if (bit > 0) // put nth element on ck position
ans = max(ans, (a[n - 1] & ck) + f(mask - pw, n - 1));
ck++;
pw *= 10;
}
return ans;
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n >> k;
a.resize(n);
for (int i = 0; i < n; i++)
cin >> a[i];
long long mask = 2;
for (int i = 2; i <= k; i++) {
mask = mask * 10 + 2;
}
cout << f(mask, n) << '\n';
dp.clear();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnZlY3Rvcjxsb25nIGxvbmc+IGE7Cm1hcDxwYWlyPGxvbmcgbG9uZywgaW50PiwgbG9uZyBsb25nPiBkcDsKaW50IGs7CmxvbmcgbG9uZyBmKGxvbmcgbG9uZyBtYXNrLCBpbnQgbikgeyAvLyAyXksgICogTgogICAgaWYgKGRwLmNvdW50KHttYXNrLCBufSkpIHJldHVybiBkcFsge21hc2ssIG59XTsKICAgIGVsc2UgaWYgKG4gPT0gMCkgcmV0dXJuIDA7CiAgICBsb25nIGxvbmcgJmFucyA9IGRwWyB7bWFzaywgbn1dOwogICAgbG9uZyBsb25nIG1zayA9IG1hc2ssIGJpdCwgcHcgPSAxLCBjayA9IDE7CiAgICB3aGlsZSAobXNrKSB7IC8vIE8oSykKICAgICAgICBiaXQgPSBtc2sgJSAxMDsKICAgICAgICBtc2sgLz0gMTA7CiAgICAgICAgaWYgKGJpdCA+IDApIC8vIHB1dCBudGggZWxlbWVudCBvbiBjayBwb3NpdGlvbgogICAgICAgICAgICBhbnMgPSBtYXgoYW5zLCAoYVtuIC0gMV0gJiBjaykgKyBmKG1hc2sgLSBwdywgbiAtIDEpKTsKICAgICAgICBjaysrOwogICAgICAgIHB3ICo9IDEwOwogICAgfQogICAgcmV0dXJuIGFuczsKfQppbnQgbWFpbigpIHsKICAgIGludCB0OwogICAgY2luID4+IHQ7CiAgICB3aGlsZSAodC0tKSB7CiAgICAgICAgaW50IG47CiAgICAgICAgY2luID4+IG4gPj4gazsKICAgICAgICBhLnJlc2l6ZShuKTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykKICAgICAgICAgICAgY2luID4+IGFbaV07CiAgICAgICAgbG9uZyBsb25nIG1hc2sgPSAyOwogICAgICAgIGZvciAoaW50IGkgPSAyOyBpIDw9IGs7IGkrKykgIHsKICAgICAgICAgICAgbWFzayA9IG1hc2sgKiAxMCArIDI7CiAgICAgICAgfQogICAgICAgIGNvdXQgPDwgZihtYXNrLCBuKSA8PCAnXG4nOwogICAgICAgIGRwLmNsZWFyKCk7CiAgICB9CiAgICByZXR1cm4gMDsKfQ==