10
4 108 114
5 30 155
8 193 197
9 71 169
2 163 166
8 120 144
8 22 151
4 21 166
2 46 127
8 38 51
 20
9 142 172
7 132 256
8 245 315
9 496 496
3 345 362
8 13 162
5 342 470
9 16 488
9 467 482
4 471 478
10 92 224
6 228 261
9 54 167
2 402 409
5 9 10
7 225 360
3 438 483
6 252 342
3 491 493
6 296 367
 1
8 1234567890123 123456789012345

0
3
0
1
3
0
5
16
9
1
 0
0
0
0
4
6
13
7
0
0
1
2
3
0
0
2
12
9
1
4
 774752389568

#include <bits/stdc++.h>
using namespace std;
template <class X, class Y>
void minimize(X &x, const Y &y) {
  if (x > y) x = y;
}
template <class X, class Y>
void maximize(X &x, const Y &y) {
  if (x < y) x = y;
}
template <class T>
T Abs(const T &x) {
  return (x < 0 ? -x : x);
}
long long countWay[11 + 2][60 + 2][(1LL << (11)) + 2];
void precalc(void) {
  for (int base = (2), _b = (11); base <= _b; base = base + 1) {
    countWay[base][0][0] = 1;
    for (int len = 0, _n = (60); len < _n; len = len + 1)
      for (int mask = 0, _n = ((1LL << (base))); mask < _n; mask = mask + 1)
        if (countWay[base][len][mask] > 0) {
          for (int ___next = 0, _n = (base); ___next < _n;
               ___next = ___next + 1)
            countWay[base][len + 1][mask ^ (1LL << (___next))] +=
                countWay[base][len][mask];
        }
  }
}
vector<int> getDigits(int base, long long value) {
  assert(value > 0);
  vector<int> v;
  while (value > 0) {
    v.push_back(value % base);
    value /= base;
  }
  return v;
}
long long answer(int base, long long limit) {
  if (limit < 1) return 0;
  vector<int> digits = getDigits(base, limit);
  long long res = 0;
  for (int len = (1), _b = (digits.size() - 1); len <= _b; len = len + 1)
    for (int dig = (1), _b = (base - 1); dig <= _b; dig = dig + 1)
      res += countWay[base][len - 1][(1LL << (dig))];
  int mask = 0;
  for (int pos = (digits.size() - 1), _a = (0); pos >= _a; pos = pos - 1) {
    for (int dig = (pos == digits.size() - 1 ? 1 : 0), _b = (digits[pos] - 1);
         dig <= _b; dig = dig + 1) {
      res += countWay[base][pos][mask ^ (1LL << (dig))];
    }
    mask ^= (1LL << (digits[pos]));
  }
  return res + (mask == 0);
}
int main(void) {
  precalc();
  int q;
  cin >> q;
  for (int pmp = 0, _n = (q); pmp < _n; pmp = pmp + 1) {
    int b;
    long long l, r;
    cin >> b >> l >> r;
    cout << answer(b, r) - answer(b, l - 1) << "\n";
  }
  return 0;
}
