#include<bits/stdc++.h> // #define SINGLE_INPUT #define ll long long #define ull unsigned long long #define N 500005 #define MOD 998244353 usingnamespace std;
random_device seed; ranlux48 engine(seed()); intrandom(int l, int r){ uniform_int_distribution<> distrib(l, r); returndistrib(engine); } template<classt,classu> ostream& operator<<(ostream& os,const pair<t,u>& p) { return os<<'['<<p.first<<", "<<p.second<<']'; } template<classt> ostream& operator<<(ostream& os,const vector<t>& v) { os<<'['; int s = 1; for(auto e:v) { if (s) s = 0; else os << ", "; os << e; } return os<<']'; } template<classt,classu> ostream& operator<<(ostream& os,const map<t,u>& mp){ os<<'{'; int s = 1; for(auto [x,y]:mp) { if (s) s = 0; else os << ", "; os<<x<<": "<<y; } return os<<'}'; }
voidsol(){ ll k; string s; cin >> s >> k; int n = s.size(); string ans = "1"+string(n, '0'); vector<int> c(10); string pre; for (int i=0; i<n; i++) { for (int j=9; j>s[i]-'0'; j--) { auto t = c; t[j] = 1; int u = count(t.begin(), t.end(), 1); if (u > k) continue; int mn = 0; if (u == k) { while (t[mn] == 0) mn++; } string x = pre+char(j+'0')+string(n-i-1, char(mn+'0')); // cout << x << endl; ans = x; } pre.push_back(s[i]); c[s[i]-'0'] = 1; } if (count(c.begin(), c.end(), 1) <= k) { ans = s; } cout << ans << "\n"; }