#include namespace suisen { template bool chmin(T& x, const T& y) { return y >= x ? false : (x = y, true); } template bool chmax(T& x, const T& y) { return y <= x ? false : (x = y, true); } template constexpr int pow_m1(T n) { return -(n & 1) | 1; } template constexpr T fld(const T x, const T y) { T q = x / y, r = x % y; return q - ((x ^ y) < 0 and (r != 0)); } template constexpr T cld(const T x, const T y) { T q = x / y, r = x % y; return q + ((x ^ y) > 0 and (r != 0)); } } namespace suisen::macro { #define IMPL_REPITER(cond) auto& begin() { return *this; } auto end() { return nullptr; } auto& operator*() { return _val; } auto& operator++() { return _val += _step, *this; } bool operator!=(std::nullptr_t) { return cond; } template == std::is_signed_v), std::nullptr_t> = nullptr> struct rep_impl { Int _val; const Int _end, _step; rep_impl(Int n) : rep_impl(0, n) {} rep_impl(IntL l, Int r, IntStep step = 1) : _val(l), _end(r), _step(step) {} IMPL_REPITER((_val < _end)) }; template == std::is_signed_v), std::nullptr_t> = nullptr> struct rrep_impl { Int _val; const Int _end, _step; rrep_impl(Int n) : rrep_impl(0, n) {} rrep_impl(IntL l, Int r) : _val(r - 1), _end(l), _step(-1) {} rrep_impl(IntL l, Int r, IntStep step) : _val(l + fld(r - l - 1, step) * step), _end(l), _step(-step) {} IMPL_REPITER((_val >= _end)) }; template struct repinf_impl { Int _val; const Int _step; repinf_impl(Int l, IntStep step = 1) : _val(l), _step(step) {} IMPL_REPITER((true)) }; #undef IMPL_REPITER } #include #include #include namespace suisen { template using constraints_t = std::enable_if_t, std::nullptr_t>; template struct bitnum { static constexpr int value = 0; }; template struct bitnum>> { static constexpr int value = std::numeric_limits>::digits; }; template static constexpr int bitnum_v = bitnum::value; template struct is_nbit { static constexpr bool value = bitnum_v == n; }; template static constexpr bool is_nbit_v = is_nbit::value; template struct safely_multipliable { using type = T; }; template struct safely_multipliable, is_nbit>> { using type = long long; }; template struct safely_multipliable, is_nbit>> { using type = __int128_t; }; template struct safely_multipliable, is_nbit>> { using type = unsigned long long; }; template struct safely_multipliable, is_nbit>> { using type = __uint128_t; }; template using safely_multipliable_t = typename safely_multipliable::type; template struct rec_value_type { using type = T; }; template struct rec_value_type> { using type = typename rec_value_type::type; }; template using rec_value_type_t = typename rec_value_type::type; template class is_iterable { template static auto test(T_ e) -> decltype(e.begin(), e.end(), std::true_type{}); static std::false_type test(...); public: static constexpr bool value = decltype(test(std::declval()))::value; }; template static constexpr bool is_iterable_v = is_iterable::value; template class is_writable { template static auto test(T_ e) -> decltype(std::declval() << e, std::true_type{}); static std::false_type test(...); public: static constexpr bool value = decltype(test(std::declval()))::value; }; template static constexpr bool is_writable_v = is_writable::value; template class is_readable { template static auto test(T_ e) -> decltype(std::declval() >> e, std::true_type{}); static std::false_type test(...); public: static constexpr bool value = decltype(test(std::declval()))::value; }; template static constexpr bool is_readable_v = is_readable::value; } // namespace suisen namespace suisen::io { template >, std::negation>>>, std::nullptr_t> = nullptr> struct InputStream { private: using istream_type = std::remove_reference_t; IStream is; struct { InputStream* is; template operator T() { T e; *is >> e; return e; } } _reader{ this }; public: template InputStream(IStream_ &&is) : is(std::move(is)) {} template InputStream(IStream_ &is) : is(is) {} template InputStream& operator>>(T& e) { if constexpr (suisen::is_readable_v) is >> e; else _read(e); return *this; } auto read() { return _reader; } template void read(Head& head, Tail &...tails) { ((*this >> head) >> ... >> tails); } istream_type& get_stream() { return is; } private: static __uint128_t _stou128(const std::string& s) { __uint128_t ret = 0; for (char c : s) if ('0' <= c and c <= '9') ret = 10 * ret + c - '0'; return ret; } static __int128_t _stoi128(const std::string& s) { return (s[0] == '-' ? -1 : +1) * _stou128(s); } void _read(__uint128_t& v) { v = _stou128(std::string(_reader)); } void _read(__int128_t& v) { v = _stoi128(std::string(_reader)); } template void _read(std::pair& a) { *this >> a.first >> a.second; } template void _read(std::tuple& a) { if constexpr (N < sizeof...(Args)) *this >> std::get(a), _read(a); } template , std::nullptr_t> = nullptr> void _read(Iterable& a) { for (auto& e : a) *this >> e; } }; template InputStream(IStream &&) -> InputStream; template InputStream(IStream &) -> InputStream; InputStream cin{ std::cin }; auto read() { return cin.read(); } template void read(Head& head, Tail &...tails) { cin.read(head, tails...); } } // namespace suisen::io namespace suisen { using io::read; } // namespace suisen namespace suisen::io { template >, std::negation>>>, std::nullptr_t> = nullptr> struct OutputStream { private: using ostream_type = std::remove_reference_t; OStream os; public: template OutputStream(OStream_ &&os) : os(std::move(os)) {} template OutputStream(OStream_ &os) : os(os) {} template OutputStream& operator<<(const T& e) { if constexpr (suisen::is_writable_v) os << e; else _print(e); return *this; } void print() { *this << '\n'; } template void print(const Head& head, const Tail &...tails) { *this << head, ((*this << ' ' << tails), ...), *this << '\n'; } template , std::nullptr_t> = nullptr> void print_all(const Iterable& v, std::string sep = " ", std::string end = "\n") { for (auto it = v.begin(); it != v.end();) if (*this << *it; ++it != v.end()) *this << sep; *this << end; } ostream_type& get_stream() { return os; } private: void _print(__uint128_t value) { char buffer[41], *d = std::end(buffer); do *--d = '0' + (value % 10), value /= 10; while (value); os.rdbuf()->sputn(d, std::end(buffer) - d); } void _print(__int128_t value) { if (value < 0) *this << '-'; _print(__uint128_t(value < 0 ? -value : value)); } template void _print(const std::pair& a) { *this << a.first << ' ' << a.second; } template void _print(const std::tuple& a) { if constexpr (N < std::tuple_size_v>) { if constexpr (N) *this << ' '; *this << std::get(a), _print(a); } } template , std::nullptr_t> = nullptr> void _print(const Iterable& a) { print_all(a, " ", ""); } }; template OutputStream(OStream_ &&) -> OutputStream; template OutputStream(OStream_ &) -> OutputStream; OutputStream cout{ std::cout }, cerr{ std::cerr }; template void print(const Args &... args) { cout.print(args...); } template , std::nullptr_t> = nullptr> void print_all(const Iterable& v, const std::string& sep = " ", const std::string& end = "\n") { cout.print_all(v, sep, end); } } // namespace suisen::io namespace suisen { using io::print, io::print_all; } // namespace suisen namespace suisen { template , std::enable_if_t, std::is_invocable_r, std::invoke_result_t>>, std::nullptr_t> = nullptr> auto comparator(const ToKey& to_key, const CompKey& comp_key = std::less<>()) { return [=](const T& x, const T& y) { return comp_key(to_key(x), to_key(y)); }; } template , std::nullptr_t> = nullptr> std::vector sorted_indices(int n, const Compare& compare) { std::vector p(n); return std::iota(p.begin(), p.end(), 0), std::sort(p.begin(), p.end(), compare), p; } template , std::nullptr_t> = nullptr> std::vector sorted_indices(int n, const ToKey& to_key) { return sorted_indices(n, comparator(to_key)); } template auto priority_queue_with_comparator(const Comparator& comparator) { return std::priority_queue, Comparator>{ comparator }; } template , std::nullptr_t> = nullptr> void sort_unique_erase(Iterable& a) { std::sort(a.begin(), a.end()), a.erase(std::unique(a.begin(), a.end()), a.end()); } template struct Dim : std::array { template Dim(const Ints& ...ns) : std::array::array{ static_cast(ns)... } {} }; template Dim(const Ints& ...) -> Dim; template auto ndvec(const Dim &ns, const T& value = {}) { if constexpr (I + 1 < D) { return std::vector(ns[I], ndvec(ns, value)); } else { return std::vector(ns[I], value); } } } namespace suisen { using int128 = __int128_t; using uint128 = __uint128_t; template using min_priority_queue = std::priority_queue, std::greater>; template using max_priority_queue = std::priority_queue, std::less>; } namespace suisen { const std::string Yes = "Yes", No = "No", YES = "YES", NO = "NO"; } #ifdef LOCAL # define debug(...) debug_impl(#__VA_ARGS__, __VA_ARGS__) template void debug_impl(const char* s, const H& h, const Ts&... t) { suisen::io::cerr << "[\033[32mDEBUG\033[m] " << s << ": " << h, ((suisen::io::cerr << ", " << t), ..., (suisen::io::cerr << "\n")); } #else # define debug(...) void(0) #endif #define FOR(e, v) for (auto &&e : v) #define CFOR(e, v) for (const auto &e : v) #define REP(i, ...) CFOR(i, suisen::macro::rep_impl(__VA_ARGS__)) #define RREP(i, ...) CFOR(i, suisen::macro::rrep_impl(__VA_ARGS__)) #define REPINF(i, ...) CFOR(i, suisen::macro::repinf_impl(__VA_ARGS__)) #define LOOP(n) for ([[maybe_unused]] const auto& _ : suisen::macro::rep_impl(n)) #define ALL(iterable) std::begin(iterable), std::end(iterable) using namespace suisen; using namespace std; struct io_setup { io_setup(int precision = 20) { std::ios::sync_with_stdio(false), std::cin.tie(nullptr); std::cout << std::fixed << std::setprecision(precision); } } io_setup_ {}; constexpr int iinf = std::numeric_limits::max() / 2; constexpr long long linf = std::numeric_limits::max() / 2; #include #include #include namespace suisen { template int count_leq(const std::vector &v, const T &key) { return std::upper_bound(v.begin(), v.end(), key) - v.begin(); } template int count_lt(const std::vector &v, const T &key) { return std::lower_bound(v.begin(), v.end(), key) - v.begin(); } template int count_geq(const std::vector &v, const T &key) { return v.size() - count_lt(v, key); } template int count_gt(const std::vector &v, const T &key) { return v.size() - count_leq(v, key); } template auto min_geq(const Container &container, const Key &key) -> decltype(std::make_optional(*container.lower_bound(key))) { if (auto it = container.lower_bound(key); it == container.end()) return std::nullopt; else return std::make_optional(*it); } template auto min_gt(const Container &container, const Key &key) -> decltype(std::make_optional(*container.upper_bound(key))) { if (auto it = container.upper_bound(key); it == container.end()) return std::nullopt; else return std::make_optional(*it); } template auto max_leq(const Container &container, const Key &key) -> decltype(std::make_optional(*container.upper_bound(key))) { if (auto it = container.upper_bound(key); it == container.begin()) return std::nullopt; else return std::make_optional(*--it); } template auto max_lt(const Container &container, const Key &key) -> decltype(std::make_optional(*container.lower_bound(key))) { if (auto it = container.lower_bound(key); it == container.begin()) return std::nullopt; else return std::make_optional(*--it); } template std::optional min_geq(const std::vector &v, const T &key) { auto it = std::lower_bound(v.begin(), v.end(), key); return it == v.end() ? std::nullopt : std::make_optional(*it); } template std::optional min_gt(const std::vector &v, const T &key) { auto it = std::upper_bound(v.begin(), v.end(), key); return it == v.end() ? std::nullopt : std::make_optional(*it); } template std::optional max_leq(const std::vector &v, const T &key) { auto it = std::upper_bound(v.begin(), v.end(), key); return it == v.begin() ? std::nullopt : std::make_optional(*--it); } template std::optional max_lt(const std::vector &v, const T &key) { auto it = std::lower_bound(v.begin(), v.end(), key); return it == v.begin() ? std::nullopt : std::make_optional(*--it); } __int128_t stoi128(const std::string& s) { __int128_t res; io::InputStream{std::istringstream{s}} >> res; return res; } __uint128_t stou128(const std::string& s) { __uint128_t res; io::InputStream{std::istringstream{s}} >> res; return res; } template , std::nullptr_t> = nullptr> std::string join(const Iterable& v, const std::string& sep, const std::string& end) { io::OutputStream os{ std::ostringstream{} }; os.print_all(v, sep, end); return os.get_stream().str(); } template , std::nullptr_t> = nullptr> std::vector split(const Iterable &s, const typename Iterable::value_type &delim) { std::vector res; for (auto itl = s.begin(), itr = itl;; itl = ++itr) { while (itr != s.end() and *itr != delim) ++itr; res.emplace_back(itl, itr); if (itr == s.end()) return res; } } template , std::nullptr_t> = nullptr> void concat(Iterable& s, const Iterable& t) { s.reserve(std::size(s) + std::size(t)); std::copy(std::begin(t), std::end(t), std::back_inserter(s)); } template , std::nullptr_t> = nullptr> Iterable concatenated(Iterable s, const Iterable& t) { concat(s, t); return s; } template , std::nullptr_t> = nullptr> auto mapped_vec(const Func& f, const Iterable& s) { std::vector> v; v.reserve(std::size(s)), std::transform(s.begin(), s.end(), std::back_inserter(v), f); return v; } template , std::nullptr_t> = nullptr> auto copied_vec(const Iterable& s) { std::vector v; v.reserve(std::size(s)), std::copy(s.begin(), s.end(), std::back_inserter(v)); return v; } namespace charmap { int fd(char c) { return c - '0'; } int fa(char c) { return c - 'a'; } int fA(char c) { return c - 'A'; } int faA(char c) { return c <= 'Z' ? c - 'A' : 26 + (c - 'a'); } } // val = Sum_i res[i] * base^i template , std::nullptr_t> = nullptr> std::string digits_str(T val, size_t width = 0) { static_assert(2 <= base and base <= 10); std::string res; for (; val or res.size() < width; val /= base) res += '0' + (val % base); return res; } // val = Sum_i res[i] * base^i template , std::is_integral>, std::nullptr_t> = nullptr> std::vector digits(T val, U base = 10) { std::vector res; for (; val; val /= base) res.push_back(val % base); if (res.empty()) res.push_back(T{ 0 }); return res; } } // namespace suisen void solve() { int t; read(t); LOOP(t) { long long n, k; read(n, k); vector dn = digits(n), dk = digits(k); reverse(ALL(dn)); reverse(ALL(dk)); dk.resize(dn.size(), -1); const int l = dn.size(); vector pd(l, vector(2, vector(2, 0))); REP(i, l) { vector dp(l, vector(2, vector(2, 0))); const int ni = dn[i]; REP(d, l) { REP(f, 2) REP(g, 2) if (pd[d][f][g]) { const int ki = dk[i - d]; REP(v, 10) { if (f == 0 and ni < v) continue; if (g == 0 and ki < v) continue; int nf = f or v < ni; int ng = g or v < ki; dp[d][nf][ng] += pd[d][f][g]; } } } REP(v, 1, 10) { if (i == 0 and dn[0] < v) continue; if (dk[0] < v) continue; int f = i or v < ni; int g = v < dk[0]; ++dp[i][f][g]; } pd.swap(dp); } long long ans = 0; REP(d, l) { REP(f, 2) REP(g, 2) { ans += pd[d][f][g]; } } print(ans); } } int main() { int test_case_num = 1; // read(test_case_num); LOOP(test_case_num) solve(); return 0; }