#include <bits/stdc++.h>
using namespace std;
template <class C>
void mini(C &a4, C b4) {
  a4 = min(a4, b4);
}
template <class C>
void maxi(C &a4, C b4) {
  a4 = max(a4, b4);
}
template <class TH>
void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << "=" << h << "\n";
}
template <class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',') cerr << *sdbg++;
  cerr << "=" << h << ",";
  _dbg(sdbg + 1, a...);
}
template <class T>
ostream &operator<<(ostream &os, vector<T> V) {
  os << "[";
  for (auto vv : V) os << vv << ",";
  return os << "]";
}
template <class L, class R>
ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
const vector<string> wbit = {"13579bdf", "2367abef", "4567cdef", "89abcdef"};
const string S = "0123456789abcdef";
vector<long long> getCountInner(long long maxLen, char maxCh, long long specHex,
                                string hexOpts) {
  vector<long long> result{1};
  for (long long i = 0; i < maxLen; i++) {
    const string &chs = (i == specHex ? hexOpts : S);
    long long cnt = 0;
    for (char ch : chs) {
      if (ch > maxCh) {
        break;
      }
      cnt++;
    }
    result.push_back(result.back() * cnt);
  }
  return result;
}
long long getCountMain(const string &x, char maxCh, long long specHex,
                       string hexOpts) {
  if (specHex >= ((long long)(x).size())) {
    return 0;
  }
  long long res = 0;
  vector<long long> inners =
      getCountInner(((long long)(x).size()), maxCh, specHex, hexOpts);
  for (long long i = 0; i < ((long long)(x).size()); i++) {
    long long bit = ((long long)(x).size()) - i - 1;
    const string &chs = (bit == specHex ? hexOpts : S);
    bool same = false;
    for (char ch : chs) {
      if (ch > maxCh) {
        break;
      }
      if (ch > x[i]) {
        break;
      }
      if (ch < x[i]) {
        res += inners[bit];
        continue;
      }
      if (ch == x[i]) {
        same = true;
      }
    }
    if (same && !bit) {
      res++;
    }
    if (!same) {
      break;
    }
  }
  return res;
}
long long getCount(string x) {
  long long result = 0;
  for (long long i = 0; i < 16; i++) {
    result += getCountMain(x, S[i], i / 4, wbit[i % 4]);
    if (i) {
      result -= getCountMain(x, S[i - 1], i / 4, wbit[i % 4]);
    }
    (i, result);
  }
  return result;
}
string sub1(string x) {
  if (x == "0") {
    return x;
  }
  stringstream ss;
  ss << std::hex << stoll(x, 0, 16) - 1;
  return ss.str();
}
void process() {
  string l, r;
  cin >> l >> r;
  l = sub1(l);
  (l, r);
  cout << getCount(r) - getCount(l) << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  if (0) cout << fixed << setprecision(6);
  long long Q;
  cin >> Q;
  while (Q--) {
    process();
  }
}
