#include <bits/stdc++.h>
using namespace std;
long long int i, n;
long long int dp[62][12][1 << 11][2];
vector<long long int> dig;
long long int solve(long long int idx, long long int base, long long int mask,
                    bool tight, bool lead) {
  if (idx == -1) return (!lead) & (mask == 0);
  if (!tight and dp[idx][base][mask][lead] != -1)
    return dp[idx][base][mask][lead];
  long long int ans = 0;
  if (tight) {
    for (long long int j = 0; j <= dig[idx]; j++) {
      if (j == 0) {
        if (lead)
          ans += solve(idx - 1, base, mask, (j == dig[idx]), lead);
        else
          ans += solve(idx - 1, base, mask ^ (1 << j), (j == dig[idx]), lead);
      } else if (j != dig[idx])
        ans += solve(idx - 1, base, mask ^ (1 << j), 0, 0);
      else
        ans += solve(idx - 1, base, mask ^ (1 << j), 1, 0);
    }
  } else {
    for (long long int j = 0; j <= base - 1; j++) {
      if (j == 0) {
        if (lead)
          ans += solve(idx - 1, base, mask, 0, 1);
        else
          ans += solve(idx - 1, base, mask ^ (1 << j), 0, 0);
      } else
        ans += solve(idx - 1, base, mask ^ (1 << j), 0, 0);
    }
  }
  if (!tight) dp[idx][base][mask][lead] = ans;
  return ans;
}
long long int go(long long int base, long long int n) {
  dig.clear();
  if (n == 0) return 0;
  while (n != 0) {
    dig.push_back(n % base);
    n /= base;
  }
  return solve(dig.size() - 1, base, 0, 1, 1);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long int q;
  memset(dp, -1, sizeof(dp));
  cin >> q;
  while (q--) {
    long long int l, r, b;
    cin >> b >> l >> r;
    cout << go(b, r) - go(b, l - 1) << endl;
  }
  return 0;
}
