#include <bits/stdc++.h>
using namespace std;
char a[61], b[61];
long long dp[61][2][2][2][1030][11];
long long solve(long long pos, long long choto, long long boro, long long shuru,
                long long mask, long long bs) {
  if (pos == 61) return mask ? 0 : 1;
  long long &ret = dp[pos][choto][boro][shuru][mask][bs];
  if (ret != -1 && boro && choto) return ret;
  ret = 0;
  long long lo = 0, hi = bs - 1;
  if (!choto) hi = b[pos] - '0';
  if (!boro) lo = a[pos] - '0';
  for (long long i = lo; i <= hi; i++)
    ret += solve(pos + 1, choto | (i < hi), boro | (i > lo), shuru | (i > 0),
                 (shuru | (i > 0)) ? mask ^ (1 << i) : mask, bs);
  return ret;
}
int main() {
  long long t;
  scanf("%lld", &t);
  memset(dp, -1, sizeof(dp));
  while (t--) {
    long long bs, l, r;
    scanf("%lld%lld%lld", &bs, &l, &r);
    for (int i = 60; i >= 0; i--) {
      a[i] = (char)((l % bs) + '0');
      b[i] = (char)((r % bs) + '0');
      l /= bs, r /= bs;
    }
    printf("%lld\n", solve(0, 0, 0, 0, 0, bs));
  }
  return 0;
}
