#include <bits/stdc++.h>
using namespace std;
int b;
long long l, r;
vector<int> v;
long long dp[11][66][(1 << 10) + 2][2];
long long solve2(int idx, int base, int L, int msk, int flag) {
  if (idx == v.size()) return msk == 0;
  long long ret = dp[base][flag][L][msk];
  if (~ret) return ret;
  ret = solve2(idx + 1, base, L - 1, msk ^ flag, flag);
  for (int i = 1; i < b; i++) {
    ret += solve2(idx + 1, base, L - 1, msk ^ (1 << i), bool(flag | i));
  }
  return ret;
}
long long solve(int idx, int base, int L, int msk, bool flag) {
  long long ret = 0;
  while (idx < (int)v.size()) {
    for (int i = 0; i <= v[idx]; i++) {
      if (i == v[idx]) {
        idx++;
        L--;
        msk ^= (1 << i);
        flag |= i;
        break;
      } else {
        if (i || flag)
          ret += dp[base][L - 1][msk ^ (1 << i)][bool(flag | i)];
        else
          ret += dp[base][L - 1][msk][flag];
      }
    }
  }
  ret += (msk == 0);
  return ret;
}
long long getAns(long long x) {
  v.clear();
  while (x) {
    v.push_back(x % b);
    x /= b;
  }
  while (v.size() && v.back() == 0) v.pop_back();
  reverse(v.begin(), v.end());
  return solve(0, b, v.size(), 0, 0);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  for (int base = 2; base <= 10; base++) {
    for (int L = 0; L <= 64; L++) {
      for (int msk = 0; msk < (1 << base); msk++) {
        for (int flag = 0; flag < 2; flag++) {
          if (L == 0 && msk == 0) {
            dp[base][L][msk][flag] = 1;
            continue;
          }
          long long &ret = dp[base][L][msk][flag];
          ret = dp[base][L - 1][msk ^ flag][flag];
          for (int i = 1; i < base; i++) {
            ret += dp[base][L - 1][msk ^ (1 << i)][bool(flag | i)];
          }
        }
      }
    }
  }
  int q;
  cin >> q;
  while (q--) {
    cin >> b >> l >> r;
    cout << getAns(r) - getAns(l - 1) << "\n";
  }
}
