#include #include using namespace std; using ll = long long int; using u64 = unsigned long long; using pll = pair; // #include // using namespace atcoder; #define REP(i, a, b) for (ll i = (a); i < (b); i++) #define REPrev(i, a, b) for (ll i = (a); i >= (b); i--) #define ALL(coll) (coll).begin(), (coll).end() #define SIZE(v) ((ll)((v).size())) #define REPOUT(i, a, b, exp, sep) REP(i, (a), (b)) cout << (exp) << (i + 1 == (b) ? "" : (sep)); cout << "\n" // @@ !! LIM(digit debug) // ---- inserted library file digit.cc // published at https://github.com/yamate11/compprog-clib/blob/master/digit.cc struct digit_util { const ll base; const vector _pow; static vector _make_pow(ll b) { vector ret; ll t = 1; while (true) { ret.push_back(t); if (__builtin_smulll_overflow(t, b, &t)) break; } return ret; }; digit_util(ll base_ = 10) : base(base_), _pow(_make_pow(base_)) {} ll pow_size() const { return _pow.size(); } ll pow(ll i) const { if (i < 0 or ssize(_pow) <= i) return -1; return _pow[i]; } ll width(ll x) const { if (x < 0) return -1; if (base == 2) return bit_width((unsigned long long)x); if (x == 0) return 0; ll ret = 0; for (; x != 0; x /= base) ret++; return ret; } ll nd_min(ll i) const { return i < 0 ? -1 : i == 0 ? 0 : pow(i - 1); } ll nd_max(ll i) const { return i < 0 ? -1 : i == 0 ? 0 : nd_min(i + 1) - 1; } ll floor(ll x) const { return (x < 0) ? -1 : x == 0 ? 0 : _pow[width(x) - 1]; } ll ceil(ll x) const { if (x < 0) return -1; if (x == 0) return 0; ll p = _pow[width(x) - 1]; return (x == p) ? p : (p * base); } ll log(ll x) const { return (x <= 0) ? -1 : width(x) - 1; } ll d_at(ll x, ll i) const { if (x < 0) return -1; if (x == 0) return 0; if (i < 0) i += width(x); return (x / pow(i)) % base; } ll d_sub(ll x, ll pos, ll len) const { if (x < 0) return -1; if (x == 0) return 0; ll w = width(x); if (pos < 0) pos += w; if (len < 0) { len = -len; pos = pos - len + 1; } if (pos < 0) { len += pos; pos = 0; } if (pos + len > w) len = w - pos; return (x % pow(pos + len)) / pow(pos); } vector to_vector(ll x) const { if (x < 0) return vector{}; if (x == 0) return vector{0}; vector ret; ret.reserve(width(x)); for ( ; x != 0; x /= base) { ret.push_back(x % base); } return ret; } string to_string(ll x, bool upcase = false) const { if (x < 0) return string(); if (x == 0) return string("0"); char ten = upcase ? 'A' : 'a'; ll w = width(x); string ret(w, ' '); for (ll i = w - 1; x != 0; x /= base, i--) { ll y = x % base; ret[i] = y < 10 ? '0' + y : ten + (y - 10); } return ret; } ll from_vector(const vector& vec) const { ll ret = 0; for (ll i = 0; i < ssize(vec); i++) ret += vec[i] * pow(i); return ret; } static ll _get_digit_char(char c) { if ('0' <= c and c <= '9') return c - '0'; else if ('a' <= c and c <= 'z') return c - 'a' + 10; else if ('A' <= c and c <= 'Z') return c - 'A' + 10; else throw runtime_error("_get_digit_char: unknown letter"); } ll from_string(string s) const { ll ret = 0; for (ll i = 0; i < ssize(s); i++) ret += _get_digit_char(s[i]) * pow(ssize(s) - 1 - i); return ret; } }; // ---- end digit.cc // ---- inserted function f:<< from util.cc // declarations template ostream& operator<< (ostream& os, const pair& p); template ostream& operator<< (ostream& os, const tuple& t); template ostream& operator<< (ostream& os, const tuple& t); template ostream& operator<< (ostream& os, const tuple& t); template ostream& operator<< (ostream& os, const tuple& t); template ostream& operator<< (ostream& os, const vector& v); template ostream& operator<< (ostream& os, const array& v); template ostream& operator<< (ostream& os, const set& v); template ostream& operator<< (ostream& os, const unordered_set& v); template ostream& operator<< (ostream& os, const multiset& v); template ostream& operator<< (ostream& os, const map& mp); template ostream& operator<< (ostream& os, const unordered_map& mp); template ostream& operator<< (ostream& os, const queue& orig); template ostream& operator<< (ostream& os, const deque& orig); template ostream& operator<< (ostream& os, const priority_queue& orig); template ostream& operator<< (ostream& os, const stack& st); #if __cplusplus >= 201703L template ostream& operator<< (ostream& os, const optional& t); #endif ostream& operator<< (ostream& os, int8_t x); ostream& operator<< (ostream& os, const __int128& x); // definitions template ostream& operator<< (ostream& os, const pair& p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template ostream& operator<< (ostream& os, const tuple& t) { os << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ")"; return os; } template ostream& operator<< (ostream& os, const tuple& t) { os << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ", " << get<3>(t) << ")"; return os; } template ostream& operator<< (ostream& os, const tuple& t) { os << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ", " << get<3>(t) << ", " << get<4>(t) << ")"; return os; } template ostream& operator<< (ostream& os, const tuple& t) { os << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ", " << get<3>(t) << ", " << get<4>(t) << ", " << get<5>(t) << ")"; return os; } template ostream& operator<< (ostream& os, const vector& v) { os << '['; for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << *it; } os << ']'; return os; } template ostream& operator<< (ostream& os, const array& v) { os << '['; for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << *it; } os << ']'; return os; } template ostream& operator<< (ostream& os, const set& v) { os << '{'; for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << *it; } os << '}'; return os; } template ostream& operator<< (ostream& os, const unordered_set& v) { os << '{'; for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << *it; } os << '}'; return os; } template ostream& operator<< (ostream& os, const multiset& v) { os << '{'; for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << *it; } os << '}'; return os; } template ostream& operator<< (ostream& os, const map& mp) { os << '['; for (auto it = mp.begin(); it != mp.end(); it++) { if (it != mp.begin()) os << ", "; os << it->first << ": " << it->second; } os << ']'; return os; } template ostream& operator<< (ostream& os, const unordered_map& mp) { os << '['; for (auto it = mp.begin(); it != mp.end(); it++) { if (it != mp.begin()) os << ", "; os << it->first << ": " << it->second; } os << ']'; return os; } template ostream& operator<< (ostream& os, const queue& orig) { queue que(orig); bool first = true; os << '['; while (!que.empty()) { T x = que.front(); que.pop(); if (!first) os << ", "; os << x; first = false; } return os << ']'; } template ostream& operator<< (ostream& os, const deque& orig) { deque que(orig); bool first = true; os << '['; while (!que.empty()) { T x = que.front(); que.pop_front(); if (!first) os << ", "; os << x; first = false; } return os << ']'; } template ostream& operator<< (ostream& os, const priority_queue& orig) { priority_queue pq(orig); bool first = true; os << '['; while (!pq.empty()) { T x = pq.top(); pq.pop(); if (!first) os << ", "; os << x; first = false; } return os << ']'; } template ostream& operator<< (ostream& os, const stack& st) { stack tmp(st); os << '['; bool first = true; while (!tmp.empty()) { T& t = tmp.top(); if (first) first = false; else os << ", "; os << t; tmp.pop(); } os << ']'; return os; } #if __cplusplus >= 201703L template ostream& operator<< (ostream& os, const optional& t) { if (t.has_value()) os << "v(" << t.value() << ")"; else os << "nullopt"; return os; } #endif ostream& operator<< (ostream& os, int8_t x) { os << (int32_t)x; return os; } // for Enum type; just displays ordinals. template typename std::enable_if::value, std::ostream&>::type operator<<(std::ostream& os, E e) { return os << static_cast::type>(e); } // This is a very ad-hoc implementation... // Known problem: "1 << 127" cannot be handled. ostream& operator<<(ostream& os, const __int128& v) { unsigned __int128 a = v < 0 ? -v : v; ll i = 0; string s(64, ' '); if (v == 0) { s[i++] = '0'; }else { while (a > 0) { s[i++] = '0' + (char)(a % 10); a /= 10; } } if (v < 0) { s[i++] = '-'; } s.erase(s.begin() + i, s.end()); reverse(s.begin(), s.end()); os << s; return os; } // ---- end f:<< // ---- inserted library file debug.cc // published at https://github.com/yamate11/compprog-clib/blob/master/debug.cc // https://github.com/yamate11/compprog-clib/blob/master/debug.cc template string dbgFormat(const char* fmt, Args... args) { size_t len = snprintf(nullptr, 0, fmt, args...); char buf[len + 1]; snprintf(buf, len + 1, fmt, args...); return string(buf); } template void dbgLog(bool with_nl, Head&& head) { cerr << head; if (with_nl) cerr << endl; } template void dbgLog(bool with_nl, Head&& head, Tail&&... tail) { cerr << head << " "; dbgLog(with_nl, forward(tail)...); } #if DEBUG #define DLOG(...) dbgLog(true, __VA_ARGS__) #define DLOGNNL(...) dbgLog(false, __VA_ARGS__) #define DFMT(...) cerr << dbgFormat(__VA_ARGS__) << endl #define DCALL(func, ...) func(__VA_ARGS__) #else #define DLOG(...) #define DLOGNNL(...) #define DFMT(...) #define DCALL(func, ...) #endif /* #if DEBUG_LIB #define DLOG_LIB(...) dbgLog(true, __VA_ARGS__) #define DLOGNNL_LIB(...) dbgLog(false, __VA_ARGS__) #define DFMT_LIB(...) cerr << dbgFormat(__VA_ARGS__) << endl #define DCALL_LIB(func, ...) func(__VA_ARGS__) #else #define DLOG_LIB(...) #define DFMT_LIB(...) #define DCALL_LIB(func, ...) #endif */ #define DUP1(E1) #E1 "=", E1 #define DUP2(E1,E2) DUP1(E1), DUP1(E2) #define DUP3(E1,...) DUP1(E1), DUP2(__VA_ARGS__) #define DUP4(E1,...) DUP1(E1), DUP3(__VA_ARGS__) #define DUP5(E1,...) DUP1(E1), DUP4(__VA_ARGS__) #define DUP6(E1,...) DUP1(E1), DUP5(__VA_ARGS__) #define DUP7(E1,...) DUP1(E1), DUP6(__VA_ARGS__) #define DUP8(E1,...) DUP1(E1), DUP7(__VA_ARGS__) #define DUP9(E1,...) DUP1(E1), DUP8(__VA_ARGS__) #define DUP10(E1,...) DUP1(E1), DUP9(__VA_ARGS__) #define DUP11(E1,...) DUP1(E1), DUP10(__VA_ARGS__) #define DUP12(E1,...) DUP1(E1), DUP11(__VA_ARGS__) #define GET_MACRO(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,NAME,...) NAME #define DUP(...) GET_MACRO(__VA_ARGS__, DUP12, DUP11, DUP10, DUP9, DUP8, DUP7, DUP6, DUP5, DUP4, DUP3, DUP2, DUP1)(__VA_ARGS__) #define DLOGK(...) DLOG(DUP(__VA_ARGS__)) #define DLOGKL(lab, ...) DLOG(lab, DUP(__VA_ARGS__)) #if DEBUG_LIB #define DLOG_LIB DLOG #define DLOGK_LIB DLOGK #define DLOGKL_LIB DLOGKL #endif // ---- end debug.cc // @@ !! LIM -- end mark -- int main(/* int argc, char *argv[] */) { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout << setprecision(20); vector du; du.push_back(digit_util(10)); du.push_back(digit_util(10)); REP(i, 2, 11) du.push_back(digit_util(i)); auto f = [&](ll x) -> ll { auto vec = du[10].to_vector(x); ll wid = ssize(vec); ll ret = 0; ll acc = 0; REP(k, 1, 10) { vector W(wid, 0LL); REPrev(i, wid - 1, 0) { if (vec[i] <= k) W[i] = vec[i]; else { REPrev(j, i, 0) W[j] = k; break; } } ll t = du[k + 1].from_vector(W); ret += (t - acc) * k; acc = t; } DLOGK(x, ret); return ret; }; auto solve = [&]() -> ll { ll L, R; cin >> L >> R; if (L == 1) return f(R); else return f(R) - f(L - 1); }; ll T; cin >> T; REP(i, 0, T) cout << solve() << "\n"; return 0; }