#include <bits/stdc++.h>
const int mb = 11;
const int ms = 68;
long long memo[mb][ms][1 << mb][2];
long long dp(int base, int on, int mask, int got) {
  if (on == 0) return mask == 0 && got == 1 ? 1 : 0;
  long long &ans = memo[base][on][mask][got];
  if (ans != -1) return ans;
  ans = 0;
  for (int i = 1 - got; i < base; i++)
    ans += dp(base, on - 1, mask ^ (1 << i), 1);
  if (got == 0) ans += dp(base, on - 1, 0, 0);
  return ans;
}
long long solve(long long x, int base) {
  if (x < base) {
    return 0;
  }
  std::vector<int> nums;
  while (x) {
    nums.push_back(x % base);
    x /= base;
  }
  std::reverse(nums.begin(), nums.end());
  long long ans = dp(base, nums.size() - 1, 0, 0);
  int mask = 0;
  for (int i = 0; i < nums.size(); i++) {
    for (int j = (i == 0 ? 1 : 0); j < nums[i]; j++) {
      ans += dp(base, nums.size() - i - 1, mask ^ (1 << j), 1);
    }
    mask ^= (1 << nums[i]);
  }
  if (mask == 0) ans++;
  return ans;
}
int main() {
  memset(memo, -1, sizeof memo);
  int q;
  std::cin >> q;
  while (q--) {
    long long b, l, r;
    scanf("%lli %lli %lli", &b, &l, &r);
    std::cout << solve(r, b) - solve(l - 1, b) << '\n';
  }
}
