#include <bits/stdc++.h>
using namespace std;
long long readi() {
  long long input = 0;
  char c = ' ';
  while (c < '-') {
    c = getchar();
  }
  bool negative = false;
  if (c == '-') {
    negative = true;
    c = getchar();
  }
  while (c >= '0') {
    input = 10 * input + (c - '0');
    c = getchar();
  }
  if (negative) {
    input = -input;
  }
  return input;
}
void printi(long long output) {
  if (output == 0) {
    putchar('0');
    return;
  }
  if (output < 0) {
    putchar('-');
    output = -output;
  }
  int aout[20];
  int ilen = 0;
  while (output) {
    aout[ilen] = ((output % 10));
    output /= 10;
    ilen++;
  }
  for (int i = ilen - 1; i >= 0; i--) {
    putchar(aout[i] + '0');
  }
  return;
}
string reads() {
  string input = "";
  char c = ' ';
  while (c <= ' ') {
    c = getchar();
  }
  while (c > ' ') {
    input += c;
    c = getchar();
  }
  return input;
}
void prints(string output) {
  for (int i = 0; i < output.length(); i++) {
    putchar(output[i]);
  }
  return;
}
long long getrand() {
  long long rand1 = (rand() % 536870912);
  long long rand2 = (rand() % 536870912);
  return (rand1 * 536870912ll + rand2);
}
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#pragma GCC target("avx,tune=native")
int Q;
long long B;
long long lt, rt;
long long dp[62][2][12];
bool seen[62][2][12];
long long val;
long long rep[62];
long long Z;
long long ans;
void gen() {
  for (int i = 0;; i++) {
    rep[i] = val % B;
    val /= B;
    if (val == 0) {
      Z = i;
      break;
    }
  }
  for (int i = 0; i < 62; i++) {
    for (int j = 0; j <= 12; j++) {
      seen[i][0][j] = false;
      seen[i][1][j] = false;
      dp[i][0][j] = 0;
      dp[i][1][j] = 0;
    }
  }
}
long long solve(int place, bool flag, long long val) {
  if (place < 0) {
    if (val == 0) {
      return 1;
    } else {
      return 0;
    }
  }
  long long res = 0ll;
  if (flag) {
    long long k = __builtin_popcountll(val);
    if (seen[place][flag][k]) {
      return dp[place][flag][k];
    }
    seen[place][flag][k] = true;
    if (place == Z) {
      res += solve(place - 1, 0, B + 1);
    } else if (rep[place] != 0) {
      res += solve(place - 1, 0, __builtin_popcountll(val xor 1));
    }
    for (int i = 1; i < rep[place]; i++) {
      res += solve(place - 1, 0, __builtin_popcountll(val xor (1ll << i)));
    }
    res += solve(place - 1, 1, val xor (1ll << rep[place]));
  } else {
    if (seen[place][flag][val]) {
      return dp[place][flag][val];
    }
    seen[place][flag][val] = true;
    if (val == B + 1) {
      res += (B - 1) * solve(place - 1, 0, 1);
      res += solve(place - 1, 0, B + 1);
    } else {
      if (val != 0) {
        res += val * solve(place - 1, 0, val - 1);
      }
      if (B != val) {
        res += (B - val) * solve(place - 1, 0, val + 1);
      }
    }
  }
  dp[place][flag][val] = res;
  return res;
}
int32_t main() {
  srand(time(NULL));
  cout << fixed << setprecision(10);
  cerr << fixed << setprecision(10);
  ios_base::sync_with_stdio(false);
  if (fopen("cf855e.in", "r")) {
    freopen("cf855e.in", "r", stdin);
  }
  cin >> Q;
  while (Q--) {
    ans = 0;
    cin >> B >> lt >> rt;
    val = rt;
    gen();
    ans += solve(Z, 1, 0);
    if (lt == 1) {
      cout << ans << '\n';
      continue;
    }
    val = lt - 1;
    gen();
    ans -= solve(Z, 1, 0);
    cout << ans << '\n';
  }
}
