#include <bits/stdc++.h>
using namespace std;
const int maxl = 66;
const int maxstate = 2049;
const int startstate = 2048;
const int invalidstate = -1;
int base = 10;
const int inf = -1;
long long f[maxl][maxstate][10];
bool isValidState(int state) { return (startstate == state || 0 == state); }
int nextState(int preState, int digit) {
  if (preState == startstate) {
    return digit == 0 ? startstate : (1 << digit);
  }
  return preState ^ (1 << digit);
}
void init() { memset(f, inf, sizeof(f)); }
long long dfs(int n, int state, bool lim, int d[]) {
  if (n == 0) return isValidState(state) ? 1 : 0;
  long long sum = f[n][state][base];
  if (lim && sum != inf) return sum;
  sum = 0;
  int maxv = lim ? base - 1 : d[n];
  for (int k = 0; k <= maxv; ++k) {
    int st = nextState(state, k);
    bool nextlim = (k < maxv) || lim;
    if (invalidstate != st) sum += dfs(n - 1, st, nextlim, d);
  }
  if (lim) f[n][state][base] = sum;
  return sum;
}
long long g(long long x) {
  if (x < 0) return 0;
  if (x == 0) return 1;
  int d[maxl];
  int n = 0;
  while (x) {
    d[++n] = x % base;
    x /= base;
  }
  return dfs(n, startstate, false, d);
}
int main() {
  int t, cas = 0;
  init();
  long long a, b;
  while (scanf("%d", &t) != EOF) {
    while (t--) {
      scanf("%d %lld %lld", &base, &a, &b);
      printf("%lld\n", g(b) - g(a - 1));
    }
  }
  return 0;
}
