#include <bits/stdc++.h>   template <typename T>   void MACRO_VAR_Scan(T& t) {     std::cin >> t;   }   template <typename First, typename... Rest>   void MACRO_VAR_Scan(First& first, Rest&... rest) {     std::cin >> first;     MACRO_VAR_Scan(rest...);   }   template <typename T>   void MACRO_VEC_ROW_Init(int n, T& t) {     t.resize(n);   }   template <typename First, typename... Rest>   void MACRO_VEC_ROW_Init(int n, First& first, Rest&... rest) {     first.resize(n);     MACRO_VEC_ROW_Init(n, rest...);   }   template <typename T>   void MACRO_VEC_ROW_Scan(int p, T& t) {     std::cin >> t[p];   }   template <typename First, typename... Rest>   void MACRO_VEC_ROW_Scan(int p, First& first, Rest&... rest) {     std::cin >> first[p];     MACRO_VEC_ROW_Scan(p, rest...);   }   template <typename T>   inline T CHMAX(T& a, const T b) {     return a = (a < b) ? b : a;   }   template <typename T>   inline T CHMIN(T& a, const T b) {     return a = (a > b) ? b : a;   }   void CHECKTIME(std::function<void()> f) {     auto start = std::chrono::system_clock::now();     f();     auto end = std::chrono::system_clock::now();     auto res = std::chrono::duration_cast<std::chrono::nanoseconds>((end - start))                    .count();     std::cerr <<  [Time:  << res <<  ns  (  << res / (1.0e9) <<  s)] n ;   }   using ll = long long;   using ull = unsigned long long;   constexpr ll INFINT = 1 << 30;   constexpr ll INFINT_LIM = (1LL << 31) - 1;   constexpr ll INFLL = 1LL << 60;   constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62);   constexpr double EPS = 1e-7;   constexpr ll MOD = 1000000007;   constexpr double PI = 3.141592653589793238462643383279;   ll dp[16][2][16];   ll dp2[16 * 16 * 16 * 16][16];   ll decode(char c) {     if (( 0  <= c && c <  9  + 1)) {       return c -  0 ;     }     return c -  a  + 10;   }   std::string decl(std::string s) {     if (s ==  0 ) return s;     ll p = (ll)s.size() - 1;     while (true) {       if (s[p] ==  0 ) {         s[p] =  f ;         --p;         continue;       }       if (s[p] ==  a )         s[p] =  9 ;       else         --s[p];       break;     }     return s;   }   ll calc(std::string s) {     if (s.size() <= 4) {       ll t = std::stoi(s, nullptr, 16);       return dp2[t][0];     }     ll res = 0;     ll t = std::stoi(s.substr(s.size() - 4), 0, 16);     ll max = 0;     for (ll i = 0; i < ll(s.size() - 4); ++i) {       CHMAX(max, decode(s[i]));     }     res += dp2[t][max];     for (ll i = 0; i < ll(16); ++i)       for (ll j = 0; j < ll(2); ++j)         for (ll k = 0; k < ll(16); ++k) dp[i][j][k] = 0;     dp[0][0][0] = 1;     for (ll i = 0; i < ll(s.size() - 4); ++i)       for (ll j = 0; j < ll(2); ++j)         for (ll k = 0; k < ll(16); ++k) {           ll sdigit = decode(s[i]);           ll lim = j ? 16 : sdigit + 1;           for (ll d = 0; d < ll(lim); ++d) {             dp[i + 1][j || d < sdigit][std::max(k, d)] += dp[i][j][k];           }         }     for (ll i = 0; i < ll(16); ++i) {       res += dp[s.size() - 4][1][i] * dp2[16 * 16 * 16 * 16 - 1][i];     }     return res;   }   signed main() {     std::ios::sync_with_stdio(false);     std::cin.tie(0);     ;     ll Q;     MACRO_VAR_Scan(Q);     ;     for (ll i1 = 0; i1 < ll(16); ++i1)       for (ll i2 = 0; i2 < ll(16); ++i2)         for (ll i3 = 0; i3 < ll(16); ++i3)           for (ll i4 = 0; i4 < ll(16); ++i4)             for (ll j = 0; j < ll(16); ++j) {               ll i = i1;               i *= 16;               i += i2;               i *= 16;               i += i3;               i *= 16;               i += i4;               if (i == 0) continue;               ll max = std::max({i1, i2, i3, i4, j});               dp2[i][j] = dp2[i - 1][j];               dp2[i][j] += (i >> max) & 1;             }     for (ll q = 0; q < ll(Q); ++q) {       std::string sl, sr;       MACRO_VAR_Scan(sl, sr);       ;       ll l = calc(decl(sl));       ll r = calc(sr);       std::cout << r - l;       std::cout <<   n ;       ;     }     return 0;   }