#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long w = 1, s = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return w * s;
}
int B, Q;
long long f[11][70][1 << 11][2], l, r, A[101000], cnt;
inline long long DFS(int base, int pos, int Sta, int lim, int app) {
  if (!pos) {
    if (!Sta && app) return 1;
    return 0;
  }
  if ((~f[base][pos][Sta][app]) && (!lim)) return f[base][pos][Sta][app];
  int M = lim ? A[pos] : (base - 1);
  long long res = 0;
  for (register int i = 0; i <= M; ++i) {
    int nw = Sta ^ (1 << i);
    if (!app && !i) nw = Sta;
    res += DFS(base, pos - 1, nw, lim & (i == M), app | (i != 0));
  }
  if (!lim) f[base][pos][Sta][app] = res;
  return res;
}
inline long long Solve(int base, long long v) {
  if (!v) return 0;
  cnt = 0;
  while (v) {
    A[++cnt] = v % base;
    v /= base;
  }
  return DFS(base, cnt, 0, 1, 0);
}
int main() {
  memset(f, -1, sizeof(f));
  Q = read();
  while (Q--) {
    B = read(), l = read(), r = read();
    cout << Solve(B, r) - Solve(B, l - 1) << "\n";
  }
  return 0;
}
