#include <bits/stdc++.h>
using namespace std;
long long l, r, b, q, a[105], dp[11][105][10005];
long long dfs(long long len, long long st, long long zero, long long lim) {
  if (len == 0) return (st == (1 << b) - 1);
  if (!zero && !lim && dp[b][len][st] != -1) return dp[b][len][st];
  long long ans = 0, ma = lim ? a[len] : b - 1;
  for (long long i = 0; i <= ma; i++)
    ans += dfs(len - 1, (zero && i == 0) ? st : st ^ (1 << i), zero && i == 0,
               lim && i == ma);
  if (!zero && !lim) dp[b][len][st] = ans;
  return ans;
}
long long solve(long long x) {
  long long len = 0;
  while (x) {
    a[++len] = x % b;
    x /= b;
  }
  return dfs(len, (1 << b) - 1, 1, 1);
}
signed main() {
  memset(dp, -1, sizeof(dp));
  scanf("%lld", &q);
  while (q--) {
    scanf("%lld%lld%lld", &b, &l, &r);
    printf("%lld\n", solve(r) - solve(l - 1));
  }
  return 0;
}
