#include <bits/stdc++.h>
using namespace std;
int fromhex(char c) {
  if (c >= '0' && c <= '9') return c - '0';
  return c - 'a' + 10;
}
long long memo[80][16][2][2][3];
string target;
long long doit(int n, int largest_bit, bool bit_set, bool bit_on, int cmp) {
  if (n == target.size()) return bit_set && bit_on && cmp <= 1;
  long long& ret = memo[n][largest_bit][bit_set][bit_on][cmp];
  if (ret != -1) return ret;
  ret = 0;
  for (int i = 0; i <= largest_bit; i++) {
    int pos = target.size() - 1 - n;
    bool rb = (4 * pos <= largest_bit && largest_bit < 4 * (pos + 1) &&
               (i & 1 << (largest_bit % 4)));
    int ncmp = cmp;
    int td = fromhex(target[n]);
    if (cmp == 1) ncmp = i < td ? 0 : (i == td ? 1 : 2);
    ret += doit(n + 1, largest_bit, bit_set | (i == largest_bit), bit_on | rb,
                ncmp);
  }
  return ret;
}
long long query(string n) {
  target = n;
  memset(memo, -1, sizeof memo);
  long long ans = 0;
  for (int i = 0; i < 16; i++) ans += doit(0, i, false, false, 1);
  return ans;
}
int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    string l, r;
    cin >> l >> r;
    long long ans = query(r);
    if (l != "0") {
      for (int i = l.size() - 1;; i--)
        if (l[i] == '0')
          l[i] = 'f';
        else {
          if (l[i] == 'a')
            l[i] = '9';
          else
            l[i]--;
          break;
        }
      ans -= query(l);
    }
    cout << ans << endl;
  }
}
