#include <bits/stdc++.h>
using namespace std;
long long dp[16][65537][17];
int position[1100005];
int bit[200000];
bool flag = false;
long long max(long long a, long long b) { return (a > b ? a : b); }
long long dfs(long m, long long p, long long pos) {
  if (dp[m][p][pos] != -1) {
    return dp[m][p][pos];
  }
  if (pos == 0) {
    if ((1 << m) & p)
      dp[m][p][pos] = 1;
    else
      dp[m][p][pos] = 0;
    return dp[m][p][pos];
  }
  dp[m][p][pos] = 0;
  for (int i = 0; i < 16; ++i)
    dp[m][p][pos] += dfs(max(m, i), position[p * 16 + i], pos - 1);
  return dp[m][p][pos];
}
long long calc(string s) {
  int len = s.length();
  long long sum = 0ll;
  for (int i = 0; i < len; ++i) {
    sum *= 16;
    if (s[i] >= '0' && s[i] <= '9')
      sum += s[i] - '0';
    else
      sum += s[i] - 'a' + 10;
  }
  if (flag) --sum;
  for (int i = 0; i < len; ++i) {
    bit[i] = sum % 16;
    sum /= 16;
  }
  long long ans = 0;
  int m = 0, p = 0;
  for (int i = len - 1; i >= 0; --i) {
    for (int j = 0; j < bit[i]; ++j) {
      ans += dp[max(m, j)][position[p * 16 + j]][i];
    }
    m = max(m, bit[i]);
    p = position[p * 16 + bit[i]];
  }
  ans += dp[m][p][0];
  return ans;
}
void solveG() {
  int q;
  cin >> q;
  while (q-- > 0) {
    flag = false;
    string left;
    cin >> left;
    string right;
    cin >> right;
    long long ans = calc(right);
    flag = true;
    ans -= calc(left);
    cout << ans << endl;
  }
}
int main() {
  ios::sync_with_stdio(false);
  memset(dp, -1, sizeof dp);
  position[0] = 0;
  for (int i = 1; i <= 1100005; ++i) {
    position[i] = position[i - 1] + 1;
    if (position[i] == 65536) position[i] = 0;
  }
  dfs(0, 0, 16);
  solveG();
  return 0;
}
