#include <bits/stdc++.h>
using namespace std;
template <class X, class Y>
void minimize(X &x, const Y &y) {
  if (x > y) x = y;
}
template <class X, class Y>
void maximize(X &x, const Y &y) {
  if (x < y) x = y;
}
template <class T>
T Abs(const T &x) {
  return (x < 0 ? -x : x);
}
long long countWay[11 + 2][60 + 2][(1LL << (11)) + 2];
void precalc(void) {
  for (int base = (2), _b = (11); base <= _b; base = base + 1) {
    countWay[base][0][0] = 1;
    for (int len = 0, _n = (60); len < _n; len = len + 1)
      for (int mask = 0, _n = ((1LL << (base))); mask < _n; mask = mask + 1)
        if (countWay[base][len][mask] > 0) {
          for (int ___next = 0, _n = (base); ___next < _n;
               ___next = ___next + 1)
            countWay[base][len + 1][mask ^ (1LL << (___next))] +=
                countWay[base][len][mask];
        }
  }
}
vector<int> getDigits(int base, long long value) {
  assert(value > 0);
  vector<int> v;
  while (value > 0) {
    v.push_back(value % base);
    value /= base;
  }
  return v;
}
long long answer(int base, long long limit) {
  if (limit < 1) return 0;
  vector<int> digits = getDigits(base, limit);
  long long res = 0;
  for (int len = (1), _b = (digits.size() - 1); len <= _b; len = len + 1)
    for (int dig = (1), _b = (base - 1); dig <= _b; dig = dig + 1)
      res += countWay[base][len - 1][(1LL << (dig))];
  int mask = 0;
  for (int pos = (digits.size() - 1), _a = (0); pos >= _a; pos = pos - 1) {
    for (int dig = (pos == digits.size() - 1 ? 1 : 0), _b = (digits[pos] - 1);
         dig <= _b; dig = dig + 1) {
      res += countWay[base][pos][mask ^ (1LL << (dig))];
    }
    mask ^= (1LL << (digits[pos]));
  }
  return res + (mask == 0);
}
int main(void) {
  precalc();
  int q;
  cin >> q;
  for (int pmp = 0, _n = (q); pmp < _n; pmp = pmp + 1) {
    int b;
    long long l, r;
    cin >> b >> l >> r;
    cout << answer(b, r) - answer(b, l - 1) << "\n";
  }
  return 0;
}
