#include <bits/stdc++.h>
using namespace std;
long long int q, b, l, r;
long long int dp[11][2048][64];
void pre() {
  memset(dp, 0, sizeof(dp));
  for (long long int i = 2; i < 11; i++) {
    dp[i][0][0] = 1;
    for (long long int j = 1; j < 64; j++) {
      for (long long int k = 0; k < 2048; k++) {
        for (long long int it = 0; it < i; it++) {
          long long int msk = (k ^ (1 << it));
          dp[i][k][j] += dp[i][msk][j - 1];
        }
      }
    }
  }
}
long long int num(long long int x) { return (dp[b][0][x] - dp[b][1][x - 1]); }
long long int calc(long long int x) {
  if (x == 0) return 0;
  long long int len = 0, temp = x;
  vector<long long int> see;
  while (temp > 0) {
    see.push_back(temp % b);
    temp /= b;
    len++;
  }
  reverse(see.begin(), see.end());
  long long int itne = 0;
  for (long long int i = 1; i < len; i++) {
    itne += num(i);
  }
  long long int msk = 0, nmsk;
  for (long long int i = 0; i < see.size(); i++) {
    for (long long int j = 0; j < see[i]; j++) {
      nmsk = (msk ^ (1 << j));
      if (i == 0 && j == 0) continue;
      itne += dp[b][nmsk][len - i - 1];
    }
    msk = (msk ^ (1 << see[i]));
  }
  if (msk == 0) itne++;
  return itne;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  pre();
  cin >> q;
  while (q--) {
    cin >> b >> l >> r;
    cout << (calc(r) - calc(l - 1)) << "\n";
  }
}
