#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
template <typename T>
void chmin(T &a, const T &b) {
  a = min(a, b);
}
template <typename T>
void chmax(T &a, const T &b) {
  a = max(a, b);
}
struct FastIO {
  FastIO() {
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
} fastiofastio;
const ll INF = 1LL << 60;
ll dp[11][65][1LL << 11][2];
ll dfs(ll base, ll pos, ll mask, ll lead, ll lt, vector<ll> num) {
  if (pos == -1) return mask == 0 ? 1 : 0;
  if (lt == 1 && dp[base][pos][mask][lead] != -1)
    return dp[base][pos][mask][lead];
  const ll lim = lt == 0 ? num[pos] : base - 1;
  ll ans = 0;
  for (ll d = (ll)0; d < (ll)lim + 1; ++d) {
    if (d == 0 && lead)
      ans += dfs(base, pos - 1, mask, 1, lt || d < lim, num);
    else
      ans += dfs(base, pos - 1, mask ^ (1LL << d), 0, lt || d < lim, num);
  }
  if (lt == 1) dp[base][pos][mask][lead] = ans;
  return ans;
}
int main(void) {
  ll q;
  cin >> q;
  vector<ll> b(q), l(q), r(q);
  for (ll i = (ll)0; i < (ll)q; ++i) cin >> b[i] >> l[i] >> r[i];
  auto solve = [&](ll x, ll b) {
    vector<ll> num;
    while (x > 0) {
      num.push_back(x % b);
      x /= b;
    }
    return dfs(b, (ll)num.size() - 1, 0, 1, 0, num);
  };
  memset(dp, -1, sizeof(dp));
  for (ll i = (ll)0; i < (ll)q; ++i) {
    ll vr = solve(r[i], b[i]);
    ll vl = solve(l[i] - 1, b[i]);
    cout << vr - vl << '\n';
  }
  cout << flush;
  return 0;
}
