#include <bits/stdc++.h>
using namespace std;
long long dp[16][2][2];
long long func(long long n, int md) {
  fill(dp[0][0], dp[16][0], 0LL);
  dp[0][0][0] = 1;
  for (int i = 1; i <= 15; ++i) {
    int d = (n >> ((15 - i) * 4)) & 0xF;
    int mask = 0;
    if ((15 - i) * 4 <= md && md < (16 - i) * 4) {
      mask = 1 << (md - (15 - i) * 4);
    }
    for (int less = 0; less < 2; ++less) {
      for (int flag = 0; flag < 2; ++flag) {
        long long o = dp[i - 1][less][flag];
        if (!o) continue;
        int jj = less ? md : min(md, d);
        for (int j = 0; j <= jj; ++j) {
          if ((j & mask) != mask) continue;
          int less2 = less || j < d;
          int flag2 = flag || j == md;
          dp[i][less2][flag2] += o;
        }
      }
    }
  }
  return dp[15][0][1] + dp[15][1][1];
}
long long func(long long n) {
  if (n < 0) return 0;
  long long ans = 0;
  for (int i = 0; i < 16; ++i) {
    ans += func(n, i);
  }
  return ans;
}
int main() {
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    long long l, r;
    scanf("%llx%llx", &l, &r);
    printf("%lld\n", func(r) - func(l - 1));
  }
  return 0;
}
