#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
void maximize(T &x, U y) {
  if (x < y) x = y;
}
template <class T, class U>
void minimize(T &x, U y) {
  if (x > y) x = y;
}
template <class T>
T Abs(T x) {
  return (x < (T)0 ? -x : x);
}
template <class T>
T safe_sqrt(T x) {
  return sqrt(max((T)0, x));
}
template <class T, class U, class V>
T addmod(T x, U k, V mod) {
  return ((x + k) % mod + mod) % mod;
}
template <class T, class U, class V>
T submod(T x, U k, V mod) {
  return ((x - k) % mod + mod) % mod;
}
template <class T, class U, class V>
T mulmod(T x, U k, V mod) {
  return (int64_t)x * k % mod;
}
namespace task {
int64_t dp[11][100][1200];
int b;
int64_t l, r;
vector<int> digits;
vector<int> trans(int64_t x, int b) {
  vector<int> vec;
  while (x > 0) vec.push_back(x % b), x /= b;
  return vec;
}
int64_t calc(int base, int idx, int mask, bool flag) {
  if (idx == -1) return (mask == 0);
  if (~dp[base][idx][mask] and flag) return dp[base][idx][mask];
  int64_t ret = 0;
  int k = flag ? b - 1 : digits[idx];
  for (int i = 0; i <= k; ++i)
    ret += calc(base, idx - 1, mask ^ ((1LL) << (i)), flag | (i < k));
  if (flag) dp[base][idx][mask] = ret;
  return ret;
}
int64_t f(int64_t x) {
  if (!x) return 0ll;
  digits.clear();
  digits = trans(x, b);
  int64_t ret = 0;
  for (int i = 0; i < ((digits).size()); ++i) {
    int up = (i == ((digits).size()) - 1 ? digits[i] : b - 1);
    for (int j = 1; j <= up; ++j)
      ret +=
          calc(b, i - 1, ((1LL) << (j)), (i < ((digits).size()) - 1 or j < up));
  }
  return ret;
}
void solve() {
  int q;
  scanf("%d", &q);
  memset(dp, -1ll, sizeof(dp));
  while (q--) {
    scanf("%d%I64d%I64d", &b, &l, &r);
    int64_t x = f(r);
    int64_t y = f(l - 1);
    int64_t ans = f(r) - f(l - 1);
    printf("%I64d\n", ans);
  }
}
}  // namespace task
int main(void) { task::solve(); }
