#include <bits/stdc++.h>
using namespace std;
int q, c[11][11];
long long f[11][62][11];
void Prep() {
  for (int i = 0; i <= 10; ++i) {
    c[i][0] = 1;
    for (int j = 1; j <= i; ++j) c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
  }
  for (int base = 2; base <= 10; ++base) {
    f[base][0][0] = 1;
    long long cur = 1;
    for (int len = 1; cur <= 1e18; ++len, cur *= base)
      for (int odd = 0; odd <= min(base, len); ++odd) {
        if (odd > 0)
          f[base][len][odd] +=
              f[base][len - 1][odd - 1] * (base - (odd - 1)) * c[base][odd - 1];
        if (odd < 10)
          f[base][len][odd] +=
              f[base][len - 1][odd + 1] * (odd + 1) * c[base][odd + 1];
        f[base][len][odd] /= c[base][odd];
      }
  }
}
vector<long long> Conv(int b, long long x) {
  vector<long long> a;
  for (; x > 0; x /= b) a.push_back(x % b);
  reverse(a.begin(), a.end());
  return a;
}
long long Calc(int b, vector<long long> a) {
  long long re = 0;
  int n = a.size(), mask = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < a[i]; ++j) {
      if (i == 0 && j == 0) continue;
      re += f[b][n - i - 1][__builtin_popcount(mask ^ 1 << j)];
    }
    mask ^= 1 << a[i];
  }
  re += !mask;
  for (int i = 1; i < n - 1; i += 2) re += f[b][i][1] * (b - 1);
  return re;
}
void Solve() {
  while (q--) {
    int b;
    long long l, r;
    scanf("%d %lld %lld", &b, &l, &r);
    printf("%lld\n",
           Calc(b, Conv(b, r)) - (l > 1 ? Calc(b, Conv(b, l - 1)) : 0));
  }
}
int main() {
  scanf("%d", &q);
  Prep();
  Solve();
  return 0;
}
