#include <bits/stdc++.h>
using namespace std;
template <class C>
void mini(C& _a4, C _b4) {
  _a4 = min(_a4, _b4);
}
template <class C>
void maxi(C& _a4, C _b4) {
  _a4 = max(_a4, _b4);
}
long long dp[(1 << 12) + 10][11][64];
long long rF(int mask, int len, int base) {
  if (dp[mask][base][len] == -1) {
    if (__builtin_popcount(mask) > len) return 0;
    if (mask == 0 && len == 0) return 1;
    if (len == 0) return 0;
    dp[mask][base][len] = 0;
    for (int i = (0); i <= ((int)(base)-1); i++)
      dp[mask][base][len] += rF(mask ^ (1 << i), len - 1, base);
  }
  return dp[mask][base][len];
}
long long qwery(long long val, int base) {
  if (val == 0) return 0;
  vector<int> cyfry;
  while (val > 0) {
    cyfry.push_back(val % base);
    val /= base;
  }
  int mask = 0;
  for (int y : cyfry) mask ^= (1 << y);
  long long res = 0;
  for (int i = 0; i < ((int)(cyfry).size()) - 1; ++i) {
    mask ^= (1 << cyfry[i]);
    for (int j = cyfry[i] - 1; j >= 0; --j) res += rF(mask ^ (1 << j), i, base);
  }
  int ord = cyfry[((int)(cyfry).size()) - 1] - 1;
  for (int i = ((int)(cyfry).size()) - 1; i >= 0; --i) {
    for (int j = ord; j > 0; --j) res += rF(1 << j, i, base);
    ord = base - 1;
  }
  return res;
}
int main() {
  for (int i = (0); i <= ((int)(1 << 11) - 1); i++)
    for (int j = (0); j <= ((int)(11) - 1); j++)
      for (int k = (0); k <= ((int)(64) - 1); k++) dp[i][j][k] = -1;
  int q;
  scanf("%d", &q);
  for (int ii = (0); ii <= ((int)(q)-1); ii++) {
    long long b, l, r;
    scanf("%lld%lld%lld", &b, &l, &r);
    printf("%lld\n", qwery(r + 1, b) - qwery(l, b));
  }
  if (0)
    for (int i = (0); i <= ((int)(100) - 1); i++)
      printf("%d: %lld\n", i, qwery(i, 10));
}
