#include <bits/stdc++.h>
using namespace std;
#define int              long long int
#define double           long double
#define print(a)         for(auto x : a) cout << x << " "; cout << endl


const int M = 1000000007;
const int N = 3e5+9;
const int INF = 2e9+1;
const int LINF = 2000000000000000001;

inline int power(int a, int b, int mod=M) {
    int x = 1;
    a %= mod;
    while (b) {
        if (b & 1) x = (x * a) % mod; 
        a = (a * a) % mod;
        b >>= 1;
    }
    return x;
}


//_ ***************************** START Below *******************************


typedef pair<int,int> pii;

string a;

string consistency(int n, int k){

    priority_queue<pii, vector<pii>, greater<pii>> digits;
    priority_queue<pii, vector<pii>, greater<pii>> zeroes;


    string ans = "";

    int i = 0;
    while(i<=k){
        if(a[i] == '0') zeroes.push({0,i});
        else digits.push({a[i]-'0',i});
        i++;
    }

    int lastI = -1;

    while(i<=n){

        while(!digits.empty() && digits.top().second <= lastI) digits.pop();
        while(!zeroes.empty() && zeroes.top().second <= lastI) zeroes.pop();

        if(ans.empty()){
            auto top = digits.top();
            digits.pop();

            int digit = top.first;
            lastI = top.second;

            ans.push_back(char(digit+'0'));
        }
        else{
            if(!zeroes.empty()){
                auto top = zeroes.top();
                zeroes.pop();

                lastI = top.second;

                ans.push_back('0');
            }
            else{
                auto top = digits.top();
                digits.pop();
                
                int digit = top.first;
                lastI = top.second;

                ans.push_back(char(digit+'0'));
            }
        }

        if(a[i] == '0') zeroes.push({0, i});
        else digits.push({a[i]-'0', i});
        i++;
    }

    return ans;

}















string practice(int n, int k){


    return 0;
}





void solve() {
    
    int k;
    cin >> a >> k;
    int n = a.size();
    
    cout << consistency(n, k) << endl;


}





int32_t main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}