#include <bits/stdc++.h>
using namespace std;
const long long MOD = (long long)1e9 + 7;
long long dp[12][65][1024], dp0[12][65][1024];
void upd(long long &a, long long b) { a = (a + b); }
int get_next(int a, int b) {
  if ((a & (1 << b)) == 0) {
    a += (1 << b);
  } else {
    a -= (1 << b);
  }
  return a;
}
void precalc() {
  for (int i = 2; i <= 10; ++i) {
    dp[i][0][0] = 1;
    dp0[i][0][0] = 1;
  }
  for (int osn = 2; osn <= 10; ++osn) {
    for (int len = 0; len <= 62; ++len) {
      for (int mask = 0; mask < (1 << osn); ++mask) {
        for (int next = 0; next < osn; ++next) {
          upd(dp[osn][len + 1][get_next(mask, next)], dp[osn][len][mask]);
          if (next == 0 && len == 0) {
            continue;
          }
          upd(dp0[osn][len + 1][get_next(mask, next)], dp0[osn][len][mask]);
        }
      }
    }
  }
}
int digits[1000];
int cur[10];
int build(int x) {
  int ans = 0;
  for (int i = x - 1; i >= 0; --i) {
    ans = ans * 2 + cur[i] % 2;
  }
  return ans;
}
long long f(long long n, long long x) {
  int cnt = 0;
  if (n == 0) {
    return 0;
  }
  while (n > 0) {
    ++cnt;
    digits[cnt] = n % x;
    n /= x;
  }
  reverse(digits + 1, digits + cnt + 1);
  memset(cur, 0, sizeof cur);
  long long answer = 0;
  for (int i = 1; i <= cnt; ++i) {
    for (int new_digit = 0; new_digit < x; ++new_digit) {
      if (new_digit == 0 && i == 1) continue;
      if (new_digit < digits[i]) {
        ++cur[new_digit];
        int cur_mask = build(x);
        answer = answer + dp[x][cnt - i][cur_mask];
        --cur[new_digit];
      } else {
        ++cur[new_digit];
        break;
      }
    }
  }
  int cur_mask = build(x);
  if (cur_mask == 0) ++answer;
  for (int i = 1; i < cnt; ++i) {
    answer += dp0[x][i][0];
  }
  return answer;
}
void solve(long long b, long long l, long long r) {
  long long ans = f(r, b) - f(l - 1, b);
  cout << ans << endl;
}
int main() {
  precalc();
  ios_base::sync_with_stdio(0);
  int test;
  cin >> test;
  for (int tst = 1; tst <= test; ++tst) {
    long long b, l, r;
    cin >> b >> l >> r;
    solve(b, l, r);
  }
  return 0;
}
