#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<tuple<ll, ll, int>> qr[11];
ll dp[70][2][1 << 10];
int b;
ll go(int i, int dz, int mask) {
  if (i == 70) {
    if (mask == 0 && dz) return 1;
    return 0;
  }
  ll& ans = dp[i][dz][mask];
  if (ans != -1) return ans;
  ans = 0;
  for (int d = 0; d < b; d++) {
    int ndz = dz || d;
    int nmask = mask;
    if (ndz) nmask ^= (1 << d);
    ans += go(i + 1, ndz, nmask);
  }
  return ans;
}
int dig[70];
ll solve(int i, int dz, int mask) {
  ll ans = 0;
  if (i == 70) {
    if (mask == 0 && dz) return 1;
    return 0;
  }
  for (int d = 0; d < dig[i]; d++) {
    int ndz = dz || d;
    int nmask = mask;
    if (ndz) nmask ^= (1 << d);
    ans += go(i + 1, ndz, nmask);
  }
  int d = dig[i];
  int ndz = dz || d;
  int nmask = mask;
  if (ndz) nmask ^= 1 << d;
  return ans + solve(i + 1, ndz, nmask);
}
void write(ll x) {
  for (int i = 69; i >= 0; i--) {
    dig[i] = x % b;
    x /= b;
  }
}
ll resp[100010];
int main() {
  ios::sync_with_stdio(false);
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b;
    ll l, r;
    cin >> b >> l >> r;
    qr[b].emplace_back(l, r, i);
  }
  for (b = 2; b <= 10; b++) {
    memset(dp, -1, sizeof dp);
    for (auto& p : qr[b]) {
      ll l, r;
      int id;
      tie(l, r, id) = p;
      write(r);
      ll ans = solve(0, 0, 0);
      write(l - 1);
      ans -= solve(0, 0, 0);
      resp[id] = ans;
    }
  }
  for (int i = 0; i < q; i++) {
    cout << resp[i] << "\n";
  }
  return 0;
}
