#include #include using namespace std; using namespace numbers; int main(){ cin.tie(0)->sync_with_stdio(0); cin.exceptions(ios::badbit | ios::failbit); auto __solve_tc = [&](auto __tc_num)->int{ string ns, k; cin >> ns >> k; long long n = stoll(ns); auto count = [&](string prefix)->long long{ long long x = stoll(prefix), low = 1; long long res = 0; for(auto len = (int)prefix.size(); len < (int)ns.size(); ++ len){ res += low; x *= 10; low *= 10; } { res += clamp(x + low, 1LL, n + 1) - clamp(x, 1LL, n + 1); } return res; }; long long res = (int)k.size() - 1; for(auto i = 0; i < (int)k.size(); ++ i){ for(auto x = i ? 0 : 1; x < k[i] - '0'; ++ x){ res += count(k.substr(0, i) + char(x + '0')); } } cout << res + 1 << "\n"; return 0; }; int __tc_cnt; cin >> __tc_cnt; for(auto __tc_num = 0; __tc_num < __tc_cnt; ++ __tc_num){ __solve_tc(__tc_num); } return 0; } /* */ //////////////////////////////////////////////////////////////////////////////////////// // // // Coded by Aeren // // // ////////////////////////////////////////////////////////////////////////////////////////