#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
long long dp[20][1 << 16][16];
long long l, r, b[20];
long long dfs(int o, int bit, int ma, bool isTop) {
  if (o == -1) return bit >> ma & 1;
  if (~dp[o][bit][ma] && isTop == false) return dp[o][bit][ma];
  long long res = 0;
  int hi = isTop ? b[o] : 15;
  for (int i = 0; i <= hi; ++i) {
    int newBit = bit;
    if (o < 4) newBit |= i << (4 * o);
    res += dfs(o - 1, newBit, max(ma, i), isTop && i == hi);
  }
  if (isTop) return res;
  return dp[o][bit][ma] = res;
}
long long work(long long o) {
  int w = 0;
  while (o > 0) {
    b[w++] = o % 16;
    o /= 16;
  }
  return dfs(w - 1, 0, 0, 1);
}
int main() {
  int T;
  scanf("%d", &T);
  memset(dp, -1, sizeof(dp));
  while (T--) {
    scanf("%llx%llx", &l, &r);
    printf("%lld\n", work(r) - work(l - 1));
  }
  return 0;
}
