#include <bits/stdc++.h>
using namespace std;
#define int int64_t
#define endl '\n'
#define pb push_back
void solve() {
string s;
int k;
cin >> s >> k;
int n = s.size();
string ans;
for(int i = 0; i < n; i++) {
while(ans.size() && ans.back() < s[i] && k) {
k--;
ans.pop_back();
}
ans.pb(s[i]);
}
while(k--) ans.pop_back();
cout << ans;
}
int32_t main() {
int t = 1;
cin >> t;
while(t--) {
solve();
cout << endl;
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIGludCBpbnQ2NF90CiNkZWZpbmUgZW5kbCAnXG4nCiNkZWZpbmUgcGIgcHVzaF9iYWNrCgp2b2lkIHNvbHZlKCkgewoJc3RyaW5nIHM7CglpbnQgazsKCWNpbiA+PiBzID4+IGs7CglpbnQgbiA9IHMuc2l6ZSgpOwoJc3RyaW5nIGFuczsKCWZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKCQl3aGlsZShhbnMuc2l6ZSgpICYmIGFucy5iYWNrKCkgPCBzW2ldICYmIGspIHsKCQkJay0tOwoJCQlhbnMucG9wX2JhY2soKTsKCQl9CgkJYW5zLnBiKHNbaV0pOwoJfQoJd2hpbGUoay0tKSBhbnMucG9wX2JhY2soKTsKCWNvdXQgPDwgYW5zOwp9CgppbnQzMl90IG1haW4oKSB7CglpbnQgdCA9IDE7CgljaW4gPj4gdDsKCXdoaWxlKHQtLSkgewoJCXNvbHZlKCk7CgkJY291dCA8PCBlbmRsOwoJfQp9