#include <bits/stdc++.h>
using namespace std;
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "RDLU";
long long ln, lk, lm;
void etp(bool f = 0) {
  puts(f ? "YES" : "NO");
  exit(0);
}
void addmod(int &x, int y, int mod = 1000000007) {
  x += y;
  if (x >= mod) x -= mod;
  assert(x >= 0 && x < mod);
}
void et() {
  puts("-1");
  exit(0);
}
long long dp[20][65540][20];
int a[20];
long long dfs(int pos, int S, int big, bool sm) {
  if (pos == 0) {
    return (S >> big) & 1;
  }
  if (sm && dp[pos][S][big] != -1) return dp[pos][S][big];
  int tar = sm ? 15 : a[pos];
  long long ans = 0;
  for (int i = 0; i <= tar; i++) {
    int tt = S;
    if (pos <= 4) {
      tt += (1 << (4 * (pos - 1))) * i;
    }
    ans += dfs(pos - 1, tt, max(big, i), sm || (!sm && i < tar));
  }
  if (sm) dp[pos][S][big] = ans;
  return ans;
}
long long cal(long long x) {
  for (m = 0; x; x /= 16) a[++m] = x % 16;
  return dfs(m, 0, 0, 0);
}
void fmain(int tid) {
  scanf("%d", &n);
  memset(dp, -1, sizeof dp);
  for (int(i) = 0; (i) < (int)(n); (i)++) {
    long long l, r;
    scanf("%llx%llx", &l, &r);
    printf("%lld\n", cal(r) - cal(l - 1));
  }
}
int main() {
  int t = 1;
  for (int(i) = 1; (i) <= (int)(t); (i)++) {
    fmain(i);
  }
  return 0;
}
