#include <bits/stdc++.h>
using namespace std;
const int L = 70;
long long d[11][L][1 << 10];
long long d1[11][L];
long long f(int b, long long n) {
  long long n0 = n;
  vector<int> x;
  while (n) {
    x.push_back(n % b);
    n /= b;
  }
  long long res = 0;
  int mask = 0;
  for (int i = x.size() - 1; i >= 0; --i) {
    for (int j = 1; j < x[i]; ++j) {
      res += d[b][i][mask ^ (1 << j)];
    }
    if (i != x.size() - 1) {
      if (x[i]) res += d[b][i][mask ^ 1];
    } else {
      res += d1[b][i];
    }
    mask ^= (1 << x[i]);
  }
  return res;
}
const int MS = 100000;
long long s[11][MS];
long long stupid(int b, int n) { return s[b][n]; }
bool mtest(int b, int n) {
  vector<int> c(b);
  while (n) {
    c[n % b]++;
    n /= b;
  }
  for (int x : c)
    if (x % 2) return 0;
  return 1;
}
int main() {
  for (int b = 2; b <= 10; ++b) {
    d[b][0][0] = 1;
    for (int i = 0; i + 1 < L; ++i) {
      for (int mask = 0; mask < (1 << b); ++mask)
        if (d[b][i][mask]) {
          for (int x = 0; x < b; ++x) {
            d[b][i + 1][mask ^ (1 << x)] += d[b][i][mask];
          }
        }
    }
    for (int l = 1; l < L; ++l) {
      d1[b][l] = d1[b][l - 1];
      for (int x = 1; x < b; ++x) d1[b][l] += d[b][l - 1][1 << x];
    }
  }
  int T;
  scanf("%d", &T);
  for (int test = 1; test <= T; ++test) {
    long long l, r;
    int b;
    scanf("%d%lld%lld", &b, &l, &r);
    printf("%lld\n", f(b, r + 1) - f(b, l));
  }
  return 0;
}
