#include <bits/stdc++.h>
using namespace std;
const int D = 64;
const int B = 11;
int digit[D];
int nDigit;
long long dp[B][D][1 << 10];
long long calc(int b, int i, int x) {
  long long &ans = dp[b][i][x];
  if (ans != -1) return ans;
  if (i == 0) return ans = x == 0;
  ans = 0;
  for (int cur = 0; cur < b; ++cur) {
    ans += calc(b, i - 1, x ^ (1 << cur));
  }
  return ans;
}
long long solve(long long n, int base) {
  if (n == 0) return 0;
  nDigit = 0;
  while (n > 0) {
    digit[++nDigit] = n % base;
    n /= base;
  }
  long long ans = 0;
  int pref = 0;
  for (int i = nDigit; i > 0; --i) {
    for (int cur = i == nDigit ? 1 : 0; cur < digit[i]; ++cur) {
      ans += calc(base, i - 1, pref ^ (1 << cur));
    }
    pref ^= 1 << digit[i];
  }
  ans += pref == 0;
  for (int i = nDigit - 1; i > 0; --i) {
    for (int cur = 1; cur < base; ++cur) {
      ans += calc(base, i - 1, 1 << cur);
    }
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int q;
  cin >> q;
  memset(dp, -1, sizeof dp);
  while (q--) {
    long long l, r, base;
    cin >> base >> l >> r;
    cout << solve(r, base) - solve(l - 1, base) << '\n';
  }
  return 0;
}
