#include <bits/stdc++.h>
using namespace std;
#define FOR(i, from, to) for (int i = (from); i <= int(to); ++i)
#define FORN(i, n) FOR(i, 0, (n) - 1)
#define endl '\n'
#define F first
#define S second
template<typename T>
bool rMin(T& v, const T& rval)
{
if (rval < v) {
v = rval;
return true;
}
return false;
}
template<typename T>
bool rMax(T& v, const T& rval)
{
if (v < rval) {
v = rval;
return true;
}
return false;
}
const int N = 2e5;
const int INF = 2 * (N + 1);
struct Dp {
int val;
int min_open, max_open;
} dp[N][2];
bool a[N];
int n;
Dp merge(Dp a, Dp b)
{
auto val = min(a.val, b.val);
int min_open = N + 1, max_open = 0;
auto relax = [&](Dp d){
if (val == d.val) {
rMin(min_open, d.min_open);
rMax(max_open, d.max_open);
}
};
relax(a);
relax(b);
return {val, min_open, max_open};
}
pair<Dp, Dp> states(int i, int v, const int C)
{
Dp open_new = dp[i - 1][!v];
open_new.val += C, ++open_new.min_open, ++open_new.max_open;
Dp cont_old = dp[i - 1][v];
return {open_new, cont_old};
}
Dp calc(const int C, const int st, const int fn)
{
dp[0][st] = {C, 1, 1};
dp[0][!st] = {INF, -1, -1};
dp[0][!a[0]].val += 2;
FOR(i, 1, n - 1)
FORN(v, 2) {
Dp open_new, cont_old;
tie(open_new, cont_old) = states(i, v, C);
dp[i][v] = merge(open_new, cont_old);
if (a[i] != v)
dp[i][v].val += 2;
}
return dp[n - 1][fn];
}
bool ans[N];
void mod(int& k, int st, int fn)
{
if (st == fn && k % 2 == 0)
--k;
if (st != fn && k % 2 == 1)
--k;
}
bool restore(const int C, int k, const int s, const int f)
{
mod(k, s, f);
auto fn = calc(C, s, f);
auto val = fn.val;
if (k > fn.max_open) {
assert(C == 0);
k = fn.max_open;
}
auto check = [&val, &k](Dp d){
return d.val == val && d.min_open <= k && k <= d.max_open;
};
int i = n - 1, v = f;
for (; i != 0; --i) {
ans[i] = v;
val = dp[i][v].val;
if (a[i] != v)
val -= 2;
Dp open_new, cont_old;
tie(open_new, cont_old) = states(i, v, C);
if (check(cont_old)) {
continue;
}
if (check(open_new)) {
--k;
v = !v;
continue;
}
assert(false);
}
ans[0] = v;
return true;
}
pair<int, int> res(int st, int fn, int k)
{
mod(k, st, fn);
if (k == 0)
return {INF, -1};
int l = -1, r = INF;
while (l + 1 != r) {
auto m = (l + r) / 2;
if (calc(m, st, fn).min_open > k)
l = m;
else
r = m;
}
return {calc(r, st, fn).val - k * r, r};
}
void solve()
{
int k;
cin >> n >> k;
FORN(i, n) {
char c;
cin >> c;
a[i] = c - '0';
}
pair<int, int> best = {INF, -1};
int bSt = -1, bFn = -1;
FORN(st, 2) {
FORN(fn, 2) {
if (rMin(best, res(st, fn, k)))
bSt = st, bFn = fn;
}
}
restore(best.S, k, bSt, bFn);
FORN(i, n)
cout << ans[i];
cout << endl;
}
int main()
{
#ifdef MY
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
#define FILEIO(TASK) do { freopen(TASK".in", "r", stdin); freopen(TASK".out", "w", stdout); } while (false)
FILEIO("penguins");
ios_base::sync_with_stdio(false);
cin.tie(0);
#endif // MY
int t;
cin >> t;
FOR(i, 1, t) {
solve();
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIEZPUihpLCBmcm9tLCB0bykgZm9yIChpbnQgaSA9IChmcm9tKTsgaSA8PSBpbnQodG8pOyArK2kpCiNkZWZpbmUgRk9STihpLCBuKSBGT1IoaSwgMCwgKG4pIC0gMSkKI2RlZmluZSBlbmRsICdcbicKI2RlZmluZSBGIGZpcnN0CiNkZWZpbmUgUyBzZWNvbmQKCnRlbXBsYXRlPHR5cGVuYW1lIFQ+CmJvb2wgck1pbihUJiB2LCBjb25zdCBUJiBydmFsKQp7CiAgICBpZiAocnZhbCA8IHYpIHsKICAgICAgICB2ID0gcnZhbDsKICAgICAgICByZXR1cm4gdHJ1ZTsKICAgIH0KICAgIHJldHVybiBmYWxzZTsKfQoKdGVtcGxhdGU8dHlwZW5hbWUgVD4KYm9vbCByTWF4KFQmIHYsIGNvbnN0IFQmIHJ2YWwpCnsKICAgIGlmICh2IDwgcnZhbCkgewogICAgICAgIHYgPSBydmFsOwogICAgICAgIHJldHVybiB0cnVlOwogICAgfQogICAgcmV0dXJuIGZhbHNlOwp9Cgpjb25zdCBpbnQgTiA9IDJlNTsKY29uc3QgaW50IElORiA9IDIgKiAoTiArIDEpOwpzdHJ1Y3QgRHAgewogICAgaW50IHZhbDsKICAgIGludCBtaW5fb3BlbiwgbWF4X29wZW47Cn0gZHBbTl1bMl07Cgpib29sIGFbTl07CmludCBuOwoKRHAgbWVyZ2UoRHAgYSwgRHAgYikKewogICAgYXV0byB2YWwgPSBtaW4oYS52YWwsIGIudmFsKTsKICAgIGludCBtaW5fb3BlbiA9IE4gKyAxLCBtYXhfb3BlbiA9IDA7CiAgICBhdXRvIHJlbGF4ID0gWyZdKERwIGQpewogICAgICAgIGlmICh2YWwgPT0gZC52YWwpIHsKICAgICAgICAgICAgck1pbihtaW5fb3BlbiwgZC5taW5fb3Blbik7CiAgICAgICAgICAgIHJNYXgobWF4X29wZW4sIGQubWF4X29wZW4pOwogICAgICAgIH0KICAgIH07CiAgICByZWxheChhKTsKICAgIHJlbGF4KGIpOwogICAgcmV0dXJuIHt2YWwsIG1pbl9vcGVuLCBtYXhfb3Blbn07Cn0KCnBhaXI8RHAsIERwPiBzdGF0ZXMoaW50IGksIGludCB2LCBjb25zdCBpbnQgQykKewogICAgRHAgb3Blbl9uZXcgPSBkcFtpIC0gMV1bIXZdOwogICAgb3Blbl9uZXcudmFsICs9IEMsICsrb3Blbl9uZXcubWluX29wZW4sICsrb3Blbl9uZXcubWF4X29wZW47CiAgICBEcCBjb250X29sZCA9IGRwW2kgLSAxXVt2XTsKICAgIHJldHVybiB7b3Blbl9uZXcsIGNvbnRfb2xkfTsKfQoKRHAgY2FsYyhjb25zdCBpbnQgQywgY29uc3QgaW50IHN0LCBjb25zdCBpbnQgZm4pCnsKICAgIGRwWzBdW3N0XSA9IHtDLCAxLCAxfTsKICAgIGRwWzBdWyFzdF0gPSB7SU5GLCAtMSwgLTF9OwogICAgZHBbMF1bIWFbMF1dLnZhbCArPSAyOwogICAgRk9SKGksIDEsIG4gLSAxKQogICAgICAgIEZPUk4odiwgMikgewogICAgICAgICAgICBEcCBvcGVuX25ldywgY29udF9vbGQ7CiAgICAgICAgICAgIHRpZShvcGVuX25ldywgY29udF9vbGQpID0gc3RhdGVzKGksIHYsIEMpOwogICAgICAgICAgICBkcFtpXVt2XSA9IG1lcmdlKG9wZW5fbmV3LCBjb250X29sZCk7CiAgICAgICAgICAgIGlmIChhW2ldICE9IHYpCiAgICAgICAgICAgICAgICBkcFtpXVt2XS52YWwgKz0gMjsKICAgICAgICB9CiAgICByZXR1cm4gZHBbbiAtIDFdW2ZuXTsKfQoKYm9vbCBhbnNbTl07Cgp2b2lkIG1vZChpbnQmIGssIGludCBzdCwgaW50IGZuKQp7CiAgICBpZiAoc3QgPT0gZm4gJiYgayAlIDIgPT0gMCkKICAgICAgICAtLWs7CiAgICBpZiAoc3QgIT0gZm4gJiYgayAlIDIgPT0gMSkKICAgICAgICAtLWs7Cn0KCmJvb2wgcmVzdG9yZShjb25zdCBpbnQgQywgaW50IGssIGNvbnN0IGludCBzLCBjb25zdCBpbnQgZikKewogICAgbW9kKGssIHMsIGYpOwogICAgYXV0byBmbiA9IGNhbGMoQywgcywgZik7CiAgICBhdXRvIHZhbCA9IGZuLnZhbDsKICAgIGlmIChrID4gZm4ubWF4X29wZW4pIHsKICAgICAgICBhc3NlcnQoQyA9PSAwKTsKICAgICAgICBrID0gZm4ubWF4X29wZW47CiAgICB9CiAgICBhdXRvIGNoZWNrID0gWyZ2YWwsICZrXShEcCBkKXsKICAgICAgICByZXR1cm4gZC52YWwgPT0gdmFsICYmIGQubWluX29wZW4gPD0gayAmJiBrIDw9IGQubWF4X29wZW47CiAgICB9OwogICAgaW50IGkgPSBuIC0gMSwgdiA9IGY7CiAgICBmb3IgKDsgaSAhPSAwOyAtLWkpIHsKICAgICAgICBhbnNbaV0gPSB2OwogICAgICAgIHZhbCA9IGRwW2ldW3ZdLnZhbDsKICAgICAgICBpZiAoYVtpXSAhPSB2KQogICAgICAgICAgICB2YWwgLT0gMjsKICAgICAgICBEcCBvcGVuX25ldywgY29udF9vbGQ7CiAgICAgICAgdGllKG9wZW5fbmV3LCBjb250X29sZCkgPSBzdGF0ZXMoaSwgdiwgQyk7CiAgICAgICAgaWYgKGNoZWNrKGNvbnRfb2xkKSkgewogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICB9CiAgICAgICAgaWYgKGNoZWNrKG9wZW5fbmV3KSkgewogICAgICAgICAgICAtLWs7CiAgICAgICAgICAgIHYgPSAhdjsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgfQogICAgICAgIGFzc2VydChmYWxzZSk7CiAgICB9CiAgICBhbnNbMF0gPSB2OwogICAgcmV0dXJuIHRydWU7Cn0KCgpwYWlyPGludCwgaW50PiByZXMoaW50IHN0LCBpbnQgZm4sIGludCBrKQp7CiAgICBtb2Qoaywgc3QsIGZuKTsKICAgIGlmIChrID09IDApCiAgICAgICAgcmV0dXJuIHtJTkYsIC0xfTsKICAgIGludCBsID0gLTEsIHIgPSBJTkY7CiAgICB3aGlsZSAobCArIDEgIT0gcikgewogICAgICAgIGF1dG8gbSA9IChsICsgcikgLyAyOwogICAgICAgIGlmIChjYWxjKG0sIHN0LCBmbikubWluX29wZW4gPiBrKQogICAgICAgICAgICBsID0gbTsKICAgICAgICBlbHNlCiAgICAgICAgICAgIHIgPSBtOwogICAgfQogICAgcmV0dXJuIHtjYWxjKHIsIHN0LCBmbikudmFsIC0gayAqIHIsIHJ9Owp9Cgp2b2lkIHNvbHZlKCkKewogICAgaW50IGs7CiAgICBjaW4gPj4gbiA+PiBrOwogICAgRk9STihpLCBuKSB7CiAgICAgICAgY2hhciBjOwogICAgICAgIGNpbiA+PiBjOwogICAgICAgIGFbaV0gPSBjIC0gJzAnOwogICAgfQoKICAgIHBhaXI8aW50LCBpbnQ+IGJlc3QgPSB7SU5GLCAtMX07CiAgICBpbnQgYlN0ID0gLTEsIGJGbiA9IC0xOwogICAgRk9STihzdCwgMikgewogICAgICAgIEZPUk4oZm4sIDIpIHsKICAgICAgICAgICAgaWYgKHJNaW4oYmVzdCwgcmVzKHN0LCBmbiwgaykpKQogICAgICAgICAgICAgICAgYlN0ID0gc3QsIGJGbiA9IGZuOwogICAgICAgIH0KICAgIH0KCiAgICByZXN0b3JlKGJlc3QuUywgaywgYlN0LCBiRm4pOwogICAgRk9STihpLCBuKQogICAgICAgIGNvdXQgPDwgYW5zW2ldOwogICAgY291dCA8PCBlbmRsOwp9CgoKaW50IG1haW4oKQp7CiNpZmRlZiBNWQogICAgZnJlb3BlbigiaW5wdXQudHh0IiwgInIiLCBzdGRpbik7CiAgICBmcmVvcGVuKCJvdXRwdXQudHh0IiwgInciLCBzdGRvdXQpOwojZWxzZQogICAgI2RlZmluZSBGSUxFSU8oVEFTSykgZG8geyBmcmVvcGVuKFRBU0siLmluIiwgInIiLCBzdGRpbik7IGZyZW9wZW4oVEFTSyIub3V0IiwgInciLCBzdGRvdXQpOyB9IHdoaWxlIChmYWxzZSkKICAgIEZJTEVJTygicGVuZ3VpbnMiKTsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgY2luLnRpZSgwKTsKI2VuZGlmIC8vIE1ZCiAgICBpbnQgdDsKICAgIGNpbiA+PiB0OwogICAgRk9SKGksIDEsIHQpIHsKICAgICAgICBzb2x2ZSgpOwogICAgfQp9