Problem: Sherlock found a piece of encrypted data which he thinks will be useful to catch Moriarty. The encrypted data consists of two integer l and r. He noticed that these integers were in hexadecimal form.

He takes each of the integers from l to r, and performs the following operations:

  1. He lists the distinct digits present in the given number. For example: for 101416, he lists the digits as 1, 0, 4. 
  2. Then he sums respective powers of two for each digit listed in the step above. Like in the above example sum = 21 + 20 + 24 = 1910. 
  3. He changes the initial number by applying bitwise xor of the initial number and the sum. Example: <image>. Note that xor is done in binary notation. 



One more example: for integer 1e the sum is sum = 21 + 214. Letters a, b, c, d, e, f denote hexadecimal digits 10, 11, 12, 13, 14, 15, respertively.

Sherlock wants to count the numbers in the range from l to r (both inclusive) which decrease on application of the above four steps. He wants you to answer his q queries for different l and r.

Input

First line contains the integer q (1 ≤ q ≤ 10000).

Each of the next q lines contain two hexadecimal integers l and r (0 ≤ l ≤ r < 1615).

The hexadecimal integers are written using digits from 0 to 9 and/or lowercase English letters a, b, c, d, e, f.

The hexadecimal integers do not contain extra leading zeros.

Output

Output q lines, i-th line contains answer to the i-th query (in decimal notation).

Examples

Input

1
1014 1014


Output

1


Input

2
1 1e
1 f


Output

1
0


Input

2
1 abc
d0e fe23


Output

412
28464

Note

For the second input,

1416 = 2010

sum = 21 + 24 = 18

<image>

Thus, it reduces. And, we can verify that it is the only number in range 1 to 1e that reduces.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
constexpr int MOD = 1000000007;
template <typename T, typename F>
T foo(T t, T g, F f) {
  return t;
}
template <typename T, typename U>
std::istream& operator>>(std::istream& i, pair<T, U>& p) {
  i >> p.first >> p.second;
  return i;
}
template <typename T>
std::istream& operator>>(std::istream& i, vector<T>& t) {
  for (auto& v : t) {
    i >> v;
  }
  return i;
}
template <typename T>
std::ostream& operator<<(std::ostream& o, const vector<T>& t) {
  for (size_t i = 0; i < t.size(); ++i) {
    o << t[i] << " \n"[i == t.size() - 1];
  }
  return o;
}
template <typename T>
using minheap = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using maxheap = priority_queue<T, vector<T>, less<T>>;
auto fraclt = [](const std::pair<int, int>& a, const std::pair<int, int>& b) {
  return (long long)a.first * b.second < (long long)b.first * a.second;
};
struct cmpfrac {
  bool operator()(const std::pair<int, int>& a,
                  const std::pair<int, int>& b) const {
    return (long long)a.first * b.second < (long long)b.first * a.second;
  }
};
template <typename T>
bool in(T a, T b, T c) {
  return a <= b && b < c;
}
int logceil(long long first) {
  int b = 0;
  while (first) {
    first >>= 1;
    ++b;
  }
  return b;
}
namespace std {
template <typename T, typename U>
struct hash<pair<T, U>> {
  hash<T> t;
  hash<U> u;
  size_t operator()(const pair<T, U>& p) const {
    return t(p.first) ^ (u(p.second) << 7);
  }
};
}  // namespace std
template <typename T, typename F>
T bsh(T l, T h, const F& f) {
  T r = -1, m;
  while (l <= h) {
    m = (l + h) / 2;
    if (f(m)) {
      l = m + 1;
      r = m;
    } else {
      h = m - 1;
    }
  }
  return r;
}
template <typename T, typename F>
T bsl(T l, T h, const F& f) {
  T r = -1, m;
  while (l <= h) {
    m = (l + h) / 2;
    if (f(m)) {
      h = m - 1;
      r = m;
    } else {
      l = m + 1;
    }
  }
  return r;
}
template <typename T>
T gcd(T a, T b) {
  if (a < b) swap(a, b);
  return b ? gcd(b, a % b) : a;
}
template <typename T>
class vector2 : public vector<vector<T>> {
 public:
  vector2() {}
  vector2(size_t a, size_t b, T t = T())
      : vector<vector<T>>(a, vector<T>(b, t)) {}
};
template <typename T>
class vector3 : public vector<vector<vector<T>>> {
 public:
  vector3() {}
  vector3(size_t a, size_t b, size_t c, T t = T())
      : vector<vector<vector<T>>>(a, vector<vector<T>>(b, vector<T>(c, t))) {}
};
template <typename T>
struct bounded_priority_queue {
  inline bounded_priority_queue(unsigned int X) : A(X), B(0) {}
  inline void push(unsigned int L, T V) {
    B = max(B, L);
    A[L].push(V);
  }
  inline const T& top() const { return A[B].front(); }
  inline void pop() {
    A[B].pop();
    while (B > 0 && A[B].empty()) --B;
  }
  inline bool empty() const { return A[B].empty(); }
  inline void clear() {
    B = 0;
    for (auto& a : A) a = queue<T>();
  }

 private:
  vector<queue<T>> A;
  unsigned int B;
};
constexpr int LEN = 15;
class TaskG {
 public:
  long long solve(vector<int>& N, int maxDigit) {
    vector3<long long> D(LEN + 1, 2, 4, 0);
    D[0][0][0] = 1;
    int failI = LEN - 1 - maxDigit / 4;
    int failMask = 1 << (maxDigit % 4);
    for (int i = 0; i < LEN; ++i) {
      for (int j = 0; j < 2; ++j) {
        for (int k = 0; k < 4; ++k) {
          if (!D[i][j][k]) continue;
          bool is_smaller_than_N = (k & 1) == 1;
          bool is_larger_than_0 = (k & 2) == 2;
          for (int d = 0; d <= maxDigit; d++) {
            if (d == 0 && !is_larger_than_0) continue;
            if (d > N[i] && !is_smaller_than_N) continue;
            if (i == failI && (d & failMask) == 0) continue;
            int newK = k;
            if (d < N[i]) newK |= 1;
            if (d > 0) newK |= 2;
            D[i + 1][j | (d == maxDigit)][newK] += D[i][j][k];
          }
          if (!is_larger_than_0) {
            if (i == failI) continue;
            int newK = k;
            if (0 < N[i]) newK |= 1;
            D[i + 1][j][newK] += D[i][j][k];
          }
        }
      }
    }
    return D[LEN][1][2] + D[LEN][1][3];
  }
  long long solve(long long N) {
    vector<int> digits(LEN);
    for (int i = LEN - 1; i >= 0; --i) {
      digits[i] = N % 16;
      N /= 16;
    }
    long long ans = 0;
    for (int i = 1; i < 16; ++i) {
      ans += solve(digits, i);
    }
    return ans;
  }
  void solve(istream& cin, ostream& cout) {
    int T;
    cin >> T;
    for (int t = 0; t < T; ++t) {
      long long L, R;
      cin >> hex >> L >> hex >> R;
      cout << solve(R) - solve(L - 1) << '\n';
    }
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  TaskG solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}