#include <bits/stdc++.h>
using namespace std;
inline bool setmin(int &x, int y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(int &x, int y) { return (y > x) ? x = y, 1 : 0; }
inline bool setmin(long long &x, long long y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(long long &x, long long y) { return (y > x) ? x = y, 1 : 0; }
const int N = 100000;
const int inf = (int)1e9 + 1;
const long long big = (long long)1e18 + 1;
const int P = 239;
const int P1 = 31;
const int P2 = 57;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const int MOD2 = 998244353;
const long double eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;
string dec(string s) {
  int p = (int)s.size() - 1;
  while (s[p] == '0') {
    s[p] = 'f';
    p--;
  }
  if (s[p] != 'a') {
    s[p]--;
  } else {
    s[p] = '9';
  }
  if ((int)s.size() > 1 && s[0] == '0') {
    return s.substr(1, (int)s.size() - 1);
  } else {
    return s;
  }
}
long long pw[16 + 1][16];
void init() {
  for (int x = (0); x < (16 + 1); x++) {
    pw[x][0] = 1;
    for (int j = (1); j < (16); j++) {
      pw[x][j] = pw[x][j - 1] * x;
    }
  }
}
long long solve1(vector<int> a, int max_d, int goal_bit) {
  if (goal_bit >= (int)a.size() * 4) {
    return 0;
  }
  int pos_bit = goal_bit % 4;
  int cnt_good = 0;
  for (int i = (0); i < (max_d + 1); i++) {
    if ((i >> pos_bit) & 1) {
      cnt_good++;
    }
  }
  long long ans = 0;
  for (int i = ((int)a.size() - 1); i > (-1); i--) {
    if (goal_bit >= i * 4 && goal_bit < (i + 1) * 4) {
      for (int x = (0); x < (min(max_d + 1, a[i])); x++) {
        if ((x >> pos_bit) & 1) {
          ans += pw[max_d + 1][i];
        }
      }
      if (!((a[i] >> pos_bit) & 1)) {
        break;
      }
    } else {
      long long val = min(max_d + 1, a[i]);
      if (goal_bit < i * 4) {
        val *= cnt_good;
        val *= pw[max_d + 1][i - 1];
      } else {
        val *= pw[max_d + 1][i];
      }
      ans += val;
    }
    if (a[i] > max_d) {
      break;
    }
  }
  bool ok = true;
  for (int i = (0); i < ((int)a.size()); i++) {
    if (a[i] > max_d) {
      ok = false;
    }
    if (goal_bit >= i * 4 && goal_bit < (i + 1) * 4) {
      if (!((a[i] >> pos_bit) & 1)) {
        ok = false;
      }
    }
  }
  if (ok) {
    ans++;
  }
  return ans;
}
vector<int> hex(string s) {
  vector<int> res;
  for (int i = ((int)s.size() - 1); i > (-1); i--) {
    res.push_back(s[i] <= '9' ? s[i] - '0' : 10 + (s[i] - 'a'));
  }
  return res;
}
long long solve(string s) {
  vector<int> a = hex(s);
  long long ans = 0;
  for (int max_d = (0); max_d < (16); max_d++) {
    ans += solve1(a, max_d, max_d);
    if (max_d > 0) {
      ans -= solve1(a, max_d - 1, max_d);
    }
  }
  return ans;
}
bool check(vector<int> a) {
  long long mask = 0, x = 0;
  for (int i = (0); i < ((int)a.size()); i++) {
    mask |= (1LL << a[i]);
    x += a[i] * pw[16][i];
  }
  return (x ^ mask) < x;
}
string to_hex(int x) {
  if (x == 0) {
    return "0";
  }
  string res;
  while (x > 0) {
    int v = x % 16;
    if (v < 10) {
      res += char('0' + v);
    } else {
      res += char('a' + v - 10);
    }
    x /= 16;
  }
  reverse(res.begin(), res.end());
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.precision(20);
  cout << fixed;
  init();
  int q;
  cin >> q;
  while (q--) {
    string l, r;
    cin >> l >> r;
    long long ans = solve(r);
    if (l != "0") {
      ans -= solve(dec(l));
    }
    cout << ans << "\n";
  }
  return 0;
}
