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>
std::vector<std::vector<std::vector<long>>> dp(
    (sizeof(long) + 4) + 20,
    std::vector<std::vector<long>>(1 << 11, std::vector<long>(200)));
static long go(const int& k, const bool& eq, const std::string& bound,
               const long& base, const int& counts, const bool& all_zeros,
               const int& sum) {
  if (k >= bound.size()) {
    return !counts && !all_zeros && sum > 0;
  }
  if (!eq && dp.at(k).at(counts).at(sum) != -1)
    return dp.at(k).at(counts).at(sum);
  int top = eq ? bound.at(k) - '0' : (base - 1);
  long cnt = 0;
  for (int i = 0; i <= top; ++i) {
    cnt +=
        go(k + 1, eq && i == top, bound, base,
           counts ^ (all_zeros && !i ? 0 : (1 << i)), all_zeros && !i, sum + i);
  }
  if (!eq)
    return dp.at(k).at(counts).at(sum) = cnt;
  else
    return cnt;
}
static std::string convert_to_base(long n, const long& base) {
  std::stringstream ss;
  while (n) {
    ss << (n % base);
    n /= base;
  }
  std::string s{ss.str()};
  for (int i = 0; i < s.length() / 2; ++i)
    std::swap(s.at(i), s.at(s.length() - i - 1));
  ss.str("");
  for (int i = 0; i + s.length() < (sizeof(long) + 4); ++i) ss << '0';
  ss << s;
  return ss.str();
}
static long step(const long& n, const long& base) {
  for (int i = 0; i <= (sizeof(long) + 4); ++i) {
    for (int j = 0; j < (1 << 11); ++j) {
      for (int k = 0; k < 200; ++k) dp.at(i).at(j).at(k) = -1;
    }
  }
  return go(0, true, convert_to_base(n, base), base, 0, true, 0);
}
static long one_case(const long& base, const long& l, const long& r) {
  return step(r, base) - step(l - 1, base);
}
int main(void) {
  int t;
  std::scanf("%d", &t);
  long b, l, r;
  while (t-- > 0) {
    std::scanf("%ld %ld %ld", &b, &l, &r);
    std::printf("%ld\n", one_case(b, l, r));
  }
  return 0;
}
