#include <bits/stdc++.h>
using namespace std;
int q, c[17][17];
long long pw[17][16];
unsigned long long l, r;
vector<int> cand[16];
int Conv(char ch) {
  if (ch <= '9') return ch - '0';
  return ch - 'a' + 10;
}
void Prep() {
  for (int i = 1; i < 16; ++i)
    for (int j = 1; j <= i; ++j)
      if (((1 << (i & 3)) ^ j) < j) cand[i].push_back(j);
  for (int i = 0; i <= 16; ++i) c[i][0] = 1;
  for (int i = 1; i <= 16; ++i)
    for (int j = 1; j <= i; ++j) c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
  for (int i = 0; i <= 16; ++i) pw[i][0] = 1;
  for (long long i = 0; i <= 16; ++i)
    for (int j = 1; j < 16; ++j) pw[i][j] = pw[i][j - 1] * i;
}
long long F(int n, int k) {
  if (n < 0) return 0;
  if (n == 0) return 1;
  return (long long)n * (k + 1);
}
long long DP(long long x) {
  vector<int> a;
  do {
    a.push_back(x & ((1 << 4) - 1)), x >>= 4;
  } while (x);
  int n = a.size(), mx = 0, val = 0;
  long long re = 0;
  for (int i = n - 1; i >= 0; --i) {
    for (int j = 0; j < a[i]; ++j) {
      int nmx = max(mx, j);
      for (int k = nmx; k < 16; ++k) {
        if (i > (k >> 2)) {
          int ncand = cand[k].size();
          if (!cand[k].empty() && cand[k].back() == k) {
            re += pw[k + 1][i - 1];
            --ncand;
          }
          for (int l = 0; l <= i - 1; ++l) {
            if (k > nmx && !l) continue;
            re += (long long)ncand * c[i - 1][l] * pw[k][i - 1 - l];
          }
        } else {
          if (!k || (k >> 2) >= n) continue;
          int tmp1 = ((k >> 2) != i ? a[k >> 2] : j), tmp2 = 1 << (k & 3);
          if ((tmp1 ^ tmp2) >= tmp1) continue;
          for (int l = 0; l <= i; ++l) {
            if (k > nmx && !l) continue;
            re += (long long)c[i][l] * pw[k][i - l];
          }
        }
      }
    }
    mx = max(mx, a[i]);
  }
  return re;
}
int main() {
  Prep();
  scanf("%d", &q);
  while (q--) {
    scanf("%llx%llx", &l, &r);
    printf("%lld", DP(r + 1) - DP(l));
    printf("\n");
  }
  return 0;
}
