#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const long long linf = (long long)1e18;
const int mod = (int)1e9 + 7;
const long double eps = (long double)1e-8;
const int maxn = (int)1e5 + 5;
const long double pi = acos(-1);
const int B = 11;
const long long MX = 1e18 + 10000;
long long my_pow(long long x, long long y) {
  if (y == 0) return 1;
  return x * my_pow(x, y - 1);
}
long long gr_l[B + 2], sum[B + 2][66], d[B + 2][66][(1 << 10)];
long long dzen(int b, long long r) {
  if (r == 0) return 0;
  vector<int> a;
  long long x = r;
  while (x > 0) {
    a.push_back(x % b);
    x /= b;
  };
  long long ans = sum[b][(int)a.size() - 1];
  int mask = 0;
  for (int i = (int)a.size() - 1; i >= 0; --i) {
    int st_j;
    if (i == (int)a.size() - 1)
      st_j = 1;
    else
      st_j = 0;
    for (int j = st_j; j < a[i]; ++j) {
      ans += d[b][i][mask ^ (1 << j)];
    }
    mask ^= (1 << a[i]);
  }
  ans += d[b][0][mask];
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  memset((d), 0, sizeof(d));
  cout << fixed << setprecision(2);
  for (auto b = 2; b != B; ++b) {
    int L = 1;
    while (pow(b, L + 1) <= MX) ++L;
    gr_l[b] = L;
    d[b][0][0] = 1;
    for (auto i = 0; i != L; ++i) {
      for (auto mask = 0; mask != (1 << b); ++mask) {
        for (auto j = 0; j != b; ++j) {
          d[b][i + 1][mask ^ (1 << j)] += d[b][i][mask];
        }
      }
    }
    sum[b][0] = 0;
    for (auto i = 1; i != L + 1; ++i) {
      sum[b][i] = sum[b][i - 1];
      for (auto j = 1; j != b; ++j) {
        sum[b][i] += d[b][i - 1][(1 << j)];
      }
    }
  }
  int T;
  cin >> T;
  for (auto numt = 0; numt != T; ++numt) {
    long long b, l, r;
    cin >> b >> l >> r;
    cout << dzen(b, r) - dzen(b, l - 1) << endl;
  }
  return 0;
}
