#include <bits/stdc++.h>
using namespace std;
const int maxxx = 15;
long long f[maxxx][2][2];
vector<int> first, z;
int n, m;
long long a, b;
void chuanbi(int m) {
  for (int i = 0; i <= maxxx - 1; i++)
    f[i][0][0] = f[i][1][0] = f[i][0][1] = f[i][1][1] = -1;
  first.clear();
  int k = m % 4;
  for (int i = 0; i <= m; ++i)
    if ((i >> k) & 1 == 1) first.push_back(i);
}
void lamviecz(long long a) {
  z.clear();
  for (int i = 0; i < maxxx; ++i) z.push_back(0);
  for (int i = maxxx - 1; i >= 0; --i) {
    z[i] = a % 16;
    a /= 16;
  }
}
long long calcu(int i, int ok, int ok1) {
  if (i == maxxx) return ok1;
  if (f[i][ok][ok1] >= 0) return f[i][ok][ok1];
  long long ketqua = 0;
  if (ok == 1) {
    if ((4 * (15 - i) > m) && (4 * (14 - i) <= m)) {
      for (auto j : first) {
        if (j < z[i])
          ketqua += calcu(i + 1, 0, ((ok1 == 1 || j == m) ? 1 : 0));
        else if (j == z[i])
          ketqua += calcu(i + 1, 1, ((ok1 == 1 || j == m) ? 1 : 0));
      }
    } else {
      if (m < z[i]) {
        ketqua += m * calcu(i + 1, 0, ok1) + calcu(i + 1, 0, 1);
      } else {
        ketqua += calcu(i + 1, 1, ((ok1 || z[i] == m) ? 1 : 0)) +
                  z[i] * calcu(i + 1, 0, ok1);
      }
    }
  } else {
    if (4 * (15 - i) > m && 4 * (14 - i) <= m)
      for (auto j : first)
        ketqua += calcu(i + 1, 0, ((ok1 == 1 || j == m) ? 1 : 0));
    else
      ketqua += m * calcu(i + 1, 0, ok1) + calcu(i + 1, 0, 1);
  }
  f[i][ok][ok1] = ketqua;
  return ketqua;
}
long long xuli(long long a) {
  lamviecz(a);
  long long ketqua = 0;
  for (int i = 0; i < 16; ++i) {
    chuanbi(i);
    m = i;
    ketqua += calcu(0, 1, 0);
  }
  return ketqua;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    scanf("%llx %llx", &a, &b);
    cout << xuli(b) - xuli((a == 0) ? 0 : a - 1) << endl;
  }
}
