#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int par[1];
int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); }
void join(int x, int y) { par[find(x)] = y; }
long long mod_expo(long long a, long long b, long long mod) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = (ans * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}
long long gcd(long long a, long long b, long long& x, long long& y) {
  if (!a) {
    x = 0;
    y = 1;
    return b;
  }
  long long x1, y1;
  long long d = gcd(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return d;
}
const int N = 105;
int s[N];
int n;
void convert(long long num, int base) {
  memset(s, 0, sizeof s);
  for (n = 0; num; ++n) {
    s[n] = num % base;
    num /= base;
  }
  s[n] = 0;
}
long long dp[2][11][75][1 << 11];
long long solve(int id, int base, int mask, int flag) {
  if (id < 0) return !mask && flag;
  long long& ans = dp[flag][base][id][mask];
  if (ans != -1) return ans;
  ans = 0;
  for (int i = 0; i < base; ++i) {
    if (flag || i) mask ^= (1 << i);
    ans += solve(id - 1, base, mask, (flag || i ? 1 : 0));
    if (flag || i) mask ^= (1 << i);
  }
  return dp[flag][base][id][mask] = ans;
}
long long solve2(int base) {
  long long ans = 0;
  int mask = 0, i, j;
  for (i = n - 1; i >= 0; --i) {
    for (j = 0; j < s[i]; ++j) {
      if (i != n - 1 || j) mask ^= (1 << j);
      ans += solve(i - 1, base, mask, (i != n - 1 || j ? 1 : 0));
      if (i != n - 1 || j) mask ^= (1 << j);
    }
    mask ^= (1 << s[i]);
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int m, b;
  memset(dp, -1, sizeof dp);
  cin >> m;
  long long l, r, ans;
  while (m--) {
    cin >> b >> l >> r;
    convert(r + 1, b);
    ans = solve2(b);
    if (l > 1) {
      convert(l, b);
      ans -= solve2(b);
    }
    cout << ans << "\n";
  }
  return 0;
}
