#include #include #include #include #include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; template vector> vec2d(int n, int m, T v){ return vector>(n, vector(m, v)); } template vector>> vec3d(int n, int m, int k, T v){ return vector>>(n, vector>(m, vector(k, v))); } template void print_vector(vector v, char delimiter=' '){ if(v.empty()) { cout << endl; return; } for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter; cout << v.back() << endl; } ll sub_solve(vector mx, vector target){ int len = mx.size(); auto dp = vec3d(len, 2, 2, 0ll); for(int x = 1; x < mx[0]; x++){ if(x < target[0]){ dp[0][0][0]++; }else if(x == target[0]){ dp[0][0][1]++; } } if(mx[0] < target[0]){ dp[0][1][0]++; }else if(mx[0] == target[0]){ dp[0][1][1]++; } // cout << "====" << endl; // debug_value(mx[0]) // print_vector(dp[0][0]); // print_vector(dp[0][1]); int target_len = target.size(); for(int i = 1; i < len; i++){ for(int j = 0; j <= 1; j++){ for(int k = 0; k <= 1; k++){ int t = target.size() > i ? target[i] : -1; for(int x = 0; x <= 9; x++){ // 大きくなってしまう if(k == 1 && x > t) continue; if(j == 1 && x > mx[i]) continue; int jj = (j == 0 || x < mx[i]) ? 0 : 1; int kk = (k == 0 || x < t) ? 0 : 1; dp[i][jj][kk] += dp[i-1][j][k]; } } } } ll ans = dp[len-1][0][0]+dp[len-1][1][0]; if(mx.size() < target.size()){ ans += dp[len-1][0][1]+dp[len-1][1][1]; } // debug_value(ans) return ans; } void solve(){ ll n, k; cin >> n >> k; string ns = to_string(n); string ks = to_string(k); vector nv, kv; for(char c: ns) nv.push_back(c-'0'); for(char c: ks) kv.push_back(c-'0'); ll ans = 0; for(int len = 1; len < ns.size(); len++){ ans += sub_solve(vector(len, 9), kv); } ans += sub_solve(nv, kv); ans++; cout << ans << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; int t; cin >> t; while(t--) solve(); }