#include <bits/stdc++.h>
int a[16], high;
long long dp[16][2];
long long solve(int dep, bool exist, bool lead, bool lim) {
  if (dep == -1) return exist;
  if (!lim && !lead && ~dp[dep][exist]) return dp[dep][exist];
  long long ret = 0;
  int up = lim ? a[dep] : 15;
  for (int i = 0; i <= up && i <= high; ++i) {
    if (dep == high >> 2 && !(i >> (high & 3) & 1)) continue;
    ret += solve(dep - 1, exist || i == high, lead && i == 0, lim && i == up);
  }
  if (!lim && !lead) dp[dep][exist] = ret;
  return ret;
}
long long solve(long long n) {
  if (n == -1) return 0;
  int tot = 0;
  while (n) a[tot++] = n & 15, n >>= 4;
  while (tot < 16) a[tot++] = 0;
  long long ans = 0;
  for (high = 1; high < 16; ++high) {
    std::memset(dp, -1, sizeof dp);
    ans += solve(15, 0, 1, 1);
  }
  return ans;
}
void solve() {
  long long l, r;
  std::cin >> std::hex >> l >> r;
  std::cout << solve(r) - solve(l - 1) << '\n';
}
int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  int tc;
  std::cin >> tc;
  while (tc--) solve();
  return 0;
}
